This program consists of the software developed for the ELEC5870M Individual Project. It runs on the mbed LPC1768. It uses the mbed RTOS to perform the following tasks: - Implements intuitive GUI with buttons, LCD TFT Display and LEDs. - Serial Communication with the RPi - I2C communication with INA219 voltage current sensors - Power control at the USB ports
Dependencies: Adafruit_GFX Adafruit_ST7735 INA219 MODSERIAL MbedJSONValue mbed-rtos mbed
Screens/MainMenuScreen.h@3:7666de697752, 2017-02-04 (annotated)
- Committer:
- OHstin
- Date:
- Sat Feb 04 20:17:44 2017 +0000
- Revision:
- 3:7666de697752
This version before adding serial communication and pathways
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
OHstin | 3:7666de697752 | 1 | #ifndef MAINMENUSCREEN_H |
OHstin | 3:7666de697752 | 2 | #define MAINMENUSCREEN_H |
OHstin | 3:7666de697752 | 3 | |
OHstin | 3:7666de697752 | 4 | #include "Icons.h" |
OHstin | 3:7666de697752 | 5 | #include "Inputs.h" |
OHstin | 3:7666de697752 | 6 | |
OHstin | 3:7666de697752 | 7 | class MainMenuScreen : public Button <MainMenuScreen> |
OHstin | 3:7666de697752 | 8 | { |
OHstin | 3:7666de697752 | 9 | public: |
OHstin | 3:7666de697752 | 10 | MainMenuScreen(uint16_t backgroundColor); |
OHstin | 3:7666de697752 | 11 | int start(); |
OHstin | 3:7666de697752 | 12 | void buttonPressed(int button); |
OHstin | 3:7666de697752 | 13 | |
OHstin | 3:7666de697752 | 14 | private: |
OHstin | 3:7666de697752 | 15 | void drawMainMenuScreen(); |
OHstin | 3:7666de697752 | 16 | void initialiseIcons(); |
OHstin | 3:7666de697752 | 17 | void drawIcons(); |
OHstin | 3:7666de697752 | 18 | void drawText(); |
OHstin | 3:7666de697752 | 19 | void updateIcons(); |
OHstin | 3:7666de697752 | 20 | void shiftBoundary(); |
OHstin | 3:7666de697752 | 21 | void deAllocateMemory(); |
OHstin | 3:7666de697752 | 22 | |
OHstin | 3:7666de697752 | 23 | // colors |
OHstin | 3:7666de697752 | 24 | uint16_t textColor; // color for text |
OHstin | 3:7666de697752 | 25 | uint16_t bgColor; // background color |
OHstin | 3:7666de697752 | 26 | uint16_t byColor; // boundary color |
OHstin | 3:7666de697752 | 27 | |
OHstin | 3:7666de697752 | 28 | |
OHstin | 3:7666de697752 | 29 | int boundaryShifter; // tracks the location of the boundary |
OHstin | 3:7666de697752 | 30 | int changeScreen; |
OHstin | 3:7666de697752 | 31 | bool scroll; // updated when a scroll event occurs |
OHstin | 3:7666de697752 | 32 | |
OHstin | 3:7666de697752 | 33 | // Icons |
OHstin | 3:7666de697752 | 34 | BatteryIcon *batteryIcon; |
OHstin | 3:7666de697752 | 35 | OutputIcon *outputIcon; |
OHstin | 3:7666de697752 | 36 | SolarIcon *solarIcon; |
OHstin | 3:7666de697752 | 37 | SettingsIcon *settingsIcon; |
OHstin | 3:7666de697752 | 38 | |
OHstin | 3:7666de697752 | 39 | }; |
OHstin | 3:7666de697752 | 40 | |
OHstin | 3:7666de697752 | 41 | |
OHstin | 3:7666de697752 | 42 | MainMenuScreen::MainMenuScreen(uint16_t backgroundColor) |
OHstin | 3:7666de697752 | 43 | { |
OHstin | 3:7666de697752 | 44 | // constuctor function |
OHstin | 3:7666de697752 | 45 | |
OHstin | 3:7666de697752 | 46 | |
OHstin | 3:7666de697752 | 47 | |
OHstin | 3:7666de697752 | 48 | bgColor = backgroundColor; |
OHstin | 3:7666de697752 | 49 | |
OHstin | 3:7666de697752 | 50 | // set text color |
OHstin | 3:7666de697752 | 51 | // determine the color of text |
OHstin | 3:7666de697752 | 52 | if (bgColor == ST7735_WHITE ) { |
OHstin | 3:7666de697752 | 53 | textColor = ST7735_BLACK; |
OHstin | 3:7666de697752 | 54 | byColor = ST7735_CYAN; |
OHstin | 3:7666de697752 | 55 | } else if (bgColor == ST7735_BLACK) { |
OHstin | 3:7666de697752 | 56 | textColor = ST7735_WHITE; |
OHstin | 3:7666de697752 | 57 | byColor = ST7735_CYAN; |
OHstin | 3:7666de697752 | 58 | } |
OHstin | 3:7666de697752 | 59 | |
OHstin | 3:7666de697752 | 60 | } |
OHstin | 3:7666de697752 | 61 | |
OHstin | 3:7666de697752 | 62 | int MainMenuScreen::start() |
OHstin | 3:7666de697752 | 63 | { |
OHstin | 3:7666de697752 | 64 | |
OHstin | 3:7666de697752 | 65 | drawMainMenuScreen(); |
OHstin | 3:7666de697752 | 66 | |
OHstin | 3:7666de697752 | 67 | // attach interrupts to buttons |
OHstin | 3:7666de697752 | 68 | Button<MainMenuScreen>::activateButtons(this); |
OHstin | 3:7666de697752 | 69 | |
OHstin | 3:7666de697752 | 70 | changeScreen = 0; |
OHstin | 3:7666de697752 | 71 | |
OHstin | 3:7666de697752 | 72 | while (changeScreen == 0){ |
OHstin | 3:7666de697752 | 73 | updateIcons(); |
OHstin | 3:7666de697752 | 74 | |
OHstin | 3:7666de697752 | 75 | // scroll event occured |
OHstin | 3:7666de697752 | 76 | if(scroll) |
OHstin | 3:7666de697752 | 77 | { |
OHstin | 3:7666de697752 | 78 | shiftBoundary(); |
OHstin | 3:7666de697752 | 79 | scroll = false; |
OHstin | 3:7666de697752 | 80 | } |
OHstin | 3:7666de697752 | 81 | |
OHstin | 3:7666de697752 | 82 | //wait_ms(500); |
OHstin | 3:7666de697752 | 83 | Thread::wait(200); |
OHstin | 3:7666de697752 | 84 | } |
OHstin | 3:7666de697752 | 85 | |
OHstin | 3:7666de697752 | 86 | // remember to detach buttons |
OHstin | 3:7666de697752 | 87 | Button<MainMenuScreen>::deActivateButtons(); |
OHstin | 3:7666de697752 | 88 | |
OHstin | 3:7666de697752 | 89 | // remember to deallocate memory |
OHstin | 3:7666de697752 | 90 | deAllocateMemory(); |
OHstin | 3:7666de697752 | 91 | |
OHstin | 3:7666de697752 | 92 | return changeScreen; |
OHstin | 3:7666de697752 | 93 | } |
OHstin | 3:7666de697752 | 94 | |
OHstin | 3:7666de697752 | 95 | void MainMenuScreen::drawMainMenuScreen() |
OHstin | 3:7666de697752 | 96 | { |
OHstin | 3:7666de697752 | 97 | // initialise icons |
OHstin | 3:7666de697752 | 98 | initialiseIcons(); |
OHstin | 3:7666de697752 | 99 | |
OHstin | 3:7666de697752 | 100 | // draw the screens icons first |
OHstin | 3:7666de697752 | 101 | drawIcons(); |
OHstin | 3:7666de697752 | 102 | |
OHstin | 3:7666de697752 | 103 | // draw the text under the icons |
OHstin | 3:7666de697752 | 104 | drawText(); |
OHstin | 3:7666de697752 | 105 | |
OHstin | 3:7666de697752 | 106 | // Highlight the first icon |
OHstin | 3:7666de697752 | 107 | boundaryShifter = 1; |
OHstin | 3:7666de697752 | 108 | shiftBoundary(); |
OHstin | 3:7666de697752 | 109 | } |
OHstin | 3:7666de697752 | 110 | |
OHstin | 3:7666de697752 | 111 | void MainMenuScreen::initialiseIcons() |
OHstin | 3:7666de697752 | 112 | { |
OHstin | 3:7666de697752 | 113 | // Battery Icon |
OHstin | 3:7666de697752 | 114 | // X Coord |
OHstin | 3:7666de697752 | 115 | int batXCoord = 15; |
OHstin | 3:7666de697752 | 116 | // Y Coord |
OHstin | 3:7666de697752 | 117 | int batYCoord = 5; |
OHstin | 3:7666de697752 | 118 | // percentage |
OHstin | 3:7666de697752 | 119 | int batteryPercentage = getBatteryPercentage(); |
OHstin | 3:7666de697752 | 120 | // batteryStatus |
OHstin | 3:7666de697752 | 121 | int batteryStatus = getBatteryStatus(); |
OHstin | 3:7666de697752 | 122 | bool batteryCharging = false; |
OHstin | 3:7666de697752 | 123 | if (batteryStatus > 0 ) |
OHstin | 3:7666de697752 | 124 | { |
OHstin | 3:7666de697752 | 125 | batteryCharging = true; |
OHstin | 3:7666de697752 | 126 | } |
OHstin | 3:7666de697752 | 127 | |
OHstin | 3:7666de697752 | 128 | // Constructor |
OHstin | 3:7666de697752 | 129 | batteryIcon = new BatteryIcon(batXCoord, batYCoord,bgColor,batteryPercentage,batteryCharging); |
OHstin | 3:7666de697752 | 130 | |
OHstin | 3:7666de697752 | 131 | // Output Icon Coordinates |
OHstin | 3:7666de697752 | 132 | // X Coord |
OHstin | 3:7666de697752 | 133 | int outXCoord = 100; |
OHstin | 3:7666de697752 | 134 | // Y Coord |
OHstin | 3:7666de697752 | 135 | int outYCoord = 8; |
OHstin | 3:7666de697752 | 136 | // Constructor |
OHstin | 3:7666de697752 | 137 | outputIcon = new OutputIcon(outXCoord, outYCoord, bgColor); |
OHstin | 3:7666de697752 | 138 | |
OHstin | 3:7666de697752 | 139 | // Solar Icon Coordinates |
OHstin | 3:7666de697752 | 140 | // X Coord |
OHstin | 3:7666de697752 | 141 | int solXCoord = 5; |
OHstin | 3:7666de697752 | 142 | // Y Coord |
OHstin | 3:7666de697752 | 143 | int solYCoord = 63; |
OHstin | 3:7666de697752 | 144 | // Constructor |
OHstin | 3:7666de697752 | 145 | solarIcon = new SolarIcon(solXCoord, solYCoord, bgColor); |
OHstin | 3:7666de697752 | 146 | |
OHstin | 3:7666de697752 | 147 | // Settings Icon Coordinates |
OHstin | 3:7666de697752 | 148 | // X Coord |
OHstin | 3:7666de697752 | 149 | int setXCoord = 100; |
OHstin | 3:7666de697752 | 150 | // Y Coord |
OHstin | 3:7666de697752 | 151 | int setYCoord = 66; |
OHstin | 3:7666de697752 | 152 | // Constructor |
OHstin | 3:7666de697752 | 153 | settingsIcon = new SettingsIcon(setXCoord, setYCoord, bgColor); |
OHstin | 3:7666de697752 | 154 | } |
OHstin | 3:7666de697752 | 155 | |
OHstin | 3:7666de697752 | 156 | void MainMenuScreen::drawIcons() |
OHstin | 3:7666de697752 | 157 | { |
OHstin | 3:7666de697752 | 158 | // draw the battery icon |
OHstin | 3:7666de697752 | 159 | batteryIcon->drawBatteryIcon(); |
OHstin | 3:7666de697752 | 160 | |
OHstin | 3:7666de697752 | 161 | // draw solar icon |
OHstin | 3:7666de697752 | 162 | solarIcon->drawSolarIcon(); |
OHstin | 3:7666de697752 | 163 | |
OHstin | 3:7666de697752 | 164 | // draw output icon |
OHstin | 3:7666de697752 | 165 | outputIcon->drawOutputIcon(); |
OHstin | 3:7666de697752 | 166 | |
OHstin | 3:7666de697752 | 167 | // draw settings icon |
OHstin | 3:7666de697752 | 168 | settingsIcon->drawSettingsIcon(); |
OHstin | 3:7666de697752 | 169 | } |
OHstin | 3:7666de697752 | 170 | |
OHstin | 3:7666de697752 | 171 | void MainMenuScreen::drawText() |
OHstin | 3:7666de697752 | 172 | { |
OHstin | 3:7666de697752 | 173 | // text under the icons |
OHstin | 3:7666de697752 | 174 | |
OHstin | 3:7666de697752 | 175 | // Batteries |
OHstin | 3:7666de697752 | 176 | tft.setCursor(7, 50); |
OHstin | 3:7666de697752 | 177 | tft.setTextColor(textColor); |
OHstin | 3:7666de697752 | 178 | tft.setTextWrap(true); |
OHstin | 3:7666de697752 | 179 | tft.printf("Batteries"); |
OHstin | 3:7666de697752 | 180 | |
OHstin | 3:7666de697752 | 181 | // Output |
OHstin | 3:7666de697752 | 182 | tft.setCursor(110, 50); |
OHstin | 3:7666de697752 | 183 | tft.setTextColor(textColor); |
OHstin | 3:7666de697752 | 184 | tft.setTextWrap(true); |
OHstin | 3:7666de697752 | 185 | tft.printf("Output"); |
OHstin | 3:7666de697752 | 186 | |
OHstin | 3:7666de697752 | 187 | // Solar |
OHstin | 3:7666de697752 | 188 | tft.setCursor(17, 117); |
OHstin | 3:7666de697752 | 189 | tft.setTextColor(textColor); |
OHstin | 3:7666de697752 | 190 | tft.setTextWrap(true); |
OHstin | 3:7666de697752 | 191 | tft.printf("Solar"); |
OHstin | 3:7666de697752 | 192 | |
OHstin | 3:7666de697752 | 193 | // Settings |
OHstin | 3:7666de697752 | 194 | tft.setCursor(103, 117); |
OHstin | 3:7666de697752 | 195 | tft.setTextColor(textColor); |
OHstin | 3:7666de697752 | 196 | tft.setTextWrap(true); |
OHstin | 3:7666de697752 | 197 | tft.printf("Settings"); |
OHstin | 3:7666de697752 | 198 | } |
OHstin | 3:7666de697752 | 199 | |
OHstin | 3:7666de697752 | 200 | void MainMenuScreen::shiftBoundary() |
OHstin | 3:7666de697752 | 201 | { |
OHstin | 3:7666de697752 | 202 | // selects the next icon to have a boundary |
OHstin | 3:7666de697752 | 203 | |
OHstin | 3:7666de697752 | 204 | if (boundaryShifter == 1) { |
OHstin | 3:7666de697752 | 205 | |
OHstin | 3:7666de697752 | 206 | // delete settings |
OHstin | 3:7666de697752 | 207 | tft.drawRect(96,114,61,13,bgColor); |
OHstin | 3:7666de697752 | 208 | tft.drawRect(97,115,59,11,bgColor); |
OHstin | 3:7666de697752 | 209 | |
OHstin | 3:7666de697752 | 210 | // draw the battery boundary |
OHstin | 3:7666de697752 | 211 | tft.drawRect(0,47,70,13,byColor); |
OHstin | 3:7666de697752 | 212 | tft.drawRect(1,48,68,11,byColor); |
OHstin | 3:7666de697752 | 213 | |
OHstin | 3:7666de697752 | 214 | } else if (boundaryShifter == 2) { |
OHstin | 3:7666de697752 | 215 | |
OHstin | 3:7666de697752 | 216 | // delete battery |
OHstin | 3:7666de697752 | 217 | tft.drawRect(0,47,70,13,bgColor); |
OHstin | 3:7666de697752 | 218 | tft.drawRect(1,48,68,11,bgColor); |
OHstin | 3:7666de697752 | 219 | |
OHstin | 3:7666de697752 | 220 | // draw output |
OHstin | 3:7666de697752 | 221 | tft.drawRect(103,47,50,13,byColor); |
OHstin | 3:7666de697752 | 222 | tft.drawRect(104,48,48,11,byColor); |
OHstin | 3:7666de697752 | 223 | |
OHstin | 3:7666de697752 | 224 | } else if (boundaryShifter == 3) { |
OHstin | 3:7666de697752 | 225 | |
OHstin | 3:7666de697752 | 226 | // delete output |
OHstin | 3:7666de697752 | 227 | tft.drawRect(103,47,50,13,bgColor); |
OHstin | 3:7666de697752 | 228 | tft.drawRect(104,48,48,11,bgColor); |
OHstin | 3:7666de697752 | 229 | |
OHstin | 3:7666de697752 | 230 | |
OHstin | 3:7666de697752 | 231 | // draw solar |
OHstin | 3:7666de697752 | 232 | tft.drawRect(10,114,42,13,byColor); |
OHstin | 3:7666de697752 | 233 | tft.drawRect(11,115,40,11,byColor); |
OHstin | 3:7666de697752 | 234 | |
OHstin | 3:7666de697752 | 235 | } else if (boundaryShifter == 4) { |
OHstin | 3:7666de697752 | 236 | |
OHstin | 3:7666de697752 | 237 | // delete solar |
OHstin | 3:7666de697752 | 238 | tft.drawRect(10,114,42,13,bgColor); |
OHstin | 3:7666de697752 | 239 | tft.drawRect(11,115,40,11,bgColor); |
OHstin | 3:7666de697752 | 240 | |
OHstin | 3:7666de697752 | 241 | |
OHstin | 3:7666de697752 | 242 | // draw settings |
OHstin | 3:7666de697752 | 243 | tft.drawRect(96,114,61,13,byColor); |
OHstin | 3:7666de697752 | 244 | tft.drawRect(97,115,59,11,byColor); |
OHstin | 3:7666de697752 | 245 | |
OHstin | 3:7666de697752 | 246 | } |
OHstin | 3:7666de697752 | 247 | |
OHstin | 3:7666de697752 | 248 | // ready the next icon to have boundary |
OHstin | 3:7666de697752 | 249 | boundaryShifter++; |
OHstin | 3:7666de697752 | 250 | |
OHstin | 3:7666de697752 | 251 | // ensure the boundary tracker remains in a loop |
OHstin | 3:7666de697752 | 252 | if (boundaryShifter > 4) { |
OHstin | 3:7666de697752 | 253 | boundaryShifter = 1; |
OHstin | 3:7666de697752 | 254 | } |
OHstin | 3:7666de697752 | 255 | |
OHstin | 3:7666de697752 | 256 | } |
OHstin | 3:7666de697752 | 257 | |
OHstin | 3:7666de697752 | 258 | void MainMenuScreen::updateIcons() |
OHstin | 3:7666de697752 | 259 | { |
OHstin | 3:7666de697752 | 260 | // update the icons |
OHstin | 3:7666de697752 | 261 | |
OHstin | 3:7666de697752 | 262 | // get the overall percentage of the battery |
OHstin | 3:7666de697752 | 263 | int percentage = getBatteryPercentage(); |
OHstin | 3:7666de697752 | 264 | |
OHstin | 3:7666de697752 | 265 | // get the charge status of the battery |
OHstin | 3:7666de697752 | 266 | bool batteryCharging = getBatteryStatus(); |
OHstin | 3:7666de697752 | 267 | |
OHstin | 3:7666de697752 | 268 | // get the output status |
OHstin | 3:7666de697752 | 269 | bool outputStatus = getOutputStatus(); |
OHstin | 3:7666de697752 | 270 | |
OHstin | 3:7666de697752 | 271 | // get the radiation status of the solar panel |
OHstin | 3:7666de697752 | 272 | bool radiationReceived = getSolarStatus(); |
OHstin | 3:7666de697752 | 273 | |
OHstin | 3:7666de697752 | 274 | // set the battery percentage accordingly |
OHstin | 3:7666de697752 | 275 | batteryIcon->setBatteryPercentage(percentage,batteryCharging); |
OHstin | 3:7666de697752 | 276 | |
OHstin | 3:7666de697752 | 277 | // animate the output icon accordingly |
OHstin | 3:7666de697752 | 278 | outputIcon->animateOutputIcon(outputStatus); |
OHstin | 3:7666de697752 | 279 | |
OHstin | 3:7666de697752 | 280 | // animate the solar icon accordingly |
OHstin | 3:7666de697752 | 281 | solarIcon->animateSolarIcon(radiationReceived); |
OHstin | 3:7666de697752 | 282 | } |
OHstin | 3:7666de697752 | 283 | void MainMenuScreen::deAllocateMemory() |
OHstin | 3:7666de697752 | 284 | { |
OHstin | 3:7666de697752 | 285 | // free up memory that was dynamically created |
OHstin | 3:7666de697752 | 286 | delete batteryIcon; |
OHstin | 3:7666de697752 | 287 | delete outputIcon; |
OHstin | 3:7666de697752 | 288 | delete solarIcon; |
OHstin | 3:7666de697752 | 289 | delete settingsIcon; |
OHstin | 3:7666de697752 | 290 | } |
OHstin | 3:7666de697752 | 291 | |
OHstin | 3:7666de697752 | 292 | void MainMenuScreen::buttonPressed(int button) |
OHstin | 3:7666de697752 | 293 | { |
OHstin | 3:7666de697752 | 294 | switch(button){ |
OHstin | 3:7666de697752 | 295 | |
OHstin | 3:7666de697752 | 296 | case BACKBUTTON: |
OHstin | 3:7666de697752 | 297 | // navigate to the previous screen |
OHstin | 3:7666de697752 | 298 | //changeScreen = -1; |
OHstin | 3:7666de697752 | 299 | break; |
OHstin | 3:7666de697752 | 300 | |
OHstin | 3:7666de697752 | 301 | case SCROLLBUTTON: |
OHstin | 3:7666de697752 | 302 | // move boundary to the next icon |
OHstin | 3:7666de697752 | 303 | //shiftBoundary(); |
OHstin | 3:7666de697752 | 304 | scroll = true; |
OHstin | 3:7666de697752 | 305 | break; |
OHstin | 3:7666de697752 | 306 | |
OHstin | 3:7666de697752 | 307 | case ENTERBUTTON: |
OHstin | 3:7666de697752 | 308 | // navigate to the selected option |
OHstin | 3:7666de697752 | 309 | if(boundaryShifter == 1){ |
OHstin | 3:7666de697752 | 310 | changeScreen = 4; |
OHstin | 3:7666de697752 | 311 | } else { |
OHstin | 3:7666de697752 | 312 | changeScreen = boundaryShifter-1; |
OHstin | 3:7666de697752 | 313 | } |
OHstin | 3:7666de697752 | 314 | default: |
OHstin | 3:7666de697752 | 315 | // do nothing |
OHstin | 3:7666de697752 | 316 | break; |
OHstin | 3:7666de697752 | 317 | } |
OHstin | 3:7666de697752 | 318 | } |
OHstin | 3:7666de697752 | 319 | |
OHstin | 3:7666de697752 | 320 | #endif |