diff --git a/src/MCP7940.cpp b/src/MCP7940.cpp index 6e28c0a..1f521ee 100644 --- a/src/MCP7940.cpp +++ b/src/MCP7940.cpp @@ -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) @@ -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() diff --git a/src/MCP7940.h b/src/MCP7940.h index 4b6ec93..3fd20d6 100644 --- a/src/MCP7940.h +++ b/src/MCP7940.h @@ -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 @@ -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 @@ -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 **