SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
SensorRTC Class Referenceabstract

#include <SensorRTC.h>

Inheritance diagram for SensorRTC:
[legend]

Public Member Functions

virtual bool begin (SensorCommCustom::CustomCallback callback)=0
 Initialize the RTC device using a custom callback.
 
virtual void setDateTime (RTC_DateTime datetime)=0
 Set the date and time of the RTC using an RTC_DateTime object.
 
virtual RTC_DateTime getDateTime ()=0
 Get the current date and time from the RTC.
 
virtual const char * getChipName ()=0
 Get the name of the RTC chip.
 
virtual void reset ()
 Reset the RTC.
 
void getDateTime (struct tm *info)
 Retrieve the current date and time from the RTC (Real-Time Clock) and populate a struct tm object.
 
void setDateTime (uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
 Set the date and time of the RTC using individual values.
 
const char * strftime (DateTimeFormat style=DT_FMT_YMD_HMS_WEEK)
 Format the current date and time according to the specified style.
 
time_t hwClockRead (const timezone *tz=NULL)
 Read the time from the RTC (Real-Time Clock) and write it to the system clock.
 
int hwClockWrite ()
 Write the current system time to the RTC clock chip.
 
uint32_t getDayOfWeek (uint32_t day, uint32_t month, uint32_t year)
 Calculate the day of the week for a given date using Zeller's congruence.
 
uint8_t getNextMonth (uint8_t curMonth)
 Get the next month based on the current month.
 
uint16_t getNextYear (uint16_t curYear)
 Get the next year based on the current year.
 
uint32_t getLeapYear (uint32_t year)
 Determine whether a given year is a leap year.
 
uint8_t getDaysInMonth (uint8_t month, uint16_t year)
 Get the number of days in a given month of a specific year, considering leap years.
 
void convertUtcToTimezone (int &year, int &month, int &day, int &hour, int &minute, int &second, int timezoneOffsetSeconds)
 Convert UTC time represented by individual values to the time in the specified timezone.
 
void convertUtcToTimezone (struct tm &utcTime, int timezoneOffsetSeconds)
 Overloaded function to convert UTC time represented by a struct tm object to the time in the specified timezone.
 

Static Public Member Functions

static uint8_t BCD2DEC (uint8_t val)
 Convert a Binary Coded Decimal (BCD) number to a decimal number.
 
static uint8_t DEC2BCD (uint8_t val)
 Convert a decimal number to a Binary Coded Decimal (BCD) number.
 

Detailed Description

Definition at line 214 of file SensorRTC.h.

Member Function Documentation

◆ BCD2DEC()

static uint8_t SensorRTC::BCD2DEC ( uint8_t  val)
inlinestatic

Convert a Binary Coded Decimal (BCD) number to a decimal number.

Parameters
valA uint8_t value representing a BCD number.
Returns
The decimal equivalent of the input BCD number.

Definition at line 657 of file SensorRTC.h.

Referenced by SensorPCF85063::getAlarm(), SensorPCF8563::getAlarm(), SensorPCF85063::getDateTime(), and SensorPCF8563::getDateTime().

◆ begin()

virtual bool SensorRTC::begin ( SensorCommCustom::CustomCallback  callback)
pure virtual

Initialize the RTC device using a custom callback.

This function allows for a custom way of initializing the RTC device. It takes a custom callback function as a parameter. The callback can be used to perform custom communication or configuration operations during the initialization process.

Parameters
callbackA pointer to the custom callback function. The callback should follow the signature defined in SensorCommCustom::CustomCallback.
Returns
true if the initialization is successful, false otherwise.

Implemented in SensorPCF85063, SensorPCF8563, and SensorRtcHelper.

◆ convertUtcToTimezone() [1/2]

void SensorRTC::convertUtcToTimezone ( int &  year,
int &  month,
int &  day,
int &  hour,
int &  minute,
int &  second,
int  timezoneOffsetSeconds 
)
inline

Convert UTC time represented by individual values to the time in the specified timezone.

Parameters
yearThe year of the UTC time.
monthThe month of the UTC time (1 - 12).
dayThe day of the UTC time.
hourThe hour of the UTC time (0 - 23).
minuteThe minute of the UTC time (0 - 59).
secondThe second of the UTC time (0 - 59).
timezoneOffsetSecondsThe offset of the target timezone from UTC (in seconds).

Definition at line 601 of file SensorRTC.h.

References getDaysInMonth(), and SENSORLIB_LOG_E.

Referenced by convertUtcToTimezone(), and loop().

◆ convertUtcToTimezone() [2/2]

void SensorRTC::convertUtcToTimezone ( struct tm &  utcTime,
int  timezoneOffsetSeconds 
)
inline

Overloaded function to convert UTC time represented by a struct tm object to the time in the specified timezone.

Parameters
utcTimeA reference to the struct tm object representing UTC time.
timezoneOffsetSecondsThe offset of the target timezone from UTC (in seconds).

Definition at line 633 of file SensorRTC.h.

References convertUtcToTimezone().

◆ DEC2BCD()

static uint8_t SensorRTC::DEC2BCD ( uint8_t  val)
inlinestatic

Convert a decimal number to a Binary Coded Decimal (BCD) number.

Parameters
valA uint8_t value representing a decimal number.
Returns
The BCD equivalent of the input decimal number.

Definition at line 667 of file SensorRTC.h.

Referenced by SensorPCF8563::setAlarm(), SensorPCF85063::setAlarm(), SensorPCF85063::setDateTime(), and SensorPCF8563::setDateTime().

◆ getChipName()

virtual const char * SensorRTC::getChipName ( )
pure virtual

Get the name of the RTC chip.

This is a pure virtual function that derived classes must implement. It returns a pointer to a null - terminated string representing the name of the RTC chip.

Returns
A pointer to a constant character array containing the name of the RTC chip.

Implemented in SensorRtcHelper, SensorPCF85063, and SensorPCF8563.

◆ getDateTime() [1/2]

virtual RTC_DateTime SensorRTC::getDateTime ( )
pure virtual

Get the current date and time from the RTC.

This is a pure virtual function that derived classes must implement. It retrieves the current date and time from the RTC and returns them as an RTC_DateTime object.

Returns
An RTC_DateTime object representing the current date and time of the RTC.

Implemented in SensorRtcHelper, SensorPCF85063, SensorPCF8563, SensorPCF85063, SensorPCF8563, and SensorRtcHelper.

Referenced by getDateTime(), hwClockRead(), and strftime().

◆ getDateTime() [2/2]

void SensorRTC::getDateTime ( struct tm *  info)
inline

Retrieve the current date and time from the RTC (Real-Time Clock) and populate a struct tm object.

This function fetches the current date and time information from the RTC device. It then fills the provided struct tm object with the corresponding values. The struct tm object is a standard C structure used to represent date and time components, such as year, month, day, hour, minute, and second.

Parameters
infoA pointer to a struct tm object where the retrieved date and time information will be stored. This pointer must not be a null pointer
Note
The year value in the struct tm object is the number of years since 1900. The month value ranges from 0 (January) to 11 (December). The day value represents the day of the month, starting from 1. The hour value ranges from 0 to 23, the minute and second values range from 0 to 59.

Definition at line 335 of file SensorRTC.h.

References getDateTime(), and RTC_DateTime::toUnixTime().

◆ getDayOfWeek()

uint32_t SensorRTC::getDayOfWeek ( uint32_t  day,
uint32_t  month,
uint32_t  year 
)
inline

Calculate the day of the week for a given date using Zeller's congruence.

Parameters
dayThe day of the month (1 - 31).
monthThe month of the year (1 - 12).
yearThe year.
Returns
The day of the week, where 0 represents Sunday, 1 represents Monday, ..., 6 represents Saturday. Returns 0xFF if the input date is invalid.

Definition at line 483 of file SensorRTC.h.

Referenced by SensorPCF85063::setDateTime(), and SensorPCF8563::setDateTime().

◆ getDaysInMonth()

uint8_t SensorRTC::getDaysInMonth ( uint8_t  month,
uint16_t  year 
)
inline

Get the number of days in a given month of a specific year, considering leap years.

Parameters
monthThe month (1 - 12).
yearThe year.
Returns
The number of days in the specified month of the given year.

Definition at line 552 of file SensorRTC.h.

References getLeapYear().

Referenced by convertUtcToTimezone(), SensorPCF8563::setAlarm(), SensorPCF85063::setAlarm(), testAlarmDay(), and testAlarmHour().

◆ getLeapYear()

uint32_t SensorRTC::getLeapYear ( uint32_t  year)
inline

Determine whether a given year is a leap year.

Parameters
yearThe year to check.
Returns
1 if the year is a leap year, 0 otherwise.

Definition at line 536 of file SensorRTC.h.

Referenced by getDaysInMonth().

◆ getNextMonth()

uint8_t SensorRTC::getNextMonth ( uint8_t  curMonth)
inline

Get the next month based on the current month.

Parameters
curMonthThe current month (1 - 12).
Returns
The next month (1 - 12). If the current month is 12, it returns 1.

Definition at line 514 of file SensorRTC.h.

◆ getNextYear()

uint16_t SensorRTC::getNextYear ( uint16_t  curYear)
inline

Get the next year based on the current year.

Parameters
curYearThe current year.
Returns
The next year.

Definition at line 525 of file SensorRTC.h.

◆ hwClockRead()

time_t SensorRTC::hwClockRead ( const timezone *  tz = NULL)
inline

Read the time from the RTC (Real-Time Clock) and write it to the system clock.

This function retrieves the current date and time from the RTC chip, converts it to a Unix timestamp, and then sets this timestamp as the system time on systems that support it (e.g., BSD systems).

Parameters
tzA pointer to a timezone structure specifying timezone information. In modern systems, this parameter is usually ignored and can be set to NULL. The default value of this parameter is NULL.
Returns
On success, returns the Unix timestamp (in seconds) read from the RTC and converted; if the mktime call fails during the conversion process, returns -1; if the settimeofday call fails on a supported system, an error log is recorded, but the timestamp is still returned.

Definition at line 427 of file SensorRTC.h.

References getDateTime(), SENSORLIB_LOG_E, timestamp, and RTC_DateTime::toUnixTime().

◆ hwClockWrite()

int SensorRTC::hwClockWrite ( )
inline

Write the current system time to the RTC clock chip.

Returns
If the system time is not obtained (time call) or the time is not converted (localtime_r call), it returns -1; If the time is successfully obtained, converted and set to the RTC chip, it returns 0

Definition at line 456 of file SensorRTC.h.

References SENSORLIB_LOG_E, and setDateTime().

◆ reset()

virtual void SensorRTC::reset ( )
inlinevirtual

Reset the RTC.

This function is available depending on the chip.

Reimplemented in SensorPCF85063, and SensorPCF8563.

Definition at line 316 of file SensorRTC.h.

◆ setDateTime() [1/2]

virtual void SensorRTC::setDateTime ( RTC_DateTime  datetime)
pure virtual

Set the date and time of the RTC using an RTC_DateTime object.

This is a pure virtual function that derived classes must implement. It allows setting the RTC's date and time using a single RTC_DateTime object.

Parameters
datetimeAn RTC_DateTime object containing the date and time information to set on the RTC.

Implemented in SensorRtcHelper, SensorPCF85063, SensorPCF8563, SensorPCF85063, SensorPCF8563, and SensorRtcHelper.

Referenced by hwClockWrite(), and setDateTime().

◆ setDateTime() [2/2]

void SensorRTC::setDateTime ( uint16_t  year,
uint8_t  month,
uint8_t  day,
uint8_t  hour,
uint8_t  minute,
uint8_t  second 
)
inline

Set the date and time of the RTC using individual values.

Parameters
yearThe year to set on the RTC. It's usually a 16 - bit unsigned integer.
monthThe month to set on the RTC, ranging from 1 (January) to 12 (December).
dayThe day of the month to set on the RTC, ranging from 1 to 31, depending on the month and leap year.
hourThe hour to set on the RTC, ranging from 0 to 23.
minuteThe minute to set on the RTC, ranging from 0 to 59.
secondThe second to set on the RTC, ranging from 0 to 59.

Definition at line 351 of file SensorRTC.h.

References setDateTime().

◆ strftime()

const char * SensorRTC::strftime ( DateTimeFormat  style = DT_FMT_YMD_HMS_WEEK)
inline

Format the current date and time according to the specified style.

This function retrieves the current date and time from the RTC (Real-Time Clock) using the getDateTime() function. Then, it formats the date and time into a string based on the provided style. The result is stored in a static buffer and a pointer to this buffer is returned.

Parameters
styleThe format style for the date and time string. It should be one of the values defined in the DateTimeFormat enumeration. The default value is DT_FMT_YMD_HMS_WEEK, which represents the format "Year-Month-Day/Hour:Minute:Second - Weekday".
Returns
A pointer to a static character array containing the formatted date and time string. Note that the buffer is static, so subsequent calls to this function will overwrite the previous result.

Definition at line 372 of file SensorRTC.h.

References DT_FMT_DMY, DT_FMT_HM, DT_FMT_HMS, DT_FMT_MDY, DT_FMT_YMD, DT_FMT_YMD_HMS, DT_FMT_YMD_HMS_WEEK, and getDateTime().

Referenced by printDateTime().


The documentation for this class was generated from the following file: