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
|
/*
* "Bistro" firmware
* This code runs on a single-channel or dual-channel driver (FET+7135)
* with an attiny25/45/85 MCU and a capacitor to measure offtime (OTC).
*
* Copyright (C) 2015 Selene Scriven
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* ATTINY25/45/85 Diagram
* ----
* -|1 8|- VCC
* OTC -|2 7|- Voltage ADC
* Star 3 -|3 6|- PWM (FET, optional)
* GND -|4 5|- PWM (1x7135)
* ----
*
* FUSES
* (check bin/flash*.sh for recommended values)
*
* STARS
* Star 3 = unused
*
* CALIBRATION
*
* To find out what values to use, flash the driver with battcheck.hex
* and hook the light up to each voltage you need a value for. This is
* much more reliable than attempting to calculate the values from a
* theoretical formula.
*
* Same for off-time capacitor values. Measure, don't guess.
*/
// Choose your MCU here, or in the build script
//#define ATTINY 13
//#define ATTINY 25
// FIXME: make 1-channel vs 2-channel power a single #define option
#define FET_7135_LAYOUT // specify an I/O pin layout
// Also, assign I/O pins in this file:
#include "tk-attiny.h"
/*
* =========================================================================
* Settings to modify per driver
*/
// FIXME: make 1-channel vs 2-channel power a single #define option
//#define FAST 0x23 // fast PWM channel 1 only
//#define PHASE 0x21 // phase-correct PWM channel 1 only
#define FAST 0xA3 // fast PWM both channels
#define PHASE 0xA1 // phase-correct PWM both channels
#define VOLTAGE_MON // Comment out to disable LVP
#define OFFTIM3 // Use short/med/long off-time presses
// instead of just short/long
// ../../bin/level_calc.py 64 1 10 1300 y 3 0.23 140
#define RAMP_SIZE 64
// log curve
//#define RAMP_7135 3,3,3,3,3,3,4,4,4,4,4,5,5,5,6,6,7,7,8,9,10,11,12,13,15,16,18,21,23,27,30,34,39,44,50,57,65,74,85,97,111,127,145,166,190,217,248,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0
//#define RAMP_FET 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,6,11,17,23,30,39,48,59,72,86,103,121,143,168,197,255
// x**2 curve
//#define RAMP_7135 3,5,8,12,17,24,32,41,51,63,75,90,105,121,139,158,178,200,223,247,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0
//#define RAMP_FET 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,4,6,9,12,16,19,22,26,30,33,37,41,45,50,54,59,63,68,73,78,84,89,94,100,106,111,117,123,130,136,142,149,156,162,169,176,184,191,198,206,214,221,255
// x**3 curve
#define RAMP_7135 3,3,4,5,6,8,10,12,15,19,23,28,33,40,47,55,63,73,84,95,108,122,137,153,171,190,210,232,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0
#define RAMP_FET 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,5,8,11,14,18,22,26,30,34,39,44,49,54,59,65,71,77,84,91,98,105,113,121,129,137,146,155,164,174,184,194,205,216,255
// x**5 curve
//#define RAMP_7135 3,3,3,4,4,5,5,6,7,8,10,11,13,15,18,21,24,28,33,38,44,50,57,66,75,85,96,108,122,137,154,172,192,213,237,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0
//#define RAMP_FET 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,3,6,9,13,17,21,25,30,35,41,47,53,60,67,75,83,91,101,111,121,132,144,156,169,183,198,213,255
// uncomment to ramp up/down to a mode instead of jumping directly
#define SOFT_START
// Enable battery indicator mode?
#define USE_BATTCHECK
// Choose a battery indicator style
//#define BATTCHECK_4bars // up to 4 blinks
//#define BATTCHECK_8bars // up to 8 blinks
#define BATTCHECK_VpT // Volts + tenths
// output to use for blinks on battery check (and other modes)
#define BLINK_BRIGHTNESS RAMP_SIZE/4
// ms per normal-speed blink
#define BLINK_SPEED 500
// Hidden modes are *before* the lowest (moon) mode, and should be specified
// in reverse order. So, to go backward from moon to turbo to strobe to
// battcheck, use BATTCHECK,STROBE,TURBO .
#define HIDDENMODES BIKING_STROBE,BATTCHECK,POLICE_STROBE,TURBO
#define TURBO RAMP_SIZE // Convenience code for turbo mode
#define BATTCHECK 254 // Convenience code for battery check mode
#define GROUP_SELECT_MODE 253
#define TEMP_CAL_MODE 252
// Uncomment to enable tactical strobe mode
//#define STROBE 251 // Convenience code for strobe mode
// Uncomment to unable a 2-level stutter beacon instead of a tactical strobe
#define BIKING_STROBE 250 // Convenience code for biking strobe mode
// comment out to use minimal version instead (smaller)
#define FULL_BIKING_STROBE
//#define RAMP 249 // ramp test mode for tweaking ramp shape
#define POLICE_STROBE 248
//#define RANDOM_STROBE 247
//#define SOS 246
// thermal step-down
#define TEMPERATURE_MON
// Calibrate voltage and OTC in this file:
#include "tk-calibration.h"
/*
* =========================================================================
*/
// Ignore a spurious warning, we did the cast on purpose
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
#include <avr/pgmspace.h>
//#include <avr/io.h>
//#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <avr/sleep.h>
//#include <avr/power.h>
#include <string.h>
#define OWN_DELAY // Don't use stock delay functions.
#define USE_DELAY_MS // Also use _delay_ms()
#define USE_DELAY_S // Also use _delay_s()
#include "tk-delay.h"
#include "tk-voltage.h"
#ifdef RANDOM_STROBE
#include "tk-random.h"
#endif
/*
* global variables
*/
// Config option variables
#define USE_FIRSTBOOT
#ifdef USE_FIRSTBOOT
#define FIRSTBOOT 0b01010101
uint8_t firstboot = FIRSTBOOT; // detect initial boot or factory reset
#endif
uint8_t modegroup = 5; // which mode group (set above in #defines)
uint8_t enable_moon = 1; // Should we add moon to the set of modes?
uint8_t reverse_modes = 0; // flip the mode order?
uint8_t memory = 0; // mode memory, or not (set via soldered star)
#ifdef OFFTIM3
uint8_t offtim3 = 1; // enable medium-press?
#endif
#ifdef TEMPERATURE_MON
uint8_t maxtemp = 79; // temperature step-down threshold
#endif
uint8_t muggle_mode = 0; // simple mode designed for muggles
// Other state variables
uint8_t mode_override = 0; // do we need to enter a special mode?
uint8_t mode_idx = 0; // current or last-used mode number
uint8_t eepos = 0;
// counter for entering config mode
// (needs to be remembered while off, but only for up to half a second)
uint8_t fast_presses __attribute__ ((section (".noinit")));
// total length of current mode group's array
#ifdef OFFTIM3
uint8_t mode_cnt;
#endif
// number of regular non-hidden modes in current mode group
uint8_t solid_modes;
// number of hidden modes in the current mode group
// (hardcoded because both groups have the same hidden modes)
//uint8_t hidden_modes = NUM_HIDDEN; // this is never used
PROGMEM const uint8_t hiddenmodes[] = { HIDDENMODES };
// default values calculated by group_calc.py
// Each group must be 8 values long, but can be cut short with a zero.
#define NUM_MODEGROUPS 9 // don't count muggle mode
PROGMEM const uint8_t modegroups[] = {
64, 0, 0, 0, 0, 0, 0, 0,
11, 64, 0, 0, 0, 0, 0, 0,
11, 35, 64, 0, 0, 0, 0, 0,
11, 26, 46, 64, 0, 0, 0, 0,
11, 23, 36, 50, 64, 0, 0, 0,
11, 20, 31, 41, 53, 64, 0, 0,
29, 64,POLICE_STROBE,0,0,0,0,0, // 7: special group A
BIKING_STROBE,BATTCHECK,11,29,64,0,0,0, // 8: special group B
9, 18, 29, 46, 64, 0, 0, 0, // 9: special group C
11, 29, 50, 0, // muggle mode, exception to "must be 8 bytes long"
};
//uint8_t modes[] = { 1,2,3,4,5,6,7,8,9, HIDDENMODES }; // make sure this is long enough...
uint8_t modes[9 + sizeof(hiddenmodes)]; // make sure this is long enough...
// Modes (gets set when the light starts up based on saved config values)
PROGMEM const uint8_t ramp_7135[] = { RAMP_7135 };
PROGMEM const uint8_t ramp_FET[] = { RAMP_FET };
void save_mode() { // save the current mode index (with wear leveling)
uint8_t oldpos=eepos;
eepos = (eepos+1) & ((EEPSIZE/2)-1); // wear leveling, use next cell
eeprom_write_byte((uint8_t *)(eepos), mode_idx); // save current state
eeprom_write_byte((uint8_t *)(oldpos), 0xff); // erase old state
}
#define OPT_firstboot (EEPSIZE-1)
#define OPT_modegroup (EEPSIZE-2)
#define OPT_memory (EEPSIZE-3)
#define OPT_offtim3 (EEPSIZE-4)
#define OPT_maxtemp (EEPSIZE-5)
#define OPT_mode_override (EEPSIZE-6)
#define OPT_moon (EEPSIZE-7)
#define OPT_revmodes (EEPSIZE-8)
#define OPT_muggle (EEPSIZE-9)
void save_state() { // central method for writing complete state
eeprom_write_byte((uint8_t *)OPT_modegroup, modegroup);
eeprom_write_byte((uint8_t *)OPT_memory, memory);
#ifdef OFFTIM3
eeprom_write_byte((uint8_t *)OPT_offtim3, offtim3);
#endif
#ifdef TEMPERATURE_MON
eeprom_write_byte((uint8_t *)OPT_maxtemp, maxtemp);
#endif
eeprom_write_byte((uint8_t *)OPT_mode_override, mode_override);
eeprom_write_byte((uint8_t *)OPT_moon, enable_moon);
eeprom_write_byte((uint8_t *)OPT_revmodes, reverse_modes);
eeprom_write_byte((uint8_t *)OPT_muggle, muggle_mode);
#ifdef USE_FIRSTBOOT
eeprom_write_byte((uint8_t *)OPT_firstboot, firstboot);
#endif
save_mode();
}
#ifndef USE_FIRSTBOOT
inline void reset_state() {
mode_idx = 0;
modegroup = 5;
save_state();
}
#endif
void restore_state() {
uint8_t eep;
#ifdef USE_FIRSTBOOT
// check if this is the first time we have powered on
eep = eeprom_read_byte((uint8_t *)OPT_firstboot);
if (eep != FIRSTBOOT) {
// not much to do; the defaults should already be set
// while defining the variables above
save_state();
return;
}
#else
uint8_t first = 1;
#endif
// find the mode index data
for(eepos=0; eepos<(EEPSIZE/2); eepos++) {
eep = eeprom_read_byte((const uint8_t *)eepos);
if (eep != 0xff) {
mode_idx = eep;
#ifndef USE_FIRSTBOOT
first = 0;
#endif
break;
}
}
#ifndef USE_FIRSTBOOT
// if no mode_idx was found, assume this is the first boot
if (first) {
reset_state();
return;
}
#endif
// load other config values
modegroup = eeprom_read_byte((uint8_t *)OPT_modegroup);
memory = eeprom_read_byte((uint8_t *)OPT_memory);
#ifdef OFFTIM3
offtim3 = eeprom_read_byte((uint8_t *)OPT_offtim3);
#endif
#ifdef TEMPERATURE_MON
maxtemp = eeprom_read_byte((uint8_t *)OPT_maxtemp);
#endif
mode_override = eeprom_read_byte((uint8_t *)OPT_mode_override);
enable_moon = eeprom_read_byte((uint8_t *)OPT_moon);
reverse_modes = eeprom_read_byte((uint8_t *)OPT_revmodes);
muggle_mode = eeprom_read_byte((uint8_t *)OPT_muggle);
// unnecessary, save_state handles wrap-around
// (and we don't really care about it skipping cell 0 once in a while)
//else eepos=0;
#ifndef USE_FIRSTBOOT
if (modegroup >= NUM_MODEGROUPS) reset_state();
#endif
}
inline void next_mode() {
mode_idx += 1;
if (mode_idx >= solid_modes) {
// Wrap around, skipping the hidden modes
// (note: this also applies when going "forward" from any hidden mode)
// FIXME? Allow this to cycle through hidden modes?
mode_idx = 0;
}
}
#ifdef OFFTIM3
inline void prev_mode() {
// simple mode has no reverse
if (muggle_mode) { return next_mode(); }
if (mode_idx == solid_modes) {
// If we hit the end of the hidden modes, go back to moon
mode_idx = 0;
} else if (mode_idx > 0) {
// Regular mode: is between 1 and TOTAL_MODES
mode_idx -= 1;
} else {
// Otherwise, wrap around (this allows entering hidden modes)
mode_idx = mode_cnt - 1;
}
}
#endif
void count_modes() {
/*
* Determine how many solid and hidden modes we have.
*
* (this matters because we have more than one set of modes to choose
* from, so we need to count at runtime)
*/
// copy config to local vars to avoid accidentally overwriting them in muggle mode
// (also, it seems to reduce overall program size)
uint8_t my_modegroup = modegroup;
uint8_t my_enable_moon = enable_moon;
uint8_t my_reverse_modes = reverse_modes;
// override config if we're in simple mode
if (muggle_mode) {
my_modegroup = NUM_MODEGROUPS;
my_enable_moon = 0;
my_reverse_modes = 0;
}
uint8_t *dest;
const uint8_t *src = modegroups + (my_modegroup<<3);
dest = modes;
// add moon mode (or not) if config says to add it
if (my_enable_moon) {
modes[0] = 1;
dest ++;
}
// Figure out how many modes are in this group
//solid_modes = modegroup + 1; // Assume group N has N modes
// No, how about actually counting the modes instead?
// (in case anyone changes the mode groups above so they don't form a triangle)
for(solid_modes=0;
(solid_modes<8) && pgm_read_byte(src);
solid_modes++, src++ )
{
*dest++ = pgm_read_byte(src);
}
// add regular modes
//memcpy_P(dest, src, solid_modes); // was already copied above
// add hidden modes
//memcpy_P(dest + solid_modes, hiddenmodes, sizeof(hiddenmodes));
// smaller than memcpy_p()
for( src=hiddenmodes; src<hiddenmodes+sizeof(hiddenmodes); src++ )
{
*dest++ = pgm_read_byte(src);
}
// final count
#ifdef OFFTIM3
mode_cnt = solid_modes + sizeof(hiddenmodes);
#endif
if (my_reverse_modes) {
// TODO: yuck, isn't there a better way to do this?
int8_t i;
src = modegroups + (my_modegroup<<3) + solid_modes;
dest = modes;
for(i=0; i<solid_modes; i++) {
src --;
*dest = pgm_read_byte(src);
dest ++;
}
if (my_enable_moon) {
*dest = 1;
}
mode_cnt --; // get rid of last hidden mode, since it's a duplicate turbo
}
if (my_enable_moon) {
mode_cnt ++;
solid_modes ++;
}
}
#ifdef ALT_PWM_LVL
inline void set_output(uint8_t pwm1, uint8_t pwm2) {
#else
inline void set_output(uint8_t pwm1) {
#endif
/* This is no longer needed since we always use PHASE mode.
// Need PHASE to properly turn off the light
if ((pwm1==0) && (pwm2==0)) {
TCCR0A = PHASE;
}
*/
PWM_LVL = pwm1;
#ifdef ALT_PWM_LVL
ALT_PWM_LVL = pwm2;
#endif
}
void set_level(uint8_t level) {
if (level == 0) {
set_output(0,0);
} else {
level -= 1;
set_output(pgm_read_byte(ramp_FET + level),
pgm_read_byte(ramp_7135 + level));
}
}
void set_mode(uint8_t mode) {
#ifdef SOFT_START
static uint8_t actual_level = 0;
uint8_t target_level = mode;
int8_t shift_amount;
int8_t diff;
do {
diff = target_level - actual_level;
shift_amount = (diff >> 2) | (diff!=0);
actual_level += shift_amount;
set_level(actual_level);
//_delay_ms(RAMP_SIZE/20); // slow ramp
_delay_ms(RAMP_SIZE/4); // fast ramp
} while (target_level != actual_level);
#else
#define set_mode set_level
//set_level(mode);
#endif // SOFT_START
}
void blink(uint8_t val, uint16_t speed)
{
for (; val>0; val--)
{
set_level(BLINK_BRIGHTNESS);
_delay_ms(speed);
set_level(0);
_delay_ms(speed);
_delay_ms(speed);
}
}
inline void strobe(uint8_t ontime, uint8_t offtime) {
uint8_t i;
for(i=0;i<8;i++) {
set_level(RAMP_SIZE);
_delay_ms(ontime);
set_level(0);
_delay_ms(offtime);
}
}
#ifdef SOS
inline void SOS_mode() {
#define SOS_SPEED 200
blink(3, SOS_SPEED);
_delay_ms(SOS_SPEED*5);
blink(3, SOS_SPEED*5/2);
//_delay_ms(SOS_SPEED);
blink(3, SOS_SPEED);
_delay_s(); _delay_s();
}
#endif
void toggle(uint8_t *var, uint8_t num) {
// Used for config mode
// Changes the value of a config option, waits for the user to "save"
// by turning the light off, then changes the value back in case they
// didn't save. Can be used repeatedly on different options, allowing
// the user to change and save only one at a time.
blink(num, BLINK_SPEED/8); // indicate which option number this is
*var ^= 1;
save_state();
// "buzz" for a while to indicate the active toggle window
blink(32, 500/32);
/*
for(uint8_t i=0; i<32; i++) {
set_level(BLINK_BRIGHTNESS * 3 / 4);
_delay_ms(20);
set_level(0);
_delay_ms(20);
}
*/
// if the user didn't click, reset the value and return
*var ^= 1;
save_state();
_delay_s();
}
#ifdef TEMPERATURE_MON
uint8_t get_temp() {
ADC_on_temperature();
// average a few values; temperature is noisy
uint16_t temp = 0;
uint8_t i;
get_temperature();
for(i=0; i<16; i++) {
temp += get_voltage();
_delay_ms(5);
}
temp >>= 4;
return temp;
}
#endif // TEMPERATURE_MON
#ifdef OFFTIM3
inline uint8_t read_otc() {
// Read and return the off-time cap value
// Start up ADC for capacitor pin
// disable digital input on ADC pin to reduce power consumption
DIDR0 |= (1 << CAP_DIDR);
// 1.1v reference, left-adjust, ADC3/PB3
ADMUX = (1 << V_REF) | (1 << ADLAR) | CAP_CHANNEL;
// enable, start, prescale
ADCSRA = (1 << ADEN ) | (1 << ADSC ) | ADC_PRSCL;
// Wait for completion
while (ADCSRA & (1 << ADSC));
// Start again as datasheet says first result is unreliable
ADCSRA |= (1 << ADSC);
// Wait for completion
while (ADCSRA & (1 << ADSC));
// ADCH should have the value we wanted
return ADCH;
}
#endif
int main(void)
{
// check the OTC immediately before it has a chance to charge or discharge
#ifdef OFFTIM3
uint8_t cap_val = read_otc(); // save it for later
#endif
// Set PWM pin to output
DDRB |= (1 << PWM_PIN); // enable main channel
#ifdef ALT_PWM_PIN
DDRB |= (1 << ALT_PWM_PIN); // enable second channel
#endif
// Set timer to do PWM for correct output pin and set prescaler timing
//TCCR0A = 0x23; // phase corrected PWM is 0x21 for PB1, fast-PWM is 0x23
//TCCR0B = 0x01; // pre-scaler for timer (1 => 1, 2 => 8, 3 => 64...)
TCCR0A = PHASE;
// Set timer to do PWM for correct output pin and set prescaler timing
TCCR0B = 0x01; // pre-scaler for timer (1 => 1, 2 => 8, 3 => 64...)
// Read config values and saved state
restore_state();
// Enable the current mode group
count_modes();
// TODO: Enable this? (might prevent some corner cases, but requires extra room)
// memory decayed, reset it
// (should happen on med/long press instead
// because mem decay is *much* slower when the OTC is charged
// so let's not wait until it decays to reset it)
//if (fast_presses > 0x20) { fast_presses = 0; }
// check button press time, unless the mode is overridden
if (! mode_override) {
#ifdef OFFTIM3
if (cap_val > CAP_SHORT) {
#else
if (fast_presses < 0x20) {
#endif
// Indicates they did a short press, go to the next mode
// We don't care what the fast_presses value is as long as it's over 15
fast_presses = (fast_presses+1) & 0x1f;
next_mode(); // Will handle wrap arounds
#ifdef OFFTIM3
} else if (cap_val > CAP_MED) {
// User did a medium press, go back one mode
fast_presses = 0;
if (offtim3) {
prev_mode(); // Will handle "negative" modes and wrap-arounds
} else {
next_mode(); // disabled-med-press acts like short-press
// (except that fast_presses isn't reliable then)
}
#endif
} else {
// Long press, keep the same mode
// ... or reset to the first mode
fast_presses = 0;
if (muggle_mode || (! memory)) {
// Reset to the first mode
mode_idx = 0;
}
}
}
save_mode();
#ifdef CAP_PIN
// Charge up the capacitor by setting CAP_PIN to output
DDRB |= (1 << CAP_PIN); // Output
PORTB |= (1 << CAP_PIN); // High
#endif
// Turn features on or off as needed
#ifdef VOLTAGE_MON
ADC_on();
#else
ADC_off();
#endif
uint8_t output;
uint8_t actual_level;
#ifdef TEMPERATURE_MON
uint8_t overheat_count = 0;
#endif
#ifdef VOLTAGE_MON
uint8_t lowbatt_cnt = 0;
uint8_t i = 0;
uint8_t voltage;
// Make sure voltage reading is running for later
ADCSRA |= (1 << ADSC);
#endif
//output = pgm_read_byte(modes + mode_idx);
output = modes[mode_idx];
actual_level = output;
// handle mode overrides, like mode group selection and temperature calibration
if (mode_override) {
// do nothing; mode is already set
//mode_idx = mode_override;
fast_presses = 0;
output = mode_idx;
}
while(1) {
if (fast_presses > 0x0f) { // Config mode
_delay_s(); // wait for user to stop fast-pressing button
fast_presses = 0; // exit this mode after one use
mode_idx = 0;
// Enter or leave "muggle mode"?
toggle(&muggle_mode, 1);
if (muggle_mode) { continue; }; // don't offer other options in muggle mode
toggle(&memory, 2);
toggle(&enable_moon, 3);
toggle(&reverse_modes, 4);
// Enter the mode group selection mode?
mode_idx = GROUP_SELECT_MODE;
toggle(&mode_override, 5);
mode_idx = 0;
#ifdef OFFTIM3
toggle(&offtim3, 6);
#endif
#ifdef TEMPERATURE_MON
// Enter temperature calibration mode?
mode_idx = TEMP_CAL_MODE;
toggle(&mode_override, 7);
mode_idx = 0;
#endif
#ifdef USE_FIRSTBOOT
toggle(&firstboot, 8);
#endif
//output = pgm_read_byte(modes + mode_idx);
output = modes[mode_idx];
actual_level = output;
}
#ifdef STROBE
else if (output == STROBE) {
// 10Hz tactical strobe
strobe(33,67);
}
#endif // ifdef STROBE
#ifdef POLICE_STROBE
else if (output == POLICE_STROBE) {
// police-like strobe
//for(i=0;i<8;i++) {
strobe(20,40);
//}
//for(i=0;i<8;i++) {
strobe(40,80);
//}
}
#endif // ifdef POLICE_STROBE
#ifdef RANDOM_STROBE
else if (output == RANDOM_STROBE) {
// pseudo-random strobe
uint8_t ms = 34 + (pgm_rand() & 0x3f);
strobe(ms, ms);
//strobe(ms, ms);
}
#endif // ifdef RANDOM_STROBE
#ifdef BIKING_STROBE
else if (output == BIKING_STROBE) {
// 2-level stutter beacon for biking and such
#ifdef FULL_BIKING_STROBE
// normal version
for(i=0;i<4;i++) {
set_output(255,0);
_delay_ms(5);
set_output(0,255);
_delay_ms(65);
}
_delay_ms(720);
#else
// small/minimal version
set_output(255,0);
_delay_ms(10);
set_output(0,255);
_delay_s();
#endif
}
#endif // ifdef BIKING_STROBE
#ifdef SOS
else if (output == SOS) { SOS_mode(); }
#endif // ifdef SOS
#ifdef RAMP
else if (output == RAMP) {
int8_t r;
// simple ramping test
for(r=1; r<=RAMP_SIZE; r++) {
set_level(r);
_delay_ms(25);
}
for(r=RAMP_SIZE; r>0; r--) {
set_level(r);
_delay_ms(25);
}
}
#endif // ifdef RAMP
#ifdef BATTCHECK
else if (output == BATTCHECK) {
#ifdef BATTCHECK_VpT
// blink out volts and tenths
_delay_ms(100);
uint8_t result = battcheck();
blink(result >> 5, BLINK_SPEED/8);
_delay_ms(BLINK_SPEED);
blink(1,5);
_delay_ms(BLINK_SPEED*3/2);
blink(result & 0b00011111, BLINK_SPEED/8);
#else // ifdef BATTCHECK_VpT
// blink zero to five times to show voltage
// (~0%, ~25%, ~50%, ~75%, ~100%, >100%)
blink(battcheck(), BLINK_SPEED/8);
#endif // ifdef BATTCHECK_VpT
// wait between readouts
_delay_s(); _delay_s();
}
#endif // ifdef BATTCHECK
else if (output == GROUP_SELECT_MODE) {
// exit this mode after one use
mode_idx = 0;
mode_override = 0;
for(i=0; i<NUM_MODEGROUPS; i++) {
modegroup = i;
save_state();
blink(1, BLINK_SPEED/3);
}
_delay_s(); _delay_s();
}
#ifdef TEMP_CAL_MODE
else if (output == TEMP_CAL_MODE) {
// make sure we don't stay in this mode after button press
mode_idx = 0;
mode_override = 0;
// Allow the user to turn off thermal regulation if they want
maxtemp = 255;
save_state();
set_mode(RAMP_SIZE/4); // start somewhat dim during turn-off-regulation mode
_delay_s(); _delay_s();
// run at highest output level, to generate heat
set_mode(RAMP_SIZE);
// measure, save, wait... repeat
while(1) {
maxtemp = get_temp();
save_state();
_delay_s(); _delay_s();
}
}
#endif // TEMP_CAL_MODE
else { // Regular non-hidden solid mode
set_mode(actual_level);
#ifdef TEMPERATURE_MON
uint8_t temp = get_temp();
// step down? (or step back up?)
if (temp >= maxtemp) {
overheat_count ++;
// reduce noise, and limit the lowest step-down level
if ((overheat_count > 15) && (actual_level > (RAMP_SIZE/8))) {
actual_level --;
//_delay_ms(5000); // don't ramp down too fast
overheat_count = 0; // don't ramp down too fast
}
} else {
// if we're not overheated, ramp up to the user-requested level
overheat_count = 0;
if ((temp < maxtemp - 2) && (actual_level < output)) {
actual_level ++;
}
}
set_mode(actual_level);
ADC_on(); // return to voltage mode
#endif
// Otherwise, just sleep.
_delay_ms(500);
// If we got this far, the user has stopped fast-pressing.
// So, don't enter config mode.
//fast_presses = 0;
}
fast_presses = 0;
#ifdef VOLTAGE_MON
if (ADCSRA & (1 << ADIF)) { // if a voltage reading is ready
voltage = ADCH; // get the waiting value
// See if voltage is lower than what we were looking for
if (voltage < ADC_LOW) {
lowbatt_cnt ++;
} else {
lowbatt_cnt = 0;
}
// See if it's been low for a while, and maybe step down
if (lowbatt_cnt >= 8) {
// DEBUG: blink on step-down:
//set_level(0); _delay_ms(100);
if (actual_level > RAMP_SIZE) { // hidden / blinky modes
// step down from blinky modes to medium
actual_level = RAMP_SIZE / 2;
} else if (actual_level > 1) { // regular solid mode
// step down from solid modes somewhat gradually
// drop by 25% each time
actual_level = (actual_level >> 2) + (actual_level >> 1);
// drop by 50% each time
//actual_level = (actual_level >> 1);
} else { // Already at the lowest mode
//mode_idx = 0; // unnecessary; we never leave this clause
//actual_level = 0; // unnecessary; we never leave this clause
// Turn off the light
set_level(0);
// Power down as many components as possible
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_mode();
}
set_mode(actual_level);
output = actual_level;
//save_mode(); // we didn't actually change the mode
lowbatt_cnt = 0;
// Wait before lowering the level again
//_delay_ms(250);
_delay_s();
}
// Make sure conversion is running for next time through
ADCSRA |= (1 << ADSC);
}
#endif // ifdef VOLTAGE_MON
}
//return 0; // Standard Return Code
}
|