-
Notifications
You must be signed in to change notification settings - Fork 23
/
pP_serial.h
268 lines (236 loc) · 6.05 KB
/
pP_serial.h
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "pP_cmd.h"
void parseData()
{
// split the data into its parts
char *strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(inputBuffer, " "); // get the first part - the string
strcpy(serialMessageIn, strtokIndx); // copy it to serialMessageIn
strtokIndx = strtok(NULL, " "); // this continues where the previous call left off
serialLong = atol(strtokIndx); // convert this part to an integer
}
/*------------------------------------------------*/
void identifyMarkers()
{
char x = Serial.read();
#ifdef I2C_INPUT
char y = Wire.read();
#endif // I2C_INPUT
if (x == '\n' || x == '\r')
{
serialIncoming = true;
inputBuffer[bytesRecvd] = 0;
parseData();
bytesRecvd = 0;
}
else
{
inputBuffer[bytesRecvd] = x;
bytesRecvd++;
if (bytesRecvd == buffSize)
{
bytesRecvd = buffSize - 1;
}
}
#ifdef I2C_INPUT
if (y == '\n' || y == '\r')
{
serialIncoming = true;
inputBuffer[bytesRecvd] = 0;
parseData();
bytesRecvd = 0;
}
else
{
inputBuffer[bytesRecvd] = y;
bytesRecvd++;
if (bytesRecvd == buffSize)
{
bytesRecvd = buffSize - 1;
}
}
#endif
}
/*------------------------------------------------*/
void serialPrintConfig()
{
Serial.print("GAIN_F ");
Serial.print(GAIN_FACTOR);
switch (GAIN_FACTOR)
{
case 0:
Serial.println(" 3x");
break;
case 1:
Serial.println(" 3.5x");
break;
case 2:
Serial.println(" 4.33x");
break;
case 3:
Serial.println(" 6x");
break;
case 4:
Serial.println(" 11x");
break;
default:
Serial.println(" INVALID");
break;
}
Serial.print("VFOL ");
Serial.println(followerThrs);
Serial.print("VCOMP ");
Serial.println(compThrs);
Serial.print("LOOP_D ");
Serial.println(LOOP_DUR);
Serial.print("TRG_D ");
Serial.println(TRG_DUR);
Serial.print("HYST ");
Serial.println(Hyst);
Serial.print("LOGIC ");
Serial.println(LOGIC);
Serial.print("PZDET ");
Serial.println(PZDET);
Serial.print("VCCSW ");
Serial.print(VCCSW);
switch (VCCSW)
{
case 0:
Serial.println(" 3.3v");
break;
case 1:
Serial.println(" 5v");
break;
default:
Serial.println(" INVALID");
break;
}
Serial.print("VM_CONST ");
Serial.println(voltMeterConstant);
Serial.print("Firmware Version ");
Serial.println(PP_VERSION);
}
void serialPrintState()
{
Serial.print("{");
Serial.print("\"Vcc\":");
Serial.print(Vin);
Serial.print(",");
Serial.print("\"VComp\":");
Serial.print((long)VComp * Vin / 1023);
Serial.print(",");
Serial.print("\"VFol\":");
Serial.print((long)VFol * Vin / 1023);
Serial.print(",");
Serial.print("\"Err\":");
Serial.print(ERR_STATE);
Serial.print(",");
Serial.print("\"PzCon\":");
Serial.print(PZ_STATE);
Serial.print(",");
Serial.print("\"Firm_Ver\":");
Serial.print(PP_VERSION);
Serial.print(",");
Serial.println("}");
}
void updateParams()
{
serialIncoming = false;
if (strcmp(serialMessageIn, "GAIN_F") == 0)
{
updateGainFactor(serialLong);
}
else if (strcmp(serialMessageIn, "VFOL") == 0)
{
updateVFol(serialLong);
}
else if (strcmp(serialMessageIn, "VCOMP") == 0)
{
updateVComp(serialLong);
}
else if (strcmp(serialMessageIn, "LOOP_D") == 0)
{
updateLoopDuration(serialLong);
}
else if (strcmp(serialMessageIn, "TRG_D") == 0)
{
updateTrigDuration(serialLong);
}
else if (strcmp(serialMessageIn, "HYST") == 0)
{
updateHysteresis(serialLong);
}
else if (strcmp(serialMessageIn, "LOGIC") == 0)
{
updateLogic(serialLong);
}
else if (strcmp(serialMessageIn, "PZDET") == 0)
{
updatePzDet(serialLong);
}
else if (strcmp(serialMessageIn, "VCCSW") == 0)
{
updateVccSwitch(serialLong);
}
else if (strcmp(serialMessageIn, "CONST") == 0)
{
updateConstant(serialLong);
}
else if (strcmp(serialMessageIn, "DEBUG") == 0)
{
updateDebug(serialLong);
}
else if (strcmp(serialMessageIn, "CONFIG") == 0)
{
serialPrintConfig();
}
else if (strcmp(serialMessageIn, "ERASE") == 0)
{
eraseEEPROM();
serialPrintConfig();
}
else if (strcmp(serialMessageIn, "STATE") == 0)
{
serialPrintState();
}
else if (strcmp(serialMessageIn, "HELP") == 0)
{
#if defined(ARDUINO_AVR_ATmega328PB)
Serial.println("To change gain factor: GAIN_F [integer for gain state - see note*]");
Serial.println("To change voltage follower voltage (low threshold): VFOL [float value]");
Serial.println("To change comparator voltage (high threshold): VCOMP [float value]");
Serial.println("To change main loop period: LOOP_D [integer for milliseconds]");
Serial.println("To change trigger active duration: TRG_D [integer for milliseconds]");
Serial.println("To change the output logic: LOGIC [0|1]");
Serial.println(" (0 for active low, 1 for active high)");
Serial.println("To enable piezo plugged detection: PZDET [0|1]");
Serial.println(" (0 for disabled, 1 for enabled)");
Serial.println("To change the main voltage of the circuit: VCCSW [0|1]")
Serial.println(" (0 for 3.3v, 1 for 5v)");
Serial.println("To change ADC hysteresis value: HYST [integer in millivolts]");
Serial.println("To enable or disable debug output: DEBUG [0|1]");
Serial.println("To print current config: CONFIG");
Serial.println("To set config to defaults: ERASE");
Serial.println("To print current state: STATE");
Serial.println("");
Serial.println("Commands are entered in this format:");
Serial.println("CMD VAL");
Serial.println("Commands are confirmed with Enter key");
Serial.println("");
Serial.println("Examples:");
Serial.println("GAIN_F 3 <~ set gain factor to index 3 (6x)");
Serial.println("VFOL 2350 <~ set the vref floor to 2.35V");
#else
Serial.println("Check docs.pyroballpcbs.com/config");
#endif // defined(ARDUINO_AVR_ATmega328PB)
}
parseData();
}
void serialInput()
{
// receive data from Serial and save it into inputBuffer
if (Serial.available() > 0)
{
// the order of these IF clauses is significant
identifyMarkers();
}
}