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!

Files at this revision

API Documentation at this revision

Comitter:
vtraveller
Date:
Sat Aug 02 13:50:40 2014 +0000
Parent:
3:ed09f95739df
Child:
5:6c9ee7e3a20c
Commit message:
Keyboard working version

Changed in this revision

AdaFruit_RGBLCDShield.cpp Show annotated file Show diff for this revision Revisions of this file
Adafruit_RGBLCDShield.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/AdaFruit_RGBLCDShield.cpp	Sat Aug 02 12:39:25 2014 +0000
+++ b/AdaFruit_RGBLCDShield.cpp	Sat Aug 02 13:50:40 2014 +0000
@@ -13,12 +13,23 @@
   BSD license, all text above must be included in any redistribution
  ****************************************************/
 
-
 #include "Adafruit_RGBLCDShield.h"
 
 #include <stdio.h>
 #include <string.h>
 #include <inttypes.h>
+
+// New MBED code
+#define OUTPUT DIR_OUTPUT
+#define INPUT DIR_INPUT
+
+#define LOW 0
+#define HIGH 1
+
+#define delayMicroseconds(a) wait(a / 1000000)
+
+/* Remove Arduino i2c (wire) interface
+
 #include <Wire.h>
 #ifdef __AVR__
  #define WIRE Wire
@@ -31,6 +42,7 @@
 #else
  #include "WProgram.h"
 #endif
+*/
 
 // When the display powers up, it is configured as follows:
 //
@@ -51,7 +63,10 @@
 // can't assume that its in that state when a sketch starts (and the
 // RGBLCDShield constructor is called).
 
-Adafruit_RGBLCDShield::Adafruit_RGBLCDShield() {
+Adafruit_RGBLCDShield::Adafruit_RGBLCDShield(MCP23017 & inMCP)
+    : Serial(SERIAL_TX, SERIAL_RX)
+    , _i2c(inMCP)
+{
   _i2cAddr = 0;
 
   _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
@@ -73,9 +88,6 @@
   // we can't begin() yet :(
 }
 
-
-
-
 void Adafruit_RGBLCDShield::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
              uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
@@ -115,8 +127,8 @@
   // check if i2c
   if (_i2cAddr != 255) {
     //_i2c.begin(_i2cAddr);
-    WIRE.begin();
-    _i2c.begin();
+    //WIRE.begin();
+    //_i2c->begin();
 
     _i2c.pinMode(8, OUTPUT);
     _i2c.pinMode(6, OUTPUT);
@@ -133,8 +145,10 @@
 
     for (uint8_t i=0; i<5; i++) {
       _i2c.pinMode(_button_pins[i], INPUT);
-      _i2c.pullUp(_button_pins[i], 1);
+      //_i2c.pullUp(_button_pins[i], 1);
     }
+    
+    _i2c.internalPullupMask(0xffff);
   }
 
   if (lines > 1) {
@@ -302,7 +316,7 @@
   location &= 0x7; // we only have 8 locations 0-7
   command(LCD_SETCGRAMADDR | (location << 3));
   for (int i=0; i<8; i++) {
-    write(charmap[i]);
+    _putc(charmap[i]);
   }
   command(LCD_SETDDRAMADDR);  // unfortunately resets the location to 0,0
 }
@@ -319,8 +333,10 @@
   return 1;
 }
 #else
-inline void Adafruit_RGBLCDShield::write(uint8_t value) {
+//inline void Adafruit_RGBLCDShield::write(uint8_t value) {
+int Adafruit_RGBLCDShield::_putc(int value) {
   send(value, HIGH);
+  return 1;
 }
 #endif
 
@@ -333,7 +349,8 @@
     _i2c.digitalWrite(p, d);
   } else {
     // straightup IO
-    digitalWrite(p, d);
+    // @@@ TURNED OFF @@@
+    //digitalWrite(p, d);
   }
 }
 
@@ -352,7 +369,8 @@
     _i2c.pinMode(p, d);
   } else {
     // straightup IO
-    pinMode(p, d);
+    //@@@ TURNED OFF @@@
+    //pinMode(p, d);
   }
 }
 
