-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathSystem.cpp
More file actions
833 lines (738 loc) · 20.2 KB
/
System.cpp
File metadata and controls
833 lines (738 loc) · 20.2 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
/**
**************************************************
* @file System.cpp
* @brief Functionality for panel hardware interaction
*
* https://github.com/e-radionicacom/Inkplate-Arduino-library
* For support, please reach over forums: forum.e-radionica.com/en
* For more info about the product, please check: www.inkplate.io
*
* This code is released under the GNU Lesser General Public
*License v3.0: https://www.gnu.org/licenses/lgpl-3.0.en.html Please review the
*LICENSE file included with this example. If you have any questions about
*licensing, please contact techsupport@e-radionica.com Distributed as-is; no
*warranty is given.
*
* @authors @ e-radionica.com
***************************************************/
#include "System.h"
#include "Wire.h"
SPIClass spi2(HSPI);
SdFat sd(&spi2);
/**
* @brief setPanelState sets panel state (on/off)
*
* @param uint8_t s
* panel state ON or OFF (1 or 0)
*/
void System::setPanelState(uint8_t s)
{
_panelOn = s;
}
/**
* @brief getPanelState gets panel state (ON/OFF)
*
* @return returns value that represents ON/OFF state of the panel
*/
uint8_t System::getPanelState()
{
return _panelOn;
}
#ifndef ARDUINO_INKPLATE2
/**
* @brief readTemperature reads panel temperature
*
* @return returns temperature in range from -10 to 85 degree C with
* accuracy of +-1 in range from 0 to 50
*/
int8_t System::readTemperature()
{
int8_t temp;
if (getPanelState() == 0)
{
WAKEUP_SET;
PWRUP_SET;
delay(5);
}
Wire.beginTransmission(0x48);
Wire.write(0x0D);
Wire.write(B10000000);
Wire.endTransmission();
delay(5);
Wire.beginTransmission(0x48);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(0x48, 1);
temp = Wire.read();
if (getPanelState() == 0)
{
PWRUP_CLEAR;
WAKEUP_CLEAR;
delay(5);
}
return temp;
}
/**
* @brief readTouchpad reads touchpad pin to check if triggered
*
* @param uint8_t _pad
* touchpad pin to check
*
* @return value to indicate if touchpad is triggered (TRUE or FALSE)
*/
uint8_t System::readTouchpad(uint8_t _pad)
{
return digitalReadInternal(MCP23017_INT_ADDR, mcpRegsInt, _pad);
}
/**
* @brief readBattery reads voltage of the battery
*
* @return returns battery voltage value
*/
double System::readBattery()
{
pinModeInternal(MCP23017_INT_ADDR, mcpRegsInt, 9, INPUT);
int state = digitalReadInternal(MCP23017_INT_ADDR, mcpRegsInt, 9);
pinModeInternal(MCP23017_INT_ADDR, mcpRegsInt, 9, OUTPUT);
if (state)
{
digitalWriteInternal(MCP23017_INT_ADDR, mcpRegsInt, 9, LOW);
}
else
{
digitalWriteInternal(MCP23017_INT_ADDR, mcpRegsInt, 9, HIGH);
}
/*
#ifdef ARDUINO_ESP32_DEV
digitalWriteInternal(MCP23017_INT_ADDR, mcpRegsInt, 9, LOW);
#else
digitalWriteInternal(MCP23017_INT_ADDR, mcpRegsInt, 9, HIGH);
#endif
*/
delay(1);
int adc = analogRead(35);
if (state)
{
pinModeInternal(MCP23017_INT_ADDR, mcpRegsInt, 9, INPUT);
}
else
{
digitalWriteInternal(MCP23017_INT_ADDR, mcpRegsInt, 9, LOW);
}
/*
#ifdef ARDUINO_ESP32_DEV
digitalWriteInternal(MCP23017_INT_ADDR, mcpRegsInt, 9, HIGH);
#else
digitalWriteInternal(MCP23017_INT_ADDR, mcpRegsInt, 9, LOW);
#endif
*/
// Calculate the voltage using the following formula
// 1.1V is internal ADC reference of ESP32, 3.548133892 is 11dB in linear
// scale (Analog signal is attenuated by 11dB before ESP32 ADC input)
return (double(adc) / 4095 * 1.1 * 3.548133892 * 2);
}
/**
* @brief sdCardInit initializes sd card trough SPI
*
* @return 0 if failed to initialise, 1 if successful
*/
int16_t System::sdCardInit()
{
spi2.begin(14, 12, 13, 15);
setSdCardOk(sd.begin(15, SD_SCK_MHZ(25)));
return getSdCardOk();
}
/**
* @brief getSdFat gets sd card object
*
* @return sd card class object
*/
SdFat System::getSdFat()
{
return sd;
}
/**
* @brief getSPIptr gets SPI class object pointer
*
* @return SPI class object
*/
SPIClass *System::getSPIptr()
{
return &spi2;
}
/**
* @brief setSdCardOk sets sd card OK status
*
* @param int16_t s
* sd card OK status, can be 1 or 0
*/
void System::setSdCardOk(int16_t s)
{
_sdCardOk = s;
}
/**
* @brief setSdCardOk gets sd card OK status
*
* @return sd card OK status, can be 1 or 0
*/
int16_t System::getSdCardOk()
{
return _sdCardOk;
}
/**
* @brief Method to set time
*
* @param uint8_t rtcHour Set the rtcHour
* @param uint8_t rtcMinute Set the minutes
* @param uint8_t rtcSecond Set the seconds
*/
void System::rtcSetTime(uint8_t rtcHour, uint8_t rtcMinute, uint8_t rtcSecond)
{
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_RAM_by);
Wire.write(170); // Write in RAM 170 to know that RTC is set
Wire.write(rtcDecToBcd(rtcSecond));
Wire.write(rtcDecToBcd(rtcMinute));
Wire.write(rtcDecToBcd(rtcHour));
Wire.endTransmission();
}
/**
* @brief Method to set the date
*
* @param uint8_t rtcWeekday Set the rtcWeekday
* @param uint8_t rtcDay Set the rtcDay
* @param uint8_t rtcMonth Set the rtcMonth
* @param uint8_t yr Set the rtcYear
*/
void System::rtcSetDate(uint8_t rtcWeekday, uint8_t rtcDay, uint8_t rtcMonth, uint16_t yr)
{
rtcYear = yr - 2000; // convert to RTC rtcYear format 0-99
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_RAM_by);
Wire.write(170); // Write in RAM 170 to know that RTC is set
Wire.endTransmission();
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_DAY_ADDR);
Wire.write(rtcDecToBcd(rtcDay));
Wire.write(rtcDecToBcd(rtcWeekday));
Wire.write(rtcDecToBcd(rtcMonth));
Wire.write(rtcDecToBcd(rtcYear));
Wire.endTransmission();
}
/**
* @brief Method to set time and date using epoch
*
* @param uint32_t _epoch Set RTC epoch
*/
void System::rtcSetEpoch(uint32_t _epoch)
{
struct tm _t;
time_t _e = _epoch;
memcpy(&_t, localtime((const time_t *)&_e), sizeof(_t));
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_RAM_by);
Wire.write(170);
Wire.write(rtcDecToBcd(_t.tm_sec));
Wire.write(rtcDecToBcd(_t.tm_min));
Wire.write(rtcDecToBcd(_t.tm_hour));
Wire.write(rtcDecToBcd(_t.tm_mday));
Wire.write(rtcDecToBcd(_t.tm_wday));
Wire.write(rtcDecToBcd(_t.tm_mon + 1));
Wire.write(rtcDecToBcd(_t.tm_year + 1900 - 2000));
Wire.endTransmission();
}
/**
* @brief Method to get time and date using epoch
*
* @returns uint32_t Returns the current epoch
*/
uint32_t System::rtcGetEpoch()
{
struct tm _t;
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_SECOND_ADDR);
Wire.endTransmission();
Wire.requestFrom(I2C_ADDR, 7); // ignore bit 7
_t.tm_sec = rtcBcdToDec(Wire.read() & 0x7F);
_t.tm_min = rtcBcdToDec(Wire.read() & 0x7F);
_t.tm_hour = rtcBcdToDec(Wire.read() & 0x3F);
_t.tm_mday = rtcBcdToDec(Wire.read() & 0x3F);
_t.tm_wday = rtcBcdToDec(Wire.read() & 0x07);
_t.tm_mon = rtcBcdToDec(Wire.read() & 0x1F) - 1;
_t.tm_year = rtcBcdToDec(Wire.read()) + 2000 - 1900;
_t.tm_isdst = -1; // we don't know dst state, so attempt to guess
Wire.endTransmission();
return (uint32_t)(mktime(&_t));
}
/**
* @brief Reads time and date from the RTC
*/
void System::rtcGetRtcData()
{
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_SECOND_ADDR); // datasheet 8.4.
Wire.endTransmission();
Wire.requestFrom(I2C_ADDR, 7);
while (Wire.available())
{
rtcSecond = rtcBcdToDec(Wire.read() & 0x7F); // ignore bit 7
rtcMinute = rtcBcdToDec(Wire.read() & 0x7F);
rtcHour = rtcBcdToDec(Wire.read() & 0x3F); // ignore bits 7 & 6
rtcDay = rtcBcdToDec(Wire.read() & 0x3F);
rtcWeekday = rtcBcdToDec(Wire.read() & 0x07); // ignore bits 7,6,5,4 & 3
rtcMonth = rtcBcdToDec(Wire.read() & 0x1F); // ignore bits 7,6 & 5
rtcYear = rtcBcdToDec(Wire.read()) + 2000;
}
}
/**
* @brief Small user method
*
* @returns uint8_t Returns the current seconds
*/
uint8_t System::rtcGetSecond()
{
return rtcSecond;
}
/**
* @brief Small user method
*
* @returns uint8_t Returns the current minutes
*/
uint8_t System::rtcGetMinute()
{
return rtcMinute;
}
/**
* @brief Small user method
*
* @returns uint8_t Returns the current hours
*/
uint8_t System::rtcGetHour()
{
return rtcHour;
}
/**
* @brief Small user method
*
* @returns uint8_t Returns the current rtcDay
*/
uint8_t System::rtcGetDay()
{
return rtcDay;
}
/**
* @brief Small user method
*
* @returns uint8_t Returns the current rtcWeekday
*/
uint8_t System::rtcGetWeekday()
{
return rtcWeekday;
}
/**
* @brief Small user method
*
* @returns uint8_t Returns the current rtcMonth
*/
uint8_t System::rtcGetMonth()
{
return rtcMonth;
}
/**
* @brief Small user method
*
* @returns uint8_t Returns the current rtcYear
*/
uint16_t System::rtcGetYear()
{
return rtcYear;
}
/**
* @brief Enables the alarm of the RTC
*/
void System::rtcEnableAlarm() // datasheet 8.5.6.
{
// check Table 2. Control_2
rtcControl2 = RTC_CTRL_2_DEFAULT | RTC_ALARM_AIE; // enable interrupt
rtcControl2 &= ~RTC_ALARM_AF; // clear alarm flag
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_CTRL_2);
Wire.write(rtcControl2);
Wire.endTransmission();
}
/**
* @brief Sets the alarm to all the params
*
* @param uint8_t rtcAlarmSecond Set the alarm seconds
* @param uint8_t rtcAlarmMinute Set the alarm minutes
* @param uint8_t rtcAlarmHour Set the alarm hours
* @param uint8_t rtcAlarmDay Set the alarm rtcDay
* @param uint8_t rtcAlarmWeekday Set the alarm rtcWeekday
*/
void System::rtcSetAlarm(uint8_t rtcAlarmSecond, uint8_t rtcAlarmMinute, uint8_t rtcAlarmHour, uint8_t rtcAlarmDay,
uint8_t rtcAlarmWeekday)
{
if (rtcAlarmSecond < 99)
{ // rtcSecond
rtcAlarmSecond = constrain(rtcAlarmSecond, 0, 59);
rtcAlarmSecond = rtcDecToBcd(rtcAlarmSecond);
rtcAlarmSecond &= ~RTC_ALARM;
}
else
{
rtcAlarmSecond = 0x0;
rtcAlarmSecond |= RTC_ALARM;
}
if (rtcAlarmMinute < 99)
{ // rtcMinute
rtcAlarmMinute = constrain(rtcAlarmMinute, 0, 59);
rtcAlarmMinute = rtcDecToBcd(rtcAlarmMinute);
rtcAlarmMinute &= ~RTC_ALARM;
}
else
{
rtcAlarmMinute = 0x0;
rtcAlarmMinute |= RTC_ALARM;
}
if (rtcAlarmHour < 99)
{ // rtcHour
rtcAlarmHour = constrain(rtcAlarmHour, 0, 23);
rtcAlarmHour = rtcDecToBcd(rtcAlarmHour);
rtcAlarmHour &= ~RTC_ALARM;
}
else
{
rtcAlarmHour = 0x0;
rtcAlarmHour |= RTC_ALARM;
}
if (rtcAlarmDay < 99)
{ // rtcDay
rtcAlarmDay = constrain(rtcAlarmDay, 1, 31);
rtcAlarmDay = rtcDecToBcd(rtcAlarmDay);
rtcAlarmDay &= ~RTC_ALARM;
}
else
{
rtcAlarmDay = 0x0;
rtcAlarmDay |= RTC_ALARM;
}
if (rtcAlarmWeekday < 99)
{ // rtcWeekday
rtcAlarmWeekday = constrain(rtcAlarmWeekday, 0, 6);
rtcAlarmWeekday = rtcDecToBcd(rtcAlarmWeekday);
rtcAlarmWeekday &= ~RTC_ALARM;
}
else
{
rtcAlarmWeekday = 0x0;
rtcAlarmWeekday |= RTC_ALARM;
}
rtcEnableAlarm();
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_SECOND_ALARM);
Wire.write(rtcAlarmSecond);
Wire.write(rtcAlarmMinute);
Wire.write(rtcAlarmHour);
Wire.write(rtcAlarmDay);
Wire.write(rtcAlarmWeekday);
Wire.endTransmission();
}
/**
* @brief Set alarm using epoch
*
* @param uint32_t _epoch RTC Epoch alarm
* @param uint8_t _match RTC Match
*/
void System::rtcSetAlarmEpoch(uint32_t _epoch, uint8_t _match)
{
struct tm _t;
time_t _e = _epoch;
memcpy(&_t, localtime((const time_t *)&_e), sizeof(_t));
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_SECOND_ALARM);
Wire.write(rtcDecToBcd(_t.tm_sec) & (~((_match & 1) << 7)));
Wire.write(rtcDecToBcd(_t.tm_min) & (~(((_match >> 1) & 1) << 7)));
Wire.write(rtcDecToBcd(_t.tm_hour) & (~(((_match >> 2) & 1) << 7)));
Wire.write(rtcDecToBcd(_t.tm_mday) & (~(((_match >> 3) & 1) << 7)));
Wire.write(rtcDecToBcd(_t.tm_wday) & (~(((_match >> 4) & 1) << 7)));
Wire.endTransmission();
rtcEnableAlarm();
}
/**
* @brief Reads the alarm of the RTC
*/
void System::rtcReadAlarm()
{
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_SECOND_ALARM); // datasheet 8.4.
Wire.endTransmission();
Wire.requestFrom(I2C_ADDR, 5);
while (Wire.available())
{
rtcAlarmSecond = Wire.read(); // read RTC_SECOND_ALARM register
if (RTC_ALARM & rtcAlarmSecond) // check is AEN = 1 (rtcSecond alarm disabled)
{
rtcAlarmSecond = 99; // using 99 as code for no alarm
}
else
{ // else if AEN = 0 (rtcSecond alarm enabled)
rtcAlarmSecond = rtcBcdToDec(rtcAlarmSecond & ~RTC_ALARM); // remove AEN flag and convert to dec number
}
rtcAlarmMinute = Wire.read(); // rtcMinute
if (RTC_ALARM & rtcAlarmMinute)
{
rtcAlarmMinute = 99;
}
else
{
rtcAlarmMinute = rtcBcdToDec(rtcAlarmMinute & ~RTC_ALARM);
}
rtcAlarmHour = Wire.read(); // rtcHour
if (RTC_ALARM & rtcAlarmHour)
{
rtcAlarmHour = 99;
}
else
{
rtcAlarmHour = rtcBcdToDec(rtcAlarmHour & 0x3F); // remove bits 7 & 6
}
rtcAlarmDay = Wire.read(); // rtcDay
if (RTC_ALARM & rtcAlarmDay)
{
rtcAlarmDay = 99;
}
else
{
rtcAlarmDay = rtcBcdToDec(rtcAlarmDay & 0x3F); // remove bits 7 & 6
}
rtcAlarmWeekday = Wire.read(); // rtcWeekday
if (RTC_ALARM & rtcAlarmWeekday)
{
rtcAlarmWeekday = 99;
}
else
{
rtcAlarmWeekday = rtcBcdToDec(rtcAlarmWeekday & 0x07); // remove bits 7,6,5,4 & 3
}
}
}
/**
* @brief Small user method
*
* @returns uint8_t Returns the current alarm seconds
*/
uint8_t System::rtcGetAlarmSecond()
{
rtcReadAlarm();
return rtcAlarmSecond;
}
/**
* @brief Small user method
*
* @returns uint8_t Returns the current alarm minutes
*/
uint8_t System::rtcGetAlarmMinute()
{
rtcReadAlarm();
return rtcAlarmMinute;
}
/**
* @brief Small user method
*
* @returns uint8_t Returns the current alarm hours
*/
uint8_t System::rtcGetAlarmHour()
{
rtcReadAlarm();
return rtcAlarmHour;
}
/**
* @brief Small user method
*
* @returns uint8_t Returns the current alarm rtcDay
*/
uint8_t System::rtcGetAlarmDay()
{
rtcReadAlarm();
return rtcAlarmDay;
}
/**
* @brief Small user method
*
* @returns uint8_t Returns the current alarm rtcWeekday
*/
uint8_t System::rtcGetAlarmWeekday()
{
rtcReadAlarm();
return rtcAlarmWeekday;
}
/**
* @brief Sets the timer countdown
*
* @param rtcCountdownSrcClock source_clock
* timer clock frequency
*
* @param timer clock frequency
* value to write in timer register
*
* @param bool int_enable
* timer interrupt enable, 0 means no interrupt generated from timer
* , 1 means interrupt is generated from timer
*
* @param bool int_pulse
* timer interrupt mode, 0 means interrupt follows timer flag
* , 1 means interrupt generates a pulse
*/
void System::rtcTimerSet(rtcCountdownSrcClock source_clock, uint8_t value, bool int_enable, bool int_pulse)
{
uint8_t timer_reg[2] = {0};
// disable the countdown timer
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_TIMER_MODE);
Wire.write(0x18); // default
Wire.endTransmission();
// clear Control_2
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_CTRL_2);
Wire.write(0x00); // default
Wire.endTransmission();
// reconfigure timer
timer_reg[1] |= RTC_TIMER_TE; // enable timer
if (int_enable)
timer_reg[1] |= RTC_TIMER_TIE; // enable interrupt
if (int_pulse)
timer_reg[1] |= RTC_TIMER_TI_TP; // interrupt mode
timer_reg[1] |= source_clock << 3; // clock source
// timer_reg[1] = 0b00011111;
timer_reg[0] = value;
// write timer value
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_TIMER_VAL);
Wire.write(timer_reg[0]);
Wire.write(timer_reg[1]);
Wire.endTransmission();
}
/**
* @brief Returns is the timer flag on
*
* @returns bool Returns true if the timer flag is on
*/
bool System::rtcCheckTimerFlag()
{
uint8_t _crtl_2 = RTC_TIMER_FLAG;
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_CTRL_2);
Wire.endTransmission();
Wire.requestFrom(I2C_ADDR, 1);
_crtl_2 &= Wire.read();
return _crtl_2;
}
/**
* @brief Returns is the alarm flag on
*
* @returns bool Returns true if the alarm flag is on
*/
bool System::rtcCheckAlarmFlag()
{
uint8_t _crtl_2 = RTC_ALARM_AF;
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_CTRL_2);
Wire.endTransmission();
Wire.requestFrom(I2C_ADDR, 1);
_crtl_2 &= Wire.read();
return _crtl_2;
}
/**
* @brief Clears alarm flag
*/
void System::rtcClearAlarmFlag()
{
uint8_t _crtl_2;
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_CTRL_2);
Wire.endTransmission();
Wire.requestFrom(I2C_ADDR, 1);
_crtl_2 = Wire.read() & ~(RTC_ALARM_AF);
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_CTRL_2);
Wire.write(_crtl_2);
Wire.endTransmission();
}
/**
* @brief Clears timer flag
*/
void System::rtcClearTimerFlag()
{
uint8_t _crtl_2;
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_CTRL_2);
Wire.endTransmission();
Wire.requestFrom(I2C_ADDR, 1);
_crtl_2 = Wire.read() & ~(RTC_TIMER_FLAG);
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_CTRL_2);
Wire.write(_crtl_2);
Wire.endTransmission();
}
/**
* @brief Disables the timer
*/
void System::rtcDisableTimer()
{
uint8_t _timerMode;
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_TIMER_MODE);
Wire.endTransmission();
Wire.requestFrom(I2C_ADDR, 1);
_timerMode = Wire.read() & ~(RTC_TIMER_TE);
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_TIMER_MODE);
Wire.write(_timerMode);
Wire.endTransmission();
}
/**
* @brief Check if the RTC is already set
*
* @returns bool Returns true if RTC is set, false if it's not
*/
bool System::rtcIsSet()
{
uint8_t _ramByte;
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_RAM_by);
Wire.endTransmission();
Wire.requestFrom(I2C_ADDR, 1);
_ramByte = Wire.read();
return ((_ramByte == 170) ? true : false);
}
/**
* @brief Resets the timer
*/
void System::rtcReset() // datasheet 8.2.1.3.
{
Wire.beginTransmission(I2C_ADDR);
Wire.write(RTC_CTRL_1);
Wire.write(0x58);
Wire.endTransmission();
}
/**
* @brief Converts decimal to BCD
*
* @param uint8_t val
* number which needs to be converted from decimal to Bcd value
*/
uint8_t System::rtcDecToBcd(uint8_t val)
{
return ((val / 10 * 16) + (val % 10));
}
/**
* @brief Converts BCD to decimal
*
* @param uint8_t val
* number which needs to be converted from Bcd to decimal value
*/
uint8_t System::rtcBcdToDec(uint8_t val)
{
return ((val / 16 * 10) + (val % 16));
}
#endif