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 30 16:30:27 2014 +0000
Parent:
19:72c02acb601d
Child:
21:c44cfd3259c0
Commit message:
Updated with temp sensor support.

Changed in this revision

AdaFruit_RGBLCDShield.lib Show annotated file Show diff for this revision Revisions of this file
MenuManager.cpp Show annotated file Show diff for this revision Revisions of this file
MenuManager.h Show annotated file Show diff for this revision Revisions of this file
Modules/TempModule.cpp Show annotated file Show diff for this revision Revisions of this file
Modules/TempModule.h Show annotated file Show diff for this revision Revisions of this file
RTclock.lib 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.lib	Thu Aug 14 12:06:47 2014 +0000
+++ b/AdaFruit_RGBLCDShield.lib	Sat Aug 30 16:30:27 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/vtraveller/code/AdaFruit_RGBLCDShield/#24ab601221e2
+http://mbed.org/users/vtraveller/code/AdaFruit_RGBLCDShield/#01f8491ca9a5
--- a/MenuManager.cpp	Thu Aug 14 12:06:47 2014 +0000
+++ b/MenuManager.cpp	Sat Aug 30 16:30:27 2014 +0000
@@ -9,7 +9,9 @@
 (
     Module **               in_pModules,
     size_t                  in_nModules,
-    Adafruit_RGBLCDShield & in_cLCD
+    Adafruit_RGBLCDShield & in_cLCD,
+    int                     in_nColumns,
+    int                     in_nRows
 )
     : m_pModules(in_pModules)
     , m_nModules(in_nModules)
@@ -19,6 +21,8 @@
     , m_nIndex(0)
     , m_nCursorX(0)
     , m_nCursorY(0)
+    , m_nColumns(in_nColumns)
+    , m_nRows(in_nRows)    
 {
 }
 
@@ -49,7 +53,7 @@
 void MenuManager::initialise()
 {    
     // Initialise LCD
-    m_cLCD.begin(16,2);
+    m_cLCD.begin(m_nColumns,m_nRows);
     createChars();
 
     m_cLCD.setCursor(0,0);
--- a/MenuManager.h	Thu Aug 14 12:06:47 2014 +0000
+++ b/MenuManager.h	Sat Aug 30 16:30:27 2014 +0000
@@ -8,7 +8,9 @@
     (
         Module **               in_pModules,
         size_t                  in_nModules,
-        Adafruit_RGBLCDShield & in_cLCD
+        Adafruit_RGBLCDShield & in_cLCD,
+        int                     in_nCols,
+        int                     in_nRows
     );    
     void loop();
 
@@ -35,6 +37,7 @@
     size_t                  m_nMenuPos;
     size_t                  m_nIndex;
     int                     m_nCursorX, m_nCursorY;
+    int                     m_nColumns, m_nRows;
 };
 
 #endif /* __MENUMANAGER_H__ */
--- a/Modules/TempModule.cpp	Thu Aug 14 12:06:47 2014 +0000
+++ b/Modules/TempModule.cpp	Sat Aug 30 16:30:27 2014 +0000
@@ -3,49 +3,91 @@
 
 #include "extra_chars.h"
 
+#define MCP9808_I2CADDR_DEFAULT        0x30
+#define MCP9808_REG_CONFIG             0x01
+
+#define MCP9808_REG_CONFIG_SHUTDOWN    0x0100
+#define MCP9808_REG_CONFIG_CRITLOCKED  0x0080
+#define MCP9808_REG_CONFIG_WINLOCKED   0x0040
+#define MCP9808_REG_CONFIG_INTCLR      0x0020
+#define MCP9808_REG_CONFIG_ALERTSTAT   0x0010
+#define MCP9808_REG_CONFIG_ALERTCTRL   0x0008
+#define MCP9808_REG_CONFIG_ALERTSEL    0x0002
+#define MCP9808_REG_CONFIG_ALERTPOL    0x0002
+#define MCP9808_REG_CONFIG_ALERTMODE   0x0001
+
+#define MCP9808_REG_UPPER_TEMP         0x02
+#define MCP9808_REG_LOWER_TEMP         0x03
+#define MCP9808_REG_CRIT_TEMP          0x04
+#define MCP9808_REG_AMBIENT_TEMP       0x05
+#define MCP9808_REG_MANUF_ID           0x06
+#define MCP9808_REG_DEVICE_ID          0x07
+
 TempModule::TempModule(Serial & in_cDisplay)
     : Module(in_cDisplay)
