Skip to content

Commit 39d5074

Browse files
author
simon
committed
Rephrased TimerState as class instead of struct
1 parent 37b9a18 commit 39d5074

File tree

4 files changed

+115
-74
lines changed

4 files changed

+115
-74
lines changed

RTClib.cpp

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,46 @@ void RTC_PCF8523::calibrate(Pcf8523OffsetMode mode, int8_t offset) {
12731273

12741274
extern const Pcf8523TimerDetails timer_details_table[];
12751275

1276+
/**************************************************************************/
1277+
/*!
1278+
@brief Constructor for timer, from member timer and interrupt fields
1279+
1280+
@param timer_value The starting countdown value of the timer, from 0-255
1281+
@param timer_freq The countdown clock frequency of the timer
1282+
@param timer_enabled Whether the timer is enabled
1283+
@param irupt_flag Whether the timer has completed since this flag was last
1284+
cleared
1285+
@param irupt_signal_enabled Whether the timer's interrupt drives its
1286+
@associated pin(s)
1287+
1288+
@note The irupt_flag can only be cleared or preserved, when set by a timer
1289+
going off; it cannot be set with a write, if unset.
1290+
*/
1291+
/**************************************************************************/
1292+
Pcf8523TimerState::Pcf8523TimerState(uint8_t timer_value, PCF8523TimerClockFreq timer_freq, bool timer_enabled, bool irupt_flag, bool irupt_signal_enabled) {
1293+
value = timer_value;
1294+
freq = timer_freq;
1295+
enabled = timer_enabled;
1296+
1297+
this->irupt_flag = irupt_flag;
1298+
irupt_enabled = irupt_signal_enabled;
1299+
}
1300+
1301+
/**************************************************************************/
1302+
/*!
1303+
@brief Copy constructor.
1304+
@param copy Pcf8523TimerState to copy.
1305+
*/
1306+
/**************************************************************************/
1307+
Pcf8523TimerState::Pcf8523TimerState(const Pcf8523TimerState &copy) {
1308+
value = copy.value;
1309+
freq = copy.freq;
1310+
enabled = copy.enabled;
1311+
1312+
irupt_flag = copy.irupt_flag;
1313+
irupt_enabled = copy.irupt_enabled;
1314+
}
1315+
12761316
/**************************************************************************/
12771317
/*!
12781318
@@ -1324,16 +1364,16 @@ void RTC_PCF8523::write_irupt(Pcf8523Timer irupt, Pcf8523IruptState *src) {
13241364
/**************************************************************************/
13251365
/*!
13261366
1327-
@brief Programs the selected timer from the given TimerState struct.
1367+
@brief Programs the selected timer from the given TimerState object.
13281368
@param timer Which timer to program.
1329-
@param src The TimerState struct that programs the timer.
1369+
@param src The TimerState object that programs the timer.
13301370
13311371
@note If the interrupt enable field is set, the square wave/CLKOUT
13321372
function will be disabled, and the SqwPinMode set to "OFF."
13331373
Instead, the INT function will be used.
13341374
*/
13351375
/**************************************************************************/
1336-
void RTC_PCF8523::write_timer(Pcf8523Timer timer, Pcf8523TimerState *src) {
1376+
void RTC_PCF8523::write_timer(Pcf8523Timer timer, const Pcf8523TimerState &src) {
13371377
/* setup the given timer in TimerState src */
13381378

13391379
const Pcf8523TimerDetails *details = &(timer_details_table[timer]);
@@ -1344,19 +1384,23 @@ void RTC_PCF8523::write_timer(Pcf8523Timer timer, Pcf8523TimerState *src) {
13441384
write_PCF8523_register(details->timer_en_register, clkout_ctrl);
13451385

13461386
// set the timer value and frequencies
1347-
write_PCF8523_register(details->timer_value_register, src->value);
1348-
write_PCF8523_register(details->timer_freq_register, (uint8_t) src->freq);
1387+
write_PCF8523_register(details->timer_value_register, src.value);
1388+
write_PCF8523_register(details->timer_freq_register, (uint8_t) src.freq);
13491389

13501390
// set the irupt flag/enable bits -- delegate to other function
1351-
write_irupt(timer, &(src->irupt_state));
1391+
Pcf8523IruptState irupt;
1392+
irupt.irupt_flag = src.irupt_flag;
1393+
irupt.irupt_enabled = src.irupt_enabled;
1394+
1395+
write_irupt(timer, &irupt);
13521396

13531397
// enable the timer, if set
1354-
if (src->enabled) {
1398+
if (src.enabled) {
13551399
clkout_ctrl = read_PCF8523_register(details->timer_en_register);
13561400

13571401
// turn off the CLKOUT function if the interrupt is enabled,
13581402
// enable the timer
1359-
if (src->irupt_state.irupt_enabled) {
1403+
if (src.irupt_enabled) {
13601404
clkout_ctrl |= PCF8523_CLKOUT_DIS;
13611405
}
13621406
clkout_ctrl |= details->timer_en_mask;
@@ -1368,28 +1412,37 @@ void RTC_PCF8523::write_timer(Pcf8523Timer timer, Pcf8523TimerState *src) {
13681412
/**************************************************************************/
13691413
/*!
13701414
1371-
@brief Reads the state of selected timer into the given TimerState struct.
1415+
@brief Reads the state of selected timer into the returned TimerState object.
13721416
@param timer Which timer to read.
1373-
@param dest The TimerState struct where the current timer state is written.
1417+
@return TimerState The timer and interrupt current state
13741418
*/
13751419
/**************************************************************************/
1376-
void RTC_PCF8523::read_timer(Pcf8523Timer timer, Pcf8523TimerState *dest) {
1420+
Pcf8523TimerState RTC_PCF8523::read_timer(Pcf8523Timer timer) {
13771421
/* return the timer contents into TimerState */
13781422

13791423
const Pcf8523TimerDetails *details = &(timer_details_table[timer]);
13801424

13811425
// retrieve the timer enabled bit
13821426
uint8_t clkout_ctrl = read_PCF8523_register(details->timer_en_register);
1383-
dest->enabled = clkout_ctrl & details->timer_en_mask;
1427+
const bool enabled = clkout_ctrl & details->timer_en_mask;
13841428

13851429
// retrieve the frequency scalar from the timer's register
1386-
dest->freq = (PCF8523TimerClockFreq) read_PCF8523_register(details->timer_freq_register);
1430+
const PCF8523TimerClockFreq freq = (PCF8523TimerClockFreq) read_PCF8523_register(details->timer_freq_register);
13871431

13881432
// retrieve the timer value from the timer's register
1389-
dest->value = read_PCF8523_register(details->timer_value_register);
1433+
const uint8_t value = read_PCF8523_register(details->timer_value_register);
13901434

13911435
// read the irupt flag/enable bits -- delegate to other function
1392-
read_irupt(timer, &(dest->irupt_state));
1436+
Pcf8523IruptState irupt;
1437+
read_irupt(timer, &irupt);
1438+
1439+
return Pcf8523TimerState(
1440+
value,
1441+
freq,
1442+
enabled,
1443+
irupt.irupt_flag,
1444+
irupt.irupt_enabled
1445+
);
13931446
}
13941447

13951448

RTClib.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,30 @@ typedef struct {
367367
bool irupt_enabled; ///< whether the flag state is tied to the interrupt pin state
368368
} Pcf8523IruptState;
369369

370-
/** Current state of a timer */
371-
typedef struct {
370+
/**************************************************************************/
371+
/*!
372+
@brief Current state of a timer
373+
374+
This class can be read from or written into the RTC. An update can be done
375+
by reading, modifying, and then writing.
376+
377+
This class stores basic information in its public members about the timer
378+
and its interrupt status: its countdown value and frequency, whether it is
379+
running (enabled); its interrupt flag, and whether the interrupt is set to
380+
drive associated pins.
381+
*/
382+
/**************************************************************************/
383+
class Pcf8523TimerState {
384+
public:
385+
Pcf8523TimerState(uint8_t timer_value, PCF8523TimerClockFreq timer_freq, bool timer_enabled, bool irupt_flag, bool irupt_signal_enabled);
386+
Pcf8523TimerState(const Pcf8523TimerState &copy);
387+
372388
bool enabled; ///< whether the timer is running
373389
uint8_t value; ///< the current value of the timer
374390
PCF8523TimerClockFreq freq; ///< the clock divider used
375-
Pcf8523IruptState irupt_state; ///< the timer's interrupt state
376-
} Pcf8523TimerState;
391+
bool irupt_flag; ///< the interrupt flag
392+
bool irupt_enabled; ///< the interrupt signal enable
393+
};
377394

378395
/* registers and masks for interacting with a timer */
379396
typedef struct {
@@ -417,8 +434,9 @@ class RTC_PCF8523 {
417434
void deconfigureAllTimers(void);
418435
void calibrate(Pcf8523OffsetMode mode, int8_t offset);
419436

420-
void write_timer(Pcf8523Timer timer, Pcf8523TimerState *src);
421-
void read_timer(Pcf8523Timer timer, Pcf8523TimerState *dest);
437+
void write_timer(Pcf8523Timer timer, const Pcf8523TimerState &src);
438+
Pcf8523TimerState read_timer(Pcf8523Timer timer);
439+
422440
void write_irupt(Pcf8523Timer irupt, Pcf8523IruptState *src);
423441
void read_irupt(Pcf8523Timer irupt, Pcf8523IruptState *dest);
424442
};

examples/pcf8523_monitor_timer/pcf8523_monitor_timer.ino

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define MONITOR_PIN 5
1212

1313
RTC_PCF8523 rtc;
14-
Pcf8523TimerState state;
1514

1615
void setup () {
1716
Serial.begin(9600);
@@ -22,33 +21,19 @@ void setup () {
2221

2322
pinMode(MONITOR_PIN, INPUT_PULLUP);
2423

25-
/*
26-
struct type signatures:
24+
Pcf8523TimerState state = Pcf8523TimerState(
25+
10, // timer value
26+
PCF8523_FrequencySecond, // timer frequency
27+
true, // timer set to run
28+
false, // timer interrupt flag (when timer has gone off)
29+
true // timer flag -> signal enable
30+
);
2731

28-
typedef struct {
29-
bool irupt_flag; // whether the timer has gone off
30-
bool irupt_enabled; // whether the flag state is tied to the interrupt pin state
31-
} Pcf8523IruptState;
32-
33-
typedef struct {
34-
bool enabled; // whether the timer is running
35-
uint8_t value; // the current value of the timer
36-
Pcf8523FrequencyDivision freq; // the clock divider used
37-
Pcf8523IruptState irupt_state; // the timer's interrupt state
38-
} Pcf8523TimerState;
39-
*/
40-
41-
state.enabled = true;
42-
state.value = 10;
43-
state.freq = PCF8523_FrequencySecond;
44-
state.irupt_state.irupt_flag = false;
45-
state.irupt_state.irupt_enabled = true;
46-
47-
rtc.write_timer(PCF8523_Timer_Countdown_B, &state);
32+
rtc.write_timer(PCF8523_Timer_Countdown_B, state);
4833
}
4934

5035
void loop () {
51-
rtc.read_timer(PCF8523_Timer_Countdown_B, &state);
36+
Pcf8523TimerState state = rtc.read_timer(PCF8523_Timer_Countdown_B);
5237
Serial.print("timer value: ");
5338
Serial.print(state.value, DEC);
5439
Serial.print(", enabled: ");
@@ -57,9 +42,9 @@ void loop () {
5742
Serial.print(state.freq, DEC);
5843
Serial.println();
5944
Serial.print("irupt flag: ");
60-
Serial.print(state.irupt_state.irupt_flag, DEC);
45+
Serial.print(state.irupt_flag, DEC);
6146
Serial.print(", enabled: ");
62-
Serial.print(state.irupt_state.irupt_enabled, DEC);
47+
Serial.print(state.irupt_enabled, DEC);
6348
Serial.println();
6449

6550
Serial.print("Interrupt pin: ");

examples/pcf8523_set_timer/pcf8523_set_timer.ino

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "RTClib.h"
77

88
RTC_PCF8523 rtc;
9-
Pcf8523TimerState state;
109

1110
void setup () {
1211
Serial.begin(9600);
@@ -15,33 +14,19 @@ void setup () {
1514
while (1);
1615
}
1716

18-
/*
19-
struct type signatures:
20-
21-
typedef struct {
22-
bool irupt_flag; // whether the timer has gone off
23-
bool irupt_enabled; // whether the flag state is tied to the interrupt pin state
24-
} Pcf8523IruptState;
25-
26-
typedef struct {
27-
bool enabled; // whether the timer is running
28-
uint8_t value; // the current value of the timer
29-
Pcf8523FrequencyDivision freq; // the clock divider used
30-
Pcf8523IruptState irupt_state; // the timer's interrupt state
31-
} Pcf8523TimerState;
32-
*/
33-
34-
state.enabled = true;
35-
state.value = 10;
36-
state.freq = PCF8523_FrequencySecond;
37-
state.irupt_state.irupt_flag = false;
38-
state.irupt_state.irupt_enabled = true;
39-
40-
rtc.write_timer(PCF8523_Timer_Countdown_A, &state);
17+
Pcf8523TimerState state = Pcf8523TimerState(
18+
10, // timer value
19+
PCF8523_FrequencySecond, // timer frequency
20+
true, // timer set to run
21+
false, // timer interrupt flag (when timer has gone off)
22+
true // timer flag -> signal enable
23+
);
24+
25+
rtc.write_timer(PCF8523_Timer_Countdown_A, state);
4126
}
4227

4328
void loop () {
44-
rtc.read_timer(PCF8523_Timer_Countdown_A, &state);
29+
Pcf8523TimerState state = rtc.read_timer(PCF8523_Timer_Countdown_A);
4530

4631
Serial.print("timer value: ");
4732
Serial.print(state.value, DEC);
@@ -52,9 +37,9 @@ void loop () {
5237
Serial.println();
5338

5439
Serial.print("irupt flag: ");
55-
Serial.print(state.irupt_state.irupt_flag, DEC);
40+
Serial.print(state.irupt_flag, DEC);
5641
Serial.print(", enabled: ");
57-
Serial.print(state.irupt_state.irupt_enabled, DEC);
42+
Serial.print(state.irupt_enabled, DEC);
5843
Serial.println();
5944

6045
Serial.println();

0 commit comments

Comments
 (0)