-
Notifications
You must be signed in to change notification settings - Fork 1
/
shiftlight.ino
668 lines (615 loc) · 22.5 KB
/
shiftlight.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
// Code inspired by a post by ivan141 on the arduino forum
// http://forum.arduino.cc/index.php/topic,8420.0.html
// Modified by Jason Melvin to
// * use an Adafruit NeoPixel strip
// * use integer math
// * only work with the latest accumulated interrupt cycle
// * use micros for better resolution at high rpm
// * use an encoder and variable settings for brightness, color scheme, rpm setpoints
// * save states to EEPROM
// ** VERSION 2 **//
// * allow for adjustment of the config variables
// * 1k and 100 digit representation with 4-bit binary (binary coded decimal)
#include <Adafruit_NeoPixel.h>
#include <Encoder.h>
#include <Bounce.h>
#include <EEPROM.h>
#include <EEPROMAnything.h>
// serial functionality
#define SERIALDEBUG_MASTER 0 // master switch (requires one below too)
#define SERIALDEBUG_SLOW 1 // button + encoder feedback
#define SERIALDEBUG_LIVE 0 // rpm calculation feedback
#define milBlinkIntervalLEDs 50 // ms interval for blinking LEDs at redline
#define milBlinkIntervalBrake 250 // ms interval for blinking brakes at redline
#define milBlinkIntervalAdj 500 // ms interval for adjustment blinking
#define numAVG 2 // number of tach interrupts to accumulate
#define milTIMEOUT_DELAY 10000 // exit setting mode if not adjusted for this long (ms)
#define milBUTTON_HOLD 700 // how long you must hold the button to get into setting mode
#define OFFSET_EEPROM 0 // memory start address
#define CONFIG_START 16 //memory address for config
#define encCHUNK 4 // decrease the sensitivity of the encoder
// list of states
enum mode_t { STOP , RUN , BRIGHT , SET , ADJ , BRIEF };
enum wipe_t { SOLID , GROW , WIPE };
enum brake_t { NONE , BRAKE };
uint8_t configBright = 32;
mode_t mode = RUN;
uint8_t iConfig = 0;
// config parameters saved to EEPROM
#define CONFIG_VERSION "s03" // version number
//***** (make sure to increment when changing structure)
#define numCONFIGS 5
struct configStruct { // structure to hold settings
char version[4]; // settings structure version
uint16_t RPMs[numCONFIGS][4];
uint8_t colors[numCONFIGS][4];
wipe_t wipe[numCONFIGS];
brake_t brake[numCONFIGS];
};
configStruct configurationDefault = { //define defaults
CONFIG_VERSION,
{
{ 5500, 7800, 8000, 8100},
{ 5500, 7800, 8000, 8100},
{ 5500, 7500, 8000, 8100},
{ 5500, 7500, 8000, 8100},
{ 1500, 2000, 3000, 4000}
},
{
{ 85 , 0, 85, 170 },
{ 85 , 85, 170, 170 },
{ 85 , 0 , 85, 170 },
{ 85 , 85, 170, 170 },
{ 85 , 0, 85, 170 }
},
{ WIPE , SOLID , WIPE , SOLID , WIPE },
{ BRAKE , BRAKE , BRAKE , BRAKE , NONE }
};
configStruct config = configurationDefault; // initialize to default params
// dot // bar // wipe // flash // wipe mode // "brake" during wipe
/*
color: 0 = green;
85 = red;
170 = blue;
wipe mode: 0 = solid;
1 = growing;
2 = wipe (from bar>wipe)
*/
/* // place holder for a possible matrix to scale brightness of individual segments
float configBrightnessScalers[numCONFIGS][4] = {
{ 0.7, 1.0, 1.0, 1.0},
{ 0.7, 1.0, 1.0, 1.0},
{ 0.7, 1.0, 1.0, 1.0},
{ 0.6, 0.6, 1.0, 1.0},
{ 0.3, 0.3, 0.6, 1.0}
};
*/
void configRead (){
if (EEPROM.read(OFFSET_EEPROM + 0) <= numCONFIGS){
iConfig = EEPROM.read(OFFSET_EEPROM + 0); // recall configuration index
}
configBright = EEPROM.read(OFFSET_EEPROM + 1); // recall brightness
// recall matrix of configs
if (EEPROM.read(OFFSET_EEPROM + CONFIG_START + 0) == CONFIG_VERSION[0] &&
EEPROM.read(OFFSET_EEPROM + CONFIG_START + 1) == CONFIG_VERSION[1] &&
EEPROM.read(OFFSET_EEPROM + CONFIG_START + 2) == CONFIG_VERSION[2] ) {
EEPROM_readAnything(OFFSET_EEPROM + CONFIG_START, config);
}
else {
configWrite();
}
}
void configWrite (){
EEPROM_writeAnything(OFFSET_EEPROM + CONFIG_START, config);
}
void configDefault(){
config = configurationDefault;
}
// hardware setup
#define numLEDs 8
#define pinLED_DATA 4
#define pinTACH 6 // just for info; defined in interrupt setup below
#define pinBRAKELIGHT 12
#define pinBUTTON 3
#define pinENC_A 7
#define pinENC_B 8
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numLEDs, pinLED_DATA, NEO_GRB + NEO_KHZ800);
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// RPM Calculation variables
volatile uint32_t micTachInterrupt;
volatile uint32_t micTachInterrupt_last;
uint32_t rpmInst, rpmAvg;
// Blink
uint8_t blinkStateLEDs = HIGH;
uint32_t milBlinkPrevLEDs = 0;
uint8_t blinkStateBrake = HIGH;
uint32_t milBlinkPrevBrake = 0;
uint8_t blinkStateAdj = HIGH;
uint32_t milBlinkPrevAdj = 0;
// Encoder
Encoder myEnc(pinENC_A, pinENC_B);
int32_t encOld = 0;
Bounce button = Bounce( pinBUTTON, 5);
// time tracking for config exit delay and display
uint32_t milTimeout;
// config adjustment variables
uint8_t iConfigAdj; // index for config variable
enum adjust_t { COLOR , RPM };
adjust_t parmAdj = COLOR;
// Init function
void setup()
{
pinMode(pinBRAKELIGHT, OUTPUT);
digitalWrite(pinBRAKELIGHT, LOW);
pinMode(pinBUTTON, INPUT);
digitalWrite(pinBUTTON, HIGH); // enable pullup resistor
configRead(); // recall config if available
if(SERIALDEBUG_MASTER) {
Serial.begin(115200);
Serial.println("Recalled parameters:");
Serial.print("mode: "); Serial.println(mode);
Serial.print(" brightness: "); Serial.println(configBright);
Serial.println(" iConfig: "); Serial.println(iConfig);
}
// set up the LED strip
strip.begin();
strip.setBrightness(8);
rainbow(2); // a colorful intro on powerup
strip.setBrightness(configBright);
strip.show();
// start tachometer interrupts
attachInterrupt(1, tachMonitor, RISING);
}
// Main loop
void loop() {
processTach(); // process log entries to calculate rpm
inputSerial(); // read serial input if enabled and available
inputButton(); // check for button presses
inputEncoder(); // check for encoder motion
checkTimeout();
updateDisplay(); // update display
delay(5); //wait a while // why?
}
void updateDisplay() {
if (mode == SET || mode == ADJ || mode == BRIGHT || mode == BRIEF){
if (mode == SET || mode == BRIGHT || mode == BRIEF) {
showColors();
}
else if (mode == ADJ){
if (parmAdj == COLOR) {
showConfigColor();
}
else if (parmAdj == RPM) {
showConfigRPM();
}
// compute blink timer
if( millis() - milBlinkPrevAdj > milBlinkIntervalAdj ) {
blinkStateAdj = blinkStateAdj ? LOW : HIGH;
milBlinkPrevAdj = millis();
}
}
}
else if (mode == RUN){
//** RPM > redline
if (rpmAvg >= config.RPMs[iConfig][3]){
// blinking LEDs at redline
colorBar(blinkStateLEDs ? Wheel(config.colors[iConfig][3]) : 0, numLEDs);
// blinking brake light at redline
digitalWrite(pinBRAKELIGHT, blinkStateBrake ? HIGH : LOW);
// compute the blink timers
if( millis() - milBlinkPrevLEDs > milBlinkIntervalLEDs ) {
blinkStateLEDs = blinkStateLEDs ? LOW : HIGH;
milBlinkPrevLEDs = millis();
}
if( millis() - milBlinkPrevBrake > milBlinkIntervalBrake ) {
blinkStateBrake = blinkStateBrake ? LOW : HIGH;
milBlinkPrevBrake= millis();
}
}
else {
// reset the blink timers so light is on immediately above redline
blinkStateLEDs = HIGH;
blinkStateBrake = HIGH;
//** shift < RPM < redline
if ( rpmAvg >= config.RPMs[iConfig][2] ){
if( config.wipe[iConfig] == WIPE) barWipe(2); // wipe from previous color to [2]
else if (config.wipe[iConfig] == GROW) barGrow(2); // growing bar of color [2]
else colorBar(Wheel(config.colors[iConfig][2]), numLEDs); // solid bar of color index [2] (wipes == 0)
if( config.brake[iConfig] ) digitalWrite(pinBRAKELIGHT, HIGH ); // optionally flash the brakelight
else digitalWrite(pinBRAKELIGHT, LOW);
}
else {
digitalWrite(pinBRAKELIGHT, LOW);
// pwr < RPM < shift
if ( rpmAvg >= config.RPMs[iConfig][1] ){
barGrow(1); // growing bar from pwr > shift
}
// start < RPM < pwr
else if ( rpmAvg >= config.RPMs[iConfig][0] ) {
colorBar(Wheel(config.colors[iConfig][0]),1); // one light above first threshold
}
// RPM < start
else {
colorBar(0,numLEDs);
}
}
}
}
// if off, then show nothing
else if (mode == STOP) colorBar(0, numLEDs);
strip.show();
}
// read the latest interval of accumulated interrupts and compute RPM
void processTach() {
uint32_t micTachInterval; // elapsed time for numAVG tach events
uint32_t micTachInterrupt_local, micTachInterrupt_last_local;
noInterrupts(); // disable interrupts while we copy the values
micTachInterrupt_local = micTachInterrupt;
micTachInterrupt_last_local = micTachInterrupt_last;
interrupts(); // reenable interrupts
micTachInterval = micTachInterrupt_local - micTachInterrupt_last_local;
if(SERIALDEBUG_MASTER && SERIALDEBUG_LIVE) {
Serial.print("t:"); Serial.print(micTachInterrupt); Serial.print("-"); Serial.println(micTachInterrupt_last);
}
if ( micTachInterval ){ // avoid divide by 0
rpmInst = 30000000UL / micTachInterval * (uint32_t)numAVG;
// Acura NSX (6 cyl), 20000000UL
// Honda Civic (4 cyl), 30000000UL
rpmAvg = ( rpmAvg * 2UL + rpmInst ) / 3UL; // smoothing function for rpm
if(SERIALDEBUG_MASTER && SERIALDEBUG_LIVE) {
Serial.print("micTachInterval: "); Serial.print(micTachInterval);
Serial.print(" rpmInst: "); Serial.print(rpmInst);
Serial.print(" rpmAvg: "); Serial.println(rpmAvg);
}
}
}
// Tach interrupt function
void tachMonitor(){
static uint8_t iTachInterrupts;
// accumulate numAVG interrupts before calculating RPM
if ( iTachInterrupts < ( numAVG - 1) ) {
iTachInterrupts++;
}
// once accumulated, record the time in microseconds
else {
iTachInterrupts = 0;
micTachInterrupt_last = micTachInterrupt;
micTachInterrupt = micros();
}
}
// encoder read function
void inputEncoder() {
int32_t encNew = myEnc.read();
int32_t encChange = encNew - encOld;
if ( abs(encChange) >= encCHUNK ) {
encOld = encNew;
milTimeout = millis();
// if (SERIALDEBUG_MASTER && SERIALDEBUG_SLOW) { Serial.print("encChange="); Serial.println(encChange); }
// if in setting mode, change the index of the configuration matrix
if (mode == SET) {
encChange /= encCHUNK; // use chunks to ensure fine control (move by a single index)
if ( (encChange>0 && iConfig < numCONFIGS-1) || (encChange<0 && iConfig>0) ){
colorBar(0,numLEDs); // blink all pixels to indicate change of setting index
strip.show();
delay(75);
}
iConfig = constrain( iConfig+encChange, 0, numCONFIGS - 1 );
if (SERIALDEBUG_MASTER && SERIALDEBUG_SLOW) { Serial.print("iConfig="); Serial.println(iConfig); }
}
// adjust RPM
else if (mode == ADJ && parmAdj == RPM) {
encChange /= encCHUNK;
if ( encChange > 0) { // increased
config.RPMs[iConfig][iConfigAdj] =
constrain (config.RPMs[iConfig][iConfigAdj] + 100, 0, 9000);
// iConfig = constrain( iConfig+1, 0, numCONFIGS - 1 );
}
else { // decreased
config.RPMs[iConfig][iConfigAdj] =
constrain (config.RPMs[iConfig][iConfigAdj] - 100, 0, 9000);
// iConfig = constrain( iConfig-1, 0, numCONFIGS - 1 );
}
if (SERIALDEBUG_MASTER && SERIALDEBUG_SLOW) {
Serial.print("RPM for iConfig:"); Serial.print(iConfig);
Serial.print(" pos:"); Serial.print(iConfigAdj);
Serial.print(" = "); Serial.println(config.RPMs[iConfig][iConfigAdj]);
}
}
// adjust color
else if (mode == ADJ && parmAdj == COLOR) {
encChange /= encCHUNK;
if (iConfigAdj < 4){
if ( encChange > 0) { // increased
config.colors[iConfig][iConfigAdj] += 5;
}
else { // decreased
config.colors[iConfig][iConfigAdj] -= 5;
}
if (SERIALDEBUG_MASTER && SERIALDEBUG_SLOW) {
Serial.print("Color for iConfig:"); Serial.print(iConfig);
Serial.print(" pos:"); Serial.print(iConfigAdj);
Serial.print(" = "); Serial.println(config.colors[iConfig][iConfigAdj]);
}
}
// adjust wipe mode
else if (iConfigAdj == 4) {
if ( encChange > 0) { // increased
if ( config.wipe[iConfig] == SOLID ) config.wipe[iConfig] = GROW;
else if ( config.wipe[iConfig] == GROW ) config.wipe[iConfig] = WIPE;
else if ( config.wipe[iConfig] == WIPE ) config.wipe[iConfig] = SOLID;
}
else { // decreased
if ( config.wipe[iConfig] == SOLID ) config.wipe[iConfig] = WIPE;
else if ( config.wipe[iConfig] == WIPE ) config.wipe[iConfig] = GROW;
else if ( config.wipe[iConfig] == GROW ) config.wipe[iConfig] = SOLID;
}
if (SERIALDEBUG_MASTER && SERIALDEBUG_SLOW) {
Serial.print("Wipe for iConfig:"); Serial.print(iConfig);
Serial.print(" = "); Serial.println(config.wipe[iConfig]);
}
}
// adjust brake while wipe
else if (iConfigAdj == 5) {
config.brake[iConfig] = config.brake[iConfig] ? NONE : BRAKE;
if (SERIALDEBUG_MASTER && SERIALDEBUG_SLOW) {
Serial.print("Brake for iConfig:"); Serial.print(iConfig);
Serial.print(" = "); Serial.println(config.brake[iConfig]);
}
}
}
// adjust brightness // also set to run mode if off
else {
// encChange /= encCHUNK; // optionally use chunks to ensure fine control (move by a single index)
configBright = constrain( configBright + encChange , 0 , 255);
strip.setBrightness( configBright );
if (SERIALDEBUG_MASTER && SERIALDEBUG_SLOW) { Serial.print("brightness="); Serial.println(configBright); }
mode = BRIGHT;
}
}
}
void inputButton(){
static uint8_t flagBtnEnable;
if (button.update ()) {
milTimeout = millis(); // check for button state and update timeout if action
}
// action when button held down and not in set mode
if ( !button.read() && button.duration() > milBUTTON_HOLD && flagBtnEnable){
flagBtnEnable = false; // disable btn so release has no action
if (mode == RUN || mode == BRIGHT) {
if(SERIALDEBUG_MASTER && SERIALDEBUG_SLOW) { Serial.println("btn held; mode=set"); }
mode = SET;
}
// action when held and already in set mode
else if ( mode == SET ) {
if(SERIALDEBUG_MASTER && SERIALDEBUG_SLOW) { Serial.println("btn held; mode=adj"); }
iConfigAdj = 0;
mode = ADJ;
}
else if ( mode == ADJ ) {
if(SERIALDEBUG_MASTER && SERIALDEBUG_SLOW) { Serial.println("btn held; exit Adj"); }
mode = RUN;
configWrite();
} // count when button pressed so releasing after long hold doesn't trigger tap action
}
// button depressed
else if ( button.fallingEdge() ) {
flagBtnEnable = true; // enable btn
}
// action when button was tapped (held for less than milBUTTON_HOLD)
else if ( button.risingEdge() && flagBtnEnable) {
// if in setting mode, store the new settings index and exit setting mode
if (mode == SET) {
if(SERIALDEBUG_MASTER) { Serial.println("EEPROM write: iConfig"); }
EEPROM.write( OFFSET_EEPROM + 0 , iConfig); // store color index
mode = RUN;
}
else if (mode == ADJ){
if (parmAdj == COLOR) { // if adjusting color
if (iConfigAdj < 4) parmAdj = RPM; // switch to RPM if there is a next setpoint
else { // if done with setpoints then go to wipe mode and brake mode
iConfigAdj++;
if (iConfigAdj > 5) iConfigAdj = 0; // return to color of first setpoint
}
}
else if (parmAdj == RPM){ // if adjusting RPM, switch to next color
iConfigAdj++;
parmAdj = COLOR;
}
}
// if in brightness mode, store the new brightness
else if (mode == BRIGHT){
if(SERIALDEBUG_MASTER) { Serial.println("EEPROM write: brightness"); }
EEPROM.write( OFFSET_EEPROM + 1 , configBright); // store brightness
mode = RUN;
}
// if in normal operation, toggle on/off state of display and save setting
else if (mode == RUN) {
mode = STOP;
}
else if (mode == STOP) {
mode = BRIEF;
}
}
}
void checkTimeout(){
// exit from pending changes if no action for milTIMEOUT_DELAY
if ( (mode == SET || mode == BRIGHT) && ( millis() - milTimeout > milTIMEOUT_DELAY ) ){
if(SERIALDEBUG_MASTER && SERIALDEBUG_SLOW) { Serial.println("timeout, SET/BRIGHT to RUN"); }
colorBar(0,numLEDs);
mode = RUN;
}
else if ( mode == ADJ && ( millis() - milTimeout > 4*milTIMEOUT_DELAY ) ){
if(SERIALDEBUG_MASTER && SERIALDEBUG_SLOW) { Serial.println("timeout, ADJ to RUN"); }
colorBar(0,numLEDs);
mode = RUN;
}
else if ( mode == BRIEF && ( millis() - milTimeout > milTIMEOUT_DELAY/4 ) ){
if(SERIALDEBUG_MASTER && SERIALDEBUG_SLOW) { Serial.println("timeout, BRIEF to RUN"); }
colorBar(0,numLEDs);
mode = RUN;
}
}
// function for growing bar
inline void barGrow(uint8_t i){
colorBar( Wheel(config.colors[iConfig][i]),
((rpmAvg - config.RPMs[iConfig][i]) * numLEDs) / (config.RPMs[iConfig][i+1] - config.RPMs[iConfig][i]) + 1
);
}
// function for wipe from config.RPMs[iConfig][i] of config.colors[iConfig][i] with background of index [i-1]
inline void barWipe(uint8_t i){
uint8_t numBar = ((rpmAvg - config.RPMs[iConfig][i]) * numLEDs) / (config.RPMs[iConfig][i+1] - config.RPMs[iConfig][i]) + 1;
colorBarPart(Wheel(config.colors[iConfig][i]), 0, numBar);
colorBarPart(Wheel(config.colors[iConfig][i-1]), numBar, numLEDs - numBar);
}
// display a bar of the four color segments, for feedback when changing config/brightness
void showColors() {
colorBarPart(Wheel(config.colors[iConfig][0] ), 0, 1);
colorBarPart(Wheel(config.colors[iConfig][1] ), 1, numLEDs - 5);
colorBarPart(Wheel(config.colors[iConfig][2] ), numLEDs - 4, 2);
colorBarPart(Wheel(config.colors[iConfig][3] ), numLEDs - 2, 2);
}
void showConfigColor(){
if (iConfigAdj < 4) // for the individual setpoints, paint whole bar with one color (to show current color setting)
colorBar(Wheel(config.colors[iConfig][iConfigAdj]), numLEDs);
else // for wipe mode and switched output, show the combo of colors
showColors();
// flash pixel(s) for iConfigAdj (to show which segment is being adjusted)
switch (iConfigAdj){
case 0:
colorBarPart(blinkStateAdj ? Wheel(config.colors[iConfig][0]) : 0, 0, 1);
break;
case 1:
colorBarPart(blinkStateAdj ? Wheel(config.colors[iConfig][1]) : 0, 1, numLEDs - 5);
break;
case 2:
colorBarPart(blinkStateAdj ? Wheel(config.colors[iConfig][2]) : 0, numLEDs - 4, 2);
break;
case 3:
colorBarPart(blinkStateAdj ? Wheel(config.colors[iConfig][3]) : 0, numLEDs - 2, 2);
break;
case 4:
// adjusting wipe mode
if (config.wipe[iConfig] == SOLID){
// show that wipe appears as a solid block (blink wipe)
colorBarPart(blinkStateAdj ? Wheel(config.colors[iConfig][2]) : 0, numLEDs - 4, 2);
}
else if (config.wipe[iConfig] == GROW){
// show that wipe grows from nothing (sequence wipe and blank bar)
colorBarPart(0, 1, numLEDs - 5);
colorBarPart(blinkStateAdj ? 0 : Wheel(config.colors[iConfig][2]), numLEDs - 4, 1);
colorBarPart(blinkStateAdj ? Wheel(config.colors[iConfig][2]) : 0, numLEDs - 3, 1);
}
else if (config.wipe[iConfig] == WIPE){
// show that wipe grows from bar (sequence wipe)
colorBarPart(blinkStateAdj ? 0 : Wheel(config.colors[iConfig][2]), numLEDs - 4, 1);
colorBarPart(blinkStateAdj ? Wheel(config.colors[iConfig][2]) : 0, numLEDs - 3, 1);
}
break;
case 5:
// switched output while wipe
colorBarPart(blinkStateAdj ? Wheel(config.colors[iConfig][2]) : 0, numLEDs - 4, 2);
digitalWrite(pinBRAKELIGHT, config.brake[iConfig] ? HIGH : LOW);
break;
}
}
void showConfigRPM(){
// convert 1000 and 100 digits to 4-bit binary
uint8_t showRPM_1000 = config.RPMs[iConfig][iConfigAdj] / 1000;
uint8_t showRPM_100 = (config.RPMs[iConfig][iConfigAdj] - 1000*showRPM_1000) / 100;
for (uint8_t i=0; i<4; i++){
strip.setPixelColor(i, (bitRead(showRPM_1000, 3-i) ? Wheel(85) : 0 ) );
}
for (uint8_t i=4; i<8; i++){
strip.setPixelColor(i, (bitRead(showRPM_100, 7-i) ? Wheel(170) : 0 ) );
}
if (numLEDs > 8) colorBarPart(0, 8, numLEDs - 8);
}
// allows control by numpad for testing
// press 0 to reset RPM to zero
// press 1/3 to decrease/increase by 10 RPM
// press 4/6 to decrease/increase by 100 RPM
// press 1/9 to decrease/increase by 1000 RPM
void inputSerial(){
uint8_t incomingByte;
if(SERIALDEBUG_MASTER && SERIALDEBUG_SLOW){
while(Serial.available()){
incomingByte = Serial.read();
// Serial.print("in: "); Serial.println(incomingByte);
switch (incomingByte) {
case 48: // 0
rpmAvg = 0; break;
case 49: // 1
rpmAvg -= 10; break;
case 50: // 2
break;
case 51: //3
rpmAvg += 10; break;
case 52: //4
rpmAvg -= 100; break;
case 53: //5
break;
case 54: //6
rpmAvg += 100; break;
case 55: //7
rpmAvg -= 1000; break;
case 56: //8
break;
case 57: //9
rpmAvg += 1000; break;
}
Serial.print("rpmAvg: "); Serial.println(rpmAvg);
}
}
}
// Adafruit NeoPixel functions
// input color and number of pixels
void colorBar(uint32_t c, uint8_t num){
for (uint16_t i=0; i<strip.numPixels();i++){
if (i < num){
strip.setPixelColor(i,c);
}
else {
strip.setPixelColor(i,0);
}
}
}
// input color, start position, and number of pixels
void colorBarPart(uint32_t c, uint8_t start, uint8_t num){
for (uint16_t i=(start); i<strip.numPixels();i++){
if (i < start + num){
strip.setPixelColor(i,c);
}
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r. (actually it looks like g-r-b)
uint32_t Wheel(uint8_t WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
// cycle a full bar through the rainbow with a delay betwen changes
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}