-    , m_nTemp(28)
+    , m_cI2C(D3, D6) //I2C_SDA, I2C_SCL)    
+    , m_nAddress(MCP9808_I2CADDR_DEFAULT)
 {
+    uint16_t nManufacturer = read16(MCP9808_REG_MANUF_ID);// != 0x0054) return false;
+    uint16_t nDeviceId = read16(MCP9808_REG_DEVICE_ID);//
+    
+    // Check to see if we need to autoscan for our device
+    bool bAutoScan = (0x0054 != nManufacturer || 0x0400 != nDeviceId);
+    if (bAutoScan) m_nAddress = 0;
+    
+    while (bAutoScan && m_nAddress < 0x80)
+    {
+        nManufacturer = read16(MCP9808_REG_MANUF_ID);// != 0x0054) return false;
+        nDeviceId = read16(MCP9808_REG_DEVICE_ID);//
+        
+        bAutoScan = (0x0054 != nManufacturer || 0x0400 != nDeviceId);
+        m_nAddress++;
+    }
 }
 
 TempModule::~TempModule()
 {
 }
 
-void TempModule::change
-(
-    size_t      in_nIndex,
-    bool        in_bUp
-)
+float TempModule::readTempC()
 {
-    enum ETemp
-    {
-        eTens = 0,
-        eSingles = 1,
-    };
+    uint16_t t = read16(MCP9808_REG_AMBIENT_TEMP);
+    
+    float temp = t & 0x0FFF;
+    temp /=  16.0f;
+    
+    // check to see if signed bit set
+    if (t & 0x1000) temp -= 0xff;
     
-    switch (in_nIndex)
-    {
-        case eTens: m_nTemp += (in_bUp ? 1 : -1) * 10;  break;
-        case eSingles: m_nTemp += (in_bUp ? 1 : -1); break;
-    }
-    
-    if (m_nTemp > 40) m_nTemp = 40;
-    if (m_nTemp < -40) m_nTemp = -40;
+    return temp;
+}
+
+void TempModule::write16(uint8_t in_nRegister, uint16_t in_nValue)
+{
+    char aBuffer[3] = { in_nRegister, in_nValue >> 8, in_nValue & 0xFF };
+    m_cI2C.write(MCP9808_I2CADDR_DEFAULT, aBuffer, sizeof(aBuffer));
 }
 
-int TempModule::getCursorOffset(size_t & inout_nIndex)
+uint16_t TempModule::read16(uint8_t in_nRegister)
 {
-    const int k_aCursor[] = { 6, 7 };
+    m_cI2C.write(m_nAddress, (const char *)&in_nRegister, sizeof(in_nRegister));
     
-    if ((int)inout_nIndex < 0) inout_nIndex = 0;
-    if (inout_nIndex >= _countof(k_aCursor)) inout_nIndex = _countof(k_aCursor) - 1;
-       
-    return k_aCursor[inout_nIndex];
+    char aReadBuffer[2];
+    m_cI2C.read(m_nAddress, aReadBuffer, sizeof(aReadBuffer));  
+    
+    return (aReadBuffer[0] << 8) | aReadBuffer[1];
 }
 
