Justin Howard / Mbed 2 deprecated AdaFruit_RGBLCD

Dependencies:   AdaFruit_RGBLCDShield MCP23017 mbed RTclock

Dependents:   SX1276_GPS

Fork of MCP_test by Wim Huiskamp

Modules/TimeModule.cpp

Committer:
vtraveller
Date:
2014-08-10
Revision:
11:96146db429de
Parent:
10:3fcab08717fc
Child:
13:9641bc42db92

File content as of revision 11:96146db429de:

#include "mbed.h"
#include "TimeModule.h"
#include "time_helper.h"

#if 0
    tm sTM;
    sTM.tm_sec = 0;
    sTM.tm_min = 0;
    sTM.tm_hour = 18;
    sTM.tm_mday = 9;
    sTM.tm_mon  = 8 - 1;
    sTM.tm_year = 2014 - 1900;
    sTM.tm_wday = 6;

    rtc.SetTime(sTM,true);
#endif


TimeModule::TimeModule
(
    Adafruit_RGBLCDShield & in_cLCD,
    RTclock & in_cRTclock
)
    : Module(in_cLCD)
    , m_cRTclock(in_cRTclock)
{
}

TimeModule::~TimeModule()
{
}

void TimeModule::change
(
    int         in_nIndexX,
    int         in_nCursorY,
    bool        in_bUp
)
{
    tm sTM;
    
    // to get the current time information
    if (!m_cRTclock.getTime(sTM)) GetTime(sTM);
    bool bTwelveHour = m_cRTclock.isTwelveHour();

    enum ETime
    {
        eHourTen = 0,
        eHourSingle,
        eMinTen,
        eMinSingle,
        eSecondTen,
        eSecondSingle,
        eAmPm,
    };    
    
    switch (in_nIndexX)
    {
        case eHourTen:      sTM.tm_hour += (in_bUp ? 1 : -1) * 10;  break;
        case eHourSingle:   sTM.tm_hour += (in_bUp ? 1 : -1); break;
        case eMinTen:       sTM.tm_min += (in_bUp ? 1 : -1) * 10;  break;
        case eMinSingle:    sTM.tm_min += (in_bUp ? 1 : -1); break;
        case eSecondTen:    sTM.tm_sec += (in_bUp ? 1 : -1) * 10;  break;
        case eSecondSingle: sTM.tm_sec += (in_bUp ? 1 : -1); break;
        case eAmPm:
            if (bTwelveHour)
            {
                if (in_bUp)
                {
                    if (sTM.tm_hour >= 12) bTwelveHour = !bTwelveHour; else sTM.tm_hour += 12;
                }
                else
                {
                    if (sTM.tm_hour >= 12) sTM.tm_hour -= 12; else bTwelveHour = !bTwelveHour;
                }
            }
            else
            {
                bTwelveHour = !bTwelveHour;
                if (in_bUp && sTM.tm_hour >= 12) sTM.tm_hour -= 12;
                if (!in_bUp && sTM.tm_hour < 12) sTM.tm_hour += 12;
            }                
            break;
    }
    
    if (sTM.tm_hour < 0)    sTM.tm_hour = 0;
    if (sTM.tm_hour > 23)   sTM.tm_hour = 23;
    if (sTM.tm_min < 0)     sTM.tm_min = 0;
    if (sTM.tm_hour > 59)   sTM.tm_min = 59;
    if (sTM.tm_sec < 0)     sTM.tm_sec = 0;
    if (sTM.tm_sec > 59)    sTM.tm_sec = 59;

    if (m_cRTclock.setTime(sTM,bTwelveHour))
    {
        m_cRTclock.mapTime();
    }
    else
    {
        SetTime(sTM);
    }
}

int TimeModule::setCursor
(
    int in_nIndex,
    int in_nCursorX,
    int in_nCursorY
)
{
    const int k_aCursor[] = { 0, 1, 3, 4, 6, 7, 10 };
    
    int nIndex = in_nIndex;
    if (nIndex < 0) nIndex = 0;
    if (nIndex >= _countof(k_aCursor)) nIndex = _countof(k_aCursor) - 1;
       
    int nCursorX = k_aCursor[nIndex];
    m_cLCD.setCursor(in_nCursorX + nCursorX,in_nCursorY);
    
    return nIndex;
}

void TimeModule::show()
{
    tm sTM;
    const char * pUnits = "  ";
   
    // to get the current time information 
    if (m_cRTclock.getTime(sTM))
    {
        // Adjust for 12 hour clock
        if (m_cRTclock.isTwelveHour())
        {
            pUnits = (sTM.tm_hour < 12) ? "am":"pm";
            sTM.tm_hour %= 12;
        }
    }
    else
    {
        // If failed get internal time (as at least that's something
        GetTime(sTM);
    }

    m_cLCD.printf ("%02i:%02i:%02i %s   ", sTM.tm_hour, sTM.tm_min, sTM.tm_sec, pUnits);    
}