COSA
An Object-Oriented Platform for Arduino Programming
PCF8563.cpp
Go to the documentation of this file.
1 
21 #include "PCF8563.hh"
22 
23 int
24 PCF8563::read(void* regs, uint8_t size, uint8_t pos)
25 {
26  twi.acquire(this);
27  twi.write(pos);
28  int count = twi.read(regs, size);
29  twi.release();
30  return (count);
31 }
32 
33 int
34 PCF8563::write(void* regs, uint8_t size, uint8_t pos)
35 {
36  twi.acquire(this);
37  int count = twi.write(pos, regs, size);
38  twi.release();
39  return (count - 1);
40 }
41 
42 bool
44 {
45  uint8_t pos = offsetof(rtcc_t,clock);
46  if (read(&now, sizeof(now), pos) != sizeof(now)) return (false);
47  // Use only relevant bits
48  now.seconds &= 0x7f;
49  now.minutes &= 0x7f;
50  now.hours &= 0x3f;
51  now.day &= 0x3f;
52  now.date &= 0x07;
53  now.month &= 0x0f;
54  // Map to time struct
55  uint8_t temp = now.day;
56  now.day = now.date + 1;
57  now.date = temp;
58  return (true);
59 }
60 
61 bool
63 {
64  // Clear alarm setting
65  if (!clear_alarm()) return (false);
66  // Map to time struct to real-time clock format
67  uint8_t temp = now.day;
68  now.day = now.date;
69  now.date = temp - 1;
70  // Set real-time clock
71  uint8_t pos = offsetof(rtcc_t,clock);
72  int res = write(&now, sizeof(now), pos);
73  return (res == sizeof(now));
74 }
75 
76 bool
78 {
79  // Read alarm setting
80  uint8_t pos = offsetof(rtcc_t,alarm);
81  int res = read(&alarm, sizeof(alarm_t), pos);
82  if (res != sizeof(alarm_t)) return (false);
83  // Check if week day needs adjusting
84  if (alarm.day & alarm_t::DISABLE) return (true);
85  alarm.day += 1;
86  return (true);
87 }
88 
89 bool
91 {
92  // Clear alarm flag
94  uint8_t pos = offsetof(rtcc_t,cs2);
95  cs2.AIE = 1;
96  int res = write(&cs2, sizeof(cs2), pos);
97  if (res != sizeof(cs2)) return (false);
98  // Check for alarm day adjust
99  if ((alarm.day & alarm_t::DISABLE) == 0) alarm.day -= 1;
100  // Write alarm setting
101  pos = offsetof(rtcc_t,alarm);
102  res = write(&alarm, sizeof(alarm_t), pos);
103  return (res == sizeof(alarm_t));
104 }
105 
106 bool
108 {
109  // Read control register and check for alarm interrupt
110  control_status_2_t cs2;
111  uint8_t pos = offsetof(rtcc_t,cs2);
112  int res = read(&cs2, sizeof(cs2), pos);
113  if (res != sizeof(cs2)) return (false);
114  return (cs2.AF);
115 }
116 
117 bool
119 {
120  // Read control register and clear alarm flags
121  control_status_2_t cs2;
122  uint8_t pos = offsetof(rtcc_t,cs2);
123  int res = write(&cs2, sizeof(cs2), pos);
124  if (res != sizeof(cs2)) return (false);
125  // Clear alarm setting
126  pos = offsetof(rtcc_t,alarm);
127  alarm_t alarm;
128  res = write(&alarm, sizeof(alarm_t), pos);
129  return (res == sizeof(alarm));
130 }
131 
133 {
134  if (alarm.day & PCF8563::alarm_t::DISABLE)
135  cout << PSTR("XX");
136  else
137  cout << bcd << alarm.day;
138  cout << ' ';
139  if (alarm.date & PCF8563::alarm_t::DISABLE)
140  cout << PSTR("DD");
141  else
142  cout << bcd << alarm.date;
143  cout << ' ';
144  if (alarm.hours & PCF8563::alarm_t::DISABLE)
145  cout << PSTR("HH");
146  else
147  cout << bcd << alarm.hours;
148  cout << ':';
150  cout << PSTR("MM");
151  else
152  cout << bcd << alarm.minutes;
153  return (cout);
154 }
155 
TWI twi
Definition: TWI.cpp:27
IOStream & bcd(IOStream &outs)
Definition: IOStream.hh:745
uint8_t minutes
00-59 Minutes.
Definition: Time.hh:104
int read(void *regs, uint8_t size, uint8_t pos=0)
Definition: PCF8563.cpp:24
uint8_t day
01-07 Day.
Definition: Time.hh:106
int write(void *regs, uint8_t size, uint8_t pos=0)
Definition: PCF8563.cpp:34
uint8_t hours
00-23 Hours (BCD).
Definition: PCF8563.hh:65
#define PSTR(s)
Definition: Types.h:202
IOStream & operator<<(IOStream &cout, PCF8563::alarm_t &alarm)
Definition: PCF8563.cpp:132
uint8_t AF
Alarm flag.
Definition: PCF8563.hh:144
bool clear_alarm()
Definition: PCF8563.cpp:118
uint8_t month
01-12 Month.
Definition: Time.hh:108
bool set_alarm(alarm_t &alarm)
Definition: PCF8563.cpp:90
void release()
Definition: TWI.cpp:58
uint8_t AIE
Alarm interrupt enable.
Definition: PCF8563.hh:142
bool set_time(time_t &now)
Definition: PCF8563.cpp:62
static const uint8_t DISABLE
Definition: PCF8563.hh:70
uint8_t date
01-31 Date (BCD).
Definition: PCF8563.hh:66
bool get_alarm(alarm_t &alarm)
Definition: PCF8563.cpp:77
int read(void *buf, size_t size)
Definition: TWI.hh:326
bool pending_alarm()
Definition: PCF8563.cpp:107
uint8_t seconds
00-59 Seconds.
Definition: Time.hh:103
int write(void *buf, size_t size)
Definition: TWI.hh:282
Definition: Time.hh:102
void acquire(TWI::Driver *dev)
Definition: TWI.cpp:36
uint8_t hours
00-23 Hours.
Definition: Time.hh:105
bool get_time(time_t &now)
Definition: PCF8563.cpp:43
uint8_t minutes
00-59 Minutes (BCD).
Definition: PCF8563.hh:64
uint8_t date
01-31 Date.
Definition: Time.hh:107
uint8_t day
00-06 Day (BCD).
Definition: PCF8563.hh:67