-
Notifications
You must be signed in to change notification settings - Fork 1
/
source.c
1557 lines (1216 loc) · 36.5 KB
/
source.c
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
/*
Calcuclock firmware v0.996 - Charlie Bruce, 2013-2014
TECH NOTES:
Uses timer0 for delay, delayMicrosecond, as Arduino does.
Uses timer1 as display update, approximately once or twice per millisecond.
Uses timer2 for 32.768khz timekeeping ("real time")
When it hasn't been pressed for a while it goes into a very deep sleep - only C/CE/ON can wake it.
In deep sleep, virtually nothing but the low-level timekeeping stuff is running.
Brown-out detection is off in sleep, on when running? Or do we use the ADC to check the battery level every so often?
WDT is to be disabled in fuses.
TODO stopwatch, finish calculation (floating-point), set mode
*/
#include <avr/sleep.h> //Needed for sleep_mode (switching the CPU into low-power mode)
#include <avr/power.h> //Needed for powering down peripherals such as the ADC, TWI and timers
#include <stdint.h> //Needed for uint8_t
#include <util/delay.h> //Needed for small delays, using the function _delay_us and _delay_ms
#include <stdio.h> //Needed for FILE definitions and printf declarations (debugging)
#include <math.h> //Needed for logarithms and powers
//The state of each 7-segment display (A..DP for displays, left = 0, right = 5).
volatile uint8_t segstates[6];
//Has the CE button been pressed?
volatile boolean button_pressed = false;
//Pin numbers for the 7-segment displays
const uint8_t segs[8] = {8, 9, 10, 11, 12, 13, 6, 7};
const uint8_t cols[6] = {4, 5, A2, A3, A4, A5};
//Set the last bit to add a decimal point.
#define WITH_DECIMAL_POINT |(1<<7)
//These are the segment patterns that correspond with digits. LSB = A, MSB = DP
const uint8_t number[11] =
{
/*0*/ 0b00111111,
/*1*/ 0b00000110,
/*2*/ 0b01011011,
/*3*/ 0b01001111,
/*4*/ 0b01100110,
/*5*/ 0b01101101,
/*6*/ 0b01111101,
/*7*/ 0b00000111,
/*8*/ 0b01111111,
/*9*/ 0b01100111, //Note: If you prefer curly 9s, use 0b01101111 instead
/*BLANK*/ 0
};
//Input buttons
const uint8_t ceButton = 2;
const uint8_t btnsA = A0;
const uint8_t btnsB = A1;
//Output pin for the LED/IR transmitter
const uint8_t ledPin = 3;
#define SEGMENT_OFF HIGH
#define SEGMENT_ON LOW
//We use a PNP transistor for controlling which display is on at a given time.
#define COLUMN_OFF HIGH
#define COLUMN_ON LOW
//preload timer with 65535 - 4000 - 4,000 cycles at 8mhz is 2khz (2,000 times per second).
//Set this to near 0 or change prescale, to demonstrate how the display code works.
#define PWM_TIME (65535-4000)
enum Days {
Sunday = 0,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};
enum Months {
January = 1,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
};
enum Keys {
KEY_0 = 0,
KEY_1,
KEY_2,
KEY_3,
KEY_4,
KEY_5,
KEY_6,
KEY_7,
KEY_8,
KEY_9,
KEY_DP,
KEY_EQ,
KEY_ADD,
KEY_SUB,
KEY_MUL,
KEY_DIV,
NO_KEY
};
enum Messages {
MSG_SET = 0,
MSG_CHRONO,
MSG_TIME,
MSG_CALC,
MSG_LOBATT,
MSG_BATT,
MSG_DONE,
MSG_ERROR,
MSG_REMOTE,
MSG_POSINF,
MSG_NEGINF,
MSG_DATE,
MSG_TODO
};
//Time variables - GMT - 24-hour. For example, to enter 12:05, in Summer time, you'd enter hours = 11; minutes = 5; (do NOT set to 05! 05 is processed differently to 5!)
volatile uint8_t hours = 11;
volatile uint8_t minutes = 5;
volatile uint8_t seconds = 0;
//Date variables - GMT
volatile int year = 2014;
volatile int month = 5;
volatile int day = 16;
//Timezone-corrected hours, days, months. Minutes and seconds don't change in different timezones
uint8_t tzc_hours = 0;
uint8_t tzc_day = 1;
uint8_t tzc_month = 1;
int tzc_year = 2000;
//Timezone - 0 is GMT, 1 is BST
//where 1 means that the displayed time is one hour greater than GMT
uint8_t timezone = 1;
//Below this battery voltage, a warning should be displayed. 2.6v (2600) is a safe number. You can go lower but the device may behave unpredictably.
#define MIN_SAFE_BATTERY_VOLTAGE 2400
// create a FILE structure to reference our UART output function
static FILE uartout = {0};
static int uart_putchar(char c, FILE *stream) {
Serial.write(c);
return 0;
}
void setup(){
//We can't sleep any more deeply than this, or else we'll start losing track of time.
set_sleep_mode(SLEEP_MODE_PWR_SAVE);
sleep_enable();
//All inputs, no pullups
for(int x=1;x<18;x++){
pinMode(x, INPUT);
digitalWrite(x, LOW);
}
//IR LED is an output
pinMode(ledPin, OUTPUT);
//C/CE/ON button is an input, with an internal pullup resistor
pinMode(ceButton, INPUT);
digitalWrite(ceButton, HIGH);
//Set up the 7-segment display pins as outputs.
for(uint8_t i=0;i<8;i++){
pinMode(segs[i], OUTPUT);
digitalWrite(segs[i], SEGMENT_OFF);
}
//Set the transistor bases as outputs too.
for(uint8_t i=0;i<6;i++){
pinMode(cols[i], OUTPUT);
segstates[i] = 0;
}
//Turn off unused hardware on the chip to save power.
power_twi_disable();
power_spi_disable();
//USART should be powered when serial debugging is enabled.
//power_usart0_disable();
//Configure the serial port for debugging
Serial.begin(9600);
// fill in the UART file descriptor with pointer to writer.
fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
// The uart is the standard output device STDOUT.
stdout = &uartout;
//Set up timer 1 - display update
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = PWM_TIME;
TCCR1B |= (1 << CS10); //no prescaler - change to CS12 and set PWM_TIME to 62410 for 256x prescaling
TIMSK1 |= (1 << TOIE1); //enable timer overflow interrupt
//Set up timer 2 - real time clock
TCCR2A = 0;
TCCR2B = (1<<CS22)|(1<<CS20); //1-second resolution
//TCCR2B = (1<<CS22)|(1<<CS21)|(1<<CS20); //8-second resolution saves power at the expense of precision.
ASSR = (1<<AS2); //Enable asynchronous operation
TIMSK2 = (1<<TOIE2); //Enable the timer 2 overflow interrupt
//Interrupt when the CE button is pressed
EICRA = (1<<ISC01); //falling edge (button press, not release)
EIMSK = (1<<INT0); //Enable the interrupt INT0
//Enable global interrupts
sei();
}
void loop() {
//turn off display segments, any pullups (except on CE), screen timer, timer0, ADC, USART (leave only timer2 and INT0 running)
goSleepUntilButton();
//We've been woken up by a CE-button press.
uint8_t mode = 0;
displayMessage(MSG_CHRONO);
_delay_ms(150);
button_pressed = false;
//Record the time - after 2.5s of no presses, enter whatever mode is being displayed
long sleepTime = millis();
while (millis() - sleepTime < 2500) {
if(button_pressed) {
mode++;
mode = mode % 4;
switch(mode){
case 0:
displayMessage(MSG_CHRONO);
break;
case 1:
displayMessage(MSG_CALC);
break;
case 2:
displayMessage(MSG_REMOTE);
break;
case 3:
displayMessage(MSG_SET);
break;
}
_delay_ms(150); //Debounce
button_pressed = false;
sleepTime = millis();
}
}
//Single press of CE button enters calculator mode, double press enters clock mode, triple press triggers TV-B-GONE, holding enters clockset mode.
//Clock-set mode should account for BST or NOT-BST (whenever the hour, day or month increments, check against the BST conditions?)
//Or just require user intervention...
switch(mode){
case 0:
clockMode();
break;
case 1:
calculatorMode();
break;
case 2:
remoteMode();
break;
case 3:
setMode();
}
//Once the above operation has completed or timed out, we will reach this point in the code.
//Measure the battery voltage - if we're at less than MIN_SAFE_BATTERY_VOLTAGE, show a warning.
//Note that this measurement happens when the display is OFF - this prevents the current draw of the 7-segment displays from affecting the measurements.
long vcc = 0;
blankDisplay();
//if ((vcc = readVcc()) < MIN_SAFE_BATTERY_VOLTAGE) {
displayMessage(MSG_BATT);
_delay_ms(1000);
for(int i=0;i<10;i++) {
blankDisplay();
vcc = readVcc();
displayInt64(vcc);
segstates[2] |= 0b10000000;//DP
_delay_ms(200);
}
// }
}
//Can't fit remote and calculator modes in to memory at the same time.
void remoteMode(){
displayMessage(MSG_TODO);
_delay_ms(3000);
}
void clockMode() {
//CLOCK MODE
//Accounts for timezone (tzc_ means timezone-corrected)
//Seconds, minutes never change between timezones, only hours/days/months
//This only allows for positive timezone change of (timezone) hours WRT. GMT. Wouldn't try this over more than a 23 hour shift
calculateTimezoneCorrection();
displayDate();
_delay_ms(3000);
for(int i=0;i<300;i++) {
calculateTimezoneCorrection();
displayTime();
_delay_ms(10);
}
}
//Helper functions to make a number positive or negative.
int64_t makeNegative(int64_t i){return(i<0?i:-i);}
int64_t makePositive(int64_t i){return(i<0?-i:i);}
float makeNegativef(float i){return(i<0?i:-i);}
float makePositivef(float i){return(i<0?-i:i);}
#define NO_OPERATION 42
void calculatorMode() {
//Start at zero.
displayInt64(0);
//Sleep timer
unsigned long sleepTime = millis();
boolean justPressedEquals = false;
//Our running totals, floating-point and integer.
int64_t iCurrNum = 0;
int64_t iEntNum = 0;
float fCurrNum = 0.0f;
float fEntNum = 0.0f;
float enteringSB = 0.1;
//Wait for a keypad button to be pressed
uint8_t keypadButton = NO_KEY;
uint8_t operation = NO_OPERATION;
//Entering a negative number?
boolean enteringNegativeNumber = false;
boolean enteringAfterDP = false;
//Loop until we're finished, and re-enter power save mode.
while(1==1) {
while((keypadButton = readKeypad()) == NO_KEY) {
//Sleep timer exceeded?
if ((millis() - sleepTime) > 15000)
return; //After 15s go to sleep again.
//CE Button pressed.
if(button_pressed)
{
button_pressed = false;
justPressedEquals = false;
displayInt64(0);
operation = NO_OPERATION;
iCurrNum = 0;
iEntNum = 0;
fCurrNum = 0.0f;
fEntNum = 0.0f;
enteringNegativeNumber = false;
enteringAfterDP = false;
enteringSB = 0.1;
sleepTime = millis();
}
}
//A key has been pressed.
//It's a number.
if (keypadButton < 10)
{
if(enteringNegativeNumber)
{
iEntNum = makePositive(iEntNum);
fEntNum = makePositivef(fEntNum);
}
if(enteringAfterDP) {
//Floats only make sense...
fEntNum = fEntNum + enteringSB * keypadButton;
enteringSB *= 0.1;
}
else {
iEntNum = iEntNum * 10;
iEntNum = iEntNum + keypadButton;
fEntNum = fEntNum * 10;
fEntNum = fEntNum + keypadButton;
}
if(enteringNegativeNumber)
{
iEntNum = makeNegative(iEntNum);
fEntNum = makeNegativef(fEntNum);
}
displayBest(iEntNum, fEntNum);
//Wait for button release - numbers don't have a "press-hold" alt function, so we just
//spin here, after the number has been displayed, to minmimise perceived lag.
while(readKeypad()!=NO_KEY)
{;}
}
//It's not a number, it's a special button.
else {
int keyTime = 0;
//Time how long the button is held for.
while(readKeypad()!=NO_KEY)
{keyTime++; _delay_ms(10);}
printf("Key held for %i ms \n", (keyTime * 10));
//Is it an operation, or a negative sign?
if(((iEntNum == 0) && (keypadButton == KEY_SUB)) || (keypadButton == KEY_DP))
{
if(keypadButton == KEY_SUB) {
//We're entering a negative number...
enteringNegativeNumber = true;
Serial.println("Entering a negative number");
} else
{
//We're pressing the decimal place here...
enteringAfterDP = true;
enteringSB = 0.1;
//TODO implement this logic
Serial.println("Decimal place pressed...");
}
}
else {
if (justPressedEquals && (keypadButton != KEY_EQ)){}
else {
//We've pressed an operation
switch((operation)){
case NO_OPERATION:
iCurrNum = iEntNum;
fCurrNum = fEntNum;
break;
case KEY_ADD:
iCurrNum = iCurrNum + iEntNum;
fCurrNum = fCurrNum + fEntNum;
break;
case KEY_SUB:
iCurrNum = iCurrNum - iEntNum;
fCurrNum = fCurrNum - fEntNum;
break;
case KEY_MUL:
iCurrNum = iCurrNum * iEntNum;
fCurrNum = fCurrNum * fEntNum;
break;
case KEY_DIV:
if (fEntNum == 0) { //Prevent division by zero
displayMessage(MSG_ERROR);
_delay_ms(3000);
button_pressed = true;
}
if(iEntNum == 0)
iCurrNum = 2^60 * sign(iCurrNum);//infinity-ish
else
iCurrNum = iCurrNum / iEntNum;
fCurrNum = fCurrNum / fEntNum;
break;
}
}
if(keypadButton == KEY_EQ){
justPressedEquals = true;
//This leads to one subtle problem
//Say you type 2 + 2 = = + 3
//Calc does 2+2+2+2 +3
//Expected behaviour 2+2+2 +3
} else {
justPressedEquals = false;
iEntNum = 0;
fEntNum = 0.0f;
enteringNegativeNumber = false;
enteringAfterDP = false;
enteringSB = 0.1;
operation = keypadButton;
}
displayBest(iCurrNum, fCurrNum);
}
}
_delay_ms(50);
//Reset the timer, something's been pressed.
sleepTime = millis();
}
}
void displayBest(int64_t i, float f) {
//Compare the integer and floating answers. If they're within a very small amount of each other, we can assume the integer is correct (FP error)
//NOTE THAT THIS PREVENTS VERY SMALL NUMBERS BEING SHOWN INTENTIONALLY!!! Work out the sensible limit - should be near FLOAT_MIN
if(abs(i - f) < 0.001)
{
displayInt64(i);
return;
}
//If the integer is close to the float, display it.
int64_t cast = (int64_t) f;
if(abs(cast - f) < 0.000001)
{
displayInt64(cast);
return;
}
displayDouble(f);
}
//Returns 1 if the number is positive, 0 if it is zero, and -1 if it is negative.
int sign(int64_t num){
if(num > 0)
return +1;
if(num < 0)
return -1;
return 0;
}
//Mode for setting the clock time.
void setMode() {
//Copy the current (GMT) datetime into a set of variables
uint8_t l_seconds = seconds;
uint8_t l_minutes = minutes;
uint8_t l_hours = hours;
uint8_t l_days = day;
uint8_t l_months = month;
int l_years = year;
//First enter date...
displayMessage(MSG_DATE);
_delay_ms(2500);
segstates[0] = number[(l_days/10)%10];
segstates[1] = number[(l_days)%10] WITH_DECIMAL_POINT;
segstates[2] = number[(l_months/10)%10];
segstates[3] = number[(l_months)%10] WITH_DECIMAL_POINT;
segstates[4] = number[((l_years-2000)/10)%10];
segstates[5] = number[((l_years-2000))%10];
_delay_ms(250);
segstates[0] = 0; //Blank the first digit.
long sleepTime = millis();
uint8_t kpb = NO_KEY;
//Update date_time (if not C/CE pressed)
int i = 0;
uint8_t values[6];
while(i<6) {
while((kpb = readKeypad()) == NO_KEY) {
if (((millis() - sleepTime) > 15000) || button_pressed)
return; //After 15s or if CE pressed, go to sleep again, without saving the changes to the time.
}
if(kpb < 10)
{
//Valid number pressed
segstates[i] = number[kpb];
values[i] = kpb;
if((i==1) || (i==3))
segstates[i] |= 0b10000000;
if(i<=4)
segstates[i+1] = 0;//Blank the next
i++;
}
//Wait for the key to be released.
while((kpb = readKeypad()) != NO_KEY)
_delay_ms(50);
//Don't go to sleep if the button has been pressed.
sleepTime = millis();
}
//OK, so we have an array of digits.
int hypotheticalDays = values[0]*10+values[1];
int hypotheticalMonths = values[2]*10+values[3];
int hypotheticalYears = values[4]*10+values[5] + 2000;
//Check if the given date is valid.
if(!dateIsValid(hypotheticalYears, hypotheticalMonths, hypotheticalDays))
{
//Note there's no way for the year to be invalid, I believe.
hypotheticalDays = 1;
hypotheticalMonths = 1;
hypotheticalYears = 2013;
displayMessage(MSG_ERROR);
_delay_ms(5000);
}
printf("Setting d=%i, m=%i, y=%i \n", hypotheticalDays, hypotheticalMonths, hypotheticalYears);
day = hypotheticalDays;
month = hypotheticalMonths;
year = hypotheticalYears;
//Repeat for the time.
displayMessage(MSG_TIME);
_delay_ms(2500);
segstates[0] = number[(l_hours/10)%10];
segstates[1] = number[(l_hours)%10] WITH_DECIMAL_POINT;
segstates[2] = number[(l_minutes/10)%10];
segstates[3] = number[(l_minutes)%10] WITH_DECIMAL_POINT;
segstates[4] = number[((l_seconds)/10)%10];
segstates[5] = number[(l_seconds)%10];
_delay_ms(250);
segstates[0] = 0;//Blank the first digit.
sleepTime = millis();
kpb = NO_KEY;
i=0;
while(i<6) {
while((kpb = readKeypad()) == NO_KEY) {
if (((millis() - sleepTime) > 15000) || button_pressed)
return; //After 15s or if CE pressed, go to sleep again, without saving the changes to the time.
}
if(kpb < 10)
{
//Valid number pressed
segstates[i] = number[kpb];
values[i] = kpb;
if((i==1) || (i==3))
segstates[i] |= 0b10000000;
if(i<=4)
segstates[i+1] = 0;//Blank the next
i++;
}
//Wait for the key to be released.
while((kpb = readKeypad()) != NO_KEY)
_delay_ms(50);
//Don't go to sleep if the button has been pressed.
sleepTime = millis();
}
//OK, so we have an array of digits.
int hypotheticalHours = values[0]*10+values[1] - (inBst(year, month, day)?1:0);
int hypotheticalMinutes = values[2]*10+values[3];
int hypotheticalSeconds = values[4]*10+values[5];
//TODO check if valid time...
printf("Setting h=%i, m=%i, s=%i \n", hypotheticalHours, hypotheticalMinutes, hypotheticalSeconds);
if(inBst(year, month, day))
Serial.print("BST time so -1 hour");
else
Serial.print("Not BST - setting directly.");
hours = (hypotheticalHours) % 24; //Technically, should subtract one from the day if less then midnight, etc. Edge case ignored for simplicity.
minutes = hypotheticalMinutes % 60;
seconds = hypotheticalSeconds % 60;
//Save into GMT time
timezone = inBst(year, month, day)?1:0;
//Display done message
displayMessage(MSG_DONE);
_delay_ms(2000);
}
//TODO make this more efficient by having an array in PROGMEM with MSG_... being an offset in that array.
void displayMessage(uint8_t msg) {
switch(msg) {
case MSG_DONE:
segstates[0] = 0b01011110;//d
segstates[1] = 0b01011100;//o
segstates[2] = 0b01010100;//n
segstates[3] = 0b01111001;//E
segstates[4] = 0;
segstates[5] = 0;
break;
case MSG_TODO:
segstates[0] = 0b01111000;// t
segstates[1] = 0b01011100;//o
segstates[2] = 0b01011110;//d
segstates[3] = 0b01011100;//o
segstates[4] = 0;
segstates[5] = 0;
break;
case MSG_DATE:
segstates[0] = 0b01011110;//d
segstates[1] = 0b01110111;//A
segstates[2] = 0b01111000;//t
segstates[3] = 0b01111001;//E
segstates[4] = 0;
segstates[5] = 0;
break;
case MSG_CALC:
segstates[0] = 0b00111001;//C
segstates[1] = 0b01110111;//A
segstates[2] = 0b00111000;//L
segstates[3] = 0b00111001;//C
segstates[4] = 0;
segstates[5] = 0;
break;
case MSG_CHRONO:
segstates[0] = 0b00111001;//C
segstates[1] = 0b01110100;//h
segstates[2] = 0b01010000;//r
segstates[3] = 0b01011100;//o
segstates[4] = 0b01010100;//n
segstates[5] = 0b01011100;//o
break;
case MSG_TIME:
segstates[0] = 0b01111000;//t
segstates[1] = 0b00010000;//i
segstates[2] = 0b01010100;//m
segstates[3] = 0b01000100;//m
segstates[4] = 0b01111001;// E
segstates[5] = 0;
break;
case MSG_SET:
segstates[0] = 0b01101101;//S
segstates[1] = 0b01111001;//E
segstates[2] = 0b01111000;//t
segstates[3] = 0;
segstates[4] = 0;
segstates[5] = 0;
break;
case MSG_ERROR:
segstates[0] = 0b01111001;// E
segstates[1] = 0b01010000;// r
segstates[2] = 0b01010000;// r
segstates[3] = 0b01011100;// o
segstates[4] = 0b01010000;// r
segstates[5] = 0;
break;
case MSG_LOBATT:
segstates[0] = 0b00111000;// L
segstates[1] = 0b11011100;// o.
segstates[2] = 0b01111100;// b
segstates[3] = 0b01110111;// A //0b11011100;// a.
segstates[4] = 0b01111000;// t
segstates[5] = 0b01111000;//
break;
case MSG_BATT:
segstates[0] = 0b01111100;// b
segstates[1] = 0b01110111;// A //0b11011100;// a.
segstates[2] = 0b01111000;// t
segstates[3] = 0b01111000;// t
segstates[4] = 0;
segstates[5] = 0;
break;
case MSG_REMOTE:
segstates[0] = 0b00111001;//C
segstates[1] = 0b01111000;// t
segstates[2] = 0b01010000;// r
segstates[3] = 0b00110000;//l
segstates[4] = 0;
segstates[5] = 0;
break;
case MSG_POSINF:
//segstates[0] = 0b01110011;// P
//segstates[1] = 0b01011100;// o
//segstates[2] = 0b01101101;// s
segstates[0] = 0b00110000;// I
segstates[1] = 0b01010100;// n
segstates[2] = 0b01110001;// f
segstates[3] = 0;
segstates[4] = 0;
segstates[5] = 0;
break;
case MSG_NEGINF:
segstates[0] = 0b01010100;// n
segstates[1] = 0b01111001;// e
segstates[2] = 0b01101111;// g
segstates[3] = 0b00010000;// i
segstates[4] = 0b01010100;// n
segstates[5] = 0b01110001;// f
break;
}
}
//32.768kHz interrupt handler - this overflows once a second
//Making this trigger once every 8 seconds would give maximum power savings
//Unfortunately this would lose the second-level resolution, and break GMT
//This updates GMT time, detects BST transition
//TODO simplify/optimise this for power saving
SIGNAL(TIMER2_OVF_vect){
seconds++;
minutes +=(seconds/60); //Use integer division intentionally here.
seconds = seconds % 60;
hours += (minutes/60);
minutes = minutes % 60;
if (hours == 24) {
//Advance once a day.
hours = 0;
day++;
if (day > daysInMonth(year, month)) {
//If we get to, for example, day 32 of January, this will be true
//So we need to advance to the next month
month++;
day = 1;
if(month > 12) {
year++;
month = 1;
//Happy New Year!
}
}
}
//BST begins at 01:00 GMT on the last Sunday of March and ends at 01:00 GMT on the last Sunday of October
//Fire at 1AM on Sundays
if ((seconds == 0) && (minutes == 0) && (hours == 1) && (dayOfWeek(year, month, day) == Sunday)) {
if((month == 3) && ((day+7)>31)){
//If it's the last Sunday of the month we're entering BST
timezone = 1;
}
else
if((month == 10) && ((day+7)>31)){
//If it's the last Sunday of the month we're leaving BST
timezone = 0;
}
}
}
//This interrupt occurs when you push the CE button
SIGNAL(INT0_vect) {
button_pressed = true;
}
//This interrupt (overflow) should happen once every few milliseconds, when the fast timer overflows
//Display update - works with an even brightness.
SIGNAL(TIMER1_OVF_vect) {
updateDisplay();
TCNT1 = PWM_TIME;
}
//TODO Brightness control
//This interrupt should be FAST.
//(if this consumes more than a few hundred cycles, it's using too many. This runs every 4000 cycles so it shouldn't take more than 400 or so.
//OR we could nest an interrupt, at the risk of making things VERY messy...
volatile uint8_t onDisplay = 0;
//TODO inline?
void updateDisplay() {
digitalWrite(cols[onDisplay],COLUMN_OFF);
onDisplay = (onDisplay+1)%6;
//TODO this can be optimised at the expense of readability - changing to direct port modification, rather than digitalWrite.
for(uint8_t s=0;s<8;s++) {
digitalWrite(segs[s], ((segstates[onDisplay] & (1 << s))?SEGMENT_ON:SEGMENT_OFF));
}
digitalWrite(cols[onDisplay],COLUMN_ON);
}
//We have designed the resistor ladder to produce approximately the following ADC values when read:
//for PinsA
//7 - 0
//4 - 128
//1 - 256
//0 - 384
//8 - 512
//5 - 640
//2 - 768
//. - 896
//No key - 1023
static const Keys keymap[] = {
KEY_7, KEY_4, KEY_1, KEY_0, KEY_8, KEY_5, KEY_2, KEY_DP, KEY_9, KEY_6, KEY_3, KEY_EQ, KEY_ADD, KEY_SUB, KEY_MUL, KEY_DIV, NO_KEY};
//Read the value of the keypad
uint8_t readKeypad(void) {
//Switch the ADC on
//TODO Read the pins until the range is low enough to consider it "settled"? Seems to work OK without.
int val = analogRead(btnsA);
if (val > (1023-64))
val = analogRead(btnsB) + 1024;
//Find out what key this value corresponds with ie 0-63 is key0, 64-191 is key1, ..
uint8_t keycnt = 0;
while((val = val-128)>=-64)
{
// val = val - 128;
keycnt++;
}
return keymap[keycnt];
//Switch the ADC off to save power.
}