-void TempModule::show(bool /*in_bRefresh*/)
+void TempModule::show(bool in_bRefresh)
 {
-    m_cDisplay.printf("Room: %i%cC     ",m_nTemp,eDegree);
+    static time_t nTime = time(NULL) + 1;
+    if (in_bRefresh || nTime < time(NULL))
+    {
+        m_cDisplay.printf("Room: %0.1f%cC     ",readTempC(),eDegree);
+        
+//        uint8_t nValue = read16(MCP9808_REG_MANUF_ID);        
+//        m_cDisplay.printf("%x - %x - %x          ", m_nAddress, nValue,read16(MCP9808_REG_DEVICE_ID));
+        
+        nTime = time(NULL) + 1;
+    }
 }
--- a/Modules/TempModule.h	Thu Aug 14 12:06:47 2014 +0000
+++ b/Modules/TempModule.h	Sat Aug 30 16:30:27 2014 +0000
@@ -1,6 +1,7 @@
 #ifndef __TEMPMODULE_H__
 #define __TEMPMODULE_H__
 
+#include "mbed.h"
 #include "module.h"
 
 class TempModule
@@ -10,16 +11,16 @@
     TempModule(Serial & in_cDisplay);
     virtual ~TempModule();
     
-    virtual void    change
-                    (
-                        size_t      in_nIndex,
-                        bool        in_bUp
-                    );
-    virtual int     getCursorOffset(size_t & inout_nIndex);
+    virtual bool    canRefresh() { return true; }
     virtual void    show(bool in_bRefresh);
 
 protected:
-    int     m_nTemp;
+    float       readTempC();
+    uint16_t    read16(uint8_t in_nRegister);
+    void        write16(uint8_t in_nRegister, uint16_t in_nValue);
+
+    I2C         m_cI2C;
+    uint8_t     m_nAddress;
 };
 
 #endif /* __TEMPMODULE_H__ */
--- a/RTclock.lib	Thu Aug 14 12:06:47 2014 +0000
+++ b/RTclock.lib	Sat Aug 30 16:30:27 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/vtraveller/code/RTclock/#2192f2809f59
+http://mbed.org/users/vtraveller/code/RTclock/#3a0ba8364ef2
--- a/main.cpp	Thu Aug 14 12:06:47 2014 +0000
+++ b/main.cpp	Sat Aug 30 16:30:27 2014 +0000
@@ -16,9 +16,12 @@
 int main()
 {    
     MCP23017 cMCP23017 = MCP23017(I2C_SDA, I2C_SCL, 0x40, true);
-    Adafruit_RGBLCDShield cLCD(cMCP23017);
+    //MCP23017 cMCP23017 = MCP23017(I2C_SDA, I2C_SCL, 0x4E, true);
     
-    RTclock cClock(I2C_SDA, I2C_SCL);
+    Adafruit_RGBLCDShield cLCD(cMCP23017);
+    //Adafruit_RGBLCDShield cLCD(cMCP23017,1<<0,1<<1, 1<< 2, 1<<4, 1<<5, 1<<6, 1<<7);
+    
+    RTclock cClock(I2C_SDA, I2C_SCL, RTclock::eDS3231);
     //RTclock cClock(D3, D6);
 
     // Spin up RTC
@@ -27,15 +30,16 @@
     // Set up display modules    
     Module * aModules[] =
     {
-        new TitleModule(cLCD),
+        new TempModule(cLCD),
         new TimeModule(cLCD,cClock),
-        new DateModule(cLCD,cClock),
-        new TempModule(cLCD),
+        new DateModule(cLCD,cClock),        
         new SyncModule(cLCD,cClock),
+        new TitleModule(cLCD),
     };
 
     // Set up the menu manager
-    MenuManager cMenuManager(aModules,_countof(aModules),cLCD);
+    MenuManager cMenuManager(aModules,_countof(aModules),cLCD,16,2);
+    //MenuManager cMenuManager(aModules,_countof(aModules),cLCD,20,4);
 
     // Start menu manager loop    
     cMenuManager.loop();