-
Notifications
You must be signed in to change notification settings - Fork 5
/
pwmtest.cpp
126 lines (99 loc) · 2.71 KB
/
pwmtest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Copyright (c) 2018, Michael McCoy
*/
#include "pwm.h"
#include <ncurses.h>
#include <csignal>
#include <cmath>
#include <algorithm>
#include <iostream>
#include "plog/Log.h"
#include "plog/Appenders/ColorConsoleAppender.h"
using namespace pwm;
/**
* Frequency tick for a given frequency
*/
double freqTick(double freqMHz) {
return pow(10.0, int(log10(freqMHz / 2.0)) - 2.0);
}
/**
* Duty cycle tick
*/
double dutyTick(double dutyCycle) {
return 0.01;
}
/**
* Significant figures needed for frequency
*/
int sigFig(double freqMHz) {
return round(std::max(4.0 - log10(freqMHz), 0.));
}
void clearLine(int offset = 0) {
int x,y;
getyx(stdscr, y, x);
move(y + offset, 0);
clrtoeol();
}
int main(int argc, char** argv) {
Peripherals& peripherals = Peripherals::getInstance();
// PLog documentation at https://github.com/SergiusTheBest/plog
static plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender;
plog::init(plog::debug, &consoleAppender);
constexpr char EXIT_CHAR = 'q';
double freqMHz = 100.0;
double dutyCycle = 0.5;
bool showUsage = false;
for (int i = 1; i < argc; i++) {
if (std::string("-f") == argv[i]) {
freqMHz = std::atof(argv[i+1]);
i++;
} else {
showUsage = true;
}
}
if (showUsage) {
std::cout << "Usage: " << argv[0] << " [-f freqMHz=100.0]" << std::endl;
return 0;
}
static const char help[] =
"Use left/right to change frequency, up/down to change duty cycle, 'q' to quit.\n";
PwmController pwmController(freqMHz, dutyCycle);
int ch = int(EXIT_CHAR);
setlocale(LC_ALL, "");
initscr(); // Start curses mode
raw(); // Line buffering disabled
keypad(stdscr, TRUE); // Get arrows, shifts, etc..
noecho(); // Don't echo() while we do getch
printw(help);
do {
pwmController.setTargetFreqMHz(freqMHz);
pwmController.setTargetDutyCycle(dutyCycle);
double actualFreqMHz = pwmController.getHardwareFreqMHz();
double actualDutyCycle = pwmController.getHardwareDutyCycle();
printw("ACTUAL: %%%.1f duty @ %.*fMHz\n",
100.0*actualDutyCycle, sigFig(actualFreqMHz), actualFreqMHz);
printw("TARGET: %%%.1f duty @ %.*fMHz\n",
100.0*dutyCycle, sigFig(freqMHz), freqMHz);
ch = getch();
switch (ch) {
case KEY_UP:
dutyCycle = std::min(1.0, dutyCycle + dutyTick(dutyCycle));
break;
case KEY_DOWN:
dutyCycle = std::max(0.0, dutyCycle - dutyTick(dutyCycle));
break;
case KEY_LEFT:
freqMHz -= freqTick(freqMHz);
break;
case KEY_RIGHT:
freqMHz += freqTick(freqMHz);
break;
default:
break;
}
clearLine(-1);
clearLine(-1);
} while (ch != int(EXIT_CHAR));
endwin();/* End curses mode */
return 0;
}