-
Notifications
You must be signed in to change notification settings - Fork 5
/
serial_functions.cpp
208 lines (197 loc) · 6.01 KB
/
serial_functions.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
/*
* © 2023, Peter Cole. All rights reserved.
*
* This file is part of EX-IOExpander.
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include "globals.h"
#include "serial_functions.h"
#include "test_functions.h"
#include "device_functions.h"
#include "display_functions.h"
bool newSerialData = false; // Flag for new serial data being received
const byte numSerialChars = 20; // Max number of chars for serial input
char serialInputChars[numSerialChars]; // Char array for serial input
/*
* Function to read and process serial input for I2C address config
*/
void processSerialInput() {
static bool serialInProgress = false;
static byte serialIndex = 0;
char startMarker = '<';
char endMarker = '>';
char serialChar;
while (Serial.available() > 0 && newSerialData == false) {
serialChar = Serial.read();
if (serialInProgress == true) {
if (serialChar != endMarker) {
serialInputChars[serialIndex] = serialChar;
serialIndex++;
if (serialIndex >= numSerialChars) {
serialIndex = numSerialChars - 1;
}
} else {
serialInputChars[serialIndex] = '\0';
serialInProgress = false;
serialIndex = 0;
newSerialData = true;
}
} else if (serialChar == startMarker) {
serialInProgress = true;
}
}
if (newSerialData == true) {
newSerialData = false;
char * strtokIndex;
strtokIndex = strtok(serialInputChars," ");
char activity = strtokIndex[0]; // activity is our first parameter
strtokIndex = strtok(NULL," "); // space is null, separator
unsigned long parameter;
uint8_t vpin, profile;
uint16_t value;
if (activity == 'W') {
parameter = strtol(strtokIndex, NULL, 16); // last parameter is the address in hex
} else if (activity == 'T') {
parameter = strtokIndex[0];
if (parameter == 'S') {
strtokIndex = strtok(NULL," ");
vpin = strtoul(strtokIndex, NULL, 10); // get Vpin, this needs to be disconnected from CS
strtokIndex = strtok(NULL, " ");
value = strtoul(strtokIndex, NULL, 10); // get value of the angle or dimming
strtokIndex = strtok(NULL, " ");
profile = strtoul(strtokIndex, NULL, 10); // get the profile
}
} else {
parameter = strtol(strtokIndex, NULL, 10);
}
switch (activity) {
case 'D': // Enable/disable diagnostic output
serialCaseD(parameter);
break;
case 'E': // Erase EEPROM
eraseI2CAddress();
break;
case 'R': // Read address from EEPROM
serialCaseR();
break;
case 'T': // Display current state of test modes
if (parameter == 'A') {
setAnalogueTesting();
} else if (parameter == 'I') {
setInputTesting();
} else if (parameter == 'O') {
setOutputTesting();
} else if (parameter == 'P') {
setPullupTesting();
} else if (parameter == 'S') {
testServo(vpin, value, profile);
} else {
serialCaseT();
}
break;
case 'V': // Display Vpin map
startupDisplay();
displayVpinMap();
break;
case 'W': // Write address to EEPROM
serialCaseW(parameter);
break;
case 'Z': // Software reboot
reset();
break;
default:
break;
}
}
}
void setAnalogueTesting() {
if (analogueTesting) {
testAnalogue(false);
USB_SERIAL.println(F("Analogue testing disabled"));
} else {
testAnalogue(true);
}
}
void serialCaseD(unsigned long parameter) {
if (diag && parameter) {
displayDelay = parameter * 1000;
USB_SERIAL.print(F("Diagnostics enabled, delay set to "));
USB_SERIAL.println(displayDelay);
diag = true;
} else if (diag && !parameter) {
USB_SERIAL.println(F("Diagnostics disabled"));
diag = false;
} else {
if (parameter) {
displayDelay = parameter * 1000;
}
USB_SERIAL.print(F("Diagnostics enabled, delay set to "));
USB_SERIAL.println(displayDelay);
diag = true;
}
}
void setInputTesting() {
if (inputTesting) {
testInput(false);
USB_SERIAL.println(F("Input testing (no pullups) disabled"));
} else {
testInput(true);
}
}
void setOutputTesting() {
if (outputTesting) {
testOutput(false);
USB_SERIAL.println(F("Output testing disabled"));
} else {
testOutput(true);
}
}
void setPullupTesting() {
if (pullupTesting) {
testPullup(false);
USB_SERIAL.println(F("Pullup input testing disabled"));
} else {
testPullup(true);
}
}
void serialCaseR() {
if (getI2CAddress() == 0) {
USB_SERIAL.println(F("I2C address not stored, using myConfig.h"));
} else {
USB_SERIAL.print(F("I2C address stored is 0x"));
USB_SERIAL.println(getI2CAddress(), HEX);
}
}
void serialCaseT() {
if (analogueTesting) {
USB_SERIAL.println(F("Analogue testing <A> enabled"));
} else if (inputTesting) {
USB_SERIAL.println(F("Input testing <I> (no pullups) enabled"));
} else if (outputTesting) {
USB_SERIAL.println(F("Output testing <O> enabled"));
} else if (pullupTesting) {
USB_SERIAL.println(F("Pullup input <P> testing enabled"));
} else {
USB_SERIAL.println(F("No testing in progress"));
}
}
void serialCaseW(unsigned long parameter) {
if (parameter > 0x07 && parameter < 0x78) {
writeI2CAddress(parameter);
} else {
USB_SERIAL.println(F("Invalid I2C address, must be between 0x08 and 0x77"));
}
}