Example of how to use an Ada Fruit RGB LCD with the Ada Fruit RGB LCD Shield Library

Dependencies:   AdaFruit_RGBLCDShield MCP23017 mbed RTclock

Dependents:   SX1276_GPS

Fork of MCP_test by Wim Huiskamp

Updated the Adafruit RGB LCD Shield test app with a module system.

It pulls in RTclock which is another library I did for controlling the DS1307 RTC in a sane way (marries stdlib time and the RTC together cleanly). You don't need an RTC to run the example, it'll just use stdlib time instead. This class also maps RTC to system time, so if you loose the RTC the mbed will free run.

Four modules are defined in the modules folder plus the module base class. These examples provide:

  • title menu item
  • time menu item (updates automatically)
  • date menu item
  • fake temp menu item

Press select to switch modes: menu->cursor->change

Menu switches menu items going up/down. Cursor allows you to move around editable fields using the cursor keys / marker. Change allows you to move left/right on a particular line and change values by using up/down on an item with the blink box.

Custom fonts are defined for UI arrows and degree character.

If you want a menu item to update over time then you need to implement the canRefresh() member function in any child module you derive from class Module. Make it return true to receive update requests in your show() member function. Date and time both check when refreshing to see if anything has changed, then update.

main() registers a table of modules with the MenuManager. Others can be added easily by creating children derived from the Module base class..

Depending on what you want to do you may need to adjust the loop wait time in MenuManager::loop(). If you don't balance this based on work you need to do then the key presses may get a little lively. I may adjust the key checking to be fixed to 200ms regardless of loop wait time, however the catch there is that you'll consume more power the more loops you do so the wait is still important.

Happy coding!

