-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrontBikeLight.ino
503 lines (430 loc) · 11.4 KB
/
FrontBikeLight.ino
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include <avr/io.h>
//#include "TimerOne.h"
#include "bike_light_constants.h"
#include <SoftwareSerial.h>
#include <SoftModem.h>
#include <ctype.h>
#include "Tlc5940.h"
#include "tlc_animations.h"
#include "SimpleTimer.h"
SoftModem modem;
SimpleTimer signalTimer;
#define MYSERIALON
#ifdef MYSERIALON
SoftwareSerial mySerial(SERIAL_RX, SERIAL_TX); // RX, TX
#define mySerial mySerial
#else
#define mySerial Serial
#endif
//#define radio void
// ============================================================================ //
// RADIO SETUP
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(RADIO_PIN_CE,RADIO_PIN_CSN);
// Radio pipe addresses for the 2 nodes to communicate.
//const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
//const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
const uint64_t writingPipe = 0xF0F0F0AA;
// The debug-friendly names of those roles
int retryCounter = 4;
// ============================================================================ //
// MODE HELPERS
bool isModeSwitchState(unsigned int mode) {
if ( mode >= LED_STATE_ALL_OFF && mode < LED_TOTAL_STATES) {
return true;
}
return false;
}
void incrementModeState() {
ledDefaultState++;
if (!isModeSwitchState(ledDefaultState) ) {
ledDefaultState = LED_STATE_ALL_OFF;
}
}
void modeButtonPress() {
printf("MODE BUTTON PRESS: ");
incrementModeState();
printf("%d\n\r",ledDefaultState);
currentLedState = ledDefaultState;
}
// ============================================================================ //
// BUTTON PRESS HELPERS
void hornButtonPress() {
printf("HORN BUTTON PRESS: ");
currentHornState = HORN_STATE_SIREN;
}
bool checkButton(int pin) {
if (digitalRead(pin) == HIGH) {
mySerial.print("Button Pressed: ");
mySerial.print(pin);
mySerial.println(" - HIGH");
while (digitalRead(pin) == HIGH) {
delay(30);
}
return true;
}
return false;
}
bool buttonStateChanged() {
bool rightButton = rightButtonPressed();
if (rightButton) {
mySerial.println("Right Press");
return true;
}
bool leftButton = leftButtonPressed();
if (leftButton) {
mySerial.println("Left Press");
return true;
}
bool modeButton = modeButtonPressed();
if (modeButton) {
deleteCurrentTimer();
mySerial.println("Mode Press");
return true;
}
bool cancelButton = cancelButtonPressed();
if (cancelButton) {
deleteCurrentTimer();
mySerial.println("Cancel Press");
return true;
}
return false;
}
bool modeButtonPressed() {
bool check = checkButton(MODE_PIN);
if (check) {
modeButtonPress();
return true;
}
return false;
}
void startSignalTimer(int time) {
currentTimerID = signalTimer.setTimeout(time, deleteCurrentTimer);
mySerial.print("Starting timer: ");
mySerial.println(currentTimerID);
}
bool leftButtonPressed() {
bool check = checkButton(LEFT_PIN);
if (check) {
currentLedState = LED_STATE_LEFT;
startSignalTimer(8000);
return true;
}
return false;
}
bool rightButtonPressed() {
bool check = checkButton(RIGHT_PIN);
if (check) {
currentLedState = LED_STATE_RIGHT;
startSignalTimer(8000);
return true;
}
return false;
}
bool cancelButtonPressed() {
bool check = checkButton(CANCEL_PIN);
if (check) {
currentLedState = ledDefaultState;
return true;
}
return false;
}
int hornClickCounter = 0;
int elapsedTimeSincePress = 0;
// ============================================================================ //
// INIT HELPERS
void rf24Init () {
mySerial.println("Starting Radio Initialization");
radio.begin();
// Setting Power amplification to highest for testing
radio.setPALevel(RF24_PA_MAX);
// optionally, increase the delay between retries & # of retries
radio.setRetries(15,15);
// optionally, reduce the payload size. seems to
// improve reliability
// radio.setPayloadSize(8);
radio.openWritingPipe(writingPipe);
radio.printDetails();
// char *moreDetail = radio.getStats();
// mySerial.println(moreDetail);
mySerial.println("Finished Radio Initialization");
}
void buttonPinInit () {
mySerial.println("Starting Button Pin Initialization");
pinMode(MODE_PIN, INPUT);
pinMode(LEFT_PIN, INPUT);
pinMode(RIGHT_PIN, INPUT);
pinMode(CANCEL_PIN, INPUT);
// digitalWrite(MODE_PIN, LOW);
// digitalWrite(LEFT_PIN, LOW);
// digitalWrite(RIGHT_PIN, LOW);
// digitalWrite(CANCEL_PIN, LOW);
// pinMode(HORN_IN_PIN, INPUT);
// pinMode(HORN_OUT_PIN, OUTPUT);
mySerial.println("Finished Button Pin Initialization");
}
void softwareSerialInit() {
// mySerial.println("Starting Software Serial Initialization");
mySerial.begin(4800);
mySerial.println("Finished Software Serial Initialization");
}
void audioModemInit() {
mySerial.println("Starting Audio Modem Initialization");
modem.begin();
mySerial.println("Finished Audio Modem Initialization");
}
void tlcInit () {
mySerial.println("Starting TLC Modem Initialization");
Tlc.init();
delay(20);
allOn();
delay(50);
allOff();
delay(50);
allOn();
delay(50);
allOff();
mySerial.println("Finished TLC Initialization");
}
// ============================================================================ //
// OTHER HELPERS
void deleteCurrentTimer(){
if (currentTimerID!=NO_TIMER)
{
mySerial.print("Deleting Current Timer: ");
mySerial.println(currentTimerID);
signalTimer.deleteTimer(currentTimerID);
currentTimerID = NO_TIMER;
currentLedState = ledDefaultState;
}
}
// ============================================================================ //
// SETUP
void setup() {
// set up the role pin
// DON'T PRINT OVER SERIAL, PINS 0 and 1 will not work. <--blog this
// Serial.begin(57600);
// while (!Serial) {
// ; // wait for serial port to connect. Needed for Leonardo only
// }
// Serial.println("Bike Buddy Starting Initialization");
#ifdef MYSERIALON
softwareSerialInit();
#endif
buttonPinInit();
tlcInit();
rf24Init();
mySerial.println("\nBike Buddy Remote Initialization\n\r");
}
LedState convertCharacterToCommand(char c) {
switch (c) {
case 'A':
return LED_STATE_RIGHT;
case 'B':
return LED_STATE_LEFT;
case 'C':
return LED_STATE_ALL_OFF;
case 'D':
return LED_STATE_ALL_ON;
case 'E':
return LED_STATE_PIN_WHEEL;
case 'F':
return LED_STATE_BLINK;
default:
return LED_STATE_NONE;
break;
}
}
LedState checkModemForData() {
if (modem.available()){
// Serial.println("CHECKING MODEM");
int c = modem.read();
if(isprint(c)){
LedState command = convertCharacterToCommand(c);
// Serial.println((char)c);
return command;
}
else{
// Serial.print("(");
// Serial.print(c,HEX);
// Serial.println(")");
}
}
// if(Serial.available()){
// modem.write(0xff);
// while(Serial.available()){
// char c = Serial.read();
// modem.write(c);
// }
// }
return LED_STATE_NONE;
}
void displayLedsForState(int state) {
mySerial.print("Current State: ");
mySerial.println(state);
switch (state) {
case LED_STATE_RIGHT: {
fadeAnimation(RIGHT_ARROW, NUM_LEDS, 9, false);
break;
}
case LED_STATE_LEFT: {
fadeAnimation(LEFT_ARROW, NUM_LEDS, 9, false);
break;
}
case LED_STATE_ALL_ON: {
allOn();
delay(50);
break;
}
case LED_STATE_ALL_OFF: {
allOff();
delay(50);
break;
}
case LED_STATE_BLINK: {
playAnimation(40, BLINK_FADE_ANN, 11, false);
playAnimation(20, BLINK_FADE_ANN, 11, true);
break;
}
case LED_STATE_PIN_WHEEL: {
playAnimation(70, CLOCK_PULSE_ANN, 10, false);
break;
}
default:
mySerial.println("Wacky State!!!");
break;
}
// end led role
}
void loop()
{
if (currentTimerID!=NO_TIMER) {
signalTimer.run();
}
bool receivedAudioModemInput = false;
// TODO enable if using audio modem feature
// LedState newState = checkModemForData();
// if (LED_STATE_NONE != newState) {
// mySerial.print("Received New State: ");
// mySerial.println(newState);
// currentLedState = newState;
// receivedAudioModemInput = true;
// }
bool buttonStateDidChange = buttonStateChanged();
if (retryCounter != 3 || buttonStateDidChange || receivedAudioModemInput)
{
broadcastMessage(currentLedState);
}
displayLedsForState(currentLedState);
}
bool broadcastMessage(int message) {
int messageLen = sizeof(message);
bool success = true;
mySerial.print("Sending Message: ");
mySerial.println(message);
bool ok = radio.write( &message, messageLen );
if (ok) {
mySerial.println("ok\n\r");
retryCounter = 3;
radio.powerDown();
}
else {
success = false;
bool tx_ok =1, tx_fail=1, rx_ready=1;
radio.whatHappened(tx_ok, tx_fail, rx_ready);
char buf[100];
sprintf(buf,"fail retries left=%d, tx_ok=%d, tx_fail=%d, rx_ready=%d\n\r",retryCounter, tx_ok, tx_fail, rx_ready);
mySerial.println(buf);
if (retryCounter>0) {
char buf2[100];
sprintf(buf2,"retry\n\r",tx_ok, tx_fail, rx_ready);
mySerial.println(buf2);
retryCounter--;
} else {
radio.powerDown();
// todo issue a fail beep regardless that the message failed
retryCounter = 3;
mySerial.println("failed\n\r");
}
}
return success;
}
// ============================================================================ //
// TLC Functions
bool canDelayFor(int time) {
bool buttonStateDidChange = buttonStateChanged();
if (buttonStateDidChange) {
return false;
}
delay(time);
return true;
}
void playAnimation(int withDelay, const int **animation, const int annLen, bool invert) {
if (invert) {
for (int i=annLen-2; i> 0; i--) {
toggleTLCLeds(animation[i]);
if (!canDelayFor(withDelay)) {
return;
}
}
} else {
for (int i=0; i< annLen; i++) {
toggleTLCLeds(animation[i]);
if (!canDelayFor(withDelay)) {
return;
}
}
}
}
void toggleTLCLeds(const int *ledsToToggle) {
/* Tlc.clear() sets all the grayscale values to zero, but does not send
them to the TLCs. To actually send the data, call Tlc.update() */
Tlc.clear();
for (int channel=0; channel< NUM_LEDS; channel++) {
int mappedChannel = mapChannelIndexToLEDPosition[channel];
int strength = ledsToToggle[channel] * 455;
if (strength > 0) {
Tlc.set(mappedChannel, strength);
}
}
/* Tlc.update() sends the data to the TLCs. This is when the LEDs will
actually change. */
while (Tlc.update());
}
void fadeAnimation(const int *ledArray, int annlen, int stepFromTo, bool fadeIn) {
int *arrCopy = (int*)calloc(sizeof(int), annlen);
if (!fadeIn) {
memcpy(arrCopy, ledArray, sizeof(int) *annlen);
}
for (int j=0; j<stepFromTo; j++) {
for (int i=0; i< annlen; i++) {
if (fadeIn) {
if (arrCopy[i]<stepFromTo && ledArray[i]>0) {
arrCopy[i]++;
}
}
else {
if (arrCopy[i]>0) {
arrCopy[i]--;
}
}
}
toggleTLCLeds(arrCopy);
if (!canDelayFor(30)) {
free (arrCopy);
return;
}
}
free (arrCopy);
}
void allOff() {
Tlc.clear();
Tlc.update();
}
void allOn() {
Tlc.setAll(4095);
Tlc.update();
}