-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathiv.c
More file actions
2114 lines (1725 loc) · 47.7 KB
/
iv.c
File metadata and controls
2114 lines (1725 loc) · 47.7 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
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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
Ice Tube Clock firmware August 13, 2009
(c) 2009 Limor Fried / Adafruit Industries
(c) 2009 Jeremy Fitzhardinge <jeremy@goop.org>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include <avr/io.h>
#include <string.h>
#include <avr/interrupt.h> // Interrupts and timers
#include <util/delay.h> // Blocking delay functions
#include <avr/pgmspace.h> // So we can store the 'font table' in ROM
#include <avr/eeprom.h> // Date/time/pref backup in permanent EEPROM
#include <avr/wdt.h> // Watchdog timer to repair lockups
#include "iv.h"
#include "util.h"
#include "fonttable.h"
static uint8_t region = REGION_US;
static uint8_t secondmode = SEC_FULL;
/* Drift correction applied each hour */
#define DRIFT_BASELINE 127
static int8_t drift = 0;
/*
* Barrier to force compiler to make sure memory is up-to-date. This
* is preferable to using "volatile" because we can just resync with
* memory when it really matters, rather than forcing the compiler to
* do a pile of memory load/stores.
*/
#define barrier() asm volatile("" : : : "memory")
static unsigned char __display_str(uint8_t *disp, const char *s);
static uint8_t setalarmstate(void);
struct timedate {
struct time {
uint8_t s, m, h;
} time;
struct date {
uint8_t m, d, y;
} date;
};
// These variables store the current time and date.
struct timedate timedate;
static volatile uint8_t suspend_update; /* if set, don't update */
// how loud is the speaker supposed to be?
uint8_t volume;
// whether the alarm is on, going off, and alarm time
static uint8_t alarm_on, alarming;
static struct time alarm;
static uint8_t alarm_days = DAYS_ALL;
/* hour morning and evening start */
static uint8_t morning, evening;
static uint8_t daybrite, nightbrite;
// are we in low power sleep mode?
volatile uint8_t sleepmode = 0;
static uint8_t timeunknown = 0; // MEME
static uint8_t restored = 0;
// Our display buffer, which is updated to show the time/date/etc
// and is multiplexed onto the tube
static uint8_t display[DISPLAYSIZE]; // stores segments, not values!
static uint8_t output_display[DISPLAYSIZE]; // stores segments, not values!
static uint8_t currdigit = 0; // which digit we are currently multiplexing
// This table allow us to index between what digit we want to light up
// and what the pin number is on the MAX6921 see the .h for values.
// Stored in ROM (PROGMEM) to save RAM
static const uint8_t digittable[] PROGMEM = {
DIG_9, DIG_8, DIG_7, DIG_6, DIG_5, DIG_4, DIG_3, DIG_2, DIG_1
};
// This table allow us to index between what segment we want to light up
// and what the pin number is on the MAX6921 see the .h for values.
// Stored in ROM (PROGMEM) to save RAM
static const uint8_t segmenttable[] PROGMEM = {
SEG_H, SEG_G, SEG_F, SEG_E, SEG_D, SEG_C, SEG_B, SEG_A
};
// muxdiv and MUX_DIVIDER divides down a high speed interrupt (31.25KHz)
// down so that we can refresh at about 100Hz (31.25KHz / 300)
// We refresh the entire display at 100Hz so each digit is updated
// 100Hz/DISPLAYSIZE
static uint16_t muxdiv = 0;
#define MUX_DIVIDER (300 / DISPLAYSIZE)
// Likewise divides 100Hz down to 1Hz for the alarm beeping
uint16_t alarmdiv = 0;
#define ALARM_DIVIDER 100
// How long we have been snoozing
static uint8_t snooze = MAXSNOOZE / 60;
static uint16_t snoozetimer = 0;
/*
* Idle MCU while waiting for interrupts; enables interrupts, so can
* be used safely where interrupts are disabled (that is, there's no
* race window between enabling interrupts and going to sleep).
*/
static inline void sleep(void)
{
asm volatile("sei $ sleep" : : : "memory");
}
// We have a non-blocking delay function, milliseconds is updated by
// an interrupt
volatile uint16_t milliseconds = 0;
static uint16_t now(void)
{
volatile uint8_t *ms = (uint8_t *)&milliseconds;
uint8_t h, l;
do {
h = ms[1];
l = ms[0];
} while(h != ms[1]);
return (h << 8) | l;
}
void delayms(uint16_t ms) {
uint16_t start = now();
while ((now() - start) < ms)
sleep();
}
// we reset the watchdog timer
static void kickthedog(void) {
wdt_reset();
}
static inline uint16_t time_since(uint16_t then)
{
return now() - then;
}
/*
Button state machine:
digraph switch {
open -> closed [label="button\npressed"];
closed -> latched [label="held for\ndebounce\nrept=init"];
closed -> open [label="button\nreleased"];
sampled -> latched [label="button repeat\ntimeout\nrept=rate"];
sampled -> open [label="button\nreleased"];
latched -> sampled [label="state\nsampled"];
latched -> open [label="button\nreleased"];
}
*/
#define DEBOUNCE 20 /* ms button must be pressed to be noticed */
#define REPT_INIT 800 /* ms before repeat starts */
#define REPT_RATE 60 /* ms between repeats */
#define NBUTTONS 4
/* Button latched bits */
#define BUT_MENU 0
#define BUT_SET 1
#define BUT_NEXT 2
#define BUT_ALARM 3 /* not really a button, but still needs debounce */
static uint8_t button_state; /* state for each button */
static uint16_t button_time[NBUTTONS]; /* timestamp for current state */
static uint16_t button_lastpress; /* timestamp last button entered LATCHED */
static uint16_t button_repeat; /* timeout for NEXT button repeat */
/* button states */
#define BS_OPEN 0 /* not pressed */
#define BS_CLOSED 1 /* just pressed */
#define BS_LATCHED 2 /* latched, but unsampled */
#define BS_SAMPLED 3 /* latched and sampled */
#define BS_LOG_NSTATES 2
#define BS_NSTATES (1 << BS_LOG_NSTATES)
#define BS_MASK (BS_NSTATES - 1)
#define BSTATE(b,s) ((s) << ((b) * BS_LOG_NSTATES))
#define BOPEN(b) BSTATE(b, BS_OPEN)
#define BCLOSED(b) BSTATE(b, BS_CLOSED)
#define BLATCHED(b) BSTATE(b, BS_LATCHED)
#define BSAMPLED(b) BSTATE(b, BS_SAMPLED)
#define BMASK(b) BSTATE(b, BS_MASK)
/*
* We got an interrupt for a button; update state. Expected to be
* called with button interrupt source masked.
* button = button number (0 - NBUTTONS)
*/
static void button_change_intr(uint8_t button, uint8_t state)
{
uint8_t bmask;
uint8_t bstate;
bmask = BMASK(button);
bstate = button_state & bmask;
if (state) {
/* pressed, so open->closed, anything else unaffected */
if (bstate == BOPEN(button)) {
bstate = BCLOSED(button);
/* record press time for debounce */
button_time[button] = now();
}
} else {
/* button released, so anything->open */
bstate = BOPEN(button);
}
/* update state */
button_state = (button_state & ~bmask) | bstate;
}
/* Called every millisecond(ish) to update button state */
static void button_state_update(void)
{
uint8_t i;
uint8_t bs;
uint16_t timeout, rept;
cli();
bs = button_state;
for (i = 0; i < NBUTTONS; i++) {
uint8_t s = bs & BS_MASK;
switch (s) {
case BS_OPEN:
case BS_LATCHED:
/* no time-based state changes */
break;
/* closed and sampled transition to latched with appropriate timeout */
case BS_CLOSED:
timeout = DEBOUNCE;
rept = REPT_INIT; /* closed->latched reset initial repeat timeout */
goto timelatch;
case BS_SAMPLED:
timeout = 0; /* no repeat for MENU and SET */
if (i == BUT_NEXT) {
timeout = button_repeat;
rept = REPT_RATE;
}
goto timelatch;
timelatch:
if (timeout && time_since(button_time[i]) >= timeout) {
s = BS_LATCHED;
/* record latched time for repeat */
button_time[i] = now();
/* update repeat rate for current state */
button_repeat = rept;
}
break;
}
/* shift out just-processed state */
bs >>= BS_LOG_NSTATES;
/* plonk in newly updated state */
bs |= s << (BS_LOG_NSTATES * (NBUTTONS-1));
}
button_state = bs;
sei();
}
/*
* Must be careful to make sure this is tested before now() rolls over
* (64k ms = ~1 min)
*/
static uint8_t button_timeout(void)
{
return time_since(button_lastpress) > (INACTIVITYTIMEOUT * 1000);
}
/* Poll the current state of button without changing it */
static uint8_t button_poll(uint8_t button)
{
barrier();
return (button_state & BMASK(button)) == BLATCHED(button);
}
/*
* Given a button, return true if a button is pressed, and consume the
* button-press.
*/
static uint8_t button_sample(uint8_t button)
{
uint8_t ret;
cli();
ret = button_poll(button);
if (ret) {
/* latched -> sampled */
button_state = (button_state & ~BMASK(button)) | BSAMPLED(button);
barrier();
button_lastpress = now();
sei();
tick();
} else
sei();
return ret;
}
/************************* LOW LEVEL DISPLAY ************************/
// Setup SPI
static void vfd_init(void) {
SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR0);
}
// Send 1 byte via SPI
static void spi_xfer(uint8_t c) {
SPDR = c;
while (! (SPSR & _BV(SPIF)));
}
// send raw data to display, its pretty straightforward. Just send 32 bits via SPI
// the bottom 20 define the segments
static void vfd_send(uint32_t d) {
// send lowest 20 bits
cli(); // to prevent flicker we turn off interrupts
spi_xfer(d >> 16);
spi_xfer(d >> 8);
spi_xfer(d);
// latch data
VFDLOAD_PORT |= _BV(VFDLOAD);
VFDLOAD_PORT &= ~_BV(VFDLOAD);
sei();
}
// This changes and updates the display
// We use the digit/segment table to determine which
// pins on the MAX6921 to turn on
static void setdisplay(uint8_t digit, uint8_t segments) {
uint32_t d = 0; // we only need 20 bits but 32 will do
uint8_t i;
// Set the digit selection pin
d |= _BV(pgm_read_byte(digittable + digit));
// Set the individual segments for this digit
for (i=0; i<8; i++) {
if (segments & _BV(i)) {
uint32_t t = 1;
t <<= pgm_read_byte(segmenttable + i);
d |= t;
}
}
// Shift the data out to the display
vfd_send(d);
}
/*
* -A-
* | |
* F B
* | |
* -G-
* | |
* E C
* | |
* -D- oH
*/
#define D0A (7)
#define D0B (6)
#define D0C (5)
#define D0D (4)
#define D0E (3)
#define D0F (2)
#define D0G (1)
#define D0H (0)
#define D1A (D0A + 8)
#define D1B (D0B + 8)
#define D1C (D0C + 8)
#define D1D (D0D + 8)
#define D1E (D0E + 8)
#define D1F (D0F + 8)
#define D1G (D0G + 8)
#define D1H (D0H + 8)
/*
* Given a pair of 7-segment digits, return a new digit generating by
* combining them accorting to 'table'. Each entry in 'table'
* dictates where the corresponding output segment will be taken from.
*
* Table is in A-H order, even those that's reversed with respect to
* their bit ordering.
*/
static uint8_t digit_transformer(uint8_t d0, uint8_t d1, const uint8_t *table)
{
uint16_t in = (d1 << 8) | d0;
uint8_t out;
signed char i;
out = 0;
for (i = 7; i >= 0; i--) {
out >>= 1;
out |= 0x80 & -!!(in & (1 << pgm_read_byte(table + i)));
}
return out;
}
static uint8_t scroll_up_top(uint8_t top, uint8_t mid)
{
static const uint8_t up[] PROGMEM = {
D0G, /* A */
D0C, /* B */
D1B, /* C */
D1A, /* D */
D1F, /* E */
D0E, /* F */
D0D, /* G */
D1H, /* H */
};
return digit_transformer(top, mid, up);
}
static uint8_t scroll_up_mid(uint8_t mid, uint8_t bottom)
{
static const uint8_t up[] PROGMEM = {
D1A, /* - */
D0C, /* Y */
D1B, /* - */
D0D, /* - */
D1F, /* - */
D0E, /* X */
D0G, /* G */
D1H, /* H */
};
return digit_transformer(mid, bottom, up);
}
static uint8_t scroll_up_bottom(uint8_t bottom)
{
static const uint8_t up[] PROGMEM = {
D0G, /* A */
D0C, /* B */
D1B, /* C */
D1A, /* D */
D1F, /* E */
D0E, /* F */
D0D, /* G */
D0H, /* H */
};
return digit_transformer(bottom, 0, up);
}
static uint8_t scroll_up(uint8_t *statep)
{
unsigned char s = *statep;
unsigned char i;
static uint8_t mid[DISPLAYSIZE];
if (s >= 4+3)
return 0;
if (s == 0) {
output_display[0] = display[0];
for(i = 1; i < DISPLAYSIZE; i++)
mid[i] = 0;
}
for(i = 1; i < DISPLAYSIZE; i++) {
uint8_t top = output_display[i];
uint8_t m = 0;
if (s >= 3) {
uint8_t bot;
bot = display[i];
m = mid[i];
mid[i] = scroll_up_mid(m, bot);
display[i] = scroll_up_bottom(bot);
}
output_display[i] = scroll_up_top(top, m);
}
*statep = ++s;
return 1000/30;
}
static uint8_t scroll_left(uint8_t *statep)
{
uint8_t s = *statep;
if (s >= DISPLAYSIZE*2)
return 0;
if (s == 0)
output_display[0] = display[0];
else {
memmove(output_display+1, output_display+2, DISPLAYSIZE-2);
if (s < DISPLAYSIZE+1)
output_display[DISPLAYSIZE-1] = 0;
else
output_display[DISPLAYSIZE-1] = display[s-DISPLAYSIZE];
}
*statep = ++s;
return 1000/40; /* 40fps */
}
static uint8_t flip(uint8_t *unused)
{
memcpy(output_display, display, sizeof(output_display));
return 0;
}
static void flip_display(transition_t* trans)
{
uint8_t state = 0;
uint8_t delay;
/* Disable interrupts while generating new output to prevent flickers */
cli();
while((delay = (*trans)(&state))) {
sei();
delayms(delay);
cli();
}
sei();
}
// called @ (F_CPU/256) = ~30khz (31.25 khz)
SIGNAL (SIG_OVERFLOW0) {
// allow other interrupts to go off while we're doing display updates
sei();
// divide down to 100Hz * digits
muxdiv++;
if (muxdiv < MUX_DIVIDER)
return;
muxdiv = 0;
// now at 100Hz * digits
// ok its not really 1ms but its like within 10% :)
milliseconds++;
// update latched and repeat state of buttons
button_state_update();
// Cycle through each digit in the display
if (currdigit >= DISPLAYSIZE)
currdigit = 0;
// Set the current display's segments
setdisplay(currdigit, output_display[currdigit]);
// and go to the next
currdigit++;
// check if we should have the alarm on
if (alarming && !snoozetimer) {
alarmdiv++;
if (alarmdiv > ALARM_DIVIDER) {
alarmdiv = 0;
} else {
return;
}
// This part only gets reached at 1Hz
// This sets the buzzer frequency
ICR1 = 250;
OCR1A = OCR1B = ICR1/2;
// ok alarm is ringing!
if (alarming & 0xF0) { // top bit indicates pulsing alarm state
alarming &= ~0xF0;
TCCR1B &= ~_BV(CS11); // turn buzzer off!
} else {
alarming |= 0xF0;
TCCR1B |= _BV(CS11); // turn buzzer on!
}
}
}
// We use the pin change interrupts to detect when buttons are pressed
// This interrupt detects switches 1 and 3
SIGNAL(SIG_PIN_CHANGE2) {
button_change_intr(0, !(PIND & _BV(BUTTON1)));
button_change_intr(2, !(PIND & _BV(BUTTON3)));
}
// Just button #2
SIGNAL(SIG_PIN_CHANGE0) {
button_change_intr(1, !(PINB & _BV(BUTTON2)));
}
// This will calculate leapyears, give it the year
// and it will return 1 (true) or 0 (false)
static uint8_t leapyear(uint16_t y) {
return ( (!(y % 4) && (y % 100)) || !(y % 400));
}
static uint8_t dotw(const struct date *date)
{
uint16_t month, year;
month = date->m;
year = 2000 + date->y;
if (month < 3) {
month += 12;
year -= 1;
}
return (date->d +
(2 * month) +
(6 * (month+1)/10) +
year + (year/4) -
(year/100) +
(year/400) + 1) % 7;
}
static void increment_time(struct timedate *td)
{
td->time.s++; // one second has gone by
// a minute!
if (td->time.s >= 60) {
td->time.s = 0;
td->time.m++;
}
// an hour...
if (td->time.m >= 60) {
td->time.m = 0;
td->time.h++;
// lets write the time to the EEPROM
eeprom_write_byte((uint8_t *)EE_HOUR, td->time.h);
eeprom_write_byte((uint8_t *)EE_MIN, td->time.m);
}
// a day....
if (td->time.h >= 24) {
td->time.h = 0;
td->date.d++;
eeprom_write_byte((uint8_t *)EE_DAY, td->date.d);
}
// a full month!
// we check the leapyear and date to verify when its time to roll over months
if ((td->date.d > 31) ||
((td->date.d == 31) && ((td->date.m == 4)||(td->date.m == 6)||(td->date.m == 9)||(td->date.m == 11))) ||
((td->date.d == 30) && (td->date.m == 2)) ||
((td->date.d == 29) && (td->date.m == 2) && !leapyear(2000+td->date.y))) {
td->date.d = 1;
td->date.m++;
eeprom_write_byte((uint8_t *)EE_MONTH, td->date.m);
}
// HAPPY NEW YEAR!
if (td->date.m >= 13) {
td->date.y++;
td->date.m = 1;
eeprom_write_byte((uint8_t *)EE_YEAR, td->date.y);
}
}
static void load_brite(void)
{
morning = eeprom_read_byte((unsigned char *)EE_MORNINGHR);
if (morning < 0 || morning > 12)
morning = 6;
evening = eeprom_read_byte((unsigned char *)EE_EVENINGHR);
if (evening < 12 || evening > 23)
evening = 18;
daybrite = eeprom_read_byte((unsigned char *)EE_DAYBRITE);
if (daybrite < BRITE_MIN || daybrite > BRITE_MAX)
daybrite = BRITE_MAX;
nightbrite = eeprom_read_byte((unsigned char *)EE_NIGHTBRITE);
if (nightbrite < BRITE_MIN || nightbrite > BRITE_MAX)
nightbrite = BRITE_MIN;
}
static void save_brite(void)
{
eeprom_write_byte((unsigned char *)EE_MORNINGHR, morning);
eeprom_write_byte((unsigned char *)EE_EVENINGHR, evening);
eeprom_write_byte((unsigned char *)EE_DAYBRITE, daybrite);
eeprom_write_byte((unsigned char *)EE_NIGHTBRITE, nightbrite);
}
static uint8_t get_brite(void)
{
uint8_t b;
if (alarming)
b = (timedate.time.s % 2) ? BRITE_MIN : BRITE_MAX;
else {
uint8_t hour = timedate.time.h;
b = nightbrite;
if (hour >= morning && hour < evening)
b = daybrite;
}
/* safety */
if (b < BRITE_MIN || b > BRITE_MAX)
b = BRITE_MIN;
return b;
}
static void set_brite(void)
{
OCR0A = get_brite();
}
/*
* This goes off once a second, driven by the external 32.768kHz
* crystal. It leaves interrupts disabled so it can never itself be
* interrupted.
*/
SIGNAL (TIMER2_COMPA_vect) {
struct timedate td;
CLKPR = _BV(CLKPCE); //MEME
CLKPR = 0;
td = timedate;
if (!suspend_update) {
increment_time(&td);
/* Apply drift correction on the first second of each hour */
if (td.time.m == 0) {
if (td.time.s == 0)
OCR2A = DRIFT_BASELINE + drift;
else if (td.time.s == 1)
OCR2A = DRIFT_BASELINE;
if (td.time.s <= 1) {
/* wait for update to take effect */
while (ASSR & _BV(OCR2AUB))
;
}
}
timedate = td;
}
// If we're in low power mode we should get out now since the display is off
if (sleepmode)
return;
if (alarm_on && (alarm_days & (1 << dotw(&td.date))) &&
(alarm.h == td.time.h) &&
(alarm.m == td.time.m) && (td.time.s == 0)) {
DEBUGP("alarm on!");
alarming = 1;
snoozetimer = 0;
}
/* set brightness according to alarm state and time */
set_brite();
if (snoozetimer)
snoozetimer--;
}
//Alarm Switch
SIGNAL(SIG_INTERRUPT0) {
uint8_t state;
state = (ALARM_PIN & _BV(ALARM));
button_change_intr(BUT_ALARM, state);
/* Turn off alarm immediately */
if (!state)
setalarmstate();
}
SIGNAL(SIG_COMPARATOR) {
//DEBUGP("COMP");
if (ACSR & _BV(ACO)) {
//DEBUGP("HIGH");
if (!sleepmode) {
VFDSWITCH_PORT |= _BV(VFDSWITCH); // turn off display
VFDCLK_PORT &= ~_BV(VFDCLK) & ~_BV(VFDDATA); // no power to vfdchip
BOOST_PORT &= ~_BV(BOOST); // pull boost fet low
SPCR &= ~_BV(SPE); // turn off spi
if (restored) {
eeprom_write_byte((uint8_t *)EE_MIN, timedate.time.m);
eeprom_write_byte((uint8_t *)EE_SEC, timedate.time.s);
}
DEBUGP("z");
TCCR0B = 0; // no boost
volume = 0; // low power buzzer
PCICR = 0; // ignore buttons
app_start();
}
} else {
//DEBUGP("LOW");
if (sleepmode) {
if (restored) {
eeprom_write_byte((uint8_t *)EE_MIN, timedate.time.m);
eeprom_write_byte((uint8_t *)EE_SEC, timedate.time.s);
}
DEBUGP("WAKERESET");
app_start();
}
}
}
/*********************** Main app **********/
#define EMIT_SLZ 1 /* suppress leading zero */
static void __emit_number(uint8_t *disp, uint8_t num, uint8_t flags)
{
if ((flags & EMIT_SLZ) && num < 10)
disp[0] = 0;
else
disp[0] = pgm_read_byte(numbertable + (num / 10));
disp[1] = pgm_read_byte(numbertable + (num % 10));
}
static void emit_number(uint8_t *disp, uint8_t num)
{
__emit_number(disp, num, 0);
}
static void emit_number_slz(uint8_t *disp, uint8_t num)
{
__emit_number(disp, num, EMIT_SLZ);
}
struct field {
unsigned char (*display)(unsigned char pos, const unsigned char *val);
void (*update)(unsigned char *val);
union {
const unsigned char *str;
unsigned char *val;
};
};
struct entry {
const char *prompt;
void (*get)(void);
void (*store)(void);
};
static const unsigned char space_P[] PROGMEM = " ";
static const unsigned char dash_P[] PROGMEM = "-";
static unsigned char show_num(unsigned char pos, const unsigned char *v)
{
emit_number(&display[pos], *v);
return 2;
}
static unsigned char show_num_slz(unsigned char pos, const unsigned char *v)
{
emit_number_slz(&display[pos], *v);
return 2;
}
static unsigned char show_hour(unsigned char pos, const unsigned char *v)
{
uint8_t h = *v;
if (region == REGION_US) {
emit_number_slz(display+pos, ((h+11) % 12) + 1);
if (h >= 12)
display[0] |= 0x1; /* pm notice */
else
display[0] &= ~0x1; /* am */
} else
emit_number(display+pos, h);
return 2;
}
static unsigned char show_str(unsigned char pos, const unsigned char *v)
{
char str[DISPLAYSIZE + 1];
strcpy_P(str, (const char *)v);
return __display_str(display+pos, str);
}
static unsigned char show_ampm(unsigned char pos, const unsigned char *v)
{
if (region != REGION_US)
return 0;
if (*v >= 12)
return show_str(pos, (const unsigned char *)PSTR("pm"));
else
return show_str(pos, (const unsigned char *)PSTR("am"));
}
static uint8_t show_separator(uint8_t pos, const uint8_t *v)
{
const unsigned char *str = space_P;
if (secondmode != SEC_DIAL && (*v & 1))
str = dash_P;
return show_str(pos, str);
}
static unsigned char show_second(unsigned char pos, const unsigned char *v)
{
struct time *t = (struct time *)v;
unsigned char s = t->s;
switch (secondmode) {
default:
case SEC_FULL:
emit_number(display+pos, s);
return 2;
case SEC_DIAL:
display[pos] = (0x80 >> (s / 10)) | ((~s & 1) << 1);
return 1;
case SEC_AMPM:
if (region == REGION_US)
return show_ampm(pos, &t->h);
/* FALLTHROUGH */
case SEC_NONE:
return 0;
}
}
static unsigned char show_days(unsigned char pos, const unsigned char *v)
{
static const char *str;
switch (*v) {
default:
case DAYS_ALL: str = PSTR("all "); break;
case DAYS_WEEKEND: str = PSTR("wknd"); break;
case DAYS_WEEK: str = PSTR("week"); break;
}
return show_str(pos, (unsigned char *)str);
}
static void update_days(unsigned char *v)
{
switch (*v) {
default:
case DAYS_WEEKEND: *v = DAYS_ALL; break;