Skip to content

Add new method to obtain the time and date from the RTC without requiring a DateTime object. #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/MCP7940.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ static uint8_t conv2d(const char* p) {
if ('0' <= *p && *p <= '9') { v = *p - '0'; } // of if-then character in range
return 10 * v + *++p - '0';
} // of method conv2d


DateTime::DateTime(uint32_t t) {
/*!
@brief DateTime constructor (overloaded)
Expand Down Expand Up @@ -877,3 +879,20 @@ bool MCP7940_Class::clearPowerFail() const {
I2C_write(MCP7940_RTCWKDAY, readByte(MCP7940_RTCWKDAY));
return true;
} // of method clearPowerFail()
bool MCP7940_Class::getTimeDate(DateTime_t &dateTimeStuct) const
{
/*!
@brief Reads the current date/time from the RTC and populates the DateTime_t structure. Avoids creating a DateTime object.
@return True if the RTC was read successfully, otherwise false.
*/
uint8_t readBuffer[7] = {0};
uint8_t bytesRead = I2C_read(MCP7940_RTCSEC, readBuffer);
dateTimeStuct.year = bcd2int(readBuffer[6]) + 2000;
dateTimeStuct.month = bcd2int(readBuffer[5] & 0x1F);
dateTimeStuct.day = bcd2int(readBuffer[4] & 0x3F);
dateTimeStuct.hour = bcd2int(readBuffer[2] & 0x3F);
dateTimeStuct.minute = bcd2int(readBuffer[1] & 0x7F);
dateTimeStuct.second = bcd2int(readBuffer[0] & 0x7F);
dateTimeStuct.weekday = readBuffer[3] & 0x07;
return bytesRead == 7;
} // of method getTimeDate()
20 changes: 18 additions & 2 deletions src/MCP7940.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,22 @@ const uint8_t MCP7940_ALM0IF{3}; ///< ALM0WKDAY register
const uint8_t MCP7940_ALM1IF{3}; ///< ALM1WKDAY register
const uint32_t SECS_1970_TO_2000{946684800}; ///< Seconds between year 1970 and 2000

class DateTime {
struct DateTime_t
{
/*!
@struct DateTime_t
@brief Structure to hold date and time information
*/
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t weekday;
uint8_t day;
uint8_t month;
uint16_t year;
};
class DateTime
{
/*!
@class DateTime
@brief Simple general-purpose date/time class
Expand Down Expand Up @@ -225,7 +240,7 @@ class DateTime {
uint8_t hh; ///< Internal hour value
uint8_t mm; ///< Internal minute value
uint8_t ss; ///< Internal seconds
}; // of class DateTime definition
}; // of class DateTime definition
class TimeSpan {
/*!
@class TimeSpan
Expand Down Expand Up @@ -296,6 +311,7 @@ class MCP7940_Class {
int32_t getPPMDeviation(const DateTime& dt) const;
void setSetUnixTime(uint32_t aTime);
uint32_t getSetUnixTime() const;
bool getTimeDate(DateTime_t& dt) const;

/*************************************************************************************************
** Template functions definitions are done in the header file **
Expand Down
Loading