@@ -1273,6 +1273,46 @@ void RTC_PCF8523::calibrate(Pcf8523OffsetMode mode, int8_t offset) {
12731273
12741274extern 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 ©) {
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
0 commit comments