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!
main.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 "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); } 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); } int main() { pc.printf("\r\nInitialise LCD\r\n"); 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) }; 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); } }