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:
Sat Aug 02 16:05:40 2014 +0000
Revision:
6:a6be2aede8f2
Parent:
5:6c9ee7e3a20c
Child:
7:d087e901b74b
Updated with some clean ups

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:88d87b1c1f8b 1 #include "mbed.h"
vtraveller 4:d70e37f6c6bd 2
vtraveller 4:d70e37f6c6bd 3 #include "Adafruit_RGBLCDShield.h"
vtraveller 4:d70e37f6c6bd 4 //#include "MCP23017.h"
wim 0:88d87b1c1f8b 5
vtraveller 3:ed09f95739df 6 Serial pc(SERIAL_TX, SERIAL_RX);
wim 0:88d87b1c1f8b 7
vtraveller 3:ed09f95739df 8 MCP23017 mcp23017 = MCP23017(I2C_SDA, I2C_SCL, 0x40);
vtraveller 6:a6be2aede8f2 9 Adafruit_RGBLCDShield lcd(mcp23017,pc);
vtraveller 3:ed09f95739df 10
vtraveller 3:ed09f95739df 11 // Allows to set the backlight, if the LCD backpack is used
vtraveller 3:ed09f95739df 12 void SetBacklight(unsigned char status)
vtraveller 3:ed09f95739df 13 {
vtraveller 3:ed09f95739df 14 pc.printf("Backlight: %i\n\r", status);
vtraveller 3:ed09f95739df 15
vtraveller 3:ed09f95739df 16 mcp23017.digitalWrite(8, (~(status >> 2) & 0x1));
vtraveller 3:ed09f95739df 17 mcp23017.digitalWrite(7, (~(status >> 1) & 0x1));
vtraveller 3:ed09f95739df 18 mcp23017.digitalWrite(6, (~status & 0x1));
vtraveller 3:ed09f95739df 19 }
wim 0:88d87b1c1f8b 20
vtraveller 4:d70e37f6c6bd 21 void loop()
vtraveller 3:ed09f95739df 22 {
vtraveller 4:d70e37f6c6bd 23 // set the cursor to column 0, line 1
vtraveller 4:d70e37f6c6bd 24 // (note: line 1 is the second row, since counting begins with 0):
vtraveller 6:a6be2aede8f2 25 lcd.setCursor(10, 1);
vtraveller 6:a6be2aede8f2 26
vtraveller 6:a6be2aede8f2 27 //lcd.cursor();
vtraveller 6:a6be2aede8f2 28 //lcd.blink();
vtraveller 4:d70e37f6c6bd 29
vtraveller 4:d70e37f6c6bd 30 uint8_t buttons = lcd.readButtons();
vtraveller 4:d70e37f6c6bd 31
vtraveller 6:a6be2aede8f2 32 lcd.setBacklight(buttons ? 1 : 0);
vtraveller 6:a6be2aede8f2 33
vtraveller 4:d70e37f6c6bd 34 if (buttons)
vtraveller 4:d70e37f6c6bd 35 {
vtraveller 4:d70e37f6c6bd 36 lcd.clear();
vtraveller 4:d70e37f6c6bd 37 lcd.setCursor(0,0);
vtraveller 4:d70e37f6c6bd 38
vtraveller 4:d70e37f6c6bd 39 if (buttons & BUTTON_UP)
vtraveller 4:d70e37f6c6bd 40 {
vtraveller 4:d70e37f6c6bd 41 pc.printf("UP ");
vtraveller 4:d70e37f6c6bd 42
vtraveller 4:d70e37f6c6bd 43 lcd.printf("UP ");
vtraveller 4:d70e37f6c6bd 44 }
vtraveller 4:d70e37f6c6bd 45
vtraveller 4:d70e37f6c6bd 46 if (buttons & BUTTON_DOWN)
vtraveller 4:d70e37f6c6bd 47 {
vtraveller 4:d70e37f6c6bd 48 pc.printf("DOWN ");
vtraveller 4:d70e37f6c6bd 49
vtraveller 4:d70e37f6c6bd 50 lcd.printf("DOWN ");
vtraveller 4:d70e37f6c6bd 51 }
vtraveller 4:d70e37f6c6bd 52
vtraveller 4:d70e37f6c6bd 53 if (buttons & BUTTON_LEFT)
vtraveller 4:d70e37f6c6bd 54 {
vtraveller 4:d70e37f6c6bd 55 pc.printf("LEFT ");
vtraveller 4:d70e37f6c6bd 56
vtraveller 4:d70e37f6c6bd 57 lcd.printf("LEFT ");
vtraveller 4:d70e37f6c6bd 58 }
vtraveller 4:d70e37f6c6bd 59
vtraveller 4:d70e37f6c6bd 60 if (buttons & BUTTON_RIGHT)
vtraveller 4:d70e37f6c6bd 61 {
vtraveller 4:d70e37f6c6bd 62 pc.printf("RIGHT ");
vtraveller 4:d70e37f6c6bd 63
vtraveller 4:d70e37f6c6bd 64 lcd.printf("RIGHT ");
vtraveller 4:d70e37f6c6bd 65 }
vtraveller 4:d70e37f6c6bd 66
vtraveller 4:d70e37f6c6bd 67 if (buttons & BUTTON_SELECT)
vtraveller 4:d70e37f6c6bd 68 {
vtraveller 4:d70e37f6c6bd 69 pc.printf("SELECT ");
vtraveller 4:d70e37f6c6bd 70
vtraveller 4:d70e37f6c6bd 71 lcd.printf("SELECT ");
vtraveller 4:d70e37f6c6bd 72 }
vtraveller 4:d70e37f6c6bd 73 }
vtraveller 6:a6be2aede8f2 74
vtraveller 6:a6be2aede8f2 75 lcd.setCursor(0,0);
vtraveller 6:a6be2aede8f2 76 lcd._putc('A');
vtraveller 6:a6be2aede8f2 77
vtraveller 6:a6be2aede8f2 78 lcd.display();
vtraveller 3:ed09f95739df 79 }
vtraveller 3:ed09f95739df 80
vtraveller 3:ed09f95739df 81 int main()
vtraveller 3:ed09f95739df 82 {
vtraveller 4:d70e37f6c6bd 83 pc.printf("\n\rSTART\n\r");
vtraveller 5:6c9ee7e3a20c 84
vtraveller 5:6c9ee7e3a20c 85 // Take MCP out of reset to show its backlight
vtraveller 6:a6be2aede8f2 86 //pc.printf("mcp23017.config(0,1,0);\n\r");
vtraveller 6:a6be2aede8f2 87 //mcp23017.config(0,1,0);
vtraveller 6:a6be2aede8f2 88 //wait(0.5);
vtraveller 5:6c9ee7e3a20c 89
vtraveller 6:a6be2aede8f2 90 pc.printf("\n\rInitialise LCD\n\r");
vtraveller 6:a6be2aede8f2 91 lcd.begin(16,2);
vtraveller 6:a6be2aede8f2 92
vtraveller 5:6c9ee7e3a20c 93 pc.printf("\n\rEntering key tracking loop\n\r");
vtraveller 4:d70e37f6c6bd 94 while (true)
vtraveller 4:d70e37f6c6bd 95 {
vtraveller 4:d70e37f6c6bd 96 loop();
vtraveller 4:d70e37f6c6bd 97 wait(0.2);
vtraveller 5:6c9ee7e3a20c 98 }
wim 0:88d87b1c1f8b 99 }