-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathClock.cpp
117 lines (104 loc) · 2.83 KB
/
Clock.cpp
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
#include <Arduino.h>
#include <Wire.h>
// disable warnings in EEPROM.h
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
#include <EEPROM.h>
#pragma GCC diagnostic pop
#include "Clock.h"
#include "Config.h"
#include "Memory.h"
Clock::Clock()
{
}
void Clock::Init()
{
// Note: seems when an RTC is connected, A4 & A5 read HIGH, otherwise floating
// pull pin low if absent, could detect presence of RTC
if (RTC_I2C_ADDR)
Wire.begin();
}
byte Clock::BCD2Dec(byte BCD)
{
return (BCD/16*10) + (BCD & 0x0F);
}
byte Clock::Dec2BCD(byte Dec)
{
return (Dec/10*16) + (Dec % 10);
}
// 0:CLK_SECONDS
// 1:CLK_MINUTES
// 2:CLK_HOURS
// 3:CLK_DAY
// 4:CLK_DATE
// 5:CLK_MONTH
// 6:CLK_YEAR
// 7:CLK_CONTROL
byte Clock::ReadByte(byte Index)
{
// general "read"
if (Index <= Config::eClockControl)
{
// reading a clock register
if (RTC_I2C_ADDR)
return ReadRTCByte(Index);
else// no RTC, return 1/1/00, 12:00:00 day=1
if (Index == Config::eClockHours24)
return 0x12;
else
return (0b00111000 >> Index) & 0x01;
}
else // reading config/user byte from RTC's SRAM, or EEPROM
{
if (RTC_I2C_ADDR && RTC_USER_SRAM_OFFSET) // RTC's SRAM
return ReadRTCByte(Index - Config::eControlFlags + RTC_USER_SRAM_OFFSET);
else
{
int idx = memory.GetEEPROMTopIdx() + Index - Config::eControlFlags + 1;
return EEPROM.read(idx);
}
}
}
void Clock::WriteByte(byte Index, byte Value)
{
// general "write"
if (Index <= Config::eClockControl)
{
// writing a clock register
if (RTC_I2C_ADDR)
return WriteRTCByte(Index, Value);
// else no RTC, ignore
}
else // writing config/user byte to RTC's SRAM, or EEPROM
{
if (RTC_I2C_ADDR && RTC_USER_SRAM_OFFSET) // RTC's SRAM
return WriteRTCByte(Index - Config::eControlFlags + RTC_USER_SRAM_OFFSET, Value);
else
{
int idx = memory.GetEEPROMTopIdx() + Index - Config::eControlFlags + 1;
return EEPROM.write(idx, Value);
}
}
}
// clock specific
byte Clock::ReadRTCByte(byte Index)
{
if (Index == Config::eClockControl && RTC_CONTROL_OFFSET == 0x00)
return 0x00;
Index = (Index == Config::eClockControl)?RTC_CONTROL_OFFSET:Index; // adjust control register
Wire.beginTransmission(RTC_I2C_ADDR);
Wire.write(Index);
Wire.endTransmission();
Wire.requestFrom(RTC_I2C_ADDR, 1);
return Wire.read();
}
void Clock::WriteRTCByte(byte Index, byte Value)
{
if (Index == Config::eClockControl && RTC_CONTROL_OFFSET == 0x00)
return;
Index = (Index == Config::eClockControl)?RTC_CONTROL_OFFSET:Index; // adjust control register
Wire.beginTransmission(RTC_I2C_ADDR);
Wire.write(Index);
Wire.write(Value);
Wire.endTransmission();
}
Clock clock = Clock();