Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SX1276Lib AdaFruit_RGBLCD MCP23017 mbed
Fork of AdaFruit_RGBLCD by
Diff: main.cpp
- Revision:
- 10:3fcab08717fc
- Parent:
- 9:1501fb01ded6
- Child:
- 11:96146db429de
--- a/main.cpp Mon Aug 04 11:38:13 2014 +0000 +++ b/main.cpp Sun Aug 10 12:34:44 2014 +0000 @@ -1,33 +1,56 @@ #include "mbed.h" #include "Adafruit_RGBLCDShield.h" -//#include "MCP23017.h" +#include "RTclock.h" +#include "extra_chars.h" + +#include "module.h" +#include "DateModule.h" +#include "TempModule.h" +#include "TimeModule.h" +#include "TitleModule.h" Serial pc(SERIAL_TX, SERIAL_RX); -MCP23017 mcp23017 = MCP23017(I2C_SDA, I2C_SCL, 0x40); +MCP23017 mcp23017 = MCP23017(I2C_SDA, I2C_SCL, 0x40, true); Adafruit_RGBLCDShield lcd(mcp23017); +RTclock rtc(I2C_SDA, I2C_SCL); + +const int k_nWidthLCD = 16; +const int k_nHeightLCD = 2; -enum EExtraChars +enum EModes { - eUp = 0, - eDown, - eDegree + eModeMenu = 0, + eModeSelect, + eModeChange, + eModeLast }; 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_aUp[] = { 0x4,0xe,0x1f,0x15,0x4,0x4,0x4,0x4 }; + //uint8_t k_aDown[] = { 0x4,0x4,0x4,0x4,0x15,0x1f,0xe,0x4 }; + + uint8_t k_aUp[] = { 0x0,0x0,0x4,0xe,0x1f,0x0,0x0 }; + uint8_t k_aDown[] = { 0x0,0x0,0x1f,0xe,0x4,0x0,0x0 }; + uint8_t k_aRight[] = { 0x0,0x8,0xc,0xe,0xc,0x8,0x0 }; + uint8_t k_aLeft[] = { 0x0,0x2,0x6,0xe,0x6,0x2,0x0 }; uint8_t k_aDegree[] = { 0xc,0x12,0x12,0xc,0x0,0x0,0x0,0x0 }; lcd.createChar(eUp,k_aUp); lcd.createChar(eDown,k_aDown); + lcd.createChar(eRight,k_aRight); + lcd.createChar(eLeft,k_aLeft); lcd.createChar(eDegree,k_aDegree); } void Initialise() { - lcd.begin(16,2); + // Spin up RTC + rtc.mapTime(); + + // Initialise LCD + lcd.begin(k_nWidthLCD,k_nHeightLCD); CreateChars(); lcd.setCursor(0,0); @@ -36,138 +59,162 @@ lcd.setCursor(0,1); lcd._putc(eDown); } - -// Allows to set the backlight, if the LCD backpack is used -void SetBacklight(unsigned char status) + +void SetCursor(bool in_bCursor,bool in_bBlink) { - pc.printf("Backlight: %i\r\n", status); + if (in_bCursor) lcd.cursor(); else lcd.noCursor(); + if (in_bBlink) lcd.blink(); else lcd.noBlink(); +} + +void ShowModules +( + Module ** in_pModuleList, + size_t in_nModuleListSize, + size_t in_nPos, + bool in_bRefresh = false +) +{ + lcd.setCursor(2,0); - mcp23017.digitalWrite(8, (~(status >> 2) & 0x1)); - mcp23017.digitalWrite(7, (~(status >> 1) & 0x1)); - mcp23017.digitalWrite(6, (~status & 0x1)); + if (in_pModuleList[in_nPos]->canRefresh() || !in_bRefresh) in_pModuleList[in_nPos]->show(); + + size_t nPos = (in_nPos + 1) % in_nModuleListSize; + lcd.setCursor(2,1); + + if (in_pModuleList[nPos]->canRefresh() || !in_bRefresh) in_pModuleList[nPos]->show(); } -uint8_t CheckKeys() +void ShowTracking(bool in_bShow) { - static uint8_t lastButtons = lcd.readButtons(); - - uint8_t buttons = lcd.readButtons(); - - if (buttons) + if (in_bShow) { - if (buttons != lastButtons) - { - lastButtons = buttons; - } - - lcd.setCursor(2,1); + lcd.setCursor(1,0); + lcd._putc(eLeft); - if (buttons & BUTTON_UP) - { - pc.printf("UP "); - - lcd.printf("UP "); - } + lcd.setCursor(1,1); + lcd._putc(eRight); + } + else + { + lcd.setCursor(1,0); + lcd._putc(' '); - if (buttons & BUTTON_DOWN) - { - pc.printf("DOWN "); - - lcd.printf("DOWN "); - } - - if (buttons & BUTTON_LEFT) - { - pc.printf("LEFT "); - - lcd.printf("LEFT "); - } + lcd.setCursor(1,1); + lcd._putc(' '); + } +} + +void UpdateDisplay +( + Module ** in_pModuleList, + size_t in_nModuleListSize, + size_t in_nMenuPos, + int & inout_nIndexX, + int in_nCursorX, + int & inout_nCursorY +) +{ + ShowModules(in_pModuleList,in_nModuleListSize,in_nMenuPos); + + size_t nCurrent = (in_nMenuPos + inout_nCursorY) % in_nModuleListSize; + inout_nIndexX = in_pModuleList[nCurrent]->setCursor(inout_nIndexX,in_nCursorX,inout_nCursorY); + + // If we didn't have anything, move the line + if (-1 == inout_nIndexX) + { + inout_nCursorY = (inout_nCursorY + 1) % k_nHeightLCD; - if (buttons & BUTTON_RIGHT) - { - pc.printf("RIGHT "); - - lcd.printf("RIGHT "); - } - - if (buttons & BUTTON_SELECT) - { - pc.printf("SELECT "); - - lcd.printf("SELECT "); - } - - lcd.printf(" "); + nCurrent = (in_nMenuPos + inout_nCursorY) % in_nModuleListSize; + inout_nIndexX = in_pModuleList[nCurrent]->setCursor(0,in_nCursorX,inout_nCursorY); } - 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); + // If nothing to show - hide everything + if (-1 == inout_nIndexX) SetCursor(false,false); } int main() { pc.printf("\r\nInitialise LCD\r\n"); - Initialise(); + Initialise(); + + EModes eMode = eModeMenu; + size_t nMenuPos = 0; - SetTime(11,48,4,8,2014); + // Set up display modules + Module * aModules[] = + { + new TitleModule(lcd), + new TimeModule(lcd,rtc), + new DateModule(lcd,rtc), + new TempModule(lcd) + }; - int nTemp = 28; - ShowTemp(nTemp); - - time_t nLast = 0; + ShowModules(aModules,_countof(aModules),nMenuPos); + + int nIndexX = 0; + int nCursorY = 0; + while (true) { - if (time(0) - nLast > 3) - { - ShowTime(); - } - - uint8_t nKeys = CheckKeys(); + uint8_t nKeys = lcd.readButtons(); if (nKeys) { - if (nKeys & BUTTON_UP) nTemp++; - if (nKeys & BUTTON_DOWN) nTemp--; + // Change mode based on select + if (nKeys & BUTTON_SELECT) + { + eMode = (EModes)((eMode + 1) % eModeLast); + + // Start at top corner + if (eModeSelect == eMode) + { + nIndexX = 0; + nCursorY = 0; + } + } + + switch (eMode) + { + case eModeMenu: + SetCursor(false,false); + ShowTracking(false); + + if (nKeys & BUTTON_UP) nMenuPos--; + if (nKeys & BUTTON_DOWN) nMenuPos++; + + nMenuPos = nMenuPos % _countof(aModules); + break; + + case eModeSelect: + SetCursor(true,false); + ShowTracking(true); + + if (nCursorY > 0 && (nKeys & BUTTON_UP)) nCursorY--; + if ((nCursorY < k_nHeightLCD - 1) && (nKeys & BUTTON_DOWN)) nCursorY++; + + if (nKeys & BUTTON_LEFT) nIndexX--; + if (nKeys & BUTTON_RIGHT) nIndexX++; + break; + + case eModeChange: + SetCursor(false,true); + ShowTracking(true); + break; + } - ShowTemp(nTemp); - nLast = time(0); + UpdateDisplay(aModules,_countof(aModules),nMenuPos,nIndexX,2,nCursorY); } - + else + { + switch (eMode) + { + case eModeMenu: + ShowModules(aModules,_countof(aModules),nMenuPos,true); + aModules[nMenuPos]->setCursor(nIndexX,2,nCursorY); + break; + } + } + wait(0.2); - } + } }