@@ -386,7 +404,8 @@
   if (_i2cAddr != 255) {
     uint16_t out = 0;
 
-    out = _i2c.readGPIOAB();
+    //out = _i2c.readGPIOAB();
+    out = _i2c.digitalWordRead();
 
     // speed up for i2c since its sluggish
     for (int i = 0; i < 4; i++) {
@@ -397,15 +416,21 @@
     // make sure enable is low
     out &= ~(1 << _enable_pin);
 
-    _i2c.writeGPIOAB(out);
+    //_i2c.writeGPIOAB(out);
+    _i2c.digitalWordWrite(out);
 
     // pulse enable
     delayMicroseconds(1);
     out |= (1 << _enable_pin);
-    _i2c.writeGPIOAB(out);
+    //_i2c.writeGPIOAB(out);
+    _i2c.digitalWordWrite(out);
+    
     delayMicroseconds(1);
     out &= ~(1 << _enable_pin);
-    _i2c.writeGPIOAB(out);   
+    
+    //_i2c.writeGPIOAB(out);   
+    _i2c.digitalWordWrite(out);
+    
     delayMicroseconds(100);
 
   } else {
--- a/Adafruit_RGBLCDShield.h	Sat Aug 02 12:39:25 2014 +0000
+++ b/Adafruit_RGBLCDShield.h	Sat Aug 02 13:50:40 2014 +0000
@@ -17,7 +17,7 @@
 #define Adafruit_RGBLCDShield_h
 
 #include <inttypes.h>
-#include "MCP23017.h"
+#include <MCP23017.h>
 
 // commands
 #define LCD_CLEARDISPLAY 0x01
@@ -63,12 +63,12 @@
 #define BUTTON_RIGHT 0x02
 #define BUTTON_SELECT 0x01
 
-
 class Adafruit_RGBLCDShield
 //    : public Print
+    : public Serial
 {
 public:
-  Adafruit_RGBLCDShield();
+  Adafruit_RGBLCDShield(MCP23017 & inMCP);
 
   void init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
         uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
@@ -98,9 +98,10 @@
   void createChar(uint8_t, uint8_t[]);
   void setCursor(uint8_t, uint8_t); 
 #if ARDUINO >= 100
-  virtual size_t write(uint8_t);
+  //virtual size_t write(uint8_t);
 #else
-  virtual void write(uint8_t);
+  //virtual void write(uint8_t);
+  virtual int _putc(int c);
 #endif
   void command(uint8_t);
   uint8_t readButtons();
@@ -127,7 +128,7 @@
   uint8_t _numlines,_currline;
 
   uint8_t _i2cAddr;
-  MCP20317 _i2c;
+  MCP23017 _i2c;
 };
 
 #endif
--- a/main.cpp	Sat Aug 02 12:39:25 2014 +0000
+++ b/main.cpp	Sat Aug 02 13:50:40 2014 +0000
@@ -1,9 +1,12 @@
 #include "mbed.h"
-#include "MCP23017.h"
+
+#include "Adafruit_RGBLCDShield.h"
+//#include "MCP23017.h"
 
 Serial pc(SERIAL_TX, SERIAL_RX);
 
 MCP23017 mcp23017 = MCP23017(I2C_SDA, I2C_SCL, 0x40);
+Adafruit_RGBLCDShield lcd(mcp23017);
 
 #define RED 0x1
 #define YELLOW 0x3
@@ -23,16 +26,64 @@
     mcp23017.digitalWrite(6, (~status & 0x1));
 }
 
-void LED(bool bOn)
+void loop()
 {
-    pc.printf("LED: %s\n\r", bOn ? "On" : "Off");
-    //mcp23017.write(PORT_A, bOn ? 0x10 : 0xFF);
-    //mcp23017.write(PORT_B, bOn ? 0x10 : 0xFF);
+    // set the cursor to column 0, line 1
+    // (note: line 1 is the second row, since counting begins with 0):
+    lcd.setCursor(0, 1);
+        
+    uint8_t buttons = lcd.readButtons();
+    
+    if (buttons)
+    {
+        lcd.clear();
+        lcd.setCursor(0,0);
+        
+        if (buttons & BUTTON_UP)
+        {
+            pc.printf("UP ");
+            
+            lcd.printf("UP ");
+            lcd.setBacklight(RED);
+        }
+        
+        if (buttons & BUTTON_DOWN)
+        {
+            pc.printf("DOWN ");
+            
+            lcd.printf("DOWN ");
+            lcd.setBacklight(YELLOW);
+        }
+        
+        if (buttons & BUTTON_LEFT)
+        {
+            pc.printf("LEFT ");
+            
+            lcd.printf("LEFT ");
+            lcd.setBacklight(GREEN);
+        }
+        
+        if (buttons & BUTTON_RIGHT)
+        {
+            pc.printf("RIGHT ");
+            
+            lcd.printf("RIGHT ");            
+            lcd.setBacklight(TEAL);
+        }
+        
+        if (buttons & BUTTON_SELECT)
+        {
+            pc.printf("SELECT ");
+            
+            lcd.printf("SELECT ");            
+            lcd.setBacklight(VIOLET);
+        }
+    }
 }
 
 int main()
 {    
-    pc.printf("START\n\r");
+    pc.printf("\n\rSTART\n\r");
     
     // I2C init
     //mcp23017.i2c.frequency(400000);
@@ -47,19 +98,30 @@
     mcp23017.config(0,1,0);
     wait(0.5);
     
-    pc.printf("mcp23017.pinMode\n\r");
-    mcp23017.pinMode(8, DIR_OUTPUT);
-    mcp23017.pinMode(7, DIR_OUTPUT);
-    mcp23017.pinMode(6, DIR_OUTPUT);
+    //pc.printf("mcp23017.pinMode\n\r");
+    //mcp23017.pinMode(8, DIR_OUTPUT);
+    //mcp23017.pinMode(7, DIR_OUTPUT);
+    //mcp23017.pinMode(6, DIR_OUTPUT);
     
-    SetBacklight(WHITE);
+    //SetBacklight(WHITE);
 
+    //wait(0.5);
+    //pc.printf("MPC Init done\n\r");           
+        
+    lcd.begin(16,2);
+    lcd.display();
+    
+    lcd._putc('A');
     wait(0.5);
-    pc.printf("MPC Init done\n\r");           
+    
+    lcd.printf("Hello, world!");
+    lcd.setBacklight(GREEN);
     
-    LED(false);
-    LED(true);
+    while (true)
+    {
+        loop();
+        wait(0.2);
+    }
     
-    SetBacklight(RED);
-    pc.printf("FINISHED\n\r");
+    //pc.printf("FINISHED\n\r");
 }