Example of how to use an Ada Fruit RGB LCD with the Ada Fruit RGB LCD Shield Library
Dependencies: AdaFruit_RGBLCDShield MCP23017 mbed RTclock
Fork of MCP_test by
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!
Revision 17:731a47339cb8, committed 2014-08-14
- Comitter:
- vtraveller
- Date:
- Thu Aug 14 10:47:27 2014 +0000
- Parent:
- 16:9e1edf28393f
- Child:
- 18:fae7e61ea160
- Commit message:
- Added mode indication to modules (allows them to change behavior based on mode changes. Means you can have a module change state but not commit the change until you leave that state.
Changed in this revision
--- a/MenuManager.cpp Tue Aug 12 04:28:23 2014 +0000
+++ b/MenuManager.cpp Thu Aug 14 10:47:27 2014 +0000
@@ -14,7 +14,7 @@
: m_pModules(in_pModules)
, m_nModules(in_nModules)
, m_cLCD(in_cLCD)
- , m_eMode(eModeMenu)
+ , m_eMode(Module::eModeLast)
, m_nMenuPos(0)
, m_nIndex(0)
, m_nCursorX(0)
@@ -64,7 +64,8 @@
void MenuManager::loop()
{
- m_eMode = eModeMenu;
+ setMode(Module::eModeMenu);
+
m_nMenuPos = 0;
m_nIndex = 0;
@@ -83,7 +84,7 @@
{
switch (m_eMode)
{
- case eModeMenu:
+ case Module::eModeMenu:
showModules(true);
int nOffsetX = m_pModules[m_nMenuPos]->getCursorOffset(m_nIndex);
@@ -101,10 +102,11 @@
// Change mode based on select
if (in_nKeys & BUTTON_SELECT)
{
- m_eMode = (EModes)((m_eMode + 1) % eModeLast);
+ Module::EModes eMode = (Module::EModes)((m_eMode + 1) % Module::eModeLast);
+ setMode(eMode);
// Start at top corner
- if (eModeSelect == m_eMode)
+ if (Module::eModeSelect == m_eMode)
{
m_nIndex = 0;
m_nCursorY = 0;
@@ -113,7 +115,7 @@
switch (m_eMode)
{
- case eModeMenu:
+ case Module::eModeMenu:
setCursor(false,false);
showTracking(false);
@@ -123,7 +125,7 @@
m_nMenuPos = m_nMenuPos % m_nModules;
break;
- case eModeSelect:
+ case Module::eModeSelect:
setCursor(true,false);
showTracking(true);
@@ -134,7 +136,7 @@
if (in_nKeys & BUTTON_RIGHT) m_nIndex++;
break;
- case eModeChange:
+ case Module::eModeChange:
setCursor(false,true);
showTracking(true);
@@ -158,6 +160,16 @@
if (in_bBlink) m_cLCD.blink(); else m_cLCD.noBlink();
}
+void MenuManager::setMode(Module::EModes in_eMode)
+{
+ m_eMode = in_eMode;
+
+ for (size_t i = 0; i < m_nModules; i++)
+ {
+ m_pModules[i]->onModeChange(m_eMode);
+ }
+}
+
void MenuManager::showModules(bool in_bRefresh)
{
m_cLCD.setCursor(2,0);
--- a/MenuManager.h Tue Aug 12 04:28:23 2014 +0000
+++ b/MenuManager.h Thu Aug 14 10:47:27 2014 +0000
@@ -2,16 +2,7 @@
#define __MENUMANAGER_H__
class MenuManager
-{
-protected:
- enum EModes
- {
- eModeMenu = 0,
- eModeSelect,
- eModeChange,
- eModeLast
- };
-
+{
public:
MenuManager
(
@@ -31,6 +22,7 @@
bool in_bCursor,
bool in_bBlink
);
+ void setMode(Module::EModes in_eMode);
void showModules(bool in_bRefresh = false);
void showTracking(bool in_bShow);
void updateDisplay();
@@ -39,7 +31,7 @@
Module ** m_pModules;
size_t m_nModules;
Adafruit_RGBLCDShield & m_cLCD;
- EModes m_eMode;
+ Module::EModes m_eMode;
size_t m_nMenuPos;
size_t m_nIndex;
int m_nCursorX, m_nCursorY;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Modules/SyncModule.cpp Thu Aug 14 10:47:27 2014 +0000
@@ -0,0 +1,71 @@
+#include "mbed.h"
+#include "SyncModule.h"
+#include "extra_chars.h"
+
+SyncModule::SyncModule
+(
+ Serial & in_cDisplay,
+ RTclock & in_cPrecisionClock,
+ RTclock & in_cClock
+)
+ : Module(in_cDisplay)
+ , m_cPrecisionClock(in_cPrecisionClock)
+ , m_cClock(in_cClock)
+ , m_eMode(eModeLast)
+ , m_bSync(false)
+{
+}
+
+SyncModule::~SyncModule()
+{
+}
+
+void SyncModule::change
+(
+ size_t /* in_nIndex */,
+ bool /* in_bUp */
+)
+{
+ m_bSync = (m_bSync) ? false : true;
+}
+
+int SyncModule::getCursorOffset(size_t & inout_nIndex)
+{
+ return m_bSync ? 13 : 12;
+}
+
+void SyncModule::onModeChange(EModes in_eMode)
+{
+ EModes eOldMode = m_eMode;
+ m_eMode = in_eMode;
+
+ if (eModeChange != eOldMode) return;
+ if (eModeMenu != m_eMode) return;
+
+ if (m_bSync)
+ {
+ // Sync the internal mbed clock (stdlib)
+ m_cPrecisionClock.mapTime();
+
+ // Sync the precision clock with the inaccurate RTC
+ tm sTM;
+ m_cPrecisionClock.getTime(sTM);
+ m_cClock.setTime(sTM,true);
+
+ m_bSync = false;
+ }
+}
+
+void SyncModule::show(bool /*in_bRefresh*/)
+{
+ switch (m_eMode)
+ {
+ case eModeChange:
+ case eModeSelect:
+ m_cDisplay.printf("Sync Time? %s ",m_bSync ? "yes" : "no");
+ break;
+
+ default:
+ m_cDisplay.printf("Sync Time %c ",eRight);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Modules/SyncModule.h Thu Aug 14 10:47:27 2014 +0000
@@ -0,0 +1,35 @@
+#ifndef __SYNCMODULE_H__
+#define __SYNCMODULE_H__
+
+#include "module.h"
+#include "RTclock.h"
+
+class SyncModule
+ : public Module
+{
+public:
+ SyncModule
+ (
+ Serial & in_cDisplay,
+ RTclock & in_cPrecisionClock,
+ RTclock & in_cClock
+ );
+ virtual ~SyncModule();
+
+ virtual void change
+ (
+ size_t in_nIndex,
+ bool in_bUp
+ );
+ virtual int getCursorOffset(size_t & inout_nIndex);
+ virtual void onModeChange(EModes in_eMode);
+ virtual void show(bool in_bRefresh);
+
+protected:
+ RTclock & m_cPrecisionClock;
+ RTclock & m_cClock;
+ EModes m_eMode;
+ bool m_bSync;
+};
+
+#endif /* __SYNCMODULE_H__ */
--- a/Modules/TempModule.h Tue Aug 12 04:28:23 2014 +0000
+++ b/Modules/TempModule.h Thu Aug 14 10:47:27 2014 +0000
@@ -17,7 +17,7 @@
);
virtual int getCursorOffset(size_t & inout_nIndex);
virtual void show(bool in_bRefresh);
-
+
protected:
int m_nTemp;
};
--- a/Modules/TimeModule.h Tue Aug 12 04:28:23 2014 +0000
+++ b/Modules/TimeModule.h Thu Aug 14 10:47:27 2014 +0000
@@ -23,7 +23,7 @@
);
virtual int getCursorOffset(size_t & inout_nIndex);
virtual void show(bool in_bRefresh);
-
+
protected:
RTclock & m_cRTclock;
tm m_sLastTM;
--- a/Modules/module.h Tue Aug 12 04:28:23 2014 +0000
+++ b/Modules/module.h Thu Aug 14 10:47:27 2014 +0000
@@ -8,6 +8,15 @@
class Module
{
public:
+ enum EModes
+ {
+ eModeMenu = 0,
+ eModeSelect,
+ eModeChange,
+ eModeLast
+ };
+
+public:
Module(Serial & in_cDisplay);
virtual ~Module();
@@ -20,6 +29,8 @@
{ ; }
virtual int getCursorOffset(size_t & inout_nIndex)
{ return -1; }
+ virtual void onModeChange(EModes in_eMode)
+ { ; }
virtual void show(bool in_bRefresh) = 0;
protected:
--- a/main.cpp Tue Aug 12 04:28:23 2014 +0000
+++ b/main.cpp Thu Aug 14 10:47:27 2014 +0000
@@ -7,6 +7,7 @@
#include "TempModule.h"
#include "TimeModule.h"
#include "TitleModule.h"
+#include "SyncModule.h"
#include "MenuManager.h"
@@ -16,18 +17,22 @@
{
MCP23017 cMCP23017 = MCP23017(I2C_SDA, I2C_SCL, 0x40, true);
Adafruit_RGBLCDShield cLCD(cMCP23017);
- RTclock cRTclock(I2C_SDA, I2C_SCL);
+
+ RTclock cPrecisionClock(I2C_SDA, I2C_SCL);
+ RTclock cClock(D3, D6);
// Spin up RTC
- cRTclock.mapTime();
-
+ cPrecisionClock.mapTime();
+
// Set up display modules
Module * aModules[] =
{
new TitleModule(cLCD),
- new TimeModule(cLCD,cRTclock),
- new DateModule(cLCD,cRTclock),
+ new TimeModule(cLCD,cPrecisionClock),
+ new TimeModule(cLCD,cClock),
+ new DateModule(cLCD,cPrecisionClock),
new TempModule(cLCD),
+ new SyncModule(cLCD,cPrecisionClock,cClock),
};
// Set up the menu manager