Committer:
vtraveller
Date:
Thu Aug 14 12:02:38 2014 +0000
Revision:
18:fae7e61ea160
Parent:
16:9e1edf28393f
Child:
28:fbcd3bac0cd7
Fix for midday format issue.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vtraveller 10:3fcab08717fc 1 #include "mbed.h"
vtraveller 10:3fcab08717fc 2 #include "TimeModule.h"
vtraveller 10:3fcab08717fc 3 #include "time_helper.h"
vtraveller 10:3fcab08717fc 4
vtraveller 10:3fcab08717fc 5 #if 0
vtraveller 10:3fcab08717fc 6 tm sTM;
vtraveller 10:3fcab08717fc 7 sTM.tm_sec = 0;
vtraveller 10:3fcab08717fc 8 sTM.tm_min = 0;
vtraveller 10:3fcab08717fc 9 sTM.tm_hour = 18;
vtraveller 10:3fcab08717fc 10 sTM.tm_mday = 9;
vtraveller 10:3fcab08717fc 11 sTM.tm_mon = 8 - 1;
vtraveller 10:3fcab08717fc 12 sTM.tm_year = 2014 - 1900;
vtraveller 10:3fcab08717fc 13 sTM.tm_wday = 6;
vtraveller 10:3fcab08717fc 14
vtraveller 10:3fcab08717fc 15 rtc.SetTime(sTM,true);
vtraveller 10:3fcab08717fc 16 #endif
vtraveller 10:3fcab08717fc 17
vtraveller 10:3fcab08717fc 18
vtraveller 10:3fcab08717fc 19 TimeModule::TimeModule
vtraveller 10:3fcab08717fc 20 (
vtraveller 13:9641bc42db92 21 Serial & in_cDisplay,
vtraveller 13:9641bc42db92 22 RTclock & in_cRTclock
vtraveller 10:3fcab08717fc 23 )
vtraveller 13:9641bc42db92 24 : Module(in_cDisplay)
vtraveller 10:3fcab08717fc 25 , m_cRTclock(in_cRTclock)
vtraveller 10:3fcab08717fc 26 {
vtraveller 15:d1eaddb363be 27 ::memset(&m_sLastTM,0,sizeof(m_sLastTM));
vtraveller 10:3fcab08717fc 28 }
vtraveller 10:3fcab08717fc 29
vtraveller 10:3fcab08717fc 30 TimeModule::~TimeModule()
vtraveller 10:3fcab08717fc 31 {
vtraveller 10:3fcab08717fc 32 }
vtraveller 11:96146db429de 33
vtraveller 11:96146db429de 34 void TimeModule::change
vtraveller 11:96146db429de 35 (
vtraveller 13:9641bc42db92 36 size_t in_nIndex,
vtraveller 11:96146db429de 37 bool in_bUp
vtraveller 11:96146db429de 38 )
vtraveller 11:96146db429de 39 {
vtraveller 11:96146db429de 40 tm sTM;
vtraveller 10:3fcab08717fc 41
vtraveller 11:96146db429de 42 // to get the current time information
vtraveller 11:96146db429de 43 if (!m_cRTclock.getTime(sTM)) GetTime(sTM);
vtraveller 11:96146db429de 44 bool bTwelveHour = m_cRTclock.isTwelveHour();
vtraveller 11:96146db429de 45
vtraveller 11:96146db429de 46 enum ETime
vtraveller 11:96146db429de 47 {
vtraveller 11:96146db429de 48 eHourTen = 0,
vtraveller 11:96146db429de 49 eHourSingle,
vtraveller 11:96146db429de 50 eMinTen,
vtraveller 11:96146db429de 51 eMinSingle,
vtraveller 11:96146db429de 52 eSecondTen,
vtraveller 11:96146db429de 53 eSecondSingle,
vtraveller 11:96146db429de 54 eAmPm,
vtraveller 11:96146db429de 55 };
vtraveller 11:96146db429de 56
vtraveller 13:9641bc42db92 57 switch (in_nIndex)
vtraveller 11:96146db429de 58 {
vtraveller 11:96146db429de 59 case eHourTen: sTM.tm_hour += (in_bUp ? 1 : -1) * 10; break;
vtraveller 11:96146db429de 60 case eHourSingle: sTM.tm_hour += (in_bUp ? 1 : -1); break;
vtraveller 11:96146db429de 61 case eMinTen: sTM.tm_min += (in_bUp ? 1 : -1) * 10; break;
vtraveller 11:96146db429de 62 case eMinSingle: sTM.tm_min += (in_bUp ? 1 : -1); break;
vtraveller 11:96146db429de 63 case eSecondTen: sTM.tm_sec += (in_bUp ? 1 : -1) * 10; break;
vtraveller 11:96146db429de 64 case eSecondSingle: sTM.tm_sec += (in_bUp ? 1 : -1); break;
vtraveller 11:96146db429de 65 case eAmPm:
vtraveller 11:96146db429de 66 if (bTwelveHour)
vtraveller 11:96146db429de 67 {
vtraveller 11:96146db429de 68 if (in_bUp)
vtraveller 11:96146db429de 69 {
vtraveller 11:96146db429de 70 if (sTM.tm_hour >= 12) bTwelveHour = !bTwelveHour; else sTM.tm_hour += 12;
vtraveller 11:96146db429de 71 }
vtraveller 11:96146db429de 72 else
vtraveller 11:96146db429de 73 {
vtraveller 11:96146db429de 74 if (sTM.tm_hour >= 12) sTM.tm_hour -= 12; else bTwelveHour = !bTwelveHour;
vtraveller 11:96146db429de 75 }
vtraveller 11:96146db429de 76 }
vtraveller 11:96146db429de 77 else
vtraveller 11:96146db429de 78 {
vtraveller 11:96146db429de 79 bTwelveHour = !bTwelveHour;
vtraveller 11:96146db429de 80 if (in_bUp && sTM.tm_hour >= 12) sTM.tm_hour -= 12;
vtraveller 11:96146db429de 81 if (!in_bUp && sTM.tm_hour < 12) sTM.tm_hour += 12;
vtraveller 11:96146db429de 82 }
vtraveller 11:96146db429de 83 break;
vtraveller 11:96146db429de 84 }
vtraveller 11:96146db429de 85
vtraveller 11:96146db429de 86 if (sTM.tm_hour < 0) sTM.tm_hour = 0;
vtraveller 11:96146db429de 87 if (sTM.tm_hour > 23) sTM.tm_hour = 23;
vtraveller 11:96146db429de 88 if (sTM.tm_min < 0) sTM.tm_min = 0;
vtraveller 11:96146db429de 89 if (sTM.tm_hour > 59) sTM.tm_min = 59;
vtraveller 11:96146db429de 90 if (sTM.tm_sec < 0) sTM.tm_sec = 0;
vtraveller 11:96146db429de 91 if (sTM.tm_sec > 59) sTM.tm_sec = 59;
vtraveller 11:96146db429de 92
vtraveller 11:96146db429de 93 if (m_cRTclock.setTime(sTM,bTwelveHour))
vtraveller 11:96146db429de 94 {
vtraveller 11:96146db429de 95 m_cRTclock.mapTime();
vtraveller 11:96146db429de 96 }
vtraveller 11:96146db429de 97 else
vtraveller 11:96146db429de 98 {
vtraveller 11:96146db429de 99 SetTime(sTM);
vtraveller 11:96146db429de 100 }
vtraveller 11:96146db429de 101 }
vtraveller 11:96146db429de 102
vtraveller 13:9641bc42db92 103 int TimeModule::getCursorOffset(size_t & inout_nIndex)
vtraveller 10:3fcab08717fc 104 {
vtraveller 10:3fcab08717fc 105 const int k_aCursor[] = { 0, 1, 3, 4, 6, 7, 10 };
vtraveller 10:3fcab08717fc 106
vtraveller 13:9641bc42db92 107 if ((int)inout_nIndex < 0) inout_nIndex = 0;
vtraveller 13:9641bc42db92 108 if (inout_nIndex >= _countof(k_aCursor)) inout_nIndex = _countof(k_aCursor) - 1;
vtraveller 10:3fcab08717fc 109
vtraveller 13:9641bc42db92 110 return k_aCursor[inout_nIndex];
vtraveller 10:3fcab08717fc 111 }
vtraveller 10:3fcab08717fc 112
vtraveller 15:d1eaddb363be 113 void TimeModule::show(bool in_bRefresh)
vtraveller 10:3fcab08717fc 114 {
vtraveller 10:3fcab08717fc 115 tm sTM;
vtraveller 10:3fcab08717fc 116 const char * pUnits = " ";
vtraveller 10:3fcab08717fc 117
vtraveller 10:3fcab08717fc 118 // to get the current time information
vtraveller 10:3fcab08717fc 119 if (m_cRTclock.getTime(sTM))
vtraveller 10:3fcab08717fc 120 {
vtraveller 10:3fcab08717fc 121 // Adjust for 12 hour clock
vtraveller 10:3fcab08717fc 122 if (m_cRTclock.isTwelveHour())
vtraveller 10:3fcab08717fc 123 {
vtraveller 10:3fcab08717fc 124 pUnits = (sTM.tm_hour < 12) ? "am":"pm";
vtraveller 18:fae7e61ea160 125 if (sTM.tm_hour > 12) sTM.tm_hour %= 12;
vtraveller 10:3fcab08717fc 126 }
vtraveller 10:3fcab08717fc 127 }
vtraveller 10:3fcab08717fc 128 else
vtraveller 10:3fcab08717fc 129 {
vtraveller 10:3fcab08717fc 130 // If failed get internal time (as at least that's something
vtraveller 10:3fcab08717fc 131 GetTime(sTM);
vtraveller 10:3fcab08717fc 132 }
vtraveller 15:d1eaddb363be 133
vtraveller 15:d1eaddb363be 134 // if refreshing - only update if there's a change
vtraveller 15:d1eaddb363be 135 if (in_bRefresh)
vtraveller 15:d1eaddb363be 136 {
vtraveller 16:9e1edf28393f 137 // Check for change based on second (rest is irrelevant)
vtraveller 16:9e1edf28393f 138 if (sTM.tm_sec == m_sLastTM.tm_sec) return;
vtraveller 15:d1eaddb363be 139 }
vtraveller 15:d1eaddb363be 140
vtraveller 15:d1eaddb363be 141 // Ensure internal struct has new TM data
vtraveller 15:d1eaddb363be 142 ::memcpy(&m_sLastTM,&sTM,sizeof(m_sLastTM));
vtraveller 10:3fcab08717fc 143
vtraveller 13:9641bc42db92 144 m_cDisplay.printf ("%02i:%02i:%02i %s ", sTM.tm_hour, sTM.tm_min, sTM.tm_sec, pUnits);
vtraveller 10:3fcab08717fc 145 }