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: Adafruit_GFX Adafruit_ST7735 INA219 MODSERIAL MbedJSONValue mbed-rtos mbed
MainMenuScreen.h
00001 #ifndef MAINMENUSCREEN_H 00002 #define MAINMENUSCREEN_H 00003 00004 #include "Icons.h" 00005 #include "Inputs.h" 00006 00007 class MainMenuScreen : public Button <MainMenuScreen> 00008 { 00009 public: 00010 MainMenuScreen(uint16_t backgroundColor); 00011 int start(); 00012 void buttonPressed(int button); 00013 00014 private: 00015 void drawMainMenuScreen(); 00016 void initialiseIcons(); 00017 void drawIcons(); 00018 void drawText(); 00019 void updateIcons(); 00020 void shiftBoundary(); 00021 void deAllocateMemory(); 00022 00023 // colors 00024 uint16_t textColor; // color for text 00025 uint16_t bgColor; // background color 00026 uint16_t byColor; // boundary color 00027 00028 00029 int boundaryShifter; // tracks the location of the boundary 00030 int changeScreen; 00031 bool scroll; // updated when a scroll event occurs 00032 00033 // Icons 00034 BatteryIcon *batteryIcon; 00035 OutputIcon *outputIcon; 00036 SolarIcon *solarIcon; 00037 SettingsIcon *settingsIcon; 00038 00039 }; 00040 00041 00042 MainMenuScreen::MainMenuScreen(uint16_t backgroundColor) 00043 { 00044 // constuctor function 00045 00046 00047 00048 bgColor = backgroundColor; 00049 00050 // set text color 00051 // determine the color of text 00052 if (bgColor == ST7735_WHITE ) { 00053 textColor = ST7735_BLACK; 00054 byColor = ST7735_CYAN; 00055 } else if (bgColor == ST7735_BLACK) { 00056 textColor = ST7735_WHITE; 00057 byColor = ST7735_CYAN; 00058 } 00059 00060 } 00061 00062 int MainMenuScreen::start() 00063 { 00064 00065 drawMainMenuScreen(); 00066 00067 // attach interrupts to buttons 00068 Button<MainMenuScreen>::activateButtons(this); 00069 00070 changeScreen = 0; 00071 00072 while (changeScreen == 0){ 00073 updateIcons(); 00074 00075 // scroll event occured 00076 if(scroll) 00077 { 00078 shiftBoundary(); 00079 scroll = false; 00080 } 00081 00082 //wait_ms(500); 00083 Thread::wait(200); 00084 } 00085 00086 // remember to detach buttons 00087 Button<MainMenuScreen>::deActivateButtons(); 00088 00089 // remember to deallocate memory 00090 deAllocateMemory(); 00091 00092 return changeScreen; 00093 } 00094 00095 void MainMenuScreen::drawMainMenuScreen() 00096 { 00097 // initialise icons 00098 initialiseIcons(); 00099 00100 // draw the screens icons first 00101 drawIcons(); 00102 00103 // draw the text under the icons 00104 drawText(); 00105 00106 // Highlight the first icon 00107 boundaryShifter = 1; 00108 shiftBoundary(); 00109 } 00110 00111 void MainMenuScreen::initialiseIcons() 00112 { 00113 // Battery Icon 00114 // X Coord 00115 int batXCoord = 15; 00116 // Y Coord 00117 int batYCoord = 5; 00118 // percentage 00119 int batteryPercentage = getBatteryPercentage(); 00120 // batteryStatus 00121 int batteryStatus = getBatteryStatus(); 00122 bool batteryCharging = false; 00123 if (batteryStatus > 0 ) 00124 { 00125 batteryCharging = true; 00126 } 00127 00128 // Constructor 00129 batteryIcon = new BatteryIcon(batXCoord, batYCoord,bgColor,batteryPercentage,batteryCharging); 00130 00131 // Output Icon Coordinates 00132 // X Coord 00133 int outXCoord = 100; 00134 // Y Coord 00135 int outYCoord = 8; 00136 // Constructor 00137 outputIcon = new OutputIcon(outXCoord, outYCoord, bgColor); 00138 00139 // Solar Icon Coordinates 00140 // X Coord 00141 int solXCoord = 5; 00142 // Y Coord 00143 int solYCoord = 63; 00144 // Constructor 00145 solarIcon = new SolarIcon(solXCoord, solYCoord, bgColor); 00146 00147 // Settings Icon Coordinates 00148 // X Coord 00149 int setXCoord = 100; 00150 // Y Coord 00151 int setYCoord = 66; 00152 // Constructor 00153 settingsIcon = new SettingsIcon(setXCoord, setYCoord, bgColor); 00154 } 00155 00156 void MainMenuScreen::drawIcons() 00157 { 00158 // draw the battery icon 00159 batteryIcon->drawBatteryIcon(); 00160 00161 // draw solar icon 00162 solarIcon->drawSolarIcon(); 00163 00164 // draw output icon 00165 outputIcon->drawOutputIcon(); 00166 00167 // draw settings icon 00168 settingsIcon->drawSettingsIcon(); 00169 } 00170 00171 void MainMenuScreen::drawText() 00172 { 00173 // text under the icons 00174 00175 // Batteries 00176 tft.setCursor(7, 50); 00177 tft.setTextColor(textColor); 00178 tft.setTextWrap(true); 00179 tft.printf("Batteries"); 00180 00181 // Output 00182 tft.setCursor(110, 50); 00183 tft.setTextColor(textColor); 00184 tft.setTextWrap(true); 00185 tft.printf("Output"); 00186 00187 // Solar 00188 tft.setCursor(17, 117); 00189 tft.setTextColor(textColor); 00190 tft.setTextWrap(true); 00191 tft.printf("Solar"); 00192 00193 // Settings 00194 tft.setCursor(103, 117); 00195 tft.setTextColor(textColor); 00196 tft.setTextWrap(true); 00197 tft.printf("Settings"); 00198 } 00199 00200 void MainMenuScreen::shiftBoundary() 00201 { 00202 // selects the next icon to have a boundary 00203 00204 if (boundaryShifter == 1) { 00205 00206 // delete settings 00207 tft.drawRect(96,114,61,13,bgColor); 00208 tft.drawRect(97,115,59,11,bgColor); 00209 00210 // draw the battery boundary 00211 tft.drawRect(0,47,70,13,byColor); 00212 tft.drawRect(1,48,68,11,byColor); 00213 00214 } else if (boundaryShifter == 2) { 00215 00216 // delete battery 00217 tft.drawRect(0,47,70,13,bgColor); 00218 tft.drawRect(1,48,68,11,bgColor); 00219 00220 // draw output 00221 tft.drawRect(103,47,50,13,byColor); 00222 tft.drawRect(104,48,48,11,byColor); 00223 00224 } else if (boundaryShifter == 3) { 00225 00226 // delete output 00227 tft.drawRect(103,47,50,13,bgColor); 00228 tft.drawRect(104,48,48,11,bgColor); 00229 00230 00231 // draw solar 00232 tft.drawRect(10,114,42,13,byColor); 00233 tft.drawRect(11,115,40,11,byColor); 00234 00235 } else if (boundaryShifter == 4) { 00236 00237 // delete solar 00238 tft.drawRect(10,114,42,13,bgColor); 00239 tft.drawRect(11,115,40,11,bgColor); 00240 00241 00242 // draw settings 00243 tft.drawRect(96,114,61,13,byColor); 00244 tft.drawRect(97,115,59,11,byColor); 00245 00246 } 00247 00248 // ready the next icon to have boundary 00249 boundaryShifter++; 00250 00251 // ensure the boundary tracker remains in a loop 00252 if (boundaryShifter > 4) { 00253 boundaryShifter = 1; 00254 } 00255 00256 } 00257 00258 void MainMenuScreen::updateIcons() 00259 { 00260 // update the icons 00261 00262 // get the overall percentage of the battery 00263 int percentage = getBatteryPercentage(); 00264 00265 // get the charge status of the battery 00266 bool batteryCharging = getBatteryStatus(); 00267 00268 // get the output status 00269 bool outputStatus = getOutputStatus(); 00270 00271 // get the radiation status of the solar panel 00272 bool radiationReceived = getSolarStatus(); 00273 00274 // set the battery percentage accordingly 00275 batteryIcon->setBatteryPercentage(percentage,batteryCharging); 00276 00277 // animate the output icon accordingly 00278 outputIcon->animateOutputIcon(outputStatus); 00279 00280 // animate the solar icon accordingly 00281 solarIcon->animateSolarIcon(radiationReceived); 00282 } 00283 void MainMenuScreen::deAllocateMemory() 00284 { 00285 // free up memory that was dynamically created 00286 delete batteryIcon; 00287 delete outputIcon; 00288 delete solarIcon; 00289 delete settingsIcon; 00290 } 00291 00292 void MainMenuScreen::buttonPressed(int button) 00293 { 00294 switch(button){ 00295 00296 case BACKBUTTON: 00297 // navigate to the previous screen 00298 //changeScreen = -1; 00299 break; 00300 00301 case SCROLLBUTTON: 00302 // move boundary to the next icon 00303 //shiftBoundary(); 00304 scroll = true; 00305 break; 00306 00307 case ENTERBUTTON: 00308 // navigate to the selected option 00309 if(boundaryShifter == 1){ 00310 changeScreen = 4; 00311 } else { 00312 changeScreen = boundaryShifter-1; 00313 } 00314 default: 00315 // do nothing 00316 break; 00317 } 00318 } 00319 00320 #endif
Generated on Wed Jul 13 2022 02:31:29 by
