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 13:9641bc42db92, committed 2014-08-11
- Comitter:
- vtraveller
- Date:
- Mon Aug 11 19:11:43 2014 +0000
- Parent:
- 12:0fea8ebe6c1a
- Child:
- 14:8b359e1e68f3
- Commit message:
- Created a scrollable menu system based on modules to work against the Adafruit RGB LCD library.; Modules can be created and installed as menu items with display, cursor and edit capabilities.
Changed in this revision
--- a/AdaFruit_RGBLCDShield.lib Sun Aug 10 16:01:43 2014 +0000 +++ b/AdaFruit_RGBLCDShield.lib Mon Aug 11 19:11:43 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/vtraveller/code/AdaFruit_RGBLCDShield/#45264ce231f9 +http://mbed.org/users/vtraveller/code/AdaFruit_RGBLCDShield/#24ab601221e2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MenuManager.cpp Mon Aug 11 19:11:43 2014 +0000
@@ -0,0 +1,222 @@
+#include "mbed.h"
+#include "module.h"
+#include "extra_chars.h"
+#include "Adafruit_RGBLCDShield.h"
+
+#include "MenuManager.h"
+
+MenuManager::MenuManager
+(
+ Module ** in_pModules,
+ size_t in_nModules,
+ Adafruit_RGBLCDShield & in_cLCD
+)
+ : m_pModules(in_pModules)
+ , m_nModules(in_nModules)
+ , m_cLCD(in_cLCD)
+ , m_eMode(eModeMenu)
+ , m_nMenuPos(0)
+ , m_nIndex(0)
+ , m_nCursorX(0)
+ , m_nCursorY(0)
+{
+}
+
+void MenuManager::changeModule(bool in_bUp)
+{
+ size_t nModule = (m_nMenuPos + m_nCursorY) % m_nModules;
+ m_pModules[nModule]->change(m_nIndex,in_bUp);
+}
+
+void MenuManager::createChars()
+{
+ //uint8_t k_aUp[] = { 0x4,0xe,0x1f,0x15,0x4,0x4,0x4,0x4 };
+ //uint8_t k_aDown[] = { 0x4,0x4,0x4,0x4,0x15,0x1f,0xe,0x4 };
+
+ uint8_t k_aUp[] = { 0x0,0x0,0x4,0xe,0x1f,0x0,0x0 };
+ uint8_t k_aDown[] = { 0x0,0x0,0x1f,0xe,0x4,0x0,0x0 };
+ uint8_t k_aRight[] = { 0x0,0x8,0xc,0xe,0xc,0x8,0x0 };
+ uint8_t k_aLeft[] = { 0x0,0x2,0x6,0xe,0x6,0x2,0x0 };
+ uint8_t k_aDegree[] = { 0xc,0x12,0x12,0xc,0x0,0x0,0x0,0x0 };
+
+ m_cLCD.createChar(eUp,k_aUp);
+ m_cLCD.createChar(eDown,k_aDown);
+ m_cLCD.createChar(eRight,k_aRight);
+ m_cLCD.createChar(eLeft,k_aLeft);
+ m_cLCD.createChar(eDegree,k_aDegree);
+}
+
+void MenuManager::initialise()
+{
+ // Initialise LCD
+ m_cLCD.begin(16,2);
+ createChars();
+
+ m_cLCD.setCursor(0,0);
+ m_cLCD._putc(eUp);
+
+ m_cLCD.setCursor(0,1);
+ m_cLCD._putc(eDown);
+
+ m_nCursorX = 2;
+ m_nCursorY = 0;
+}
+
+void MenuManager::loop()
+{
+ m_eMode = eModeMenu;
+ m_nMenuPos = 0;
+ m_nIndex = 0;
+
+ initialise();
+
+ showModules(m_nMenuPos);
+
+ while (true)
+ {
+ uint8_t nKeys = m_cLCD.readButtons();
+ if (nKeys)
+ {
+ processKeys(nKeys);
+ }
+ else
+ {
+ switch (m_eMode)
+ {
+ case eModeMenu:
+ showModules(true);
+
+ int nOffsetX = m_pModules[m_nMenuPos]->getCursorOffset(m_nIndex);
+ m_cLCD.setCursor(m_nCursorY + nOffsetX,m_nCursorY);
+ break;
+ }
+ }
+
+ wait(0.2);
+ }
+}
+
+void MenuManager::processKeys(uint8_t in_nKeys)
+{
+ // Change mode based on select
+ if (in_nKeys & BUTTON_SELECT)
+ {
+ m_eMode = (EModes)((m_eMode + 1) % eModeLast);
+
+ // Start at top corner
+ if (eModeSelect == m_eMode)
+ {
+ m_nIndex = 0;
+ m_nCursorY = 0;
+ }
+ }
+
+ switch (m_eMode)
+ {
+ case eModeMenu:
+ setCursor(false,false);
+ showTracking(false);
+
+ if (in_nKeys & BUTTON_UP) m_nMenuPos--;
+ if (in_nKeys & BUTTON_DOWN) m_nMenuPos++;
+
+ m_nMenuPos = m_nMenuPos % m_nModules;
+ break;
+
+ case eModeSelect:
+ setCursor(true,false);
+ showTracking(true);
+
+ if (m_nCursorY > 0 && (in_nKeys & BUTTON_UP)) m_nCursorY--;
+ if ((m_nCursorY < m_cLCD.lines() - 1) && (in_nKeys & BUTTON_DOWN)) m_nCursorY++;
+
+ if (in_nKeys & BUTTON_LEFT) m_nIndex--;
+ if (in_nKeys & BUTTON_RIGHT) m_nIndex++;
+ break;
+
+ case eModeChange:
+ setCursor(false,true);
+ showTracking(true);
+
+ if (in_nKeys & (BUTTON_UP | BUTTON_DOWN))
+ {
+ bool bUp = (in_nKeys & BUTTON_UP) ? true : false;
+ changeModule(bUp);
+ }
+
+ if (in_nKeys & BUTTON_LEFT) m_nIndex--;
+ if (in_nKeys & BUTTON_RIGHT) m_nIndex++;
+ break;
+ }
+
+ updateDisplay();
+}
+
+void MenuManager::setCursor(bool in_bCursor,bool in_bBlink)
+{
+ if (in_bCursor) m_cLCD.cursor(); else m_cLCD.noCursor();
+ if (in_bBlink) m_cLCD.blink(); else m_cLCD.noBlink();
+}
+
+void MenuManager::showModules(bool in_bRefresh)
+{
+ m_cLCD.setCursor(2,0);
+
+ if (m_pModules[m_nMenuPos]->canRefresh() || !in_bRefresh) m_pModules[m_nMenuPos]->show();
+
+ size_t nPos = (m_nMenuPos + 1) % m_nModules;
+ m_cLCD.setCursor(2,1);
+
+ if (m_pModules[nPos]->canRefresh() || !in_bRefresh) m_pModules[nPos]->show();
+}
+
+void MenuManager::showTracking(bool in_bShow)
+{
+ if (in_bShow)
+ {
+ m_cLCD.setCursor(1,0);
+ m_cLCD._putc(eLeft);
+
+ m_cLCD.setCursor(1,1);
+ m_cLCD._putc(eRight);
+ }
+ else
+ {
+ m_cLCD.setCursor(1,0);
+ m_cLCD._putc(' ');
+
+ m_cLCD.setCursor(1,1);
+ m_cLCD._putc(' ');
+ }
+}
+
+void MenuManager::updateDisplay()
+{
+ showModules();
+
+ size_t nCurrent = (m_nMenuPos + m_nCursorY) % m_nModules;
+
+ int nOffsetX = m_pModules[nCurrent]->getCursorOffset(m_nIndex);
+
+ // If we didn't have anything, move the line
+ if (-1 == nOffsetX)
+ {
+ m_nCursorY = (m_nCursorY + 1) % m_cLCD.lines();
+
+ nCurrent = (m_nMenuPos + m_nCursorY) % m_nModules;
+
+ m_nIndex = 0;
+ nOffsetX = m_pModules[nCurrent]->getCursorOffset(m_nIndex);
+ }
+
+ if ((size_t)-1 != m_nIndex)
+ {
+ // Move cursor to new position
+ m_cLCD.setCursor(m_nCursorX + nOffsetX,m_nCursorY);
+ }
+ else
+ {
+ // If nothing to show - hide everything
+ setCursor(false,false);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MenuManager.h Mon Aug 11 19:11:43 2014 +0000
@@ -0,0 +1,48 @@
+#ifndef __MENUMANAGER_H__
+#define __MENUMANAGER_H__
+
+class MenuManager
+{
+protected:
+ enum EModes
+ {
+ eModeMenu = 0,
+ eModeSelect,
+ eModeChange,
+ eModeLast
+ };
+
+public:
+ MenuManager
+ (
+ Module ** in_pModules,
+ size_t in_nModules,
+ Adafruit_RGBLCDShield & in_cLCD
+ );
+ void loop();
+
+protected:
+ void changeModule(bool in_bUp);
+ void createChars();
+ void initialise();
+ void processKeys(uint8_t in_nKeys);
+ void setCursor
+ (
+ bool in_bCursor,
+ bool in_bBlink
+ );
+ void showModules(bool in_bRefresh = false);
+ void showTracking(bool in_bShow);
+ void updateDisplay();
+
+protected:
+ Module ** m_pModules;
+ size_t m_nModules;
+ Adafruit_RGBLCDShield & m_cLCD;
+ EModes m_eMode;
+ size_t m_nMenuPos;
+ size_t m_nIndex;
+ int m_nCursorX, m_nCursorY;
+};
+
+#endif /* __MENUMANAGER_H__ */
--- a/Modules/DateModule.cpp Sun Aug 10 16:01:43 2014 +0000
+++ b/Modules/DateModule.cpp Mon Aug 11 19:11:43 2014 +0000
@@ -6,10 +6,10 @@
DateModule::DateModule
(
- Adafruit_RGBLCDShield & in_cLCD,
- RTclock & in_cRTclock
+ Serial & in_cDisplay,
+ RTclock & in_cRTclock
)
- : Module(in_cLCD)
+ : Module(in_cDisplay)
, m_cRTclock(in_cRTclock)
{
}
@@ -20,8 +20,7 @@
void DateModule::change
(
- int in_nIndexX,
- int in_nCursorY,
+ size_t in_nIndex,
bool in_bUp
)
{
@@ -42,7 +41,7 @@
eYearSingle,
};
- switch (in_nIndexX)
+ switch (in_nIndex)
{
case eWeekDay: sTM.tm_wday += (in_bUp ? 1 : -1); break;
case eDayTen: sTM.tm_mday += (in_bUp ? 1 : -1) * 10; break;
@@ -72,23 +71,14 @@
}
}
-int DateModule::setCursor
-(
- int in_nIndex,
- int in_nCursorX,
- int in_nCursorY
-)
+int DateModule::getCursorOffset(size_t & inout_nIndex)
{
const int k_aCursor[] = { 2, 4, 5, 7, 8, 12, 13 };
- int nIndex = in_nIndex;
- if (nIndex < 0) nIndex = 0;
- if (nIndex >= _countof(k_aCursor)) nIndex = _countof(k_aCursor) - 1;
+ if ((int)inout_nIndex < 0) inout_nIndex = 0;
+ if (inout_nIndex >= _countof(k_aCursor)) inout_nIndex = _countof(k_aCursor) - 1;
- int nCursorX = k_aCursor[nIndex];
- m_cLCD.setCursor(in_nCursorX + nCursorX,in_nCursorY);
-
- return nIndex;
+ return k_aCursor[inout_nIndex];
}
void DateModule::show()
@@ -98,5 +88,5 @@
// to get the current time information
if (!m_cRTclock.getTime(sTM)) GetTime(sTM);
- m_cLCD.printf ("%s %02i/%02i/%04i ", k_aWeekDays[sTM.tm_wday], sTM.tm_mday, sTM.tm_mon + 1, 1900 + sTM.tm_year);
+ m_cDisplay.printf ("%s %02i/%02i/%04i ", k_aWeekDays[sTM.tm_wday], sTM.tm_mday, sTM.tm_mon + 1, 1900 + sTM.tm_year);
}
--- a/Modules/DateModule.h Sun Aug 10 16:01:43 2014 +0000
+++ b/Modules/DateModule.h Mon Aug 11 19:11:43 2014 +0000
@@ -10,23 +10,17 @@
public:
DateModule
(
- Adafruit_RGBLCDShield & in_cLCD,
- RTclock & in_cRTclock
+ Serial & in_cDisplay,
+ RTclock & in_cRTclock
);
virtual ~DateModule();
virtual void change
(
- int in_nIndexX,
- int in_nCursorY,
+ size_t in_nIndex,
bool in_bUp
);
- virtual int setCursor
- (
- int in_nIndex,
- int in_nCursorX,
- int in_nCursorY
- );
+ virtual int getCursorOffset(size_t & inout_nIndex);
virtual void show();
protected:
--- a/Modules/TempModule.cpp Sun Aug 10 16:01:43 2014 +0000
+++ b/Modules/TempModule.cpp Mon Aug 11 19:11:43 2014 +0000
@@ -3,8 +3,8 @@
#include "extra_chars.h"
-TempModule::TempModule(Adafruit_RGBLCDShield & in_cLCD)
- : Module(in_cLCD)
+TempModule::TempModule(Serial & in_cDisplay)
+ : Module(in_cDisplay)
, m_nTemp(28)
{
}
@@ -15,8 +15,7 @@
void TempModule::change
(
- int in_nIndexX,
- int in_nCursorY,
+ size_t in_nIndex,
bool in_bUp
)
{
@@ -26,7 +25,7 @@
eSingles = 1,
};
- switch (in_nIndexX)
+ switch (in_nIndex)
{
case eTens: m_nTemp += (in_bUp ? 1 : -1) * 10; break;
case eSingles: m_nTemp += (in_bUp ? 1 : -1); break;
@@ -36,26 +35,17 @@
if (m_nTemp < -40) m_nTemp = -40;
}
-int TempModule::setCursor
-(
- int in_nIndex,
- int in_nCursorX,
- int in_nCursorY
-)
+int TempModule::getCursorOffset(size_t & inout_nIndex)
{
const int k_aCursor[] = { 6, 7 };
- int nIndex = in_nIndex;
- if (nIndex < 0) nIndex = 0;
- if (nIndex >= _countof(k_aCursor)) nIndex = _countof(k_aCursor) - 1;
+ if (inout_nIndex < 0) inout_nIndex = 0;
+ if (inout_nIndex >= _countof(k_aCursor)) inout_nIndex = _countof(k_aCursor) - 1;
- int nCursorX = k_aCursor[nIndex];
- m_cLCD.setCursor(in_nCursorX + nCursorX,in_nCursorY);
-
- return nIndex;
+ return k_aCursor[inout_nIndex];
}
void TempModule::show()
{
- m_cLCD.printf("Room: %i%cC ",m_nTemp,eDegree);
+ m_cDisplay.printf("Room: %i%cC ",m_nTemp,eDegree);
}
--- a/Modules/TempModule.h Sun Aug 10 16:01:43 2014 +0000
+++ b/Modules/TempModule.h Mon Aug 11 19:11:43 2014 +0000
@@ -7,21 +7,15 @@
: public Module
{
public:
- TempModule(Adafruit_RGBLCDShield & in_cLCD);
+ TempModule(Serial & in_cDisplay);
virtual ~TempModule();
virtual void change
(
- int in_nIndexX,
- int in_nCursorY,
+ size_t in_nIndex,
bool in_bUp
- );
- virtual int setCursor
- (
- int in_nIndex,
- int in_nCursorX,
- int in_nCursorY
);
+ virtual int getCursorOffset(size_t & inout_nIndex);
virtual void show();
protected:
--- a/Modules/TimeModule.cpp Sun Aug 10 16:01:43 2014 +0000
+++ b/Modules/TimeModule.cpp Mon Aug 11 19:11:43 2014 +0000
@@ -18,10 +18,10 @@
TimeModule::TimeModule
(
- Adafruit_RGBLCDShield & in_cLCD,
- RTclock & in_cRTclock
+ Serial & in_cDisplay,
+ RTclock & in_cRTclock
)
- : Module(in_cLCD)
+ : Module(in_cDisplay)
, m_cRTclock(in_cRTclock)
{
}
@@ -32,8 +32,7 @@
void TimeModule::change
(
- int in_nIndexX,
- int in_nCursorY,
+ size_t in_nIndex,
bool in_bUp
)
{
@@ -54,7 +53,7 @@
eAmPm,
};
- switch (in_nIndexX)
+ switch (in_nIndex)
{
case eHourTen: sTM.tm_hour += (in_bUp ? 1 : -1) * 10; break;
case eHourSingle: sTM.tm_hour += (in_bUp ? 1 : -1); break;
@@ -100,23 +99,14 @@
}
}
-int TimeModule::setCursor
-(
- int in_nIndex,
- int in_nCursorX,
- int in_nCursorY
-)
+int TimeModule::getCursorOffset(size_t & inout_nIndex)
{
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;
+ if ((int)inout_nIndex < 0) inout_nIndex = 0;
+ if (inout_nIndex >= _countof(k_aCursor)) inout_nIndex = _countof(k_aCursor) - 1;
- int nCursorX = k_aCursor[nIndex];
- m_cLCD.setCursor(in_nCursorX + nCursorX,in_nCursorY);
-
- return nIndex;
+ return k_aCursor[inout_nIndex];
}
void TimeModule::show()
@@ -140,5 +130,5 @@
GetTime(sTM);
}
- m_cLCD.printf ("%02i:%02i:%02i %s ", sTM.tm_hour, sTM.tm_min, sTM.tm_sec, pUnits);
+ m_cDisplay.printf ("%02i:%02i:%02i %s ", sTM.tm_hour, sTM.tm_min, sTM.tm_sec, pUnits);
}
--- a/Modules/TimeModule.h Sun Aug 10 16:01:43 2014 +0000
+++ b/Modules/TimeModule.h Mon Aug 11 19:11:43 2014 +0000
@@ -10,24 +10,18 @@
public:
TimeModule
(
- Adafruit_RGBLCDShield & in_cLCD,
- RTclock & in_cRTclock
+ Serial & in_cDisplay,
+ RTclock & in_cRTclock
);
virtual ~TimeModule();
virtual bool canRefresh() { return true; }
virtual void change
(
- int in_nIndexX,
- int in_nCursorY,
+ size_t in_nIndex,
bool in_bUp
);
- virtual int setCursor
- (
- int in_nIndex,
- int in_nCursorX,
- int in_nCursorY
- );
+ virtual int getCursorOffset(size_t & inout_nIndex);
virtual void show();
protected:
--- a/Modules/TitleModule.cpp Sun Aug 10 16:01:43 2014 +0000
+++ b/Modules/TitleModule.cpp Mon Aug 11 19:11:43 2014 +0000
@@ -1,8 +1,8 @@
#include "mbed.h"
#include "TitleModule.h"
-TitleModule::TitleModule(Adafruit_RGBLCDShield & in_cLCD)
- : Module(in_cLCD)
+TitleModule::TitleModule(Serial & in_cDisplay)
+ : Module(in_cDisplay)
{
}
@@ -12,5 +12,5 @@
void TitleModule::show()
{
- m_cLCD.printf("System Running");
+ m_cDisplay.printf("System Running");
}
--- a/Modules/TitleModule.h Sun Aug 10 16:01:43 2014 +0000
+++ b/Modules/TitleModule.h Mon Aug 11 19:11:43 2014 +0000
@@ -7,7 +7,7 @@
: public Module
{
public:
- TitleModule(Adafruit_RGBLCDShield & in_cLCD);
+ TitleModule(Serial & in_cDisplay);
virtual ~TitleModule();
virtual void show();
--- a/Modules/module.cpp Sun Aug 10 16:01:43 2014 +0000
+++ b/Modules/module.cpp Mon Aug 11 19:11:43 2014 +0000
@@ -2,8 +2,8 @@
#include "Adafruit_RGBLCDShield.h"
#include "module.h"
-Module::Module(Adafruit_RGBLCDShield & in_cLCD)
- : m_cLCD(in_cLCD)
+Module::Module(Serial & in_cDisplay)
+ : m_cDisplay(in_cDisplay)
{
}
--- a/Modules/module.h Sun Aug 10 16:01:43 2014 +0000
+++ b/Modules/module.h Mon Aug 11 19:11:43 2014 +0000
@@ -1,8 +1,6 @@
#ifndef __MODULE_H__
#define __MODULE_H__
-#include "Adafruit_RGBLCDShield.h"
-
#ifndef _countof
#define _countof(a) (sizeof(a) / sizeof(a[0]))
#endif
@@ -10,28 +8,22 @@
class Module
{
public:
- Module(Adafruit_RGBLCDShield & in_cLCD);
+ Module(Serial & in_cDisplay);
virtual ~Module();
virtual bool canRefresh() { return false; }
virtual void change
(
- int in_nIndexX,
- int in_nCursorY,
+ size_t in_nIndex,
bool in_bUp
)
{ ; }
- virtual int setCursor
- (
- int in_nIndex,
- int in_nCursorX,
- int in_nCursorY
- )
+ virtual int getCursorOffset(size_t & inout_nIndex)
{ return -1; }
virtual void show() = 0;
protected:
- Adafruit_RGBLCDShield & m_cLCD;
+ Serial & m_cDisplay;
};
#endif /* __MODULE_H__ */
--- a/RTclock.lib Sun Aug 10 16:01:43 2014 +0000 +++ b/RTclock.lib Mon Aug 11 19:11:43 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/vtraveller/code/RTclock/#3621025e7949 +http://mbed.org/users/vtraveller/code/RTclock/#2192f2809f59
--- a/main.cpp Sun Aug 10 16:01:43 2014 +0000
+++ b/main.cpp Mon Aug 11 19:11:43 2014 +0000
@@ -1,243 +1,38 @@
#include "mbed.h"
+
#include "Adafruit_RGBLCDShield.h"
#include "RTclock.h"
-#include "extra_chars.h"
-#include "module.h"
#include "DateModule.h"
#include "TempModule.h"
#include "TimeModule.h"
#include "TitleModule.h"
-Serial pc(SERIAL_TX, SERIAL_RX);
-
-MCP23017 mcp23017 = MCP23017(I2C_SDA, I2C_SCL, 0x40, true);
-Adafruit_RGBLCDShield lcd(mcp23017);
-RTclock rtc(I2C_SDA, I2C_SCL);
-
-const int k_nWidthLCD = 16;
-const int k_nHeightLCD = 2;
-
-enum EModes
-{
- eModeMenu = 0,
- eModeSelect,
- eModeChange,
- eModeLast
-};
-
-void ChangeModule
-(
- Module ** in_pModuleList,
- size_t in_nModuleListSize,
- size_t in_nMenuPos,
- int in_nIndexX,
- int in_nCursorY,
- bool in_bUp
-)
-{
- size_t nModule = (in_nMenuPos + in_nCursorY) % in_nModuleListSize;
- in_pModuleList[nModule]->change(in_nIndexX,in_nCursorY,in_bUp);
-}
-
-void CreateChars()
-{
- //uint8_t k_aUp[] = { 0x4,0xe,0x1f,0x15,0x4,0x4,0x4,0x4 };
- //uint8_t k_aDown[] = { 0x4,0x4,0x4,0x4,0x15,0x1f,0xe,0x4 };
-
- uint8_t k_aUp[] = { 0x0,0x0,0x4,0xe,0x1f,0x0,0x0 };
- uint8_t k_aDown[] = { 0x0,0x0,0x1f,0xe,0x4,0x0,0x0 };
- uint8_t k_aRight[] = { 0x0,0x8,0xc,0xe,0xc,0x8,0x0 };
- uint8_t k_aLeft[] = { 0x0,0x2,0x6,0xe,0x6,0x2,0x0 };
- uint8_t k_aDegree[] = { 0xc,0x12,0x12,0xc,0x0,0x0,0x0,0x0 };
-
- lcd.createChar(eUp,k_aUp);
- lcd.createChar(eDown,k_aDown);
- lcd.createChar(eRight,k_aRight);
- lcd.createChar(eLeft,k_aLeft);
- lcd.createChar(eDegree,k_aDegree);
-}
-
-void Initialise()
-{
- // Spin up RTC
- rtc.mapTime();
-
- // Initialise LCD
- lcd.begin(k_nWidthLCD,k_nHeightLCD);
- CreateChars();
-
- lcd.setCursor(0,0);
- lcd._putc(eUp);
-
- lcd.setCursor(0,1);
- lcd._putc(eDown);
-}
+#include "MenuManager.h"
-void SetCursor(bool in_bCursor,bool in_bBlink)
-{
- if (in_bCursor) lcd.cursor(); else lcd.noCursor();
- if (in_bBlink) lcd.blink(); else lcd.noBlink();
-}
-
-void ShowModules
-(
- Module ** in_pModuleList,
- size_t in_nModuleListSize,
- size_t in_nPos,
- bool in_bRefresh = false
-)
-{
- lcd.setCursor(2,0);
-
- if (in_pModuleList[in_nPos]->canRefresh() || !in_bRefresh) in_pModuleList[in_nPos]->show();
-
- size_t nPos = (in_nPos + 1) % in_nModuleListSize;
- lcd.setCursor(2,1);
-
- if (in_pModuleList[nPos]->canRefresh() || !in_bRefresh) in_pModuleList[nPos]->show();
-}
-
-void ShowTracking(bool in_bShow)
-{
- if (in_bShow)
- {
- lcd.setCursor(1,0);
- lcd._putc(eLeft);
-
- lcd.setCursor(1,1);
- lcd._putc(eRight);
- }
- else
- {
- lcd.setCursor(1,0);
- lcd._putc(' ');
-
- lcd.setCursor(1,1);
- lcd._putc(' ');
- }
-}
-
-void UpdateDisplay
-(
- Module ** in_pModuleList,
- size_t in_nModuleListSize,
- size_t in_nMenuPos,
- int & inout_nIndexX,
- int in_nCursorX,
- int & inout_nCursorY
-)
-{
- ShowModules(in_pModuleList,in_nModuleListSize,in_nMenuPos);
-
- size_t nCurrent = (in_nMenuPos + inout_nCursorY) % in_nModuleListSize;
- inout_nIndexX = in_pModuleList[nCurrent]->setCursor(inout_nIndexX,in_nCursorX,inout_nCursorY);
-
- // If we didn't have anything, move the line
- if (-1 == inout_nIndexX)
- {
- inout_nCursorY = (inout_nCursorY + 1) % k_nHeightLCD;
-
- nCurrent = (in_nMenuPos + inout_nCursorY) % in_nModuleListSize;
- inout_nIndexX = in_pModuleList[nCurrent]->setCursor(0,in_nCursorX,inout_nCursorY);
- }
-
- // If nothing to show - hide everything
- if (-1 == inout_nIndexX) SetCursor(false,false);
-}
+//Serial cPC(SERIAL_TX, SERIAL_RX);
int main()
-{
- pc.printf("\r\nInitialise LCD\r\n");
+{
+ MCP23017 cMCP23017 = MCP23017(I2C_SDA, I2C_SCL, 0x40, true);
+ Adafruit_RGBLCDShield cLCD(cMCP23017);
+ RTclock cRTclock(I2C_SDA, I2C_SCL);
+
+ // Spin up RTC
+ cRTclock.mapTime();
- Initialise();
-
- EModes eMode = eModeMenu;
- size_t nMenuPos = 0;
-
// Set up display modules
Module * aModules[] =
{
- new TitleModule(lcd),
- new TimeModule(lcd,rtc),
- new DateModule(lcd,rtc),
- new TempModule(lcd)
+ new TitleModule(cLCD),
+ new TimeModule(cLCD,cRTclock),
+ new DateModule(cLCD,cRTclock),
+ new TempModule(cLCD),
};
-
- ShowModules(aModules,_countof(aModules),nMenuPos);
-
- int nIndexX = 0;
- int nCursorY = 0;
-
- while (true)
- {
- uint8_t nKeys = lcd.readButtons();
- if (nKeys)
- {
- // Change mode based on select
- if (nKeys & BUTTON_SELECT)
- {
- eMode = (EModes)((eMode + 1) % eModeLast);
-
- // Start at top corner
- if (eModeSelect == eMode)
- {
- nIndexX = 0;
- nCursorY = 0;
- }
- }
- switch (eMode)
- {
- case eModeMenu:
- SetCursor(false,false);
- ShowTracking(false);
-
- if (nKeys & BUTTON_UP) nMenuPos--;
- if (nKeys & BUTTON_DOWN) nMenuPos++;
-
- nMenuPos = nMenuPos % _countof(aModules);
- break;
-
- case eModeSelect:
- SetCursor(true,false);
- ShowTracking(true);
-
- if (nCursorY > 0 && (nKeys & BUTTON_UP)) nCursorY--;
- if ((nCursorY < k_nHeightLCD - 1) && (nKeys & BUTTON_DOWN)) nCursorY++;
-
- if (nKeys & BUTTON_LEFT) nIndexX--;
- if (nKeys & BUTTON_RIGHT) nIndexX++;
- break;
-
- case eModeChange:
- SetCursor(false,true);
- ShowTracking(true);
-
- if (nKeys & (BUTTON_UP | BUTTON_DOWN))
- {
- bool bUp = (nKeys & BUTTON_UP) ? true : false;
- ChangeModule(aModules,_countof(aModules),nMenuPos,nIndexX,nCursorY,bUp);
- }
-
- if (nKeys & BUTTON_LEFT) nIndexX--;
- if (nKeys & BUTTON_RIGHT) nIndexX++;
- break;
- }
-
- UpdateDisplay(aModules,_countof(aModules),nMenuPos,nIndexX,2,nCursorY);
- }
- else
- {
- switch (eMode)
- {
- case eModeMenu:
- ShowModules(aModules,_countof(aModules),nMenuPos,true);
- aModules[nMenuPos]->setCursor(nIndexX,2,nCursorY);
- break;
- }
- }
-
- wait(0.2);
- }
+ // Set up the menu manager
+ MenuManager cMenuManager(aModules,_countof(aModules),cLCD);
+
+ // Start menu manager loop
+ cMenuManager.loop();
}
--- a/time_helper.cpp Sun Aug 10 16:01:43 2014 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,18 +0,0 @@
-#include "mbed.h"
-#include "time_helper.h"
-
-void GetTime(tm & out_sTM)
-{
- time_t nTime = time(0);
- tm * pTM = ::localtime(&nTime);
- ::memcpy(&out_sTM,pTM,sizeof(out_sTM));
-}
-
-void SetTime(const tm & in_sTM)
-{
- tm sTM = { 0 };
- memcpy(&sTM,&in_sTM,sizeof(sTM));
-
- time_t nTime = mktime(&sTM);
- set_time(nTime);
-}
--- a/time_helper.h Sun Aug 10 16:01:43 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -#ifndef __TIME_HELPER_H__ -#define __TIME_HELPER_H__ - -#include <time.h> - -void GetTime(tm & out_sTM); -void SetTime(const tm & in_sTM); - -#endif /* __TIME_HELPER_H__ */ \ No newline at end of file
