-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
249 lines (209 loc) · 5.59 KB
/
main.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* File: main.cpp
* Author: Mark Furneaux
* Copyright: 2014 Romaco Canada, Mark Furneaux
*
* Created on December 25, 2014, 5:08 PM
*
* This file is part of PSUControl.
*/
#include <cstdlib>
#include <ncurses.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <string>
#include "main.h"
#include "serial.h"
using namespace std;
const uint32_t nonBlockingTimeoutMs = 10;
bool outputOn = false;
double maxVoltage = 0;
double maxCurrent = 0;
double currVoltage = 0;
double currCurrent = 0;
double currentDivider = 10;
int main(int argc, char** argv) {
int ch;
char portAddr[255];
double userFloatInput = 0;
//start curses
initscr();
//disable line buffering
cbreak();
//capture function/arrow keys
keypad(stdscr, TRUE);
//wait for the user to select a serial port
setInteractive();
if (argc>1) {
snprintf(portAddr, 255, "%s", argv[1]);
} else {
if (!getSerialPort(portAddr)) {
endwin();
printf("Error: Invalid Serial Port\n");
exit(-1);
}
}
erase();
move(0, 0);
printw("Device is at: %s", portAddr);
refresh();
//nothing to wait on anymore
setNonblocking();
//open the port
int fd = open(portAddr, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
endwin();
printf("Error: Could not open serial port\n");
return -2;
}
//set port to 9600 baud, 8N1
if (set_interface_attribs(fd, B9600, 0) < 0) {
endwin();
printf("Error: Could not set port attributes\n");
return -3;
}
//lock the front controls
//FIXME: the device unlocks automatically after so many seconds of inactivity,
//so when the user inputs a new setting, the lock is defeated
//toggleRemote(fd);
//set control limits
getDeviceCapabilities(fd);
//write version info
writeVersion();
//print command reference
printHelp();
do {
//get all the live parameters
getSettings(fd);
getCurrent(fd);
if (ch == ERR) {
// Wait 0.05s if no character was available last time
usleep(50000);
}
//get any input, if available
ch = getch();
//set voltage mode
if (ch == 'v') {
//prompt and wait
move((LINES / 2) + 7, (COLS / 2) - 10);
setInteractive();
printw("Set New Voltage: ");
refresh();
//get the new voltage
if (scanw("%lf", &userFloatInput) == 1) {
setVoltage(fd, userFloatInput);
}
//remove the prompt and unblock all input
move((LINES / 2) + 7, (COLS / 2) - 10);
clrtoeol();
setNonblocking();
} else if (ch == 'c') {
//prompt and wait
move((LINES / 2) + 7, (COLS / 2) - 10);
setInteractive();
printw("Set New Current: ");
refresh();
//get the new current
if (scanw("%lf", &userFloatInput) == 1) {
setCurrent(fd, userFloatInput);
}
//remove the prompt and unblock all input
move((LINES / 2) + 7, (COLS / 2) - 10);
clrtoeol();
setNonblocking();
} else if (ch == 'o') {
//toggle output
outputOn = (outputOn) ? true : false;
setOutput(fd, outputOn);
} else if (ch == KEY_UP) {
//increase voltage by one step
currVoltage += 0.1;
setVoltage(fd, currVoltage);
//don't let arrow keys sit in the buffer (to prevent accidental
//overrun)
flushArrows();
} else if (ch == KEY_DOWN) {
//decrease voltage by one step
currVoltage -= 0.1;
setVoltage(fd, currVoltage);
//don't let arrow keys sit in the buffer (to prevent accidental
//overrun)
flushArrows();
}
//exit on q pressed
} while (ch != 'q');
//DEBUG to be removed
// move(21, 0);
// printw("Exiting...");
//
// //sleep(40);
// timeout(-1);
// move(22, 0);
// printw("Press any key to exit...");
// getch();
//close the port
close(fd);
//exit curses
endwin();
return 0;
}
/*
* Shows the cursor, echos all input, and blocks on input indefinitely.
*/
void setInteractive() {
curs_set(1);
echo();
timeout(-1);
}
/*
* Hides the cursor, hides all input, and times out on input in 500ms.
*/
void setNonblocking() {
curs_set(0);
noecho();
timeout(nonBlockingTimeoutMs);
}
void writeVersion() {
move(LINES - 1, COLS - 59);
printw("Version 0.0.1 Copyright 2014 Romaco Canada, Mark Furneaux");
refresh();
}
void printHelp() {
move(LINES - 6, 0);
printw("q - quit");
move(LINES - 5, 0);
printw("v - set voltage");
move(LINES - 4, 0);
printw("c - set current");
move(LINES - 3, 0);
printw("UP - increase voltage");
move(LINES - 2, 0);
printw("DOWN - decrease voltage");
move(LINES - 1, 0);
printw("o - toggle output on/off");
refresh();
}
/*
* Flushes all available arrow keys out of the buffer
*
* Sets timeout to 10 ms on exit (i.e. assumes that the window is in
* nonblocking mode)
*/
void flushArrows()
{
int ch;
timeout(0);
do {
ch = getch();
} while (ch == KEY_UP || ch == KEY_DOWN);
timeout(nonBlockingTimeoutMs);
if (ch != ERR) {
ungetch(ch);
}
}