-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp_display.cpp
More file actions
1590 lines (1405 loc) · 45.1 KB
/
temp_display.cpp
File metadata and controls
1590 lines (1405 loc) · 45.1 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
// Copyright (c) 2012 All Right Reserved, Kirill Shklovsky
//
// 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/>.
/*
Cathodes
SR595 pin SR595 log ULN in ULN out
pin 7 Q7 pin 1 pin 16 - LED5&6
pin 6 Q6 pin 2 pin 15 - LED1&2
pin 5 Q5 pin 3 pin 14 - LED3&4
pin 4 Q4 pin 4 pin 13 NC
pin 3 Q3 pin 5 pin 12 - LED3
pin 2 Q2 pin 6 pin 11 - LED2
pin 1 Q1 pin 7 pin 10 - LED1
pin 15 Q0
pin 5 GND | pin 9 - VCC
LED1&2 = SR595 Q6
LED3&4 = SR595 Q5
LED5&6 = SR595 Q7
7SEG1 = SR595 Q1
7SEG2 = SR595 Q2
7SEG3 = SR595 Q3
Channel Wire What?
2 Purple RESET
4 Blue OE
1 Black output to SR595OE
*/
#define SPEEDUP8X
//~ #undef SPEEDUP8X
#ifdef SPEEDUP8X
#define F_CPU 16000000UL /* 8 MHz Internal Oscillator */
//~ #define F_CPU 8000000UL /* 8 MHz Internal Oscillator */
#else
#define F_CPU 2000000UL /* 1 MHz Internal Oscillator */
//~ #define F_CPU 1000000UL /* 1 MHz Internal Oscillator */
#endif
#include <avr/io.h>
#include <stdlib.h>
#include <string.h>
#include <util/delay.h>
#include <util/atomic.h>
#include <avr/interrupt.h>
#include "sr595.h"
//~ #include <math.h>
#define READ_BTH
#define LEDSEG_A (1<<5)
#define LEDSEG_B (1<<4)
#define LEDSEG_C (1<<2)
#define LEDSEG_DP (1<<3)
#define LEDSEG_D (1<<1)
#define LEDSEG_E (1<<0)
#define LEDSEG_F (1<<6)
#define LEDSEG_G (1<<7)
#define kINDICATOR_COUNT 6
#define IND0_R LEDSEG_D
#define IND0_G LEDSEG_E
#define IND0_B LEDSEG_F
#define IND1_R LEDSEG_A
#define IND1_G LEDSEG_B
#define IND1_B LEDSEG_C
uint8_t aintIndicatorSegmentR[2] = {IND0_R, IND1_R};
uint8_t aintIndicatorSegmentG[2] = {IND0_G, IND1_G};
uint8_t aintIndicatorSegmentB[2] = {IND0_B, IND1_B};
//~ uint8_t aintIndicatorCathode[kINDICATOR_COUNT] = {6, 6, 5, 5, 7, 7, 1};
uint8_t aintIndicatorCathode[kINDICATOR_COUNT] = {6, 6, 5, 5, 7, 7};
// Font and digits for 7segment
// ... digits
#define LED7OUT_0 LEDSEG_A | LEDSEG_B | LEDSEG_C | LEDSEG_D | LEDSEG_E | LEDSEG_F
#define LED7OUT_1 LEDSEG_B | LEDSEG_C
#define LED7OUT_2 LEDSEG_A | LEDSEG_B | LEDSEG_G | LEDSEG_E | LEDSEG_D
#define LED7OUT_3 LEDSEG_A | LEDSEG_B | LEDSEG_G | LEDSEG_C | LEDSEG_D
#define LED7OUT_4 LEDSEG_F | LEDSEG_G | LEDSEG_B | LEDSEG_C
#define LED7OUT_5 LEDSEG_A | LEDSEG_F | LEDSEG_G | LEDSEG_C | LEDSEG_D
#define LED7OUT_6 LEDSEG_A | LEDSEG_F | LEDSEG_G | LEDSEG_E | LEDSEG_C | LEDSEG_D
#define LED7OUT_7 LEDSEG_A | LEDSEG_B | LEDSEG_C
#define LED7OUT_8 LEDSEG_A | LEDSEG_B | LEDSEG_C | LEDSEG_D | LEDSEG_E | LEDSEG_F | LEDSEG_G
#define LED7OUT_9 LEDSEG_A | LEDSEG_B | LEDSEG_C | LEDSEG_D | LEDSEG_F | LEDSEG_G
// ... letters
#define LED7OUT_A LEDSEG_A | LEDSEG_F | LEDSEG_B | LEDSEG_G | LEDSEG_E | LEDSEG_C
#define LED7OUT_B LEDSEG_F | LEDSEG_G | LEDSEG_E | LEDSEG_C | LEDSEG_D
#define LED7OUT_C LEDSEG_A | LEDSEG_F | LEDSEG_E | LEDSEG_D
#define LED7OUT_D LEDSEG_B | LEDSEG_G | LEDSEG_E | LEDSEG_C | LEDSEG_D
#define LED7OUT_E LEDSEG_A | LEDSEG_F | LEDSEG_G | LEDSEG_E | LEDSEG_D
#define LED7OUT_F LEDSEG_A | LEDSEG_F | LEDSEG_G | LEDSEG_E
// ... symbols
#define LED7OUT_DASH LEDSEG_G
#define LED7OUT_ALL 0xFF
uint8_t aint7segdigits[] = {LED7OUT_0, LED7OUT_1,LED7OUT_2,LED7OUT_3,LED7OUT_4,LED7OUT_5,LED7OUT_6,LED7OUT_7,LED7OUT_8,LED7OUT_9};
/////////////////////////////////////////////////////////////////////
// SR595
#define SR74XX595_PORT PORTD
#define SR74XX595_DDR DDRD
#define SR74XX595_DS 02
#define SR74XX595_SHCP 05
#define SR74XX595_STCP0 04
#define SR74XX595_OE 07
#define SR74XX595_STCP1 06
#ifdef BTH_USE_PINKEY
#define BTH_PINKEY_DDR DDRD
#define BTH_PINKEY_PORT PORTD
#define BTH_PINKEY_PIN 1<<7
#endif
#define BTH_USE_POWER
#ifdef BTH_USE_POWER
#define BTH_POWER_DDR DDRB
#define BTH_POWER_PORT PORTB
#define BTH_POWER_PIN 1<<0
#endif
#define DELAY_LONG 1000
#define DO_SHORT_DELAY _delay_ms(250)
//~ #define DO_SHORT_DELAY ;
uint8_t STCP[2] = {SR74XX595_STCP0, SR74XX595_STCP1};
#undef PARALLEL_595
sr595 sr(
2, // nCascadeCount
# ifdef PARALLEL_595
1, // fParallel
# else PARALLEL_595
0, // fParallel
# endif PARALLEL_595
&SR74XX595_PORT, // ptrPort
&SR74XX595_DDR, // ptrDir
SR74XX595_OE, // nOE
1, // nInvertOE
SR74XX595_DS, // nDS
SR74XX595_SHCP, // nSHCP
STCP // anSTCP
);
/////////////////////////////////////////////////////////////////////
// Regimes
#define kREGIME_DISPLAYVALUES 0
#define kREGIME_SETTIMER 1
#define kREGIME_TESTLEDS 2
volatile uint8_t nRegime = 0;
/////////////////////////////////////////////////////////////////////
// Keys control
#define TIMER1_OCR1A_FPU_DIV 351360
#define kMAX_KEYBOUNCE_CHECKS 6
#define INPUTS_DIR DDRB
#define INPUTS_PORT PORTB
#define INPUTS_PIN PINB
#define INPUT_BTNRIGHT (01<<02)
#define INPUT_BTNLEFT (01<<04)
#define INPUT_ENCODERLEFT (01<<01)
#define INPUT_ENCODERRIGHT (01<<03)
#define INPUT_ENCODERBTN (01<<05)
#define INPUT_ALL (INPUT_BTNRIGHT | INPUT_BTNLEFT | INPUT_ENCODERLEFT | INPUT_ENCODERRIGHT | INPUT_ENCODERBTN)
uint8_t aintDebounceState[kMAX_KEYBOUNCE_CHECKS];
uint8_t intKeyState;
uint8_t idxKeyState;
uint8_t nStopValueCycling;
#define INPUT_IGNORE_ENCODER_OPPDIR_TICK_COUNT 24
uint8_t nEncoderRotationIgnoreTickCount;
/////////////////////////////////////////////////////////////////////
// Other low-res timer stuff
// Value cycling
volatile uint16_t nValueCycleCount; // Count of for the purposes of value cycling
#define KEYTIMER_MAX_VALUECYCLE 515 /* Update the display every 1.5 seconds
since the speed of the slow timer is independent of the CPU speed
this value need not be adjusted for FCPU
*/
volatile uint32_t nTimingCount; // Count for the purposes of displaying a timer
uint16_t nDisplayTimerValue;
uint8_t nMinuteTimerDot;
volatile uint16_t nTimerSeconds;
volatile uint8_t nTimerMinutes;
uint8_t nCountUp = 1;
#define COUNTDOWNTIMER_MAXMINUTES 120
uint16_t nKeyPressCycleCount; /* Counts how long a key has been pressed
zero when nothing is pressed */
uint8_t nIgnoreKeyRelease;
#define kLONGPRESS_MAX_CYCLE_COUNT 500 /* Long press
since the speed of the slow timer is independent of the CPU speed
this value need not be adjusted for FCPU
*/
volatile uint32_t nBthUpdateCount;
#define KEYTIMER_MAX_BTHUPDATECOUNT 515 /* Update bluetooth every 1.5 seconds
since the speed of the slow timer is independent of the CPU speed
this value need not be adjusted for FCPU
*/
/////////////////////////////////////////////////////////////////////
// Led control
/* Number of values (from sensors) displayed */
#define kDISPVALUE_COUNT 5 /* In the current setup,
4 values are sensors (3 temperature)
and one is the timer */
#define kIDXDISPVALUE_SETTING kDISPVALUE_COUNT
#define kIDXDISPVALUE_TIMER 0
#define kTEMPERATURE_MAX 220
#define kWATERLEVEL_MAX 999L
// Special display value definitions: used to indicate exceptional or error conditions
#define kDISPVALUE_NOVALUEAVAILABLE 0xFFFF // No value for this sensor yet
#define kDISPVALUE_BADREADING 0xFFFE // Wrong reading
#define kDISPVALUE_ERROR_M 0xFE00 // Some sort of error: low word is the error number
#define kDISPVALUE_DIGITCOUNT 3
#define kDISPVALUE_INDPAIRSCOUNT 3
#define kDISPVALUE_CATHODECOUNT (kDISPVALUE_DIGITCOUNT+kDISPVALUE_INDPAIRSCOUNT)
// Error value definitions
#define ERR_UNEXPECTEDSENSORTYPE 0x0001
volatile uint16_t aintDisplayValue[kDISPVALUE_COUNT+1]; // Value to be displayed in the LEDs
// Extra value is for regimes other than value cycling,
volatile uint8_t idxDisplayValue = 0; // Value (usually temperature) currently displayed
volatile uint8_t aintDisplaySegments[kDISPVALUE_COUNT+1][kDISPVALUE_CATHODECOUNT]; // Value to be displayed on each led
// Extra value is for other regimes
volatile uint8_t nResetting;
//~ volatile uint8_t idxActiveCathode = 0;
inline uint16_t getDisplayValue(uint8_t idxDispValue) {
return aintDisplayValue[idxDispValue];
}
/* This function sets the value (generally some ADC reading)
* that will be displayed on the 7-segment LEDs
* Mainly what the function does is shove the various digits into arrays
* for easy output to 7-segment leds
*/
void setDisplayValue(uint8_t idxDispValue, uint16_t intNewValue, uint8_t nDecimalDotMask = 0) {
if (nResetting) { return; }
if (aintDisplayValue[idxDispValue]==intNewValue) { return; }
aintDisplayValue[idxDispValue] = intNewValue;
if (aintDisplayValue[idxDispValue] == kDISPVALUE_NOVALUEAVAILABLE) {
// Special "no value available" value
for (int idxDigit=0; idxDigit<kDISPVALUE_DIGITCOUNT; idxDigit++) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
aintDisplaySegments[idxDispValue][idxDigit] = LED7OUT_DASH;
}
}
} else if (aintDisplayValue[idxDispValue] == kDISPVALUE_BADREADING) {
int idxDigit = kDISPVALUE_DIGITCOUNT;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
// Display "bad"
if (--idxDigit>=0) aintDisplaySegments[idxDispValue][idxDigit] = LED7OUT_B;
if (--idxDigit>=0) aintDisplaySegments[idxDispValue][idxDigit] = LED7OUT_A;
if (--idxDigit>=0) aintDisplaySegments[idxDispValue][idxDigit] = LED7OUT_D;
}
} else {
/* Either error display or regular value here
Both display some sort of value, so we factor that code out below the conditional
*/
uint16_t nDispValue; // Value to be displayed
uint8_t nDigitLoopMax; // Process all digits below this one
if ( (aintDisplayValue[idxDispValue] & kDISPVALUE_ERROR_M) == kDISPVALUE_ERROR_M) {
// Error display
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
// Display "E" on the leftmost digit
aintDisplaySegments[idxDispValue][kDISPVALUE_DIGITCOUNT-1] = LED7OUT_E;
// Display the error number on the lower digits
nDispValue = (aintDisplayValue[idxDispValue] & 0xFF);
nDigitLoopMax = 2;
}
} else {
// Regular value
aintDisplaySegments[idxDispValue][kDISPVALUE_DIGITCOUNT-1] = LED7OUT_A;
nDispValue = aintDisplayValue[idxDispValue];
nDigitLoopMax = kDISPVALUE_DIGITCOUNT;
}
// Show the value, either from error or from ADC
for (int idxDigit=0; idxDigit<nDigitLoopMax; idxDigit++) {
uint8_t nDigitValue = nDispValue % 10;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
aintDisplaySegments[idxDispValue][idxDigit] = aint7segdigits[nDigitValue];
if (nDecimalDotMask & 1<<idxDigit) {
aintDisplaySegments[idxDispValue][idxDigit] |= LEDSEG_DP;
}
}
nDispValue /= 10;
}
/* OLD CODE, PLEASE REMOVE
for (int idxDigit=0; idxDigit<kDISPVALUE_DIGITCOUNT; idxDigit++) {
int nCurrentDigit = aintDisplayValue[idxDispValue];
for (int i = 0; i<idxDigit; i++) {
nCurrentDigit = nCurrentDigit / 10;
}
nCurrentDigit = nCurrentDigit % 10;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
aintDisplaySegments[idxDispValue][idxDigit] = aint7segdigits[nCurrentDigit];
}
}
*/
} // Block actually displaying some value
}
//////////////////////////////////////////////
// ADC stuff
#define kADC_BITS 15
#define kDECIMATE_RIGHTSHIFT (kADC_BITS-10)
#define kSAMPLE_COUNT (1UL<<(kDECIMATE_RIGHTSHIFT*2UL))
#define kADCVALUE_MAX ((uint32_t)1<<((uint32_t)kADC_BITS))
/*
#if ADC_VBITS==13
// Virtual 13-bit ADC settings
#define kSAMPLE_COUNT 64
#define kDECIMATE_RIGHTSHIFT 3
#define kADV_TEMPCONVERT_MULT 0.125
#elif ADC_VBITS==14
// Virtual 14-bit ADC settings
#define kSAMPLE_COUNT 256
#define kDECIMATE_RIGHTSHIFT 4
#define kADV_TEMPCONVERT_MULT 0.0625
#elif ADC_VBITS==15
// Virtual 15-bit ADC settings
#define kSAMPLE_COUNT 1024
#define kDECIMATE_RIGHTSHIFT 5
#define kADV_TEMPCONVERT_MULT 0.03125
#elif ADC_VBITS==16
// Virtual 16-bit ADC settings
#define kSAMPLE_COUNT 4096
#define kDECIMATE_RIGHTSHIFT 6
#define kADV_TEMPCONVERT_MULT 0.015625
#endif
*/
#define kADC_COUNT 4
typedef enum {vtINVALID, vtTEMPERATURE, vtWATERLEVEL} adcValueType_t;
int8_t aintValueAdjust[kADC_COUNT] = {0, 0, +7, 0};
uint8_t aidxADC2DispValue[kADC_COUNT] = {1, 2, 3, 4}; // Index of ADC value to Disp value index
uint32_t anADCRead[kADC_COUNT]; // Values read from the ADC
uint16_t anSampleCount[kADC_COUNT]; // Number of samples read from the ADC
volatile uint16_t anLastDisplayedValue[kADC_COUNT]; // Final values
adcValueType_t anValueType[kADC_COUNT] = {vtTEMPERATURE, vtTEMPERATURE, vtTEMPERATURE, vtWATERLEVEL};
uint8_t idxADCValue; // Index of the ADC value we are reading
uint8_t ADMUXbase; // ADMUX without the channel bits
uint16_t tempFromADC(uint16_t intADCValue) {
/* Michaelis-Menten Isotope Displacement Double ([Hot] subsumed) With Offset
y = a / (b + x) + c / (d + x) + Offset
Fri Jul 13 15:52:38 2012 local server time
*/
double a = -1.7317894461536247E+05;
double b = -1.5625932997339971E+03;
double c = -1.9589674327464432E+04;
double d = 1.4053861754270164E+02;
double Offset = 1.5684078545920563E+01;
double ADCValueStart = (float)intADCValue / (float)(1<<(kADC_BITS-10));
double fRetVal;
fRetVal = (a / (b + ADCValueStart) + c / (d + ADCValueStart) ) + Offset;
return round(fRetVal);
}
float waterLevelFromADC(uint16_t intADCValue) {
#ifdef DEBUGWATERLEVEL
double retval = ((double)intADCValue/(double)kADCVALUE_MAX);
return round(retval * 10000.0);
#elif FIRST_VALUE
double gallons = ((double)intADCValue/(double)kADCVALUE_MAX) * 0.010516827;
return round(gallons*10);
#else
float gallons = ((float)intADCValue/(float)kADCVALUE_MAX) / 0.0106625 - 1.148182884;
if (gallons<0) return 0;
return round(gallons*10);
#endif
}
//////////////////////////////////////////////
// Beeps
#define kBEEP_CONTROL_MAXENTRIES 9
#define SPKOUT_OC2A 1
#define SPKOUT_OC2B 2
#define SPEAKER_OUT_PIN SPKOUT_OC2B
#if SPEAKER_OUT_PIN == 0
#error Programming error in speaker settings
#elif SPEAKER_OUT_PIN == SPKOUT_OC2A
#warning SPEAKER_OUT_PIN is set to SPKOUT_OC2A
#define kSPEAKER_PORT PORTB
#define kSPEAKER_DDR DDRB
#define kSPEAKER_PIN 3
#elif SPEAKER_OUT_PIN == SPKOUT_OC2B
#warning SPEAKER_OUT_PIN is set to SPKOUT_OC2B
#define kSPEAKER_PORT PORTD
#define kSPEAKER_DDR DDRD
#define kSPEAKER_PIN 3
#else
#error Unknown SPEAKER_OUT_PIN
#endif
#ifndef kSPEAKER_PORT
#error kSPEAKER_PORT is not defined
#endif
#ifndef kSPEAKER_DDR
#error kSPEAKER_DDR is not defined
#endif
#ifndef kSPEAKER_PIN
#error kSPEAKER_PIN is not defined
#endif
#define kIDXNEXTBEEP_NEXT 0xFF
#define kIDXNEXTBEEP_STOP 0xFE
#define kIDXNEXTBEEP_FIRST 0x00
typedef struct {
uint16_t nFreq;
uint16_t nAudibleCount;
uint16_t nSilentCount;
uint8_t idxNextBeep;
} beepStruct;
volatile beepStruct anBeepControl[kBEEP_CONTROL_MAXENTRIES];
#define kBEEPCONTROL_NOBEEP 0xFF
volatile uint8_t idxBeepControl = kBEEPCONTROL_NOBEEP;
volatile uint8_t nBeepControlCount;
uint16_t nBeepTimer;
uint8_t nBeepAudible;
volatile uint8_t nBeginBeep;
inline void beepTurnOff() {
kSPEAKER_DDR &= ~(1<<kSPEAKER_PIN); /* turn off the OC PIN */
kSPEAKER_PORT |= 1<<kSPEAKER_PIN; /* set this pin high */
TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)); /* Normal operation: OC pins disconnected */
//~ TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0)); /* Normal operation: OC pins disconnected */
TCCR2B = 0; // Timer stopped
}
void beepStop() {
nBeepControlCount = 0;
idxBeepControl = kBEEPCONTROL_NOBEEP;
beepTurnOff();
}
void beepProcessing() {
if ( nBeginBeep || (nBeepControlCount && (idxBeepControl != kBEEPCONTROL_NOBEEP)) ) {
// We are beeping
uint8_t nDoNextBeepStructure = 0;
if (nBeginBeep) {
idxBeepControl = 0;
nBeepTimer = 0;
nDoNextBeepStructure = 1;
} else {
if (nBeepAudible) {
if (++nBeepTimer > anBeepControl[idxBeepControl].nAudibleCount) {
if (anBeepControl[idxBeepControl].nSilentCount > 0) {
// Do the silent part of the beep
beepTurnOff();
nBeepAudible = 0;
nBeepTimer = 0;
} else {
// Proceed to next beep structure
nDoNextBeepStructure = 1;
}
} // else: still doing audible
} else { // Silent part
if (++nBeepTimer > anBeepControl[idxBeepControl].nSilentCount) {
// Proceed to next beep structure
nDoNextBeepStructure = 1;
} // else: still doing silent
}
}
if (nDoNextBeepStructure) {
nBeepTimer = 0;
if (!nBeginBeep) {
switch (anBeepControl[idxBeepControl].idxNextBeep) {
case kIDXNEXTBEEP_NEXT :
idxBeepControl++;
break;
case kIDXNEXTBEEP_STOP :
idxBeepControl=nBeepControlCount;
break;
default:
idxBeepControl = anBeepControl[idxBeepControl].idxNextBeep;
}
} else {
nBeginBeep = 0;
}
if (idxBeepControl < nBeepControlCount) {
nBeepAudible = 1;
kSPEAKER_DDR |= 1<<kSPEAKER_PIN; /* Connect the OC PIN */
TIMSK2 = 0; // No interrupts
TCNT2 = 0; // Initial counter value
/* CTC mode, toggle the output pin */
TCCR2A = 0
#if SPEAKER_OUT_PIN == SPKOUT_OC2A
| 0<<COM2A1 // COM2A1
| 1<<COM2A0 // COM2A0
| 0<<COM2B1 // COM2B1
| 0<<COM2B0 // COM2B0
#elif SPEAKER_OUT_PIN == SPKOUT_OC2B
| 0<<COM2A1 // COM2A1
| 0<<COM2A0 // COM2A0
| 0<<COM2B1 // COM2B1
| 1<<COM2B0 // COM2B0
#endif
// -
//
| 1<<WGM21 // WGM21
| 0<<WGM20 // WGM20
;
TCCR2B = 0
| 0<<FOC2A // FOC2A
| 0<<FOC2B // FOC2B
//
//
| 0<<WGM22 // WGM22
;
// Set the prescaler
uint16_t nPrescaler;
switch (F_CPU) {
case 20000000UL-16000000UL:
// Prescaler = 1024
nPrescaler = 1024;
TCCR2B |= (1<<CS22)|(1<<CS21)|(1<<CS20);
break;
// Prescaler = 1024
nPrescaler = 1024;
TCCR2B |= (1<<CS22)|(1<<CS21)|(1<<CS20);
break;
case 8000000UL :
// Prescaler = 256
nPrescaler = 256;
TCCR2B |= (1<<CS22)|(1<<CS21)|(0<<CS20);
break;
case 1000000UL :
// Prescaler = 32
nPrescaler = 32;
TCCR2B |= (0<<CS22)|(1<<CS21)|(1<<CS20);
break;
default:
// Prescaler = 256
nPrescaler = 256;
TCCR2B |= (1<<CS22)|(1<<CS21)|(0<<CS20);
break;
}
// Calculate the TOP
//~ kSPEAKER_OCR2 = F_CPU / nPrescaler / anBeepControl[idxBeepControl].nFreq;
OCR2A = F_CPU / nPrescaler / anBeepControl[idxBeepControl].nFreq;
OCR2B = F_CPU / nPrescaler / anBeepControl[idxBeepControl].nFreq;
} else {
// Done beeping
idxBeepControl = kBEEPCONTROL_NOBEEP;
nBeepControlCount = 0;
beepTurnOff();
}
}
}
}
void beepA() {
anBeepControl[0].nFreq = 2000;
anBeepControl[0].nAudibleCount = 1;
anBeepControl[0].nSilentCount = 0;
anBeepControl[0].idxNextBeep = kIDXNEXTBEEP_STOP;
nBeepControlCount = 1;
nBeginBeep = 1;
}
void beepB() {
anBeepControl[0].nFreq = 440;
anBeepControl[0].nAudibleCount = 16;
anBeepControl[0].nSilentCount = 0;
anBeepControl[0].idxNextBeep = kIDXNEXTBEEP_NEXT;
anBeepControl[1].nFreq = 554;
anBeepControl[1].nAudibleCount = 16;
anBeepControl[1].nSilentCount = 0;
anBeepControl[1].idxNextBeep = kIDXNEXTBEEP_NEXT;
anBeepControl[2].nFreq = 659;
anBeepControl[2].nAudibleCount = 16;
anBeepControl[2].nSilentCount = 0;
anBeepControl[2].idxNextBeep = kIDXNEXTBEEP_NEXT;
anBeepControl[3].nFreq = 880;
anBeepControl[3].nAudibleCount = 32;
anBeepControl[3].nSilentCount = 0;
anBeepControl[3].idxNextBeep = kIDXNEXTBEEP_STOP;
nBeepControlCount = 4;
nBeginBeep = 1;
}
void beepC_lowlong() {
anBeepControl[0].nFreq = 220;
anBeepControl[0].nAudibleCount = 64;
anBeepControl[0].nSilentCount = 0;
anBeepControl[0].idxNextBeep = kIDXNEXTBEEP_STOP;
nBeepControlCount = 1;
nBeginBeep = 1;
}
void beepD_higshort() {
anBeepControl[0].nFreq = 800;
anBeepControl[0].nAudibleCount = 32;
anBeepControl[0].nSilentCount = 0;
anBeepControl[0].idxNextBeep = kIDXNEXTBEEP_STOP;
nBeepControlCount = 1;
nBeginBeep = 1;
}
void beepE_timerexpired() {
for (int i = 0; i<5; i++) {
anBeepControl[i].nFreq = 1000;
anBeepControl[i].nAudibleCount = 16;
anBeepControl[i].nSilentCount = 4;
anBeepControl[i].idxNextBeep = kIDXNEXTBEEP_NEXT;
}
anBeepControl[4].idxNextBeep = 0;
anBeepControl[4].nSilentCount = 80;
nBeepControlCount = 5;
nBeginBeep = 1;
}
void beepF_setTimer() {
anBeepControl[0].nFreq = 800;
anBeepControl[0].nAudibleCount = 16;
anBeepControl[0].nSilentCount = 0;
anBeepControl[0].idxNextBeep = kIDXNEXTBEEP_NEXT;
anBeepControl[1].nFreq = 951;
anBeepControl[1].nAudibleCount = 16;
anBeepControl[1].nSilentCount = 0;
anBeepControl[1].idxNextBeep = kIDXNEXTBEEP_NEXT;
anBeepControl[2].nFreq = 1270;
anBeepControl[2].nAudibleCount = 32;
anBeepControl[2].nSilentCount = 0;
anBeepControl[2].idxNextBeep = kIDXNEXTBEEP_STOP;
nBeepControlCount = 3;
nBeginBeep = 1;
}
void beepG_finishSetTimer() {
anBeepControl[0].nFreq = 1270;
anBeepControl[0].nAudibleCount = 16;
anBeepControl[0].nSilentCount = 0;
anBeepControl[0].idxNextBeep = kIDXNEXTBEEP_NEXT;
anBeepControl[1].nFreq = 951;
anBeepControl[1].nAudibleCount = 16;
anBeepControl[1].nSilentCount = 0;
anBeepControl[1].idxNextBeep = kIDXNEXTBEEP_NEXT;
anBeepControl[2].nFreq = 800;
anBeepControl[2].nAudibleCount = 32;
anBeepControl[2].nSilentCount = 0;
anBeepControl[2].idxNextBeep = kIDXNEXTBEEP_STOP;
nBeepControlCount = 3;
nBeginBeep = 1;
}
void beepH_440forever() {
anBeepControl[0].nFreq = 440;
anBeepControl[0].nAudibleCount = 999;
anBeepControl[0].nSilentCount = 0;
anBeepControl[0].idxNextBeep = kIDXNEXTBEEP_FIRST;
nBeepControlCount = 1;
nBeginBeep = 1;
}
void beepG_charge() {
#define DEFAULT_LENGTH 32
nBeepControlCount = 6;
for (int i = 0; i<nBeepControlCount; i++) {
anBeepControl[i].nAudibleCount = DEFAULT_LENGTH;
anBeepControl[i].nSilentCount = DEFAULT_LENGTH/4;
anBeepControl[i].nSilentCount = 0;
anBeepControl[i].idxNextBeep = kIDXNEXTBEEP_NEXT;
}
anBeepControl[0].nFreq = 800;
anBeepControl[1].nFreq = 1068;
anBeepControl[2].nFreq = 1345;
anBeepControl[3].nFreq = 1600;
anBeepControl[3].nAudibleCount = DEFAULT_LENGTH*2;
anBeepControl[4].nFreq = 1345;
anBeepControl[4].nAudibleCount = DEFAULT_LENGTH/2;
anBeepControl[5].nFreq = 1600;
anBeepControl[5].nAudibleCount = DEFAULT_LENGTH*2;
nBeginBeep = 1;
}
void beepH_vlesurodilas() {
#define DEFAULT_LENGTH 32
nBeepControlCount = 8;
for (int i = 0; i<nBeepControlCount; i++) {
anBeepControl[i].nAudibleCount = DEFAULT_LENGTH;
anBeepControl[i].nSilentCount = DEFAULT_LENGTH/4;
anBeepControl[i].nSilentCount = 0;
anBeepControl[i].idxNextBeep = kIDXNEXTBEEP_NEXT;
}
anBeepControl[0].nFreq = 440;
anBeepControl[1].nFreq = 784;
anBeepControl[2].nFreq = 784;
anBeepControl[3].nFreq = 698;
anBeepControl[4].nFreq = 784;
anBeepControl[5].nFreq = 622;
anBeepControl[6].nFreq = 440;
anBeepControl[7].nFreq = 440;
nBeginBeep = 1;
}
void beepI_vlesuonarosla() {
#define DEFAULT_LENGTH 32
nBeepControlCount = 6;
for (int i = 0; i<nBeepControlCount; i++) {
anBeepControl[i].nAudibleCount = DEFAULT_LENGTH;
anBeepControl[i].nSilentCount = DEFAULT_LENGTH/4;
anBeepControl[i].nSilentCount = 0;
anBeepControl[i].idxNextBeep = kIDXNEXTBEEP_NEXT;
}
anBeepControl[0].nFreq = 440;
anBeepControl[1].nFreq = 784;
anBeepControl[2].nFreq = 784;
anBeepControl[3].nFreq = 698;
anBeepControl[4].nFreq = 932;
anBeepControl[5].nFreq = 784;
anBeepControl[5].nAudibleCount = DEFAULT_LENGTH*2;
nBeginBeep = 1;
}
//~ #include "uart.h"
//////////////////////////////////////////////
// Serial
#include <stdio.h>
#include <avr/io.h>
#ifndef F_CPU
# error Must define F_CPU or pass it as compiler argument
#endif
extern "C"{
FILE * uart_out;
}
//~ FILE uart_out = FDEV_SETUP_STREAM(uart_putChar, NULL, _FDEV_SETUP_WRITE);
/*
void uart_init(){
# define BAUD 9600
# include <util/setbaud.h>
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
//~ # if USE_2x
//~ USCR0A |= _BV(U2X);
//~ # else
//~ USCR0A &= ~_BV(U2X);
//~ # endif
UCSR0B |= _BV(TXEN); // enable transmit
}
int uart_putChar(char c, FILE *unused){
if (c == '\n') // a line feed character also
uart_putChar('\r', unused); // requires a carriage return
loop_until_bit_is_set(UCSR0A, UDRE0); // wait for UDR0 to be ready
UDR0 = c; // write character
return 0; // return
}
#endif
*/
//////////////////////////////////////////////
// Serial
#include <stdio.h>
#define BAUD 9600
#include <util/setbaud.h>
void uart_init(void) {
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= _BV(U2X0);
#else
UCSR0A &= ~(_BV(U2X0));
#endif
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); // 8-bit data
UCSR0B = _BV(RXEN0) | _BV(TXEN0); // Enable RX and TX
}
int uart_putChar(char c, FILE *stream) {
if (c == '\n') {
uart_putChar('\r', stream);
}
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
return 0;
}
int uart_getChar(FILE *stream) {
loop_until_bit_is_set(UCSR0A, RXC0); // Wait until data exists.
return UDR0;
}
//////////////////////////////////////////////
// Setting indicators
inline void setIndicator(uint8_t idxDisplayValue, uint8_t idxIndicatorLed, uint8_t R, uint8_t G, uint8_t B) {
uint8_t nCathode = kDISPVALUE_DIGITCOUNT + (idxIndicatorLed>>1);
aintDisplaySegments[idxDisplayValue][nCathode] =
(R ? aintIndicatorSegmentR[idxIndicatorLed % 2] : 0)
|
(G ? aintIndicatorSegmentG[idxIndicatorLed % 2] : 0)
|
(B ? aintIndicatorSegmentB[idxIndicatorLed % 2] : 0)
;
}
//////////////////////////////////////////////
// Bluetooth update
inline void doBthUpdate() {
static char strValue[10];
fputs("@", stdout);
fputs(((nCountUp)?"+":"-"), stdout);
itoa(nTimerMinutes, strValue, 10);
fputs(strValue, stdout);
fputs(":", stdout);
uint8_t nSecs = nTimerSeconds % 60;
itoa(nSecs, strValue, 10);
if (nSecs<10) { fputs("0", stdout); }
fputs(strValue, stdout);
for (int i=0; i<kADC_COUNT; i++) {
fputs(" ADC", stdout);
itoa(i, strValue, 10);
fputs(strValue, stdout);
fputs(":", stdout);
itoa(anLastDisplayedValue[i], strValue, 10);
fputs(strValue, stdout);
}
puts("");
}
//////////////////////////////////////////////
// Main
int main(void) {
sr.setOutput(0);
//~ sr.setOeDisableDuringLoad(1);
//~ _delay_ms(1000); // Wait a bit for bluetooth module to power up
// Set up the bluetooth
/* Set up the serial comm */
uart_out = fdevopen(uart_putChar, uart_getChar);
stdout = stdin = uart_out;
uart_init();
#ifdef BTH_USE_POWER
BTH_POWER_DDR |= BTH_POWER_PIN;
//~ BTH_POWER_PORT |= 0; // Turn off everything on that port
#endif
# ifdef BTH_USE_PINKEY
BTH_PINKEY_DDR |= BTH_PINKEY_PIN; // Make the key pin output
BTH_PINKEY_PORT &= ~(BTH_PINKEY_PIN); // Set the key pin low
//~ BTH_PINKEY_PORT |= BTH_PINKEY_PIN; // Set the key pin high
# endif
//~ _delay_ms(1500); // Wait a bit for everything to clear
#ifdef BTH_USE_POWER
BTH_POWER_PORT |= BTH_POWER_PIN; // Power up the bluetooth module
#endif
if (0) {
// Set the name of the device
fputs ("AT+NAMETemp-o-matic", stdout);
_delay_ms(1500);
// Set device password
fputs ("AT+PIN1234", stdout);
_delay_ms(1500);
}
# ifdef BTH_USE_PINKEY
BTH_PINKEY_PORT |= BTH_PINKEY_PIN; // Set the key pin high
#endif
# ifdef BTH_USE_PINKEY
_delay_ms(1500);
//~ BTH_PINKEY_PORT &= ~(BTH_PINKEY_PIN); // Lower the key line to make bluetooth transparent
#endif
// Run at 8mhz
#ifdef SPEEDUP8X
CLKPR = 1<<CLKPCE;
CLKPR = 0;
#else
CLKPR = 1<<CLKPCE;
CLKPR = 3;
#endif
DDRD = 0xFF;
//~ sr.writeByte(1, 0xFF); // Enable all common cathodes
//~ sr.writeByte(0, 0xFF); // Nothing is on
//~ sr.setOutput(1);
while (0) {
sr.writeByte(1, 1<<3); // Enable all common cathodes
//~ _delay_ms(250);
//~ sr.writeByte(0, 0x7E); // Nothing is on
sr.setOutput(1);
}
/* Hi-res timebase - using timer 0
Used for display PWM -
Using 8 bit timer because this update happens fast,
the timer does not need to count very high
*/
TCNT0 = 0; // Initial counter value
TCCR0A =(1<<WGM01); // CTC (Clear on capture = comparison) mode
//~ TCCR0B = (1<<CS02) | (0<<CS01) | (1<<CS00); // Prescaler = 1024
//~ TCCR0B = (1<<CS02) | (0<<CS01) | (0<<CS00); // Prescaler = 256
//~ TCCR0B = (1<CS02) | (0<<CS01) | (1<<CS00); // Prescaler = 8
TCCR0B = (0<CS02) | (1<<CS01) | (1<<CS00); // Prescaler = 8
//~ OCR0A = F_CPU/1024/1000; // Refresh every second
OCR0A = 0x40; // About 1000 times per second (1Khz)
//~ OCR0A = 0xFF; // About 1000 times per second (1Khz)
TIMSK0 |= (1<<OCIE0A); // Enable interrupts on compare match A
// Set up the outputs
// ... Initialize everything to zero
for (int i = 0; i<kDISPVALUE_COUNT+1; i++) {
for (int j = 0; j<kDISPVALUE_CATHODECOUNT; j++) {
aintDisplaySegments[i][j] = 0;
}
}
// Initialize the indicators to green
for (int i = 0; i<kDISPVALUE_COUNT; i++) {
setIndicator(i, i, 0, 1, 0);
}
// Set up the inputs
// ... Initialize reading direction
INPUTS_DIR &= ~(INPUT_ALL);
// ... Initialize pullup resistors
INPUTS_PORT |= INPUT_ALL;
// ... Initialize the debouncing structure
idxKeyState = 0;
for (int i=0; i<kMAX_KEYBOUNCE_CHECKS; i++) {
aintDebounceState[i] = INPUT_ALL; // Inputs are high by default,
// so set them up to be high
}
intKeyState = INPUT_ALL;
/* Low res timebase - using timer 1
Used for
cycling between readings
reading keys
*/
TCNT1 = 0; // Initial counter value
TCCR1A =0x00; // Not connected to any pin, normal operation
/* Prescaler = 1024