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
UtilityScreen.h
00001 #ifndef UTILITYSCREEN_H 00002 #define UTILITYSCREEN_H 00003 00004 00005 DigitalOut utilityPin(p8); 00006 int uCxn = 0; // tracks the connection of the raspberry pi 00007 int prevuCxn = 0; // tracks the previous connection of the raspberry pi 00008 00009 class UtilityScreen: public Button <UtilityScreen> 00010 { 00011 public: 00012 UtilityScreen(uint16_t backgroundColor); 00013 int start(); 00014 void buttonPressed(int button); 00015 00016 private: 00017 void drawUtilityScreen(); 00018 void drawData(); 00019 void displayStaticData(); 00020 void displayDynamicData(); 00021 void updateData(); 00022 void deAllocateMemory(); 00023 void turnOnUtilityPort(); 00024 void turnOffUtilityPort(); 00025 00026 // list 00027 ListController *list; 00028 // updated when a scroll event occurs 00029 bool scroll; 00030 // updated when an enter event occurs 00031 bool enter; 00032 // background color 00033 uint16_t bgColor; 00034 // text color; 00035 uint16_t textColor; 00036 00037 float current; 00038 float voltage; 00039 float power; 00040 00041 int changeScreen; 00042 00043 }; 00044 00045 UtilityScreen::UtilityScreen(uint16_t backgroundColor) 00046 { 00047 bgColor = backgroundColor; 00048 // determine the color of text 00049 if (bgColor == ST7735_WHITE ) { 00050 textColor = ST7735_RED; 00051 } else if (bgColor == ST7735_BLACK) { 00052 textColor = ST7735_CYAN; 00053 } 00054 } 00055 00056 int UtilityScreen::start() 00057 { 00058 // initial values 00059 current = -1.0; 00060 voltage = -1.0; 00061 power = -1.0; 00062 00063 // draw the screen 00064 drawUtilityScreen(); 00065 00066 // attach interrupts to buttons 00067 Button<UtilityScreen>::activateButtons(this); 00068 00069 changeScreen = 0; 00070 scroll = false; 00071 enter = false; 00072 00073 while(changeScreen==0) { 00074 00075 // update the raspberyy pi data 00076 updateData(); 00077 00078 // scroll event occured 00079 if(scroll) { 00080 // perform scroll on the list 00081 list->scroll(); 00082 // reset variable 00083 scroll = false; 00084 } 00085 00086 // enter event occured 00087 if (enter) { 00088 // read the current option on the list 00089 int option = list->getCurrentOption(); 00090 00091 if (option == 1) { 00092 // make connection with pi 00093 //myled = 1; 00094 00095 // first status of utility port 00096 //piCxn = 1; 00097 00098 // utility port is not yet turned on 00099 if (uCxn == 0) { 00100 // turn on utlity port 00101 uCxn = 1; 00102 00103 // initiate connection here 00104 turnOnUtilityPort(); 00105 00106 } else { 00107 // do nothing 00108 } 00109 00110 00111 } else { 00112 // disconnect with pi 00113 //piCxn = 3; 00114 00115 // first check the pi connection 00116 if (uCxn == 1) { // utility port is turned on 00117 // turn off the utility port 00118 uCxn = 0; 00119 turnOffUtilityPort(); 00120 00121 } else { 00122 // do nothing 00123 } 00124 //myled = 0; 00125 } 00126 enter = false; 00127 } 00128 00129 Thread::wait(200); 00130 } 00131 00132 // detach interrupts to buttons 00133 Button<UtilityScreen>::deActivateButtons(); 00134 00135 // free up any memory 00136 deAllocateMemory(); 00137 00138 return changeScreen; 00139 } 00140 00141 void UtilityScreen::updateData() 00142 { 00143 00144 displayDynamicData(); 00145 00146 00147 00148 } 00149 00150 void UtilityScreen::drawUtilityScreen() 00151 { 00152 // draw static data here 00153 tft.setTextColor(textColor); 00154 00155 00156 displayStaticData(); 00157 00158 00159 00160 // construct the list 00161 list = new ListController( 0, 00162 63, 00163 "Turn Port on", 00164 "Yes", 00165 "No", 00166 "", 00167 "", 00168 bgColor, 00169 2); 00170 00171 // draw the list 00172 list->drawList(); 00173 00174 00175 } 00176 00177 void UtilityScreen::displayStaticData() 00178 { 00179 00180 // the static data is displayed here e.g headings 00181 tft.setTextWrap(true); 00182 tft.setTextColor(textColor); 00183 00184 // display title 00185 char title[20]; 00186 if (uCxn){ 00187 strcpy(title,"UTILITY PORT ON"); 00188 } else { 00189 strcpy(title,"UTILITY PORT OFF"); 00190 } 00191 tft.setCursor((tft.width()/2)-50,4); 00192 tft.printf("%s",title); 00193 00194 // display voltage 00195 tft.setCursor(0, 20); 00196 tft.printf("VOLTAGE[V]:"); 00197 00198 // display minimum 00199 tft.setCursor(0, 35); 00200 tft.printf("CURRENT[mA]:"); 00201 00202 // display capacity remaining 00203 tft.setCursor(0, 50); 00204 tft.printf("POWER[W]:"); 00205 00206 } 00207 00208 00209 void UtilityScreen::displayDynamicData() 00210 { 00211 // first retrieve data from the sensor suite 00212 SensorSuite suite; 00213 ConsumptionModel model = suite.getConsumptionData(); 00214 00215 float newCurrent; 00216 float newVoltage; 00217 float newPower; 00218 00219 newCurrent = model.consumptionCurrent; 00220 newVoltage = model.consumptionVoltage; 00221 newPower = newCurrent*newVoltage*0.001; 00222 00223 00224 00225 00226 tft.setTextWrap(true); 00227 //tft.setTextColor(textColor); 00228 00229 // the dynamic data is displayed here e.g values 00230 00231 if (newVoltage == voltage) 00232 { 00233 // do nothing 00234 } else { 00235 00236 tft.setCursor(tft.width()/2, 20); 00237 // first delete the previous value 00238 tft.setTextColor(backgroundColor); 00239 tft.printf("%+6.3f",voltage); 00240 00241 tft.setCursor(tft.width()/2, 20); 00242 // then write the new voltage values 00243 tft.setTextColor(textColor); 00244 tft.printf("%+6.3f",newVoltage); 00245 } 00246 00247 if (newCurrent == current){ 00248 // do nothing 00249 } else { 00250 00251 // first delete the previous value 00252 tft.setCursor(tft.width()/2, 35); 00253 tft.setTextColor(backgroundColor); 00254 tft.printf("%+6.3f",current); 00255 00256 // then write the new current values 00257 tft.setCursor(tft.width()/2, 35); 00258 tft.setTextColor(textColor); 00259 tft.printf("%+6.3f",newCurrent); 00260 } 00261 00262 if (newPower == power) 00263 { 00264 // do nothing 00265 } else { 00266 00267 tft.setCursor(tft.width()/2, 50); 00268 // first delete the previous value 00269 tft.setTextColor(backgroundColor); 00270 tft.printf("%+6.3f",power); 00271 00272 tft.setCursor(tft.width()/2, 50); 00273 // then write the new voltage values 00274 tft.setTextColor(textColor); 00275 tft.printf("%+6.3f",newPower); 00276 } 00277 00278 00279 00280 00281 voltage = newVoltage; 00282 current = newCurrent; 00283 power = newPower; 00284 } 00285 00286 00287 void UtilityScreen::deAllocateMemory() 00288 { 00289 // deallocate memory that was created dynamically 00290 delete list; 00291 } 00292 00293 void UtilityScreen::buttonPressed(int button) 00294 { 00295 switch(button) { 00296 00297 case BACKBUTTON: 00298 // navigate to the previous screen 00299 changeScreen = -1; 00300 break; 00301 00302 case SCROLLBUTTON: 00303 // scroll to the next option 00304 scroll = true; 00305 break; 00306 00307 case ENTERBUTTON: 00308 // navigate to the selected option 00309 enter = true; 00310 break; 00311 00312 default: 00313 // do nothing 00314 break; 00315 } 00316 } 00317 00318 void UtilityScreen::turnOnUtilityPort() 00319 { 00320 /** 00321 ui_serial_letter *letter2 = ui_serial_mail.alloc(); 00322 letter2->activateSerial = true; 00323 ui_serial_mail.put(letter2); 00324 00325 // start the thread 00326 // launch serial communication with raspberry pi 00327 00328 thread3.start(raspiSerial); 00329 00330 **/ 00331 00332 // turn on utility port 00333 utilityPin = 1; 00334 utilityLED = 1; 00335 00336 // delete text "UTILITY PORT OFF" 00337 tft.setTextColor(backgroundColor); 00338 tft.setCursor((tft.width()/2)-50,4); 00339 tft.printf("UTILITY PORT OFF"); 00340 00341 00342 // print text "UTILITY PORT ON" 00343 tft.setTextColor(textColor); 00344 tft.setCursor((tft.width()/2)-50,4); 00345 tft.printf("UTILITY PORT ON"); 00346 } 00347 00348 void UtilityScreen::turnOffUtilityPort() 00349 { 00350 /** 00351 ui_serial_letter *letter2 = ui_serial_mail.alloc(); 00352 letter2->activateSerial = false; 00353 ui_serial_mail.put(letter2); 00354 **/ 00355 00356 // turn off utility port 00357 utilityPin = 0; 00358 utilityLED = 0; 00359 00360 // delete text "UTILITY PORT ON" 00361 tft.setTextColor(backgroundColor); 00362 tft.setCursor((tft.width()/2)-50,4); 00363 tft.printf("UTILITY PORT ON"); 00364 00365 00366 // print text "UTILITY PORT OFF" 00367 tft.setTextColor(textColor); 00368 tft.setCursor((tft.width()/2)-50,4); 00369 tft.printf("UTILITY PORT OFF"); 00370 } 00371 00372 #endif
Generated on Wed Jul 13 2022 02:31:29 by
