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!
Diff: main.cpp
- Revision:
- 9:1501fb01ded6
- Parent:
- 7:d087e901b74b
- Child:
- 10:3fcab08717fc
--- a/main.cpp Sun Aug 03 15:17:16 2014 +0000 +++ b/main.cpp Mon Aug 04 11:38:13 2014 +0000 @@ -1,5 +1,4 @@ #include "mbed.h" - #include "Adafruit_RGBLCDShield.h" //#include "MCP23017.h" @@ -7,29 +6,61 @@ MCP23017 mcp23017 = MCP23017(I2C_SDA, I2C_SCL, 0x40); Adafruit_RGBLCDShield lcd(mcp23017); + +enum EExtraChars +{ + eUp = 0, + eDown, + eDegree +}; + +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_aDegree[] = { 0xc,0x12,0x12,0xc,0x0,0x0,0x0,0x0 }; + + lcd.createChar(eUp,k_aUp); + lcd.createChar(eDown,k_aDown); + lcd.createChar(eDegree,k_aDegree); +} + +void Initialise() +{ + lcd.begin(16,2); + CreateChars(); + + lcd.setCursor(0,0); + lcd._putc(eUp); + + lcd.setCursor(0,1); + lcd._putc(eDown); +} // Allows to set the backlight, if the LCD backpack is used void SetBacklight(unsigned char status) { - pc.printf("Backlight: %i\n\r", status); + pc.printf("Backlight: %i\r\n", status); mcp23017.digitalWrite(8, (~(status >> 2) & 0x1)); mcp23017.digitalWrite(7, (~(status >> 1) & 0x1)); mcp23017.digitalWrite(6, (~status & 0x1)); } -void loop() +uint8_t CheckKeys() { - static int count = 0; + static uint8_t lastButtons = lcd.readButtons(); uint8_t buttons = lcd.readButtons(); if (buttons) { - count = 0; - - lcd.clear(); - lcd.setCursor(0,0); + if (buttons != lastButtons) + { + lastButtons = buttons; + } + + lcd.setCursor(2,1); if (buttons & BUTTON_UP) { @@ -65,24 +96,78 @@ lcd.printf("SELECT "); } + + lcd.printf(" "); } - else - { - lcd.setCursor(0,1); - lcd.printf("Waiting... %i",count++); - } + + return buttons; +} + +void ShowTemp(int in_nTemp) +{ + lcd.setCursor(2,0); + lcd.printf("Room: %i%cC ",in_nTemp,eDegree); +} + +void SetTime +( + uint8_t in_nHour, + uint8_t in_nMin, + uint8_t in_nDay, + uint8_t in_nMonth, + uint16_t in_nYear +) +{ + tm sCurrentTime = { 0 }; + sCurrentTime.tm_year = in_nYear - 1900; + sCurrentTime.tm_mon = in_nMonth - 1; + sCurrentTime.tm_mday = in_nDay; + + sCurrentTime.tm_hour = in_nHour; + sCurrentTime.tm_min = in_nMin; + + time_t nCurrentTime = mktime(&sCurrentTime); + set_time(nCurrentTime); +} + +void ShowTime() +{ + lcd.setCursor(2,1); + + time_t rawtime = time(0); + tm * timeinfo = localtime(&rawtime); + lcd.printf ("%02i:%02i:%02i ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); } int main() -{ - pc.printf("\n\rInitialise LCD\n\r"); - lcd.begin(16,2); +{ + pc.printf("\r\nInitialise LCD\r\n"); + + Initialise(); - lcd.printf("AdaFruit RGB LCD Shield"); - pc.printf("\n\rEntering key tracking loop\n\r"); + SetTime(11,48,4,8,2014); + + int nTemp = 28; + ShowTemp(nTemp); + + time_t nLast = 0; while (true) { - loop(); + if (time(0) - nLast > 3) + { + ShowTime(); + } + + uint8_t nKeys = CheckKeys(); + if (nKeys) + { + if (nKeys & BUTTON_UP) nTemp++; + if (nKeys & BUTTON_DOWN) nTemp--; + + ShowTemp(nTemp); + nLast = time(0); + } + wait(0.2); } }