A simple WIP that logs data from a Grove sensor, and can send and receive information over USB and SMS.

Dependencies:   DHT DS_1337 SDFileSystem USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rtc.cpp Source File

rtc.cpp

00001 #include "rtc.h"
00002 #include "DS1337.h"
00003 
00004 // declare reference to interface to the hardware real time clock
00005 extern DS1337* RTC_DS1337;
00006 
00007 // RTC enabled flag
00008 static int _rtcEnabled;
00009 
00010 time_t my_rtc_read()
00011 {
00012     time_t  retval = 0;     // time since start of epoch
00013     tm      _time_tm;
00014 
00015     RTC_DS1337->readTime();
00016 
00017     // extract values from RTC to tm struct
00018     _time_tm.tm_year = ((int)RTC_DS1337->getYears()) - 1900;       // struct tm stores (year - 1900)
00019     _time_tm.tm_mon  = (int)RTC_DS1337->getMonths() - 1;         // struct tm stores months 0 - 11, DS1337 stores 1-12
00020     _time_tm.tm_mday = (int)RTC_DS1337->getDays();
00021     _time_tm.tm_hour = (int)RTC_DS1337->getHours();
00022     _time_tm.tm_min  = (int)RTC_DS1337->getMinutes();
00023     _time_tm.tm_sec  = (int)RTC_DS1337->getSeconds();
00024 
00025     // convert to time_t    
00026     retval = mktime(&_time_tm);
00027     if (retval == (time_t) -1)
00028         return (time_t)1;               // error
00029     else
00030         return retval;
00031 }
00032 
00033 
00034 //https://developer.mbed.org/teams/Seeed/code/Seeed_Arch_GPRS_V2_RTC_HelloWorld/file/6db7dfbab0f1/main.cpp
00035 void my_rtc_write(time_t _time)
00036 {
00037     // extract time_t to time info struct
00038     tm * timeinfo = localtime(&_time);
00039 
00040     RTC_DS1337->setSeconds   (timeinfo->tm_sec);
00041     RTC_DS1337->setMinutes   (timeinfo->tm_min);
00042     RTC_DS1337->setHours     (timeinfo->tm_hour);
00043     RTC_DS1337->setDays      (timeinfo->tm_mday);           // day of month
00044     RTC_DS1337->setDayOfWeek (timeinfo->tm_wday);
00045     RTC_DS1337->setMonths    (timeinfo->tm_mon + 1);        // struct tm stores months 0 - 11, DS1337 stores 1-12
00046     RTC_DS1337->setYears     (timeinfo->tm_year + 1900);    // struct tm subtracts 1900 from year
00047 
00048     RTC_DS1337->setTime();
00049 }
00050 
00051 void my_rtc_init()
00052 {
00053     RTC_DS1337->start();
00054     _rtcEnabled = 1;
00055 }
00056 
00057 int my_rtc_enabled()
00058 {
00059     return _rtcEnabled;
00060 }