Local Library for DS3231 RTC
Fork of ds3231 by
Diff: ds3231.cpp
- Revision:
- 15:b87f3e7258bb
- Parent:
- 12:b9f13fd8c1b6
--- a/ds3231.cpp Wed Mar 18 00:06:59 2015 +0000 +++ b/ds3231.cpp Mon Jan 15 20:24:23 2018 +0000 @@ -681,6 +681,49 @@ return(rtn_val); } +uint16_t Ds3231::set_aging(uint8_t age){ + uint16_t rtn_val = 1; + rtn_val = write(w_adrs, (const char*) age, 1); + + return(rtn_val); + + +} + +/********************************************************** +* Set RTC time and date based on epoch time +* +* return 0 on success +**********************************************************/ + +uint16_t Ds3231::set_epoch(time_t epoch_time){ + //assume success until failure + int success = 0; + + //system vars + struct tm * sys_time; + //RTC vars + ds3231_time_t rtc_time = {0,0,0,0,0}; + ds3231_calendar_t rtc_calendar = {0,0,0,0}; + + sys_time = localtime(&epoch_time); + + //localtime comes back as 24hour + rtc_time.mode = 0; + rtc_time.hours = sys_time->tm_hour; + rtc_time.minutes = sys_time->tm_min; + rtc_time.seconds = sys_time->tm_sec; + + rtc_calendar.day = sys_time->tm_wday + 1; + rtc_calendar.date = sys_time->tm_mday; + rtc_calendar.month = sys_time->tm_mon + 1; + rtc_calendar.year = sys_time->tm_year - 100; + + success += set_calendar(rtc_calendar); + success += set_time(rtc_time); + + return success; +} /**********************************************************//** * Get epoch time based on current RTC time and date.