Justin Howard / Mbed 2 deprecated AdaFruit_RGBLCD

Dependencies:   AdaFruit_RGBLCDShield MCP23017 mbed RTclock

Dependents:   SX1276_GPS

Fork of MCP_test by Wim Huiskamp

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TimeModule.cpp Source File

TimeModule.cpp

00001 #include "mbed.h"
00002 #include "TimeModule.h"
00003 #include "time_helper.h"
00004 
00005 #if 0
00006     tm sTM;
00007     sTM.tm_sec = 0;
00008     sTM.tm_min = 0;
00009     sTM.tm_hour = 18;
00010     sTM.tm_mday = 9;
00011     sTM.tm_mon  = 8 - 1;
00012     sTM.tm_year = 2014 - 1900;
00013     sTM.tm_wday = 6;
00014 
00015     rtc.SetTime(sTM,true);
00016 #endif
00017 
00018 
00019 TimeModule::TimeModule
00020 (
00021     Serial &    in_cDisplay,
00022     RTclock &   in_cRTclock
00023 )
00024     : Module(in_cDisplay)
00025     , m_cRTclock(in_cRTclock)
00026 {
00027     ::memset(&m_sLastTM,0,sizeof(m_sLastTM));
00028 }
00029 
00030 TimeModule::~TimeModule()
00031 {
00032 }
00033 
00034 void TimeModule::change
00035 (
00036     size_t      in_nIndex,
00037     bool        in_bUp
00038 )
00039 {
00040     tm sTM;
00041     
00042     // to get the current time information
00043     if (!m_cRTclock.getTime(sTM)) GetTime(sTM);
00044     bool bTwelveHour = m_cRTclock.isTwelveHour();
00045 
00046     enum ETime
00047     {
00048         eHourTen = 0,
00049         eHourSingle,
00050         eMinTen,
00051         eMinSingle,
00052         eSecondTen,
00053         eSecondSingle,
00054         eAmPm,
00055     };    
00056     
00057     switch (in_nIndex)
00058     {
00059         case eHourTen:      sTM.tm_hour += (in_bUp ? 1 : -1) * 10;  break;
00060         case eHourSingle:   sTM.tm_hour += (in_bUp ? 1 : -1); break;
00061         case eMinTen:       sTM.tm_min += (in_bUp ? 1 : -1) * 10;  break;
00062         case eMinSingle:    sTM.tm_min += (in_bUp ? 1 : -1); break;
00063         case eSecondTen:    sTM.tm_sec += (in_bUp ? 1 : -1) * 10;  break;
00064         case eSecondSingle: sTM.tm_sec += (in_bUp ? 1 : -1); break;
00065         case eAmPm:
00066             if (bTwelveHour)
00067             {
00068                 if (in_bUp)
00069                 {
00070                     if (sTM.tm_hour >= 12) bTwelveHour = !bTwelveHour; else sTM.tm_hour += 12;
00071                 }
00072                 else
00073                 {
00074                     if (sTM.tm_hour >= 12) sTM.tm_hour -= 12; else bTwelveHour = !bTwelveHour;
00075                 }
00076             }
00077             else
00078             {
00079                 bTwelveHour = !bTwelveHour;
00080                 if (in_bUp && sTM.tm_hour >= 12) sTM.tm_hour -= 12;
00081                 if (!in_bUp && sTM.tm_hour < 12) sTM.tm_hour += 12;
00082             }                
00083             break;
00084     }
00085     
00086     if (sTM.tm_hour < 0)    sTM.tm_hour = 0;
00087     if (sTM.tm_hour > 23)   sTM.tm_hour = 23;
00088     if (sTM.tm_min < 0)     sTM.tm_min = 0;
00089     if (sTM.tm_min > 59)    sTM.tm_min = 59;
00090     if (sTM.tm_sec < 0)     sTM.tm_sec = 0;
00091     if (sTM.tm_sec > 59)    sTM.tm_sec = 59;
00092 
00093     if (m_cRTclock.setTime(sTM,bTwelveHour))
00094     {
00095         m_cRTclock.mapTime();
00096     }
00097     else
00098     {
00099         SetTime(sTM);
00100     }
00101 }
00102 
00103 int TimeModule::getCursorOffset(size_t & inout_nIndex)
00104 {
00105     const int k_aCursor[] = { 0, 1, 3, 4, 6, 7, 10 };
00106     
00107     if ((int)inout_nIndex < 0) inout_nIndex = 0;
00108     if (inout_nIndex >= _countof(k_aCursor)) inout_nIndex = _countof(k_aCursor) - 1;
00109        
00110     return k_aCursor[inout_nIndex];
00111 }
00112 
00113 void TimeModule::show(bool in_bRefresh)
00114 {
00115     tm sTM;
00116     const char * pUnits = "  ";
00117    
00118     // to get the current time information 
00119     if (m_cRTclock.getTime(sTM))
00120     {
00121         // Adjust for 12 hour clock
00122         if (m_cRTclock.isTwelveHour())
00123         {
00124             pUnits = (sTM.tm_hour < 12) ? "am":"pm";
00125             if (sTM.tm_hour > 12) sTM.tm_hour %= 12;
00126         }
00127     }
00128     else
00129     {
00130         // If failed get internal time (as at least that's something
00131         GetTime(sTM);
00132     }
00133     
00134     // if refreshing - only update if there's a change
00135     if (in_bRefresh)
00136     {        
00137         // Check for change based on second (rest is irrelevant)
00138         if (sTM.tm_sec == m_sLastTM.tm_sec) return;
00139     }
00140     
00141     // Ensure internal struct has new TM data
00142     ::memcpy(&m_sLastTM,&sTM,sizeof(m_sLastTM));
00143 
00144     m_cDisplay.printf ("%02i:%02i:%02i %s   ", sTM.tm_hour, sTM.tm_min, sTM.tm_sec, pUnits);    
00145 }