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!
Modules/TempModule.cpp@21:c44cfd3259c0, 2014-09-02 (annotated)
- Committer:
- vtraveller
- Date:
- Tue Sep 02 07:12:23 2014 +0000
- Revision:
- 21:c44cfd3259c0
- Parent:
- 20:93c70a1869ee
Changed to using a shared I2C object
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vtraveller | 10:3fcab08717fc | 1 | #include "mbed.h" |
vtraveller | 10:3fcab08717fc | 2 | #include "TempModule.h" |
vtraveller | 10:3fcab08717fc | 3 | |
vtraveller | 10:3fcab08717fc | 4 | #include "extra_chars.h" |
vtraveller | 10:3fcab08717fc | 5 | |
vtraveller | 21:c44cfd3259c0 | 6 | #define MCP9808_I2CADDR_DEFAULT (0x18 << 1) |
vtraveller | 20:93c70a1869ee | 7 | #define MCP9808_REG_CONFIG 0x01 |
vtraveller | 20:93c70a1869ee | 8 | |
vtraveller | 20:93c70a1869ee | 9 | #define MCP9808_REG_CONFIG_SHUTDOWN 0x0100 |
vtraveller | 20:93c70a1869ee | 10 | #define MCP9808_REG_CONFIG_CRITLOCKED 0x0080 |
vtraveller | 20:93c70a1869ee | 11 | #define MCP9808_REG_CONFIG_WINLOCKED 0x0040 |
vtraveller | 20:93c70a1869ee | 12 | #define MCP9808_REG_CONFIG_INTCLR 0x0020 |
vtraveller | 20:93c70a1869ee | 13 | #define MCP9808_REG_CONFIG_ALERTSTAT 0x0010 |
vtraveller | 20:93c70a1869ee | 14 | #define MCP9808_REG_CONFIG_ALERTCTRL 0x0008 |
vtraveller | 20:93c70a1869ee | 15 | #define MCP9808_REG_CONFIG_ALERTSEL 0x0002 |
vtraveller | 20:93c70a1869ee | 16 | #define MCP9808_REG_CONFIG_ALERTPOL 0x0002 |
vtraveller | 20:93c70a1869ee | 17 | #define MCP9808_REG_CONFIG_ALERTMODE 0x0001 |
vtraveller | 20:93c70a1869ee | 18 | |
vtraveller | 20:93c70a1869ee | 19 | #define MCP9808_REG_UPPER_TEMP 0x02 |
vtraveller | 20:93c70a1869ee | 20 | #define MCP9808_REG_LOWER_TEMP 0x03 |
vtraveller | 20:93c70a1869ee | 21 | #define MCP9808_REG_CRIT_TEMP 0x04 |
vtraveller | 20:93c70a1869ee | 22 | #define MCP9808_REG_AMBIENT_TEMP 0x05 |
vtraveller | 20:93c70a1869ee | 23 | #define MCP9808_REG_MANUF_ID 0x06 |
vtraveller | 20:93c70a1869ee | 24 | #define MCP9808_REG_DEVICE_ID 0x07 |
vtraveller | 20:93c70a1869ee | 25 | |
vtraveller | 21:c44cfd3259c0 | 26 | TempModule::TempModule(Serial & in_cDisplay,I2C & in_nI2C,uint8_t in_nAddress) |
vtraveller | 13:9641bc42db92 | 27 | : Module(in_cDisplay) |
vtraveller | 21:c44cfd3259c0 | 28 | , m_cI2C(in_nI2C) |
vtraveller | 21:c44cfd3259c0 | 29 | , m_nAddress(in_nAddress) |
vtraveller | 21:c44cfd3259c0 | 30 | , m_bValid(false) |
vtraveller | 10:3fcab08717fc | 31 | { |
vtraveller | 20:93c70a1869ee | 32 | uint16_t nManufacturer = read16(MCP9808_REG_MANUF_ID);// != 0x0054) return false; |
vtraveller | 20:93c70a1869ee | 33 | uint16_t nDeviceId = read16(MCP9808_REG_DEVICE_ID);// |
vtraveller | 20:93c70a1869ee | 34 | |
vtraveller | 20:93c70a1869ee | 35 | // Check to see if we need to autoscan for our device |
vtraveller | 21:c44cfd3259c0 | 36 | m_bValid = (0x0054 == nManufacturer || 0x0400 == nDeviceId); |
vtraveller | 21:c44cfd3259c0 | 37 | |
vtraveller | 21:c44cfd3259c0 | 38 | // bool bAutoScan = true; |
vtraveller | 21:c44cfd3259c0 | 39 | // m_nAddress = 0; |
vtraveller | 21:c44cfd3259c0 | 40 | // |
vtraveller | 21:c44cfd3259c0 | 41 | // while (bAutoScan && m_nAddress < 0x80) |
vtraveller | 21:c44cfd3259c0 | 42 | // { |
vtraveller | 21:c44cfd3259c0 | 43 | // nManufacturer = read16(MCP9808_REG_MANUF_ID);// != 0x0054) return false; |
vtraveller | 21:c44cfd3259c0 | 44 | // nDeviceId = read16(MCP9808_REG_DEVICE_ID);// |
vtraveller | 21:c44cfd3259c0 | 45 | // |
vtraveller | 21:c44cfd3259c0 | 46 | // bAutoScan = (0x0054 != nManufacturer || 0x0400 != nDeviceId); |
vtraveller | 21:c44cfd3259c0 | 47 | // m_nAddress++; |
vtraveller | 21:c44cfd3259c0 | 48 | // } |
vtraveller | 10:3fcab08717fc | 49 | } |
vtraveller | 10:3fcab08717fc | 50 | |
vtraveller | 10:3fcab08717fc | 51 | TempModule::~TempModule() |
vtraveller | 10:3fcab08717fc | 52 | { |
vtraveller | 10:3fcab08717fc | 53 | } |
vtraveller | 11:96146db429de | 54 | |
vtraveller | 20:93c70a1869ee | 55 | float TempModule::readTempC() |
vtraveller | 11:96146db429de | 56 | { |
vtraveller | 20:93c70a1869ee | 57 | uint16_t t = read16(MCP9808_REG_AMBIENT_TEMP); |
vtraveller | 20:93c70a1869ee | 58 | |
vtraveller | 20:93c70a1869ee | 59 | float temp = t & 0x0FFF; |
vtraveller | 20:93c70a1869ee | 60 | temp /= 16.0f; |
vtraveller | 20:93c70a1869ee | 61 | |
vtraveller | 20:93c70a1869ee | 62 | // check to see if signed bit set |
vtraveller | 20:93c70a1869ee | 63 | if (t & 0x1000) temp -= 0xff; |
vtraveller | 10:3fcab08717fc | 64 | |
vtraveller | 20:93c70a1869ee | 65 | return temp; |
vtraveller | 20:93c70a1869ee | 66 | } |
vtraveller | 20:93c70a1869ee | 67 | |
vtraveller | 20:93c70a1869ee | 68 | void TempModule::write16(uint8_t in_nRegister, uint16_t in_nValue) |
vtraveller | 20:93c70a1869ee | 69 | { |
vtraveller | 20:93c70a1869ee | 70 | char aBuffer[3] = { in_nRegister, in_nValue >> 8, in_nValue & 0xFF }; |
vtraveller | 20:93c70a1869ee | 71 | m_cI2C.write(MCP9808_I2CADDR_DEFAULT, aBuffer, sizeof(aBuffer)); |
vtraveller | 11:96146db429de | 72 | } |
vtraveller | 11:96146db429de | 73 | |
vtraveller | 20:93c70a1869ee | 74 | uint16_t TempModule::read16(uint8_t in_nRegister) |
vtraveller | 10:3fcab08717fc | 75 | { |
vtraveller | 20:93c70a1869ee | 76 | m_cI2C.write(m_nAddress, (const char *)&in_nRegister, sizeof(in_nRegister)); |
vtraveller | 10:3fcab08717fc | 77 | |
vtraveller | 20:93c70a1869ee | 78 | char aReadBuffer[2]; |
vtraveller | 20:93c70a1869ee | 79 | m_cI2C.read(m_nAddress, aReadBuffer, sizeof(aReadBuffer)); |
vtraveller | 20:93c70a1869ee | 80 | |
vtraveller | 20:93c70a1869ee | 81 | return (aReadBuffer[0] << 8) | aReadBuffer[1]; |
vtraveller | 10:3fcab08717fc | 82 | } |
vtraveller | 10:3fcab08717fc | 83 | |
vtraveller | 20:93c70a1869ee | 84 | void TempModule::show(bool in_bRefresh) |
vtraveller | 10:3fcab08717fc | 85 | { |
vtraveller | 20:93c70a1869ee | 86 | static time_t nTime = time(NULL) + 1; |
vtraveller | 20:93c70a1869ee | 87 | if (in_bRefresh || nTime < time(NULL)) |
vtraveller | 20:93c70a1869ee | 88 | { |
vtraveller | 20:93c70a1869ee | 89 | m_cDisplay.printf("Room: %0.1f%cC ",readTempC(),eDegree); |
vtraveller | 20:93c70a1869ee | 90 | |
vtraveller | 20:93c70a1869ee | 91 | // uint8_t nValue = read16(MCP9808_REG_MANUF_ID); |
vtraveller | 20:93c70a1869ee | 92 | // m_cDisplay.printf("%x - %x - %x ", m_nAddress, nValue,read16(MCP9808_REG_DEVICE_ID)); |
vtraveller | 20:93c70a1869ee | 93 | |
vtraveller | 20:93c70a1869ee | 94 | nTime = time(NULL) + 1; |
vtraveller | 20:93c70a1869ee | 95 | } |
vtraveller | 10:3fcab08717fc | 96 | } |