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:
Sun Aug 10 12:34:44 2014 +0000
Revision:
10:3fcab08717fc
Parent:
9:1501fb01ded6
Child:
11:96146db429de
Added module system.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:88d87b1c1f8b 1 #include "mbed.h"
vtraveller 4:d70e37f6c6bd 2 #include "Adafruit_RGBLCDShield.h"
vtraveller 10:3fcab08717fc 3 #include "RTclock.h"
vtraveller 10:3fcab08717fc 4 #include "extra_chars.h"
vtraveller 10:3fcab08717fc 5
vtraveller 10:3fcab08717fc 6 #include "module.h"
vtraveller 10:3fcab08717fc 7 #include "DateModule.h"
vtraveller 10:3fcab08717fc 8 #include "TempModule.h"
vtraveller 10:3fcab08717fc 9 #include "TimeModule.h"
vtraveller 10:3fcab08717fc 10 #include "TitleModule.h"
wim 0:88d87b1c1f8b 11
vtraveller 3:ed09f95739df 12 Serial pc(SERIAL_TX, SERIAL_RX);
wim 0:88d87b1c1f8b 13
vtraveller 10:3fcab08717fc 14 MCP23017 mcp23017 = MCP23017(I2C_SDA, I2C_SCL, 0x40, true);
vtraveller 7:d087e901b74b 15 Adafruit_RGBLCDShield lcd(mcp23017);
vtraveller 10:3fcab08717fc 16 RTclock rtc(I2C_SDA, I2C_SCL);
vtraveller 10:3fcab08717fc 17
vtraveller 10:3fcab08717fc 18 const int k_nWidthLCD = 16;
vtraveller 10:3fcab08717fc 19 const int k_nHeightLCD = 2;
vtraveller 9:1501fb01ded6 20
vtraveller 10:3fcab08717fc 21 enum EModes
vtraveller 9:1501fb01ded6 22 {
vtraveller 10:3fcab08717fc 23 eModeMenu = 0,
vtraveller 10:3fcab08717fc 24 eModeSelect,
vtraveller 10:3fcab08717fc 25 eModeChange,
vtraveller 10:3fcab08717fc 26 eModeLast
vtraveller 9:1501fb01ded6 27 };
vtraveller 9:1501fb01ded6 28
vtraveller 9:1501fb01ded6 29 void CreateChars()
vtraveller 9:1501fb01ded6 30 {
vtraveller 10:3fcab08717fc 31 //uint8_t k_aUp[] = { 0x4,0xe,0x1f,0x15,0x4,0x4,0x4,0x4 };
vtraveller 10:3fcab08717fc 32 //uint8_t k_aDown[] = { 0x4,0x4,0x4,0x4,0x15,0x1f,0xe,0x4 };
vtraveller 10:3fcab08717fc 33
vtraveller 10:3fcab08717fc 34 uint8_t k_aUp[] = { 0x0,0x0,0x4,0xe,0x1f,0x0,0x0 };
vtraveller 10:3fcab08717fc 35 uint8_t k_aDown[] = { 0x0,0x0,0x1f,0xe,0x4,0x0,0x0 };
vtraveller 10:3fcab08717fc 36 uint8_t k_aRight[] = { 0x0,0x8,0xc,0xe,0xc,0x8,0x0 };
vtraveller 10:3fcab08717fc 37 uint8_t k_aLeft[] = { 0x0,0x2,0x6,0xe,0x6,0x2,0x0 };
vtraveller 9:1501fb01ded6 38 uint8_t k_aDegree[] = { 0xc,0x12,0x12,0xc,0x0,0x0,0x0,0x0 };
vtraveller 9:1501fb01ded6 39
vtraveller 9:1501fb01ded6 40 lcd.createChar(eUp,k_aUp);
vtraveller 9:1501fb01ded6 41 lcd.createChar(eDown,k_aDown);
vtraveller 10:3fcab08717fc 42 lcd.createChar(eRight,k_aRight);
vtraveller 10:3fcab08717fc 43 lcd.createChar(eLeft,k_aLeft);
vtraveller 9:1501fb01ded6 44 lcd.createChar(eDegree,k_aDegree);
vtraveller 9:1501fb01ded6 45 }
vtraveller 9:1501fb01ded6 46
vtraveller 9:1501fb01ded6 47 void Initialise()
vtraveller 9:1501fb01ded6 48 {
vtraveller 10:3fcab08717fc 49 // Spin up RTC
vtraveller 10:3fcab08717fc 50 rtc.mapTime();
vtraveller 10:3fcab08717fc 51
vtraveller 10:3fcab08717fc 52 // Initialise LCD
vtraveller 10:3fcab08717fc 53 lcd.begin(k_nWidthLCD,k_nHeightLCD);
vtraveller 9:1501fb01ded6 54 CreateChars();
vtraveller 9:1501fb01ded6 55
vtraveller 9:1501fb01ded6 56 lcd.setCursor(0,0);
vtraveller 9:1501fb01ded6 57 lcd._putc(eUp);
vtraveller 9:1501fb01ded6 58
vtraveller 9:1501fb01ded6 59 lcd.setCursor(0,1);
vtraveller 9:1501fb01ded6 60 lcd._putc(eDown);
vtraveller 9:1501fb01ded6 61 }
vtraveller 10:3fcab08717fc 62
vtraveller 10:3fcab08717fc 63 void SetCursor(bool in_bCursor,bool in_bBlink)
vtraveller 3:ed09f95739df 64 {
vtraveller 10:3fcab08717fc 65 if (in_bCursor) lcd.cursor(); else lcd.noCursor();
vtraveller 10:3fcab08717fc 66 if (in_bBlink) lcd.blink(); else lcd.noBlink();
vtraveller 10:3fcab08717fc 67 }
vtraveller 10:3fcab08717fc 68
vtraveller 10:3fcab08717fc 69 void ShowModules
vtraveller 10:3fcab08717fc 70 (
vtraveller 10:3fcab08717fc 71 Module ** in_pModuleList,
vtraveller 10:3fcab08717fc 72 size_t in_nModuleListSize,
vtraveller 10:3fcab08717fc 73 size_t in_nPos,
vtraveller 10:3fcab08717fc 74 bool in_bRefresh = false
vtraveller 10:3fcab08717fc 75 )
vtraveller 10:3fcab08717fc 76 {
vtraveller 10:3fcab08717fc 77 lcd.setCursor(2,0);
vtraveller 3:ed09f95739df 78
vtraveller 10:3fcab08717fc 79 if (in_pModuleList[in_nPos]->canRefresh() || !in_bRefresh) in_pModuleList[in_nPos]->show();
vtraveller 10:3fcab08717fc 80
vtraveller 10:3fcab08717fc 81 size_t nPos = (in_nPos + 1) % in_nModuleListSize;
vtraveller 10:3fcab08717fc 82 lcd.setCursor(2,1);
vtraveller 10:3fcab08717fc 83
vtraveller 10:3fcab08717fc 84 if (in_pModuleList[nPos]->canRefresh() || !in_bRefresh) in_pModuleList[nPos]->show();
vtraveller 3:ed09f95739df 85 }
wim 0:88d87b1c1f8b 86
vtraveller 10:3fcab08717fc 87 void ShowTracking(bool in_bShow)
vtraveller 3:ed09f95739df 88 {
vtraveller 10:3fcab08717fc 89 if (in_bShow)
vtraveller 4:d70e37f6c6bd 90 {
vtraveller 10:3fcab08717fc 91 lcd.setCursor(1,0);
vtraveller 10:3fcab08717fc 92 lcd._putc(eLeft);
vtraveller 4:d70e37f6c6bd 93
vtraveller 10:3fcab08717fc 94 lcd.setCursor(1,1);
vtraveller 10:3fcab08717fc 95 lcd._putc(eRight);
vtraveller 10:3fcab08717fc 96 }
vtraveller 10:3fcab08717fc 97 else
vtraveller 10:3fcab08717fc 98 {
vtraveller 10:3fcab08717fc 99 lcd.setCursor(1,0);
vtraveller 10:3fcab08717fc 100 lcd._putc(' ');
vtraveller 4:d70e37f6c6bd 101
vtraveller 10:3fcab08717fc 102 lcd.setCursor(1,1);
vtraveller 10:3fcab08717fc 103 lcd._putc(' ');
vtraveller 10:3fcab08717fc 104 }
vtraveller 10:3fcab08717fc 105 }
vtraveller 10:3fcab08717fc 106
vtraveller 10:3fcab08717fc 107 void UpdateDisplay
vtraveller 10:3fcab08717fc 108 (
vtraveller 10:3fcab08717fc 109 Module ** in_pModuleList,
vtraveller 10:3fcab08717fc 110 size_t in_nModuleListSize,
vtraveller 10:3fcab08717fc 111 size_t in_nMenuPos,
vtraveller 10:3fcab08717fc 112 int & inout_nIndexX,
vtraveller 10:3fcab08717fc 113 int in_nCursorX,
vtraveller 10:3fcab08717fc 114 int & inout_nCursorY
vtraveller 10:3fcab08717fc 115 )
vtraveller 10:3fcab08717fc 116 {
vtraveller 10:3fcab08717fc 117 ShowModules(in_pModuleList,in_nModuleListSize,in_nMenuPos);
vtraveller 10:3fcab08717fc 118
vtraveller 10:3fcab08717fc 119 size_t nCurrent = (in_nMenuPos + inout_nCursorY) % in_nModuleListSize;
vtraveller 10:3fcab08717fc 120 inout_nIndexX = in_pModuleList[nCurrent]->setCursor(inout_nIndexX,in_nCursorX,inout_nCursorY);
vtraveller 10:3fcab08717fc 121
vtraveller 10:3fcab08717fc 122 // If we didn't have anything, move the line
vtraveller 10:3fcab08717fc 123 if (-1 == inout_nIndexX)
vtraveller 10:3fcab08717fc 124 {
vtraveller 10:3fcab08717fc 125 inout_nCursorY = (inout_nCursorY + 1) % k_nHeightLCD;
vtraveller 4:d70e37f6c6bd 126
vtraveller 10:3fcab08717fc 127 nCurrent = (in_nMenuPos + inout_nCursorY) % in_nModuleListSize;
vtraveller 10:3fcab08717fc 128 inout_nIndexX = in_pModuleList[nCurrent]->setCursor(0,in_nCursorX,inout_nCursorY);
vtraveller 4:d70e37f6c6bd 129 }
vtraveller 9:1501fb01ded6 130
vtraveller 10:3fcab08717fc 131 // If nothing to show - hide everything
vtraveller 10:3fcab08717fc 132 if (-1 == inout_nIndexX) SetCursor(false,false);
vtraveller 3:ed09f95739df 133 }
vtraveller 3:ed09f95739df 134
vtraveller 3:ed09f95739df 135 int main()
vtraveller 9:1501fb01ded6 136 {
vtraveller 9:1501fb01ded6 137 pc.printf("\r\nInitialise LCD\r\n");
vtraveller 9:1501fb01ded6 138
vtraveller 10:3fcab08717fc 139 Initialise();
vtraveller 10:3fcab08717fc 140
vtraveller 10:3fcab08717fc 141 EModes eMode = eModeMenu;
vtraveller 10:3fcab08717fc 142 size_t nMenuPos = 0;
vtraveller 7:d087e901b74b 143
vtraveller 10:3fcab08717fc 144 // Set up display modules
vtraveller 10:3fcab08717fc 145 Module * aModules[] =
vtraveller 10:3fcab08717fc 146 {
vtraveller 10:3fcab08717fc 147 new TitleModule(lcd),
vtraveller 10:3fcab08717fc 148 new TimeModule(lcd,rtc),
vtraveller 10:3fcab08717fc 149 new DateModule(lcd,rtc),
vtraveller 10:3fcab08717fc 150 new TempModule(lcd)
vtraveller 10:3fcab08717fc 151 };
vtraveller 9:1501fb01ded6 152
vtraveller 10:3fcab08717fc 153 ShowModules(aModules,_countof(aModules),nMenuPos);
vtraveller 10:3fcab08717fc 154
vtraveller 10:3fcab08717fc 155 int nIndexX = 0;
vtraveller 10:3fcab08717fc 156 int nCursorY = 0;
vtraveller 10:3fcab08717fc 157
vtraveller 4:d70e37f6c6bd 158 while (true)
vtraveller 4:d70e37f6c6bd 159 {
vtraveller 10:3fcab08717fc 160 uint8_t nKeys = lcd.readButtons();
vtraveller 9:1501fb01ded6 161 if (nKeys)
vtraveller 9:1501fb01ded6 162 {
vtraveller 10:3fcab08717fc 163 // Change mode based on select
vtraveller 10:3fcab08717fc 164 if (nKeys & BUTTON_SELECT)
vtraveller 10:3fcab08717fc 165 {
vtraveller 10:3fcab08717fc 166 eMode = (EModes)((eMode + 1) % eModeLast);
vtraveller 10:3fcab08717fc 167
vtraveller 10:3fcab08717fc 168 // Start at top corner
vtraveller 10:3fcab08717fc 169 if (eModeSelect == eMode)
vtraveller 10:3fcab08717fc 170 {
vtraveller 10:3fcab08717fc 171 nIndexX = 0;
vtraveller 10:3fcab08717fc 172 nCursorY = 0;
vtraveller 10:3fcab08717fc 173 }
vtraveller 10:3fcab08717fc 174 }
vtraveller 10:3fcab08717fc 175
vtraveller 10:3fcab08717fc 176 switch (eMode)
vtraveller 10:3fcab08717fc 177 {
vtraveller 10:3fcab08717fc 178 case eModeMenu:
vtraveller 10:3fcab08717fc 179 SetCursor(false,false);
vtraveller 10:3fcab08717fc 180 ShowTracking(false);
vtraveller 10:3fcab08717fc 181
vtraveller 10:3fcab08717fc 182 if (nKeys & BUTTON_UP) nMenuPos--;
vtraveller 10:3fcab08717fc 183 if (nKeys & BUTTON_DOWN) nMenuPos++;
vtraveller 10:3fcab08717fc 184
vtraveller 10:3fcab08717fc 185 nMenuPos = nMenuPos % _countof(aModules);
vtraveller 10:3fcab08717fc 186 break;
vtraveller 10:3fcab08717fc 187
vtraveller 10:3fcab08717fc 188 case eModeSelect:
vtraveller 10:3fcab08717fc 189 SetCursor(true,false);
vtraveller 10:3fcab08717fc 190 ShowTracking(true);
vtraveller 10:3fcab08717fc 191
vtraveller 10:3fcab08717fc 192 if (nCursorY > 0 && (nKeys & BUTTON_UP)) nCursorY--;
vtraveller 10:3fcab08717fc 193 if ((nCursorY < k_nHeightLCD - 1) && (nKeys & BUTTON_DOWN)) nCursorY++;
vtraveller 10:3fcab08717fc 194
vtraveller 10:3fcab08717fc 195 if (nKeys & BUTTON_LEFT) nIndexX--;
vtraveller 10:3fcab08717fc 196 if (nKeys & BUTTON_RIGHT) nIndexX++;
vtraveller 10:3fcab08717fc 197 break;
vtraveller 10:3fcab08717fc 198
vtraveller 10:3fcab08717fc 199 case eModeChange:
vtraveller 10:3fcab08717fc 200 SetCursor(false,true);
vtraveller 10:3fcab08717fc 201 ShowTracking(true);
vtraveller 10:3fcab08717fc 202 break;
vtraveller 10:3fcab08717fc 203 }
vtraveller 9:1501fb01ded6 204
vtraveller 10:3fcab08717fc 205 UpdateDisplay(aModules,_countof(aModules),nMenuPos,nIndexX,2,nCursorY);
vtraveller 9:1501fb01ded6 206 }
vtraveller 10:3fcab08717fc 207 else
vtraveller 10:3fcab08717fc 208 {
vtraveller 10:3fcab08717fc 209 switch (eMode)
vtraveller 10:3fcab08717fc 210 {
vtraveller 10:3fcab08717fc 211 case eModeMenu:
vtraveller 10:3fcab08717fc 212 ShowModules(aModules,_countof(aModules),nMenuPos,true);
vtraveller 10:3fcab08717fc 213 aModules[nMenuPos]->setCursor(nIndexX,2,nCursorY);
vtraveller 10:3fcab08717fc 214 break;
vtraveller 10:3fcab08717fc 215 }
vtraveller 10:3fcab08717fc 216 }
vtraveller 10:3fcab08717fc 217
vtraveller 4:d70e37f6c6bd 218 wait(0.2);
vtraveller 10:3fcab08717fc 219 }
wim 0:88d87b1c1f8b 220 }