-
Notifications
You must be signed in to change notification settings - Fork 1
/
SlideBright.ino
955 lines (803 loc) · 23.9 KB
/
SlideBright.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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
/*
** SlideBright - Firmware for overengineered Hexbright flashlight.
** Hexbright: http://hexbright.com/
** ~ Tobias Jäger <[email protected]>
**
**
** Based on darcbright by Robert Quattlebaum <[email protected]>
**
** --------------------------------------------------------------------------
**
** List of Modes:
** * 3-brightnesse-level mode (43,175,500)
** * momentary mode
** * strobe mode
** * ajust-brightness-by-roll mode
*/
/*
Accelorometer axies:
(x,y,z)
If the flashlight is facing straight up:
(0, 21, 0)
If the flashlight is facing straight down:
(0, -21, 0)
If the flashlight is battery-up:
(0, 0, -21)
If the flashlight is battery-down:
(0, 0, 21)
If the flashlight is logo-up:
(-21, 0, 0)
If the flashlight is logo-down:
(21, 0, 0)
*/
#include <math.h>
#include <Wire.h>
#include <EEPROM.h>
#include "pt.h"
#include <avr/wdt.h>
// Settings
#define VOLTAGE_NOMINAL 3330
#define VOLTAGE_LOW 3100
#define OVERTEMP_SHUTDOWN_C 60
#define OVERTEMP_THROTTLE_C 50
#define OVERTEMP_SHUTDOWN (OVERTEMP_SHUTDOWN_C*10+500)
#define OVERTEMP_THROTTLE (OVERTEMP_THROTTLE_C*10+500)
#define BUTTON_BRIGHTNESS_THRESHOLD 1000 // time in ms, after which a button press turns off
#define BUTTON_DEBOUNCE 20
#define POWER_ON_BUTTON_THRESHOLD 150 // Period of time button initially needs to be held to keep the light on.
#define BRT_MIN_LUMEN 4
#define BRT_MED_LUMEN 255 // 175 lumens
#define BRT_LOW_LUMEN (BRT_MED_LUMEN/4) // 43 lumens
#define BRT_MAX_LUMEN (BRT_MED_LUMEN+(3*255)) // 500 lumens
// Constants
#define ACC_ADDRESS 0x4C
#define ACC_REG_XOUT 0
#define ACC_REG_YOUT 1
#define ACC_REG_ZOUT 2
#define ACC_REG_TILT 3
#define ACC_REG_INTS 6
#define ACC_REG_MODE 7
// Pin assignments
#define DPIN_RLED_SW 2
#define DPIN_GLED 5
#define DPIN_PWR 8
#define DPIN_DRV_MODE 9
#define DPIN_DRV_EN 10
#define DPIN_ACC_INT 3
#define APIN_TEMP 0
#define APIN_CHARGE 3
// Interrupts
#define INT_SW 0
#define INT_ACC 1
// State
byte light_mode;
unsigned short overtemp_max;
unsigned short amount_current;
unsigned short amount_begin;
unsigned short amount_end;
unsigned short amount_fade_duration;
unsigned long amount_fade_start;
byte amount_flash,amount_off;
// Protothread States
struct pt fade_control_pt;
struct pt power_pt;
struct pt button_led_pt;
// Mode Protothread States
struct pt light_pt;
struct pt light_momentary_pt;
struct pt light_blinky_pt;
struct pt light_knob_pt;
// Variables updated every loop
bool button_is_pressed;
unsigned long button_pressed_time;
unsigned long button_released_time;
unsigned long button_pressed_duration;
unsigned long button_released_duration;
short vcc_current;
short vcc_filter_table[3];
short vcc_filtered;
short temp_filter_table[3];
short temp_filtered;
short temp_current;
enum {
BATT_DISCHARGING,
BATT_CHARGING,
BATT_CHARGED,
} batt_state;
unsigned long time_current;
float angle_pitch;
float angle_roll;
#define PT_WAIT_FOR_PERIOD(pt,x) \
lastTime = time_current; \
PT_WAIT_UNTIL(pt, (time_current-lastTime) > (x));
bool orientation_enabled;
void
enable_orientation(void) {
pinMode(DPIN_ACC_INT, INPUT);
digitalWrite(DPIN_ACC_INT, HIGH);
// Configure accelerometer
static const byte config[] = {
ACC_REG_INTS, // First register (see next line)
0xE4, // Interrupts: shakes, taps
0x00, // Mode: not enabled yet
0x00, // Sample rate: 120 Hz
0x0F, // Tap threshold
0x10 // Tap debounce samples
};
Wire.beginTransmission(ACC_ADDRESS);
Wire.write(config, sizeof(config));
Wire.endTransmission();
// Enable accelerometer
static const byte enable[] = {ACC_REG_MODE, 0x01}; // Mode: active!
Wire.beginTransmission(ACC_ADDRESS);
Wire.write(enable, sizeof(enable));
Wire.endTransmission();
orientation_enabled = 1;
}
void
update_loop_variables(void) {
static char i;
// Time stamp for this loop cycle.
time_current = millis();
// Grab the voltage and the temperature.
vcc_filter_table[i] = vcc_current = readVcc();
temp_filter_table[i] = analogRead(APIN_TEMP);
temp_current = temp_filter_table[i]*(long)vcc_current/1024;
vcc_filtered = median_short(vcc_filter_table[0],vcc_filter_table[1],vcc_filter_table[2]);
temp_filtered *= 7;
temp_filtered += median_short(temp_filter_table[0],temp_filter_table[1],temp_filter_table[2])*(long)vcc_filtered/1024;
temp_filtered /= 8;
if(++i==3)
i=0;
// Read out the state of but button.
{
bool prev_value = digitalRead(DPIN_RLED_SW);
pinMode(DPIN_RLED_SW, INPUT);
digitalWrite(DPIN_RLED_SW, 0);
button_is_pressed = digitalRead(DPIN_RLED_SW);
pinMode(DPIN_RLED_SW, OUTPUT);
digitalWrite(DPIN_RLED_SW, prev_value);
}
if(button_is_pressed) {
button_released_time = time_current;
button_pressed_duration = time_current - button_pressed_time;
} else {
button_pressed_time = time_current;
button_released_duration = time_current - button_released_time;
button_pressed_duration = 0;
}
short chargeState = analogRead(APIN_CHARGE);
if (chargeState < 128) { // Low - charging
batt_state = BATT_CHARGING;
} else if (chargeState > 768) { // High - fully charged.
batt_state = BATT_CHARGED;
} else { // Hi-Z - Not charging, not pulged in.
// But if the voltage is over 3.4 volts, then we know we are plugged in.
if(vcc_current>3400)
batt_state = BATT_CHARGING;
else
batt_state = BATT_DISCHARGING;
}
if(orientation_enabled) {
char acc[3];
readAccelFiltered(acc);
angle_roll = atan2(acc[0],acc[2]);
angle_pitch = atan2(acc[1],sqrt(acc[0]*acc[0]+acc[2]*acc[2]));
}
// Check if the accelerometer wants to interrupt
// byte tapped = 0, shaked = 0;
// if (!digitalRead(DPIN_ACC_INT)) {
// Wire.beginTransmission(ACC_ADDRESS);
// Wire.write(ACC_REG_TILT);
// Wire.endTransmission(false); // End, but do not stop!
// Wire.requestFrom(ACC_ADDRESS, 1); // This one stops.
// byte tilt = Wire.read();
//
// if (time-lastAccTime > 500) {
// lastAccTime = time;
//
// tapped = !!(tilt & 0x20);
// shaked = !!(tilt & 0x80);
//
// if (tapped) Serial.println("Tap!");
// if (shaked) Serial.println("Shake!");
// }
// }
}
short
readVcc(void) {
short result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
noInterrupts();
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
interrupts();
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}
void readAccel(char *acc)
{
while (1)
{
Wire.beginTransmission(ACC_ADDRESS);
Wire.write(ACC_REG_XOUT);
Wire.endTransmission(false); // End, but do not stop!
Wire.requestFrom(ACC_ADDRESS, 3); // This one stops.
for (int i = 0; i < 3; i++)
{
if (!Wire.available())
continue;
acc[i] = Wire.read();
if (acc[i] & 0x40) // Indicates failed read; redo!
continue;
if (acc[i] & 0x20) // Sign-extend
acc[i] |= 0xC0;
}
break;
}
}
void readAccelFiltered(char *acc_filtered) {
static char acc[3][3];
static char i;
readAccel(acc[i++]);
if(i==3)
i=0;
acc_filtered[0] = median_char(acc[0][0],acc[1][0],acc[2][0]);
acc_filtered[1] = median_char(acc[0][1],acc[1][1],acc[2][1]);
acc_filtered[2] = median_char(acc[0][2],acc[1][2],acc[2][2]);
}
/* Returns the median value of the given three parameters */
char median_char(char a, char b, char c) {
if(a<c) {
if(b<a) {
return a;
} else if(c<b) {
return c;
}
} else {
if(a<b) {
return a;
} else if(b<c) {
return c;
}
}
return b;
}
short median_short(short a, short b, short c) {
if(a<c) {
if(b<a) {
return a;
} else if(c<b) {
return c;
}
} else {
if(a<b) {
return a;
} else if(b<c) {
return c;
}
}
return b;
}
void
retrieve_settings(void) {
byte data[4];
data[0] = EEPROM.read(0);
data[1] = EEPROM.read(1);
data[2] = EEPROM.read(2);
data[3] = EEPROM.read(3);
if(data[0] == (byte)~(data[1]^data[2]^data[3] + sizeof(data))) {
light_mode = data[1];
if(light_mode) {
set_amount(data[2]+((unsigned short)data[3]<<8));
}
Serial.println("Settings retrieved");
}
}
void
save_settings(void) {
byte data[4];
data[1] = light_mode;
data[2] = (byte)amount_current;
data[3] = (amount_current>>8);
data[0] = ~(data[1]^data[2]^data[3] + sizeof(data));
EEPROM.write(0,data[0]);
EEPROM.write(1,data[1]);
EEPROM.write(2,data[2]);
EEPROM.write(3,data[3]);
}
void set_brightness(unsigned short b) {
noInterrupts();
if(b<=255) {
digitalWrite(DPIN_DRV_MODE, LOW);
analogWrite(DPIN_DRV_EN, b);
} else if(b<=BRT_MAX_LUMEN) {
analogWrite(DPIN_DRV_MODE, ((long)b-BRT_MED_LUMEN)*255/((long)BRT_MAX_LUMEN-BRT_MED_LUMEN));
digitalWrite(DPIN_DRV_EN, HIGH);
} else {
digitalWrite(DPIN_DRV_EN, HIGH);
digitalWrite(DPIN_DRV_MODE, HIGH);
}
interrupts();
}
void
set_amount(unsigned short amount) {
amount_current = amount_begin = amount_end = amount;
amount_fade_duration = 0;
amount_off = 0;
set_brightness(amount_current);
pinMode(DPIN_PWR, OUTPUT);
if(time_current<POWER_ON_BUTTON_THRESHOLD || amount_current==0)
digitalWrite(DPIN_PWR, LOW);
else
digitalWrite(DPIN_PWR, HIGH);
}
void
fade_to_amount(unsigned short amount, unsigned short fade_duration) {
amount_begin = amount_current;
amount_end = amount;
amount_fade_duration = fade_duration;
amount_fade_start = millis();
amount_off = 0;
}
PT_THREAD(fade_control_pt_func(struct pt *pt))
{
static long fade_time;
PT_BEGIN(pt);
do {
PT_YIELD(pt);
if(amount_flash) {
analogWrite(DPIN_DRV_EN, 0);
fade_time = time_current;
PT_WAIT_UNTIL(pt, (time_current-fade_time) > (100));
}
amount_flash = 0;
fade_time = time_current - amount_fade_start;
if(fade_time >= amount_fade_duration) {
if(amount_current != amount_end) {
amount_current = amount_end;
}
} else {
amount_current = (((long)amount_end - (long)amount_begin)*fade_time)/amount_fade_duration + amount_begin;
}
pinMode(DPIN_PWR, OUTPUT);
if(time_current<POWER_ON_BUTTON_THRESHOLD || amount_current==0)
digitalWrite(DPIN_PWR, LOW);
else
digitalWrite(DPIN_PWR, HIGH);
if(amount_current>overtemp_max) {
amount_current = overtemp_max;
}
set_brightness(amount_off?0:amount_current);
} while(1);
PT_END(pt);
}
PT_THREAD(button_led_pt_func(struct pt *pt))
{
PT_BEGIN(pt);
do {
switch(batt_state) {
case BATT_CHARGING:
{
const unsigned long pulseTime = time_current;
// Smoothly pulse the green LED over a two-second interval,
// as if it were "breathing". This is the charging indication.
byte pulse = ((pulseTime>>2)&0xFF);
pulse = ((pulse * pulse) >> 8);
pulse = ((pulseTime>>2)&0x0100)?0xFF-pulse:pulse;
analogWrite(DPIN_GLED, pulse);
}
if(overtemp_max<BRT_MAX_LUMEN) {
digitalWrite(DPIN_RLED_SW, (time_current&0x03FF)>0x01FF?LOW:HIGH);
} else {
digitalWrite(DPIN_RLED_SW, LOW);
}
break;
case BATT_CHARGED:
// Solid green LED.
digitalWrite(DPIN_GLED, HIGH);
if(overtemp_max<BRT_MAX_LUMEN) {
digitalWrite(DPIN_RLED_SW, (time_current&0x03FF)>0x01FF?LOW:HIGH);
} else {
digitalWrite(DPIN_RLED_SW, LOW);
}
break;
case BATT_DISCHARGING:
// Blink the indicator LED now and then.
if(overtemp_max<BRT_MAX_LUMEN) {
digitalWrite(DPIN_RLED_SW, (time_current&0x03FF)>50?LOW:HIGH);
digitalWrite(DPIN_GLED, LOW);
} else {
digitalWrite(DPIN_GLED, (time_current&0x03FF)>10?LOW:HIGH);
digitalWrite(DPIN_RLED_SW, LOW);
}
break;
}
PT_YIELD(pt);
} while(1);
PT_END(pt);
}
PT_THREAD(power_pt_func(struct pt *pt))
{
static unsigned short count;
count++;
PT_BEGIN(pt);
overtemp_max = BRT_MAX_LUMEN;
do {
// Check the temperature sensor
{
static bool low_power_condition = false;
static uint8_t anti_flicker = 255;
if(temp_filtered > OVERTEMP_SHUTDOWN) {
if(amount_current)
Serial.println("Overheat shutdown!");
set_amount(0);
digitalWrite(DPIN_DRV_MODE, LOW);
digitalWrite(DPIN_DRV_EN, LOW);
digitalWrite(DPIN_PWR, LOW);
}
if(low_power_condition || (vcc_current<VOLTAGE_LOW)) {
if(!low_power_condition) {
overtemp_max = amount_current;
}
low_power_condition = true;
if((vcc_filtered < VOLTAGE_LOW) && (overtemp_max > BRT_MIN_LUMEN)) {
digitalWrite(DPIN_DRV_EN, LOW);
digitalWrite(DPIN_DRV_MODE, LOW);
analogWrite(DPIN_DRV_EN,BRT_MIN_LUMEN);
overtemp_max = overtemp_max/2 + BRT_MIN_LUMEN;
anti_flicker = 0;
}
}
if(anti_flicker == 255) {
if((vcc_filtered > VOLTAGE_NOMINAL+50) && (overtemp_max != BRT_MAX_LUMEN))
overtemp_max++;
anti_flicker -= 10;
} else {
anti_flicker++;
}
if(temp_filtered > OVERTEMP_THROTTLE) {
short new_max = (256-((temp_filtered-OVERTEMP_THROTTLE)*2)<<2)-1;
if((overtemp_max>BRT_MIN_LUMEN) && (new_max<(signed)overtemp_max)) {
overtemp_max--;
anti_flicker = 0;
}
}
static unsigned long lastStatTime;
if(time_current-lastStatTime > 1000) {
while(time_current-lastStatTime > 1000)
lastStatTime += 1000;
Serial.print("stat: ");
Serial.print(time_current);
switch(batt_state) {
case BATT_CHARGING:
Serial.print(" [CHARGING]");
break;
case BATT_CHARGED:
Serial.print(" [CHARGED]");
break;
case BATT_DISCHARGING:
Serial.print(" [BATTERY]");
break;
}
Serial.print(" lHz=");
Serial.print(count);
count = 0;
Serial.print(" duty=");
Serial.print(amount_current);
Serial.print(" Vcc=");
Serial.print(vcc_filtered);
Serial.print("mv");
Serial.print(" Temp=");
Serial.print((temp_filtered-500)/10.0f);
Serial.print("C");
if(orientation_enabled) {
Serial.print(" roll=");
Serial.print(angle_roll);
Serial.print(" pitch=");
Serial.print(angle_pitch);
}
if(overtemp_max<BRT_MAX_LUMEN) {
Serial.print(" THRTTL=");
Serial.print(overtemp_max);
}
Serial.println("");
}
}
PT_YIELD(pt);
} while(1);
PT_END(pt);
}
PT_THREAD(light_momentary_pt_func(struct pt *pt))
{
// If more than two minutes go by without the user
// pressing a button, then go ahead and shut down.
if(button_released_duration > 120000) {
pinMode(DPIN_PWR, OUTPUT);
digitalWrite(DPIN_PWR, LOW);
}
PT_BEGIN(pt);
do {
pinMode(DPIN_PWR, OUTPUT);
digitalWrite(DPIN_PWR, HIGH);
amount_off = 0;
PT_WAIT_UNTIL(pt, !button_is_pressed);
amount_off = 1;
PT_WAIT_UNTIL(pt, button_is_pressed);
} while(amount_current);
PT_END(pt);
}
PT_THREAD(light_blinky_pt_func(struct pt *pt))
{
PT_BEGIN(pt);
PT_WAIT_UNTIL(pt, !button_is_pressed && (button_released_duration > BUTTON_DEBOUNCE));
button_pressed_duration = 0;
do {
amount_off = !((time_current&0xFF)<=64) || !((time_current&0x1F)<=16);
PT_YIELD(pt);
} while(amount_current && (button_pressed_duration<BUTTON_DEBOUNCE));
amount_off = 0;
PT_WAIT_UNTIL(pt,!button_is_pressed);
PT_END(pt);
}
PT_THREAD(light_knob_pt_func(struct pt *pt))
{
static unsigned long lastTime;
static float lastKnobAngle, knob;
PT_BEGIN(pt);
enable_orientation();
// Set the initial knob value based on our current light bightness level.
knob = sqrt((float)amount_current/(float)BRT_MAX_LUMEN);
// Wait for the user to let go of the button.
PT_WAIT_UNTIL(pt,!button_is_pressed);
// Wait for a brief moment for any vibrations to stabalize.
PT_WAIT_FOR_PERIOD(pt,50);
lastKnobAngle = angle_roll;
do {
{
// Make apparent brightness changes linear by squaring the
// value and dividing back down into range. This gives us
// a gamma correction of 2.0, which is close enough.
unsigned short bright = (uint16_t)(knob * knob * BRT_MAX_LUMEN);
// Avoid ever appearing off in this mode!
if (bright < BRT_MIN_LUMEN) bright = BRT_MIN_LUMEN;
if((amount_current != amount_end) || abs((int16_t)(amount_end-bright)) > 4)
fade_to_amount(bright,100);
}
PT_WAIT_FOR_PERIOD(pt,50);
{
#define DEG_TO_RAD(x) ((PI*(x))/180.0f)
float change = angle_roll - lastKnobAngle;
lastKnobAngle = angle_roll;
// Don't bother updating our brightness reading if our angle isn't good.
if(abs(angle_pitch) < DEG_TO_RAD(60)) {
if (change > PI) change -= 2.0f*PI;
if (change < -PI) change += 2.0f*PI;
knob += change / -7.0f;
if (knob < 0) knob = 0;
if (knob > 1) knob = 1;
}
}
} while(amount_current && (button_pressed_duration<BUTTON_DEBOUNCE));
PT_WAIT_UNTIL(pt,!button_is_pressed);
PT_END(pt);
}
PT_THREAD(light_pt_func(struct pt *pt))
{
static unsigned long lastTime;
static byte level;
PT_BEGIN(pt);
// Estimate what the current brighness level is closest to.
if(amount_current < (BRT_LOW_LUMEN/2)) {
level = 0;
} else if(amount_current < (BRT_MED_LUMEN/2)) {
level = 1;
} else if(amount_current < (BRT_MAX_LUMEN/2)) {
level = 2;
} else {
level = 3;
}
button_released_time = time_current;
button_released_duration = 0;
Serial.println("Starting light thread.");
do {
PT_WAIT_UNTIL(pt, button_is_pressed && (button_pressed_duration > BUTTON_DEBOUNCE));
if(!amount_current || (button_released_duration<BUTTON_BRIGHTNESS_THRESHOLD)) {
level++;
level &= 3;
Serial.print("intensity=");
Serial.println(level);
} else {
Serial.println("turning off");
level = 0;
}
switch(level) {
case 0:
break;
case 1:
fade_to_amount(BRT_LOW_LUMEN,250);
break;
case 2:
fade_to_amount(BRT_MED_LUMEN,500);
break;
case 3:
fade_to_amount(BRT_MAX_LUMEN,500);
break;
}
PT_WAIT_UNTIL(pt, !button_is_pressed && (button_released_duration > BUTTON_DEBOUNCE));
if(!level)
fade_to_amount(0,500);
} while(1);
PT_END(pt);
}
void
check_serial_port(void)
{
// Check the serial port
if(Serial.available()) {
char c = Serial.read();
switch(c) {
case '+':
if(BRT_MAX_LUMEN-amount_current<BRT_MIN_LUMEN)
set_amount(BRT_MAX_LUMEN);
else
set_amount(amount_current+BRT_MIN_LUMEN);
break;
case '-':
if(amount_current<BRT_MIN_LUMEN)
set_amount(0);
else
set_amount(amount_current-BRT_MIN_LUMEN);
break;
case 'X':
case 'x': set_amount(0); break;
case 'H':
case 'h': set_amount(BRT_MAX_LUMEN); amount_off = 0; break;
case '0': light_mode = 0; amount_off = 0; break;
case '1': light_mode = 1; amount_off = 0; break;
case '2': light_mode = 2; amount_off = 0; break;
case '3': light_mode = 3; amount_off = 0; break;
case '4': light_mode = 4; amount_off = 0; break;
case '5': light_mode = 5; amount_off = 0; break;
case 'R':
// Let the watchdog reset us.
Serial.println("Rebooting");
wdt_enable(WDTO_15MS);
while(1) { }
break;
/*
case 'C':
case 'c': Serial.println("+------+---------------------+");
Serial.println("| + | increase brightness |");
Serial.println("| - | decrease brightness |");
Serial.println("| x/X | turn light off |");
Serial.println("| h/H | max brightness |");
Serial.println("| 0..5 | set light_mode |");
Serial.println("| c/C | list commands |");
Serial.println("| R | reboot |");
Serial.println("+------+---------------------+");
*/
default:
break;
}
}
}
void
setup(void)
{
// Set our watchdog to kill us if we don't
// check in every two seconds.
wdt_enable(WDTO_2S);
// We just powered on! That means either we got plugged
// into USB, or (more likely) the user is pressing the
// power button. We need to pull up the enable pin of
// the regulator very soon so we don't lose power.
// We don't pull the pin high here quite yet because
// we will do that when we transition out of MODE_OFF,
// somewhere in the main loop. Delaying this as long
// as possible acts like a debounce for accidental button
// taps.
pinMode(DPIN_PWR, INPUT);
digitalWrite(DPIN_PWR, LOW);
// Initialize GPIO
pinMode(DPIN_RLED_SW, INPUT);
pinMode(DPIN_GLED, OUTPUT);
pinMode(DPIN_DRV_MODE, OUTPUT);
pinMode(DPIN_DRV_EN, OUTPUT);
digitalWrite(DPIN_DRV_MODE, LOW);
digitalWrite(DPIN_DRV_EN, LOW);
// Initialize serial busses
Serial.begin(9600);
Wire.begin();
button_released_time = millis();
button_pressed_time = millis();
update_loop_variables();
update_loop_variables();
update_loop_variables();
// Don't bother loading the settings if we are connected to USB.
// Only load the settings when we are running from the battery.
if (batt_state == BATT_DISCHARGING) {
retrieve_settings();
}
Serial.println("Powered up!");
}
void
loop(void)
{
static unsigned long lastTime;
unsigned long time = millis();
// Reset the watchdog timer
wdt_reset();
update_loop_variables();
check_serial_port();
button_led_pt_func(&button_led_pt);
power_pt_func(&power_pt);
fade_control_pt_func(&fade_control_pt);
#define NUMBER_OF_MODES (4)
static byte last_mode;
static byte save_mode;
if((button_pressed_duration > 2048+1024+512*NUMBER_OF_MODES+1024)) {
if(save_mode == 1) {
amount_off = 1;
save_settings();
amount_off = 0;
save_mode = 2;
}
} else if((button_pressed_duration > 2048+1024+512*NUMBER_OF_MODES)) {
amount_off = ((button_pressed_duration/64) & 1);
if(save_mode == 0) {
save_mode = 1;
last_mode = 0;
}
} else if((button_pressed_duration > 2048)) {
byte selected_mode = (button_pressed_duration-2048)/512 + 1;
amount_off = 0;
if(selected_mode-1>=light_mode) {
selected_mode++;
}
if(selected_mode > NUMBER_OF_MODES)
selected_mode = NUMBER_OF_MODES;
if(selected_mode-1==light_mode)
selected_mode--;
if(selected_mode != last_mode) {
amount_flash = 1;
last_mode = selected_mode;
Serial.print("Mode selected: ");
Serial.println(selected_mode-1);
}
} else {
char thread_status;
if(save_mode) {
save_mode = 0;
amount_off = 0;
button_pressed_duration = 0;
button_released_duration = 0;
}
if(last_mode) {
light_mode = last_mode-1;
last_mode = 0;
Serial.print("Mode change: ");
Serial.println(light_mode);
PT_INIT(&light_pt);
PT_INIT(&light_momentary_pt);
PT_INIT(&light_blinky_pt);
PT_INIT(&light_knob_pt);
}
switch(light_mode) {
default:
case 0: thread_status = light_momentary_pt_func(&light_momentary_pt); break;
case 1: thread_status = light_pt_func(&light_pt); break;
case 2: thread_status = light_blinky_pt_func(&light_blinky_pt); break;
case 3: thread_status = light_knob_pt_func(&light_knob_pt); break;
}
if(!PT_SCHEDULE(thread_status)) {
light_mode = 0;
fade_to_amount(0,500);
}
}
return;
}