-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimpleAlarmClock.cpp
More file actions
1842 lines (1703 loc) · 66.5 KB
/
SimpleAlarmClock.cpp
File metadata and controls
1842 lines (1703 loc) · 66.5 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
#include <SimpleAlarmClock.h>
#include <Wire.h>
/** **********************************************************************
* SimpleAlarmClock Library - cpp file
* A library for the ZS-042 Module that has a DS3231 RTC and AT24C32 EEPROM.
* @author Ricardo Moreno
* @version 1.0.0
* @copyright Ricardo Moreno
* @license MIT License
************************************************************************
* Versions:
* 11/25/2018 v1.0.0 Initial version/release
*
*********************************************************************** */
SimpleAlarmClock::SimpleAlarmClock(byte _rtc_Address, byte _eeprom_Address, bool alarmIntEnabled){
/** ******************************************************************
* Constructor *
******************************************************************* */
RTC_ADDRESS = _rtc_Address;
EEPROM_ADDRESS = _eeprom_Address;
if (alarmIntEnabled == true){ INTCN = 1;} else { INTCN = 0;}
// begin method will update local variables
}
byte SimpleAlarmClock::readByte(byte Address, byte &dataBuffer){
/** ******************************************************************
* readByte performs a simple single byte Wire.read to the varable
* dataBuffer using a pointer at the Address provided.
* Paramters:
* Address - register address location
* dataBuffer - the varable, as a pointer, to store byte value
*
* Returns:
* Bits 7-4: error from Wire.endTransmission
* Bits 3-0: length from Wire.requestFrom
****************************************************************** */
byte upperbyte = 0;
byte lowerbyte = 0;
Wire.beginTransmission(RTC_ADDRESS);
Wire.write(Address);
//This holds the device for exclusive communication
upperbyte = Wire.endTransmission(false);
/* Returns:
byte, which indicates the status of the transmission:
0:success
1:data too long to fit in transmit buffer
2:received NACK on transmit of address
3:received NACK on transmit of data
4:other error
*/
if (upperbyte == 0) {
lowerbyte = Wire.requestFrom((int)RTC_ADDRESS, 1, (int)true);
/* Returns:
# - the number of bytes returned from the slave device
*/
//while(Wire.available()<=1){ dataBuffer = Wire.read(); }
dataBuffer = Wire.read();
} else {
lowerbyte = 0;
}
return ((upperbyte << 4) | lowerbyte);
}
byte SimpleAlarmClock::readBytes(byte Address, byte dataBuffer[], byte Length){
/** ******************************************************************
* Reads multiple bytes from the register and stores them in the
* dataBuffer array.
* Parameters:
* Address - registry location value
* databuffer[] - array to store byte values, as a pointer
* Length - number of bytes or array length
*
* Returns:
* Bits 7-4: error from Wire.endTransmission
* Bits 3-0: length from Wire.requestFrom
****************************************************************** */
byte upperbyte;
byte lowerbyte;
Wire.beginTransmission(RTC_ADDRESS); //I2C address of the device
Wire.write(Address); //Starting register for Readings
//Hold the device for exclusive communication
upperbyte = Wire.endTransmission(false);
/* Wire.endTransmission will return the following values:
0:success
1:data too long to fit in transmit buffer
2:received NACK on transmit of address
3:received NACK on transmit of data
4:other error
*/
if (upperbyte == 0) {
//Request "number" of bytes and release I2C bus
lowerbyte = Wire.requestFrom((int)RTC_ADDRESS, (int)Length, (int)true);
/* Returns:
# - the number of bytes returned from the slave device
*/
if (lowerbyte == Length){
//while(!Wire.available()){}; //wait
for (byte i=0; i<Length; i++){
dataBuffer[i] = Wire.read();
}
}
} else {
lowerbyte = 0;
}
return((upperbyte << 4) | lowerbyte);
}
byte SimpleAlarmClock::writeByte(byte Address, byte dataBuffer){
/** ******************************************************************
* writeByte performs a simple single byte Wire.write of the
* dataBuffer to the Address provided.
* Paramters:
* Address - register address location
* dataBuffer - the byte value to write
*
* Returns:
* 0:success
* 1:data too long to fit in transmit buffer
* 2:received NACK on transmit of address
* 3:received NACK on transmit of data
* 4:other error
****************************************************************** */
Wire.beginTransmission(RTC_ADDRESS); // This begins the I2C communication
Wire.write(Address); // Access the register
Wire.write(dataBuffer); // Set the byte value to register above
return Wire.endTransmission(true); // Close I2C communication
}
byte SimpleAlarmClock::writeBytes(byte Address, byte dataBuffer[], byte Length){
/** ******************************************************************
* Writes multiple bytes to registry from an array dataBuffer
* Parameters:
* Address - registry location value
* databuffer[] - array to with byte values
* Length - number of bytes or array length
*
* Returns:
* 0:success
* 1:data too long to fit in transmit buffer
* 2:received NACK on transmit of address
* 3:received NACK on transmit of data
* 4:other error
****************************************************************** */
Wire.beginTransmission(RTC_ADDRESS); //device address
Wire.write(Address); //Registry address
for (byte i=0; i<Length; i++) {
Wire.write(dataBuffer[i]);
}
return Wire.endTransmission(true); // Close I2C communication
}
void SimpleAlarmClock::begin(void){
/** ******************************************************************
* The begin method to get SimpleAlarmClock working.
*
* Rather than resetting the RTC after startup. It assumes that there
* was a power loss. The RTC battery may have saved all the clock
* configurations. To avoid a rest, perform a simple check the
* clock's year. If it is equal to or greater than 2018 (or 18), it's
* probably OK, else initialize the clock and reset alarms.
*
* If the RTC battery fails, the RTC.year returns 2001 (or just 1).
****************************************************************** */
byte year = 0;
byte _byteValue;
Wire.begin();
// Read and define various registry values stored as variables,
// See datasheet Figure 1 Timekeeping Registers
//Set INTCN based on passed constructor value
if (INTCN == 1){setInterruptCtrl(true);} else {setInterruptCtrl(false);}
getCtrlRegister();
// this function returns the register 0x0e as a byte, not used here,
// but also sets the following variables:
// EOSC, BBSQW, CONV, RS2, RS1, INTCN
// variables might be used in the future?
// Clear all snoozing
SnoozingA1 = false;
SnoozingA2 = false;
/* ******************************************
* Determine the RTC year
* *************************************** */
// First check the Century bit
byte i = readByte(0x05, _byteValue);
// i used for future error checking
// bit 7 of month register 0x05 indicates the century
if (_byteValue & _BV(7)) { year += 100; }
// Read year from register 0x06
i = readByte(0x06, _byteValue);
// i used for future error checking
year += bcd2bin(_byteValue);
/* ******************************************
* Check for valid year
* *************************************** */
// If year is less than 2018, reset RTC
if (year < 18){
resetClock();
resetAlarm(1);
resetAlarm(2);
} else {
// RTC clock might be OK - check alarms
AlarmTime alarm_i;
//check eepromAlarm1
if (checkMemAlarm(1) == true){
// checkMemAlarm() has a few alarm checks.
// If power was lost and snoozed was pressed
// assume alarm stored in Mem is correct
// restore saved alarm
alarm_i = readMemAlarm(1);
setAlarm(alarm_i,1);
} else {
//Reset Alarm
resetAlarm(1);
}
//check eepromAlarm2
if (checkMemAlarm(2) == true){
//if power was lost and snoozed was pressed
//assume alarm stored in Mem is correct
//restore saved alarm
alarm_i = readMemAlarm(2);
setAlarm(alarm_i,2);
} else {
//Reset Alarm
resetAlarm(2);
}
}
}
void SimpleAlarmClock::resetClock(void){
/** ******************************************************************
* Resets the RTC clock to 12:00 AM and 01/01/2018
* sets ClockMode to 12hr
****************************************************************** */
DateTime currentDate; //New local object
currentDate.Second = 0;
currentDate.Minute = 0;
currentDate.Hour = 12;
currentDate.Dow = 2; // 1-7 2=Monday (Day Of Week)
currentDate.Day = 1; // calendar dd
currentDate.Month = 1; // calendar mm
currentDate.Year = 18; // calendar yyy, note 2000 must be addded
currentDate.ClockMode = 0; // 0-2; 0=AM, 1=PM, 2=24hour
write(currentDate);
//Reset Register 0x0e
// assume default bits values with alarms off
writeByte(0x0e, 0b00011100);
getCtrlRegister();
// this function returns the register 0x0e as a byte, not used here,
// but also sets the following variables:
// EOSC, BBSQW, CONV, RS2, RS1, INTCN
}
void SimpleAlarmClock::resetAlarm(byte alarmSelected){
/** ******************************************************************
* Reset Alarms - 12hr ClockMode
* Parameters:
* alarmSelected - 1 or 2 for alarm1 or alarm2, anything else is
* ignored.
****************************************************************** */
//Check for valid alarmSelected
if ((alarmSelected == 1)||(alarmSelected == 2)){
AlarmTime alarm_i;
alarm_i.Second = 0; // 0-59 = 6 bits
alarm_i.Minute = 0; // 0-59 = 6 bits
alarm_i.Hour = 6; // 0-23 = 5 bits
alarm_i.AlarmMode = 1; // 0=Daily, 1=Weekday, 2=Weekend, 3=Once
alarm_i.ClockMode = 0; // 0-2; 0=AM, 1=PM, 2=24hour
alarm_i.Enabled = false;
setAlarm(alarm_i, alarmSelected);
}
// else do nothing
}
DateTime SimpleAlarmClock::read(void){
/** ******************************************************************
* The method to capture the time, date, and clockmode of the RTC
* and returns it to the passed DateTime object. All times, dow,
* dates are stored in BCD format.
* Returns:
* DateTime object
****************************************************************** */
DateTime currentDate;
byte _byteValue[7];
// Read in the next 7 bytes which store the
// Seconds, Minutes, Hours, Day-Of-Week, Day, Month, Year
// uses readBytes into an array to read all the bytes at once
readBytes(0x00, _byteValue, 7);
// Seconds from register 0x00
currentDate.Second = bcd2bin(_byteValue[0]);
// Minutes from register 0x01
currentDate.Minute = bcd2bin(_byteValue[1]);
// Hour byte from register 0x02
// Determine if 12 or 24h modes
// 6th Bit of hour indicates 12/24 Hour mode
// 5th Bit of hour indicates 10hr or AM=0/PM=1
if(_byteValue[2] & _BV(6)) {
// 12hr is enabled
if (_byteValue[2] & _BV(5)) {
//PM found
currentDate.ClockMode = 1;
} else {
//AM found
currentDate.ClockMode = 0;
}
//Mask out upper bits
currentDate.Hour = bcd2bin(_byteValue[2] & 0B00011111);
} else {
//24hr found
currentDate.ClockMode = 2;
//Mask out upper bits
currentDate.Hour = bcd2bin(_byteValue[2] & 0B00111111);
}
// Day of the week from register 0x03
currentDate.Dow = bcd2bin(_byteValue[3]);
// dd date from register 0x04
currentDate.Day = bcd2bin(_byteValue[4]);
// Month byte from register 0x05
// bit 7 of month indicates the year century
if (_byteValue[5] & _BV(7)) {
currentDate.Year = 100;
} else {
currentDate.Year = 0;
}
currentDate.Month = bcd2bin(_byteValue[5] & 0B00011111);
// Year from register 0x06
currentDate.Year += bcd2bin(_byteValue[6]);
return currentDate;
}
byte SimpleAlarmClock::write(const DateTime ¤tDate){
/** ******************************************************************
* Writes the Clock values stored in a DataTime object into RTC's
* registers. Use setAlarm(i) to set alarms. Code inherited from
* DS3231_Simple library. To be updated.
* Parameters:
* DataTime Oject with values to be stored
*
* Returns: (work in progress)
* 0 - fail
* 1 - success
****************************************************************** */
byte _byteValue[7];
//Need to set or validate DOW
byte Dow = calcDow(currentDate.Month,currentDate.Day,2000+currentDate.Year);
//Register 0x00
_byteValue[0] = (bin2bcd(currentDate.Second));
// Register 0x01
_byteValue[1] = (bin2bcd(currentDate.Minute));
//Register 0x02
//Format Hour byte Bit 7 and 6 could be stripped from Hour value
if (currentDate.ClockMode == 2){
//24hr
// set bits 7=0, 6/24hr=0
_byteValue[2] = (bin2bcd(currentDate.Hour) & 0B00111111);
} else {
//12hr
if (currentDate.ClockMode == 0){
//AM
//set bits 7=0, 5/AM=0
_byteValue[2] = bin2bcd(currentDate.Hour) & 0B01011111;
//set bite 6/12hr=1
_byteValue[2] |= (1 << 6);
} else {
//PM
//set bits 7=0, 6/12hr=1, 5/PM=1
_byteValue[2] = bin2bcd(currentDate.Hour) & 0B01111111;
_byteValue[2] |= (3 << 5);
}
}
//Register 0x03
_byteValue[3] = bin2bcd(Dow);
//Register 0x04
_byteValue[4] = bin2bcd(currentDate.Day);
//Registers 0x05 and 0x06
//Format Month byte's bit 7 century if available
if (currentDate.Year <= 99) {
//No Century bit
_byteValue[5] = bin2bcd(currentDate.Month);
_byteValue[6] = bin2bcd(currentDate.Year);
} else {
//Century bit found
_byteValue[5] = (bin2bcd(currentDate.Month) | 0B10000000);
_byteValue[6] = bin2bcd(currentDate.Year - 100);
//Note: range for this byte is only 0-99
}
return writeBytes(0x00, _byteValue, 7);
}
AlarmTime SimpleAlarmClock::readAlarm(byte alarmSelected){
/** ******************************************************************
* readAlarm will read from RTC if SnoozingAx = false, else it will
* read eeprom.
* Parameters:
* alarmSelected = 1 or 2 for alarm1 or alarm2,
* ignore invalid values
*
* Returns:
* AlarmTime object value
****************************************************************** */
AlarmTime alarm_i;
switch (alarmSelected){
case 1:
if (SnoozingA1==true) {
alarm_i = readMemAlarm(1);
} else {
alarm_i = readRtcAlarm(1);
}
break;
case 2:
if (SnoozingA2==true) {
alarm_i = readMemAlarm(2);
} else {
alarm_i = readRtcAlarm(2);
}
break;
default:
//do nothing
break;
}
return alarm_i;
}
AlarmTime SimpleAlarmClock::readRtcAlarm(byte alarmSelected){
/** ******************************************************************
* Private: Called by readAlarm method.
* Parameters:
* alarmSelected - 1 or 2 for alarm1 or alarm2
*
* AlarmTime structure:
* byte Second; // 0-59 = 6 bits 0=for alarm2
* byte Minute; // 0-59 = 6 bits
* byte Hour; // 0-23 = 5 bits
* byte AlarmMode; // 0=Daily, 1=Weekday, 2=Weekend, 3=Once
* byte ClockMode; // 0-2; 0=AM, 1=PM, 2=24hour
* bool Enabled; // true or false
*
* Returns:
* AlarmTime object
*
*TODO: error checking for readByte
****************************************************************** */
AlarmTime alarm_i;
byte _byteValue[4];
byte _bValue;
byte _address;
// Alarm1 uses 4 registers Starting at 0x07
// Alarm2 uses 3 registers Starting at 0x0b
// To keep them even, Alarm2 will start at register 0x0a
if (alarmSelected == 1){ _address = 0x07; } else { _address = 0x0a; }
readBytes(_address, _byteValue, 4);
// clear out Alarm2 zero byte
if (alarmSelected == 2){_byteValue[0] = 0;}
// Seconds
alarm_i.Second = bcd2bin(_byteValue[0] & 0B01111111);
// minutes byte
// alarm1 = register 0x08
// alarm2 = register 0x0b
// 7th bit A1M2/A2M2 = 0
alarm_i.Minute = bcd2bin(_byteValue[1] & 0B01111111);
// Hour byte
// alarm1 = register 0x09
// alarm2 = register 0x0c
// 7th Bit A1M3/A2M3 = 0
// 6th Bit of hour indicates 12/24 Hour mode
// 5th Bit of hour indicates 10hr or AM=0/PM=1
if(_byteValue[2] & _BV(6)) {
// 12hr is enabled
if (_byteValue[2] & _BV(5)) {
// PM found
alarm_i.ClockMode = 1;
} else {
// AM found
alarm_i.ClockMode = 0;
}
// mask out upper bits
alarm_i.Hour = bcd2bin(_byteValue[2] & 0B00011111);
} else {
// 24hr found
alarm_i.ClockMode = 2;
// mask out upper bits
alarm_i.Hour = bcd2bin(_byteValue[2] & 0B00111111);
}
// Day/Date byte
// alarm1 = register 0x0a
// alarm2 = register 0x0d
// 7th Bit A1M4/A2M4 = 0 for day mode, = 1 for daily or once
// - we can ignore it here, but used for alarm purposes
// 6th Bit DY/DT = 1 always
// 5th & 4th Bits store 10Date value (0-3)
// Four bits, bit 3-bit up to bit 0, store the next specific Day
// that the alarm will go off, unless daily or once.
// This is automatically handled by other routines, and can be
// ignored here. We don't care about the specfic DAY.
// Read AlarmMode
// Relocated to EEprom register 0x08
// alarm1 = bits 3&2
// alarm2 = bits 1&0
_bValue = readMem(0x08);
if (alarmSelected == 1){
_bValue &= 0B00000011;
} else {
_bValue = (_bValue >> 2);
_bValue &= 0B00000011;
}
alarm_i.AlarmMode = (_bValue);
// Get alarm enabled status from register 0x0e
readByte(0x0e, _bValue);
// mask out all but bit 0
if (alarmSelected == 1){
// alarm1
if(_bValue & _BV(0)) {
alarm_i.Enabled = true;
} else {
alarm_i.Enabled = false;
}
} else {
// alarm2
if(_bValue & _BV(1)) {
alarm_i.Enabled = true;
} else {
alarm_i.Enabled = false;
}
}
return alarm_i;
}
AlarmTime SimpleAlarmClock::readMemAlarm(byte alarmSelected){
/** ******************************************************************
* Private: Called by readAlarm method.
* Parameters:
* alarmSelected - 1 or 2 for alarm1 or alarm2
*
* AlarmTime structure:
* byte Second; // 0-59 = 6 bits 0=for alarm2
* byte Minute; // 0-59 = 6 bits
* byte Hour; // 0-23 = 5 bits
* byte AlarmMode; // 0=Daily, 1=Weekday, 2=Weekend, 3=Once
* byte ClockMode; // 0-2; 0=AM, 1=PM, 2=24hour
* bool Enabled; // true or false
*
* Returns:
* AlarmTime object
*
*TODO: error checking for readByte
****************************************************************** */
/* readAlarm1 will read from RTC if SnoozingA1 = false
else, it will read eeprom alarm1 address
*/
AlarmTime alarm_i;
byte _byteValue;
byte _address;
// readeepromAlarm
// Typically Seconds are ignored, However
// Read Second byte from MEM 0x00
// A1M1 = 0
if (alarmSelected == 1){
_byteValue = bcd2bin(readMem(0x00));
} else {
_byteValue = 0;
}
alarm_i.Second = _byteValue;
// Read Minute
// alarm1 from MEM 0x01
// alarm2 from MEM 0x04
// A1M2/A2M2 = 0
if (alarmSelected == 1){ _address = 0x01; } else { _address = 0x04; }
_byteValue = bcd2bin(readMem(_address));
alarm_i.Minute = _byteValue;
//Read Hour
// alarm1 from Mem 0x02
// alarm2 from Mem 0x05
// 7th Bit A1M3 = 0
// 6th Bit of hour indicates 12/24 Hour mode
// 5th Bit - 10hr bit (ie 20) or AM=0/PM=1
// 4th Bit - 10hr bit (ie 10)
// 3dr Bit - hour
// 2nd Bit - hour
// 1st Bit - hour
// 0 Bit - hour
if (alarmSelected == 1){ _address = 0x02;} else { _address = 0x05;}
_byteValue = readMem(_address);
if(_byteValue & _BV(6)) {
// 12hr is enabled
if (_byteValue & _BV(5)) {
//PM found
alarm_i.ClockMode = 1;
} else {
//AM found
alarm_i.ClockMode = 0;
}
alarm_i.Hour = bcd2bin(_byteValue & 0B00011111); //mask out upper bits
} else {
//24hr found
alarm_i.ClockMode = 2;
alarm_i.Hour = bcd2bin(_byteValue & 0B00111111); //mask out upper bits
}
//Read Day/Date byte
// alarm1 = register 0x03
// alarm2 = register 0x06
// 7th Bit A1M4 = 0 for day mode, = 1 for daily or once
// - we can ignore it here, but used for alarm purposes
// 6th Bit DY/DT = 1 always, assumed by Library
// 5th Bit 10date Value
// 4th Bit 10Date Value
// 3rd Bit - Day
// 2nd Bit - Day
// 1st Bit - Day
// 0 Bit - Day
// Ok - Day is managed by the Library so we can ignore it
// Read AlarmMode
// Relocated to EEprom register 0x08
// alarm1 = bits 3&2
// alarm2 = bits 1&0
_byteValue = readMem(0x08);
if (alarmSelected == 1){
_byteValue &= 0B00000011;
} else {
_byteValue = (_byteValue >> 2);
_byteValue &= 0B00000011;
}
alarm_i.AlarmMode = (_byteValue);
//Get alarm enabled status
_byteValue = readMem(0x7);
//Alarm1 is located in bit 0
if (alarmSelected == 1){
if(_byteValue & _BV(0)) {
alarm_i.Enabled = true;
} else {
alarm_i.Enabled = false;
}
} else {
if(_byteValue & _BV(1)) {
alarm_i.Enabled = true;
} else {
alarm_i.Enabled = false;
}
}
return alarm_i;
}
byte SimpleAlarmClock::nextAlarmDay(byte _AlarmMode, byte _ClockMode, byte _Hour, byte _Minute){
/** ******************************************************************
* Returns the next appropriate alarm day value
* Parameters:
* _AlarmMode
* _ClockMode
* _Hour
* _Minute
*
* Returns:
* 1=Sunday 2 3 4 5 6 7=Saturday
****************************************************************** */
int CurrentX;
int AlarmX;
byte dayReturn;
DateTime currentTime;
currentTime = read();
//These are not actual times, but are useful for comparison purposes
// example: current: AM,08,35 = 0*10000+8*100+35 = 835
// alarm: PM,08,30 = 1*10000+8*100+30 = 10835
//The alarm could occur that same day.
CurrentX = currentTime.ClockMode*10000+currentTime.Hour*100+currentTime.Minute;
AlarmX = _ClockMode*10000+_Hour*100+_Minute;
//Find current Day value
if (AlarmX > CurrentX){
//Could happen later same day
switch (_AlarmMode){
case 0:
//Daily
dayReturn = currentTime.Dow;
break;
case 1:
//WeekDay 2-6
//If it's a weekday now
if ((currentTime.Dow > 1)&&(currentTime.Dow < 7)){
dayReturn = currentTime.Dow;
} else {
//It's a weekend now
dayReturn = 2; //Monday
}
break;
case 2:
//WeekEnd
//If it's a weekend now
if ((currentTime.Dow == 1)||(currentTime.Dow == 7)){
dayReturn = currentTime.Dow;
} else {
//it's a weekday now
dayReturn = 7;
}
break;
case 3:
//Once
dayReturn = currentTime.Dow;
break;
default:
//Return nothing
break;
}
} else {
//It's too late today what's the next day?
switch (_AlarmMode){
case 0:
//Daily
dayReturn = currentTime.Dow + 1;
if (dayReturn == 8){dayReturn = 1;}
break;
case 1:
//WeekDay
dayReturn = currentTime.Dow + 1;
if (dayReturn > 6){ dayReturn = 2;}
break;
case 2:
//WeekEnd
if ((currentTime.Dow < 7)||(currentTime.Dow == 1)) {
dayReturn = 7;
} else {
dayReturn = 1;
}
break;
case 3:
//Once
dayReturn = currentTime.Dow + 1;
if (dayReturn == 8){dayReturn = 1;}
break;
default:
//Return nothing
break;
}
}
return dayReturn;
}
byte SimpleAlarmClock::getCtrlRegister(void){
/** ******************************************************************
* Returns the entire Control register 0x0e
* sets various local variables except alarm1&alarm2 enabled status
****************************************************************** */
byte _byteValue;
readByte(0x0e, _byteValue);
if(_byteValue & _BV(7)) {EOSC=1;}else{EOSC=0;}
if(_byteValue & _BV(6)) {BBSQW=1;}else{BBSQW=0;}
if(_byteValue & _BV(5)) {CONV=1;}else{CONV=0;}
if(_byteValue & _BV(4)) {RS2=1;}else{RS2=0;}
if(_byteValue & _BV(3)) {RS1=1;}else{RS1=0;}
if(_byteValue & _BV(2)) {INTCN=1;}else{INTCN=0;}
//last two bits are the alarms, which are ignored here
return _byteValue;
}
byte SimpleAlarmClock::getStatusRegister(void){
/** ******************************************************************
* Returns the entire Status register 0x0f
*
****************************************************************** */
byte _byteValue;
readByte(0x0f, _byteValue);
return _byteValue;
}
void SimpleAlarmClock::setEnableOscillator(bool Enable){
/** ******************************************************************
* Set or clear EOSC bit in register 0x0e
*
* EOSC = 0 - oscillator is started <default>
* = 1 - the oscillator is stopped when the DS3231 switches to VBAT
*
* When the DS3231 is powered by VCC, the oscillator is always
* on regardless of the status of the EOSC bit.
****************************************************************** */
byte _byteValue;
_byteValue = getCtrlRegister();
if (Enable == true){
_byteValue &= ~(1<<7);
EOSC = 0;
} else {
_byteValue |= (1<<7);
EOSC = 1;
}
writeByte(0x0e, _byteValue);
}
void SimpleAlarmClock::setBatteryBackedSquareWave(bool Enable){
/** ******************************************************************
* Sets or clears the BBSQW bit on register 0x0e
* BBSQW = 0 - the INT/SQW pin goes high impedance when VCC falls
* below the power-fail trip point. <default>
* = 1 - When set to logic 1 and the DS3231 is being
* powered by the VBAT pin, this bit enables the
* squarewave or interrupt output when VCC is absent.
****************************************************************** */
byte _byteValue;
_byteValue = getCtrlRegister();
if (Enable = true) {
_byteValue |= (1<<6); //set bit to one
BBSQW = 1;
} else {
_byteValue &= ~(1<<6); //set bit to zero
BBSQW = 0;
}
writeByte(0x0e, _byteValue);
}
void SimpleAlarmClock::setConvertTemperature(void){
/** ******************************************************************
* Set CONV bit in register 0x0e, have to wait until bsy bit is
* cleared first.
* CONV = 1 - forces the temperature sensor to convert the temperature
* into digital code and execute the TCXO algorithm
* to update the capacitance array to the oscillator. Once
* completed, it returns to 0.
* = 0 - <default> Can't be set
****************************************************************** */
byte _byteValue;
byte x;
_byteValue = getCtrlRegister();
//Set the CONV bit
_byteValue |= (1<<5);
//Read Register 0x0f to get the bsy bit
readByte(0x0f, x);
//check the bsy bit
while(x & _BV(2)) {
//bsy bit is set so we must wait
delay(10);
readByte(0x0f, x);
} //repeat until bsy is done
writeByte(0x0e, _byteValue);
}
byte SimpleAlarmClock::setRateSelect(byte Data){
/** ******************************************************************
* Rate Select control the frequency of the square-wave output when
* the square wave has been enabled. The following table
* shows the square-wave frequencies that can be selected
* with the RS bits. These bits are both set to logic 1
* (8.192kHz) when power is first applied.
* Table from Data sheet:
* --------------------------------------------
* | RS2 | RS1 | SQUARE-WAVE OUTPUT FREQUENCY |
* --------------------------------------------
* | 0 | 0 | 1 Hz |
* --------------------------------------------
* | 0 | 1 | 1.024kHz |
* --------------------------------------------
* | 1 | 0 | 4.096kHz |
* --------------------------------------------
* | 1 | 1 | 8.192kHz <default> |
* --------------------------------------------
*
* Returns:
* 4 = Error
* 0-3 = Data value
****************************************************************** */
byte _byteValue;
_byteValue = getCtrlRegister();
if (Data <= 3){
_byteValue |= (Data << 3);
writeByte(0x0e, _byteValue);
return(Data);
} else {
return(4);
}
}
void SimpleAlarmClock::setInterruptCtrl(bool Enable){
/** ******************************************************************
* Sets of clears the INTCN bit in register 0x0e
* INTCN = 0 - SQW pin output square wave.
* = 1 - SQW Stay HIGH, goes LOW only during alarm
* Typically, set to 1 when first powered up
****************************************************************** */
byte _byteValue;
_byteValue = getCtrlRegister();
if (Enable = true) {
_byteValue |= (1<<2); //set bit to one
} else {
_byteValue &= ~(1<<2); //set bit to zero
}
writeByte(0x0e, _byteValue);
}
byte SimpleAlarmClock::setAlarm(const AlarmTime &alarm_i, byte alarmSelected){
/** ******************************************************************
* sets to both RTC and MEM for either alarm1 or alarm2
* Returns:
* returnValue = 0 failed
* 1 Success
* 2 wrong alarm index
****************************************************************** */
byte returnValue = 0;
switch (alarmSelected){
case 1:
returnValue = setRtcAlarm(alarm_i, alarmSelected);
returnValue = setMemAlarm(alarm_i, alarmSelected);
break;
case 2:
returnValue = setRtcAlarm(alarm_i, alarmSelected);
returnValue = setMemAlarm(alarm_i, alarmSelected);
break;
default:
//do nothing
returnValue = 2;
break;
}
return returnValue;
}
byte SimpleAlarmClock::setRtcAlarm(const AlarmTime &alarm_i, byte alarmSelected){
/** ******************************************************************
* Private called by setAlarm
* Alarm settings - see Table 2 Alarm Mask Bits
* --------------------------------------------------------------------
* |DY/DT| A1M4| A1M3| A1M2| A1M1| Alarm1 Comments
* --------------------------------------------------------------------
* |DY/DT| A2M4| A2M3| A2M2| X | Alarm2 Comments (no seconds)
* --------------------------------------------------------------------
* | X | 1 | 1 | 1 | 1 | Alarm1 only once per second
* --------------------------------------------------------------------
* | X | 1 | 1 | 1 | 0 | Alarm1 second match
* | | 1 | 1 | 1 | X | Alarm2 only once per minute
* --------------------------------------------------------------------
* | X | 1 | 1 | 0 | 0 | Alarm minute (& second) match
* --------------------------------------------------------------------
* | X | 1 | 0 | 0 | 0 | Alarm hour, min, (& sec) match
* --------------------------------------------------------------------
* | 0 | 0 | 0 | 0 | 0 | Alarm date, hrs, min,(& sec) match
* --------------------------------------------------------------------
* | 1 | 0 | 0 | 0 | 0 | Alarm day, hrs, min,(& sec) match
* --------------------------------------------------------------------
****************************************************************** */
/* AlarmTime structure:
byte Second; // 0-59 = 6 bits 0=for alarm2
byte Minute; // 0-59 = 6 bits
byte Hour; // 0-23 = 5 bits
byte AlarmMode; // 0=Daily, 1=Weekday, 2=Weekend, 3=Once
byte ClockMode; // 0-2; 0=AM, 1=PM, 2=24hour
bool Enabled; // true or false
******************************************************************* */
byte _byteValue[3];
byte _address;
byte _Dow;
// Seconds
// alarm1 = Register 0x07 only
if (alarmSelected == 1) {
_byteValue[0] = bin2bcd(alarm_i.Second & 0B01111111);
writeByte(0x07, _byteValue[0]); //A1M1=0