-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenMicro.ino
More file actions
386 lines (344 loc) · 11.3 KB
/
Copy pathOpenMicro.ino
File metadata and controls
386 lines (344 loc) · 11.3 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* OpenMicro - Controles fisicos, razonamiento e indicador RGB de estado
*
* Hardware:
* - Pulsador de dictado normalmente abierto entre D2 y GND.
* - Pulsador de envio normalmente abierto entre D3 y GND.
* - Pulsadores de tarea anterior/siguiente entre D4/D5 y GND.
* - Potenciometro de 10 kohm: extremos a 5V/GND y cursor a A0.
* - LED RGB de catodo comun: R-D9, G-D10 y B-D11, cada canal mediante 220 ohm.
*
* Protocolo serie a 115200 baudios:
* Arduino -> host: BUTTON PRESS/RELEASE, BUTTON 2 PRESS/RELEASE,
* TASK PREVIOUS PRESS/RELEASE y TASK NEXT PRESS/RELEASE.
* REASONING LOW/MEDIUM/HIGH/XHIGH.
* [DEBUG] POT raw=... filtered=... level=... cada 500 ms.
* Host -> Arduino: LED OFF, IDLE, WORKING, COMPLETE, NEEDS_INPUT, ERROR,
* DICTATION o FLASH_ACTION (todos con el prefijo "LED ").
*/
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
constexpr uint8_t BUTTON_PINS[] = {2, 3, 4, 5};
constexpr uint8_t BUTTON_COUNT = sizeof(BUTTON_PINS) / sizeof(BUTTON_PINS[0]);
constexpr uint8_t RGB_RED_PIN = 9;
constexpr uint8_t RGB_GREEN_PIN = 10;
constexpr uint8_t RGB_BLUE_PIN = 11;
constexpr uint8_t REASONING_POT_PIN = A0;
constexpr unsigned long BLINK_INTERVAL_MS = 500;
constexpr unsigned long DEBOUNCE_INTERVAL_MS = 30;
constexpr unsigned long SERIAL_WAIT_MS = 3000;
constexpr unsigned long RGB_UPDATE_INTERVAL_MS = 10;
constexpr unsigned long POT_SAMPLE_INTERVAL_MS = 10;
constexpr unsigned long POT_STARTUP_DELAY_MS = 100;
constexpr unsigned long POT_DIAGNOSTIC_INTERVAL_MS = 500;
constexpr unsigned long WORKING_PULSE_PERIOD_MS = 1600;
constexpr unsigned long ACTION_FLASH_MS = 300;
constexpr uint8_t SERIAL_COMMAND_CAPACITY = 32;
constexpr uint16_t POT_ZONE_WIDTH = 256;
constexpr uint16_t POT_HYSTERESIS = 20;
enum class LedStatus : uint8_t {
Off,
Idle,
Working,
Complete,
NeedsInput,
Error,
Dictation,
};
enum class ReasoningLevel : uint8_t {
Low,
Medium,
High,
ExtraHigh,
};
unsigned long lastBlinkMs = 0;
unsigned long lastRgbUpdateMs = 0;
unsigned long lastPotSampleMs = 0;
unsigned long lastPotDiagnosticMs = 0;
unsigned long heartbeatCount = 0;
unsigned long lastButtonChangeMs[BUTTON_COUNT] = {};
unsigned long actionFlashUntilMs = 0;
bool builtinLedState = false;
bool rawButtonStates[BUTTON_COUNT] = {};
bool stableButtonStates[BUTTON_COUNT] = {};
LedStatus ledStatus = LedStatus::Off;
char serialCommand[SERIAL_COMMAND_CAPACITY] = {};
uint8_t serialCommandLength = 0;
bool serialCommandOverflow = false;
uint8_t lastRed = 255;
uint8_t lastGreen = 255;
uint8_t lastBlue = 255;
uint16_t rawPotReading = 0;
uint16_t filteredPotReading = 0;
ReasoningLevel reasoningLevel = ReasoningLevel::Low;
bool reasoningLevelReady = false;
void setRgb(const uint8_t red, const uint8_t green, const uint8_t blue) {
if (red == lastRed && green == lastGreen && blue == lastBlue) {
return;
}
analogWrite(RGB_RED_PIN, red);
analogWrite(RGB_GREEN_PIN, green);
analogWrite(RGB_BLUE_PIN, blue);
lastRed = red;
lastGreen = green;
lastBlue = blue;
}
void updateRgb(const unsigned long nowMs) {
if (nowMs - lastRgbUpdateMs < RGB_UPDATE_INTERVAL_MS) {
return;
}
lastRgbUpdateMs = nowMs;
if (static_cast<long>(actionFlashUntilMs - nowMs) > 0) {
setRgb(0, 220, 0);
return;
}
switch (ledStatus) {
case LedStatus::Off:
setRgb(0, 0, 0);
break;
case LedStatus::Idle:
setRgb(72, 72, 72);
break;
case LedStatus::Working: {
const unsigned long phase = nowMs % WORKING_PULSE_PERIOD_MS;
const unsigned long halfPeriod = WORKING_PULSE_PERIOD_MS / 2;
const unsigned long ramp = phase < halfPeriod ? phase
: WORKING_PULSE_PERIOD_MS - phase;
const uint8_t blue = 12 + (ramp * 168UL) / halfPeriod;
setRgb(0, 0, blue);
break;
}
case LedStatus::Complete:
setRgb(0, 160, 0);
break;
case LedStatus::NeedsInput:
setRgb(180, 55, 0);
break;
case LedStatus::Error:
setRgb(180, 0, 0);
break;
case LedStatus::Dictation:
setRgb(0, 0, 180);
break;
}
}
void applyLedCommand(const char* command) {
if (strcmp(command, "LED OFF") == 0) {
ledStatus = LedStatus::Off;
} else if (strcmp(command, "LED IDLE") == 0) {
ledStatus = LedStatus::Idle;
} else if (strcmp(command, "LED WORKING") == 0) {
ledStatus = LedStatus::Working;
} else if (strcmp(command, "LED COMPLETE") == 0) {
ledStatus = LedStatus::Complete;
} else if (strcmp(command, "LED NEEDS_INPUT") == 0) {
ledStatus = LedStatus::NeedsInput;
} else if (strcmp(command, "LED ERROR") == 0) {
ledStatus = LedStatus::Error;
} else if (strcmp(command, "LED DICTATION") == 0) {
ledStatus = LedStatus::Dictation;
} else if (strcmp(command, "LED FLASH_ACTION") == 0) {
actionFlashUntilMs = millis() + ACTION_FLASH_MS;
return;
} else {
return;
}
actionFlashUntilMs = 0;
updateRgb(millis());
}
void readSerialCommands() {
while (Serial.available() > 0) {
const char received = static_cast<char>(Serial.read());
if (received == '\r') {
continue;
}
if (received == '\n') {
if (!serialCommandOverflow) {
serialCommand[serialCommandLength] = '\0';
applyLedCommand(serialCommand);
}
serialCommandLength = 0;
serialCommandOverflow = false;
continue;
}
if (serialCommandOverflow) {
continue;
}
if (serialCommandLength < SERIAL_COMMAND_CAPACITY - 1) {
serialCommand[serialCommandLength++] = received;
} else {
// Descarta una linea demasiado larga sin desbordar el buffer.
serialCommandLength = 0;
serialCommandOverflow = true;
}
}
}
void updateBuiltinLed() {
bool anyButtonPressed = false;
for (uint8_t index = 0; index < BUTTON_COUNT; ++index) {
anyButtonPressed = anyButtonPressed || stableButtonStates[index] == LOW;
}
digitalWrite(LED_BUILTIN, anyButtonPressed || builtinLedState ? HIGH : LOW);
}
void printReasoningLevelName() {
switch (reasoningLevel) {
case ReasoningLevel::Low:
Serial.print(F("LOW"));
break;
case ReasoningLevel::Medium:
Serial.print(F("MEDIUM"));
break;
case ReasoningLevel::High:
Serial.print(F("HIGH"));
break;
case ReasoningLevel::ExtraHigh:
Serial.print(F("XHIGH"));
break;
}
}
void printReasoningLevel() {
Serial.print(F("REASONING "));
printReasoningLevelName();
Serial.println();
}
ReasoningLevel reasoningLevelForReading(const uint16_t reading) {
const uint8_t currentLevel = static_cast<uint8_t>(reasoningLevel);
if (currentLevel < 3 &&
reading >= (currentLevel + 1) * POT_ZONE_WIDTH + POT_HYSTERESIS) {
return static_cast<ReasoningLevel>(currentLevel + 1);
}
if (currentLevel > 0 &&
reading + POT_HYSTERESIS < currentLevel * POT_ZONE_WIDTH) {
return static_cast<ReasoningLevel>(currentLevel - 1);
}
return reasoningLevel;
}
void updateReasoningPot(const unsigned long nowMs) {
if (nowMs - lastPotSampleMs < POT_SAMPLE_INTERVAL_MS) {
return;
}
lastPotSampleMs = nowMs;
rawPotReading = analogRead(REASONING_POT_PIN);
if (!reasoningLevelReady) {
filteredPotReading = rawPotReading;
if (nowMs < POT_STARTUP_DELAY_MS) {
return;
}
const uint8_t initialLevel = filteredPotReading / POT_ZONE_WIDTH;
reasoningLevel = static_cast<ReasoningLevel>(initialLevel > 3 ? 3
: initialLevel);
reasoningLevelReady = true;
printReasoningLevel();
return;
}
// Filtro IIR: reduce el ruido del ADC sin bloquear botones, RGB ni serie.
filteredPotReading =
(filteredPotReading * 7UL + static_cast<unsigned long>(rawPotReading)) /
8UL;
const ReasoningLevel nextLevel =
reasoningLevelForReading(filteredPotReading);
if (nextLevel != reasoningLevel) {
reasoningLevel = nextLevel;
printReasoningLevel();
}
}
void updatePotDiagnostics(const unsigned long nowMs) {
if (!reasoningLevelReady ||
nowMs - lastPotDiagnosticMs < POT_DIAGNOSTIC_INTERVAL_MS) {
return;
}
lastPotDiagnosticMs = nowMs;
Serial.print(F("[DEBUG] POT raw="));
Serial.print(rawPotReading);
Serial.print(F(" filtered="));
Serial.print(filteredPotReading);
Serial.print(F(" level="));
printReasoningLevelName();
Serial.println();
}
void printButtonEvent(const uint8_t index) {
if (index == 0) {
Serial.println(stableButtonStates[index] == LOW ? F("BUTTON PRESS")
: F("BUTTON RELEASE"));
return;
}
if (index == 1) {
Serial.println(stableButtonStates[index] == LOW ? F("BUTTON 2 PRESS")
: F("BUTTON 2 RELEASE"));
return;
}
if (index == 2) {
Serial.println(stableButtonStates[index] == LOW
? F("TASK PREVIOUS PRESS")
: F("TASK PREVIOUS RELEASE"));
return;
}
Serial.println(stableButtonStates[index] == LOW ? F("TASK NEXT PRESS")
: F("TASK NEXT RELEASE"));
}
void updateButton(const uint8_t index, const unsigned long nowMs) {
const bool currentButtonState = digitalRead(BUTTON_PINS[index]);
if (currentButtonState != rawButtonStates[index]) {
rawButtonStates[index] = currentButtonState;
lastButtonChangeMs[index] = nowMs;
}
if (rawButtonStates[index] != stableButtonStates[index] &&
nowMs - lastButtonChangeMs[index] >= DEBOUNCE_INTERVAL_MS) {
stableButtonStates[index] = rawButtonStates[index];
printButtonEvent(index);
updateBuiltinLed();
}
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(RGB_RED_PIN, OUTPUT);
pinMode(RGB_GREEN_PIN, OUTPUT);
pinMode(RGB_BLUE_PIN, OUTPUT);
pinMode(REASONING_POT_PIN, INPUT);
setRgb(0, 0, 0);
for (uint8_t index = 0; index < BUTTON_COUNT; ++index) {
pinMode(BUTTON_PINS[index], INPUT_PULLUP);
rawButtonStates[index] = digitalRead(BUTTON_PINS[index]);
stableButtonStates[index] = rawButtonStates[index];
}
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(115200);
const unsigned long serialStartMs = millis();
while (!Serial && millis() - serialStartMs < SERIAL_WAIT_MS) {
// Espera limitada.
}
Serial.println();
Serial.println(F("=== OpenMicro controles, razonamiento y estado RGB ==="));
Serial.print(F("[OK] Arranque completado en "));
Serial.print(millis());
Serial.println(F(" ms"));
Serial.println(F("[OK] Pulsadores D2-D5 con INPUT_PULLUP."));
Serial.println(F("[OK] Potenciometro de razonamiento en A0."));
Serial.println(F("[OK] LED RGB: R=D9, G=D10, B=D11, catodo comun a GND."));
Serial.println(F("[INFO] El RGB permanece apagado hasta conectar el puente."));
updateBuiltinLed();
}
void loop() {
const unsigned long nowMs = millis();
readSerialCommands();
updateRgb(nowMs);
updateReasoningPot(nowMs);
updatePotDiagnostics(nowMs);
for (uint8_t index = 0; index < BUTTON_COUNT; ++index) {
updateButton(index, nowMs);
}
if (nowMs - lastBlinkMs >= BLINK_INTERVAL_MS) {
lastBlinkMs = nowMs;
builtinLedState = !builtinLedState;
updateBuiltinLed();
if (!builtinLedState) {
++heartbeatCount;
Serial.print(F("[OK] heartbeat "));
Serial.print(heartbeatCount);
Serial.print(F(" | uptime: "));
Serial.print(nowMs);
Serial.println(F(" ms"));
}
}
}