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

Committer:
OHstin
Date:
Sun Apr 30 17:19:22 2017 +0000
Revision:
6:196a63a3378d
Final Code for mbed operation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
OHstin 6:196a63a3378d 1 #ifndef UTILITYSCREEN_H
OHstin 6:196a63a3378d 2 #define UTILITYSCREEN_H
OHstin 6:196a63a3378d 3
OHstin 6:196a63a3378d 4
OHstin 6:196a63a3378d 5 DigitalOut utilityPin(p8);
OHstin 6:196a63a3378d 6 int uCxn = 0; // tracks the connection of the raspberry pi
OHstin 6:196a63a3378d 7 int prevuCxn = 0; // tracks the previous connection of the raspberry pi
OHstin 6:196a63a3378d 8
OHstin 6:196a63a3378d 9 class UtilityScreen: public Button <UtilityScreen>
OHstin 6:196a63a3378d 10 {
OHstin 6:196a63a3378d 11 public:
OHstin 6:196a63a3378d 12 UtilityScreen(uint16_t backgroundColor);
OHstin 6:196a63a3378d 13 int start();
OHstin 6:196a63a3378d 14 void buttonPressed(int button);
OHstin 6:196a63a3378d 15
OHstin 6:196a63a3378d 16 private:
OHstin 6:196a63a3378d 17 void drawUtilityScreen();
OHstin 6:196a63a3378d 18 void drawData();
OHstin 6:196a63a3378d 19 void displayStaticData();
OHstin 6:196a63a3378d 20 void displayDynamicData();
OHstin 6:196a63a3378d 21 void updateData();
OHstin 6:196a63a3378d 22 void deAllocateMemory();
OHstin 6:196a63a3378d 23 void turnOnUtilityPort();
OHstin 6:196a63a3378d 24 void turnOffUtilityPort();
OHstin 6:196a63a3378d 25
OHstin 6:196a63a3378d 26 // list
OHstin 6:196a63a3378d 27 ListController *list;
OHstin 6:196a63a3378d 28 // updated when a scroll event occurs
OHstin 6:196a63a3378d 29 bool scroll;
OHstin 6:196a63a3378d 30 // updated when an enter event occurs
OHstin 6:196a63a3378d 31 bool enter;
OHstin 6:196a63a3378d 32 // background color
OHstin 6:196a63a3378d 33 uint16_t bgColor;
OHstin 6:196a63a3378d 34 // text color;
OHstin 6:196a63a3378d 35 uint16_t textColor;
OHstin 6:196a63a3378d 36
OHstin 6:196a63a3378d 37 float current;
OHstin 6:196a63a3378d 38 float voltage;
OHstin 6:196a63a3378d 39 float power;
OHstin 6:196a63a3378d 40
OHstin 6:196a63a3378d 41 int changeScreen;
OHstin 6:196a63a3378d 42
OHstin 6:196a63a3378d 43 };
OHstin 6:196a63a3378d 44
OHstin 6:196a63a3378d 45 UtilityScreen::UtilityScreen(uint16_t backgroundColor)
OHstin 6:196a63a3378d 46 {
OHstin 6:196a63a3378d 47 bgColor = backgroundColor;
OHstin 6:196a63a3378d 48 // determine the color of text
OHstin 6:196a63a3378d 49 if (bgColor == ST7735_WHITE ) {
OHstin 6:196a63a3378d 50 textColor = ST7735_RED;
OHstin 6:196a63a3378d 51 } else if (bgColor == ST7735_BLACK) {
OHstin 6:196a63a3378d 52 textColor = ST7735_CYAN;
OHstin 6:196a63a3378d 53 }
OHstin 6:196a63a3378d 54 }
OHstin 6:196a63a3378d 55
OHstin 6:196a63a3378d 56 int UtilityScreen::start()
OHstin 6:196a63a3378d 57 {
OHstin 6:196a63a3378d 58 // initial values
OHstin 6:196a63a3378d 59 current = -1.0;
OHstin 6:196a63a3378d 60 voltage = -1.0;
OHstin 6:196a63a3378d 61 power = -1.0;
OHstin 6:196a63a3378d 62
OHstin 6:196a63a3378d 63 // draw the screen
OHstin 6:196a63a3378d 64 drawUtilityScreen();
OHstin 6:196a63a3378d 65
OHstin 6:196a63a3378d 66 // attach interrupts to buttons
OHstin 6:196a63a3378d 67 Button<UtilityScreen>::activateButtons(this);
OHstin 6:196a63a3378d 68
OHstin 6:196a63a3378d 69 changeScreen = 0;
OHstin 6:196a63a3378d 70 scroll = false;
OHstin 6:196a63a3378d 71 enter = false;
OHstin 6:196a63a3378d 72
OHstin 6:196a63a3378d 73 while(changeScreen==0) {
OHstin 6:196a63a3378d 74
OHstin 6:196a63a3378d 75 // update the raspberyy pi data
OHstin 6:196a63a3378d 76 updateData();
OHstin 6:196a63a3378d 77
OHstin 6:196a63a3378d 78 // scroll event occured
OHstin 6:196a63a3378d 79 if(scroll) {
OHstin 6:196a63a3378d 80 // perform scroll on the list
OHstin 6:196a63a3378d 81 list->scroll();
OHstin 6:196a63a3378d 82 // reset variable
OHstin 6:196a63a3378d 83 scroll = false;
OHstin 6:196a63a3378d 84 }
OHstin 6:196a63a3378d 85
OHstin 6:196a63a3378d 86 // enter event occured
OHstin 6:196a63a3378d 87 if (enter) {
OHstin 6:196a63a3378d 88 // read the current option on the list
OHstin 6:196a63a3378d 89 int option = list->getCurrentOption();
OHstin 6:196a63a3378d 90
OHstin 6:196a63a3378d 91 if (option == 1) {
OHstin 6:196a63a3378d 92 // make connection with pi
OHstin 6:196a63a3378d 93 //myled = 1;
OHstin 6:196a63a3378d 94
OHstin 6:196a63a3378d 95 // first status of utility port
OHstin 6:196a63a3378d 96 //piCxn = 1;
OHstin 6:196a63a3378d 97
OHstin 6:196a63a3378d 98 // utility port is not yet turned on
OHstin 6:196a63a3378d 99 if (uCxn == 0) {
OHstin 6:196a63a3378d 100 // turn on utlity port
OHstin 6:196a63a3378d 101 uCxn = 1;
OHstin 6:196a63a3378d 102
OHstin 6:196a63a3378d 103 // initiate connection here
OHstin 6:196a63a3378d 104 turnOnUtilityPort();
OHstin 6:196a63a3378d 105
OHstin 6:196a63a3378d 106 } else {
OHstin 6:196a63a3378d 107 // do nothing
OHstin 6:196a63a3378d 108 }
OHstin 6:196a63a3378d 109
OHstin 6:196a63a3378d 110
OHstin 6:196a63a3378d 111 } else {
OHstin 6:196a63a3378d 112 // disconnect with pi
OHstin 6:196a63a3378d 113 //piCxn = 3;
OHstin 6:196a63a3378d 114
OHstin 6:196a63a3378d 115 // first check the pi connection
OHstin 6:196a63a3378d 116 if (uCxn == 1) { // utility port is turned on
OHstin 6:196a63a3378d 117 // turn off the utility port
OHstin 6:196a63a3378d 118 uCxn = 0;
OHstin 6:196a63a3378d 119 turnOffUtilityPort();
OHstin 6:196a63a3378d 120
OHstin 6:196a63a3378d 121 } else {
OHstin 6:196a63a3378d 122 // do nothing
OHstin 6:196a63a3378d 123 }
OHstin 6:196a63a3378d 124 //myled = 0;
OHstin 6:196a63a3378d 125 }
OHstin 6:196a63a3378d 126 enter = false;
OHstin 6:196a63a3378d 127 }
OHstin 6:196a63a3378d 128
OHstin 6:196a63a3378d 129 Thread::wait(200);
OHstin 6:196a63a3378d 130 }
OHstin 6:196a63a3378d 131
OHstin 6:196a63a3378d 132 // detach interrupts to buttons
OHstin 6:196a63a3378d 133 Button<UtilityScreen>::deActivateButtons();
OHstin 6:196a63a3378d 134
OHstin 6:196a63a3378d 135 // free up any memory
OHstin 6:196a63a3378d 136 deAllocateMemory();
OHstin 6:196a63a3378d 137
OHstin 6:196a63a3378d 138 return changeScreen;
OHstin 6:196a63a3378d 139 }
OHstin 6:196a63a3378d 140
OHstin 6:196a63a3378d 141 void UtilityScreen::updateData()
OHstin 6:196a63a3378d 142 {
OHstin 6:196a63a3378d 143
OHstin 6:196a63a3378d 144 displayDynamicData();
OHstin 6:196a63a3378d 145
OHstin 6:196a63a3378d 146
OHstin 6:196a63a3378d 147
OHstin 6:196a63a3378d 148 }
OHstin 6:196a63a3378d 149
OHstin 6:196a63a3378d 150 void UtilityScreen::drawUtilityScreen()
OHstin 6:196a63a3378d 151 {
OHstin 6:196a63a3378d 152 // draw static data here
OHstin 6:196a63a3378d 153 tft.setTextColor(textColor);
OHstin 6:196a63a3378d 154
OHstin 6:196a63a3378d 155
OHstin 6:196a63a3378d 156 displayStaticData();
OHstin 6:196a63a3378d 157
OHstin 6:196a63a3378d 158
OHstin 6:196a63a3378d 159
OHstin 6:196a63a3378d 160 // construct the list
OHstin 6:196a63a3378d 161 list = new ListController( 0,
OHstin 6:196a63a3378d 162 63,
OHstin 6:196a63a3378d 163 "Turn Port on",
OHstin 6:196a63a3378d 164 "Yes",
OHstin 6:196a63a3378d 165 "No",
OHstin 6:196a63a3378d 166 "",
OHstin 6:196a63a3378d 167 "",
OHstin 6:196a63a3378d 168 bgColor,
OHstin 6:196a63a3378d 169 2);
OHstin 6:196a63a3378d 170
OHstin 6:196a63a3378d 171 // draw the list
OHstin 6:196a63a3378d 172 list->drawList();
OHstin 6:196a63a3378d 173
OHstin 6:196a63a3378d 174
OHstin 6:196a63a3378d 175 }
OHstin 6:196a63a3378d 176
OHstin 6:196a63a3378d 177 void UtilityScreen::displayStaticData()
OHstin 6:196a63a3378d 178 {
OHstin 6:196a63a3378d 179
OHstin 6:196a63a3378d 180 // the static data is displayed here e.g headings
OHstin 6:196a63a3378d 181 tft.setTextWrap(true);
OHstin 6:196a63a3378d 182 tft.setTextColor(textColor);
OHstin 6:196a63a3378d 183
OHstin 6:196a63a3378d 184 // display title
OHstin 6:196a63a3378d 185 char title[20];
OHstin 6:196a63a3378d 186 if (uCxn){
OHstin 6:196a63a3378d 187 strcpy(title,"UTILITY PORT ON");
OHstin 6:196a63a3378d 188 } else {
OHstin 6:196a63a3378d 189 strcpy(title,"UTILITY PORT OFF");
OHstin 6:196a63a3378d 190 }
OHstin 6:196a63a3378d 191 tft.setCursor((tft.width()/2)-50,4);
OHstin 6:196a63a3378d 192 tft.printf("%s",title);
OHstin 6:196a63a3378d 193
OHstin 6:196a63a3378d 194 // display voltage
OHstin 6:196a63a3378d 195 tft.setCursor(0, 20);
OHstin 6:196a63a3378d 196 tft.printf("VOLTAGE[V]:");
OHstin 6:196a63a3378d 197
OHstin 6:196a63a3378d 198 // display minimum
OHstin 6:196a63a3378d 199 tft.setCursor(0, 35);
OHstin 6:196a63a3378d 200 tft.printf("CURRENT[mA]:");
OHstin 6:196a63a3378d 201
OHstin 6:196a63a3378d 202 // display capacity remaining
OHstin 6:196a63a3378d 203 tft.setCursor(0, 50);
OHstin 6:196a63a3378d 204 tft.printf("POWER[W]:");
OHstin 6:196a63a3378d 205
OHstin 6:196a63a3378d 206 }
OHstin 6:196a63a3378d 207
OHstin 6:196a63a3378d 208
OHstin 6:196a63a3378d 209 void UtilityScreen::displayDynamicData()
OHstin 6:196a63a3378d 210 {
OHstin 6:196a63a3378d 211 // first retrieve data from the sensor suite
OHstin 6:196a63a3378d 212 SensorSuite suite;
OHstin 6:196a63a3378d 213 ConsumptionModel model = suite.getConsumptionData();
OHstin 6:196a63a3378d 214
OHstin 6:196a63a3378d 215 float newCurrent;
OHstin 6:196a63a3378d 216 float newVoltage;
OHstin 6:196a63a3378d 217 float newPower;
OHstin 6:196a63a3378d 218
OHstin 6:196a63a3378d 219 newCurrent = model.consumptionCurrent;
OHstin 6:196a63a3378d 220 newVoltage = model.consumptionVoltage;
OHstin 6:196a63a3378d 221 newPower = newCurrent*newVoltage*0.001;
OHstin 6:196a63a3378d 222
OHstin 6:196a63a3378d 223
OHstin 6:196a63a3378d 224
OHstin 6:196a63a3378d 225
OHstin 6:196a63a3378d 226 tft.setTextWrap(true);
OHstin 6:196a63a3378d 227 //tft.setTextColor(textColor);
OHstin 6:196a63a3378d 228
OHstin 6:196a63a3378d 229 // the dynamic data is displayed here e.g values
OHstin 6:196a63a3378d 230
OHstin 6:196a63a3378d 231 if (newVoltage == voltage)
OHstin 6:196a63a3378d 232 {
OHstin 6:196a63a3378d 233 // do nothing
OHstin 6:196a63a3378d 234 } else {
OHstin 6:196a63a3378d 235
OHstin 6:196a63a3378d 236 tft.setCursor(tft.width()/2, 20);
OHstin 6:196a63a3378d 237 // first delete the previous value
OHstin 6:196a63a3378d 238 tft.setTextColor(backgroundColor);
OHstin 6:196a63a3378d 239 tft.printf("%+6.3f",voltage);
OHstin 6:196a63a3378d 240
OHstin 6:196a63a3378d 241 tft.setCursor(tft.width()/2, 20);
OHstin 6:196a63a3378d 242 // then write the new voltage values
OHstin 6:196a63a3378d 243 tft.setTextColor(textColor);
OHstin 6:196a63a3378d 244 tft.printf("%+6.3f",newVoltage);
OHstin 6:196a63a3378d 245 }
OHstin 6:196a63a3378d 246
OHstin 6:196a63a3378d 247 if (newCurrent == current){
OHstin 6:196a63a3378d 248 // do nothing
OHstin 6:196a63a3378d 249 } else {
OHstin 6:196a63a3378d 250
OHstin 6:196a63a3378d 251 // first delete the previous value
OHstin 6:196a63a3378d 252 tft.setCursor(tft.width()/2, 35);
OHstin 6:196a63a3378d 253 tft.setTextColor(backgroundColor);
OHstin 6:196a63a3378d 254 tft.printf("%+6.3f",current);
OHstin 6:196a63a3378d 255
OHstin 6:196a63a3378d 256 // then write the new current values
OHstin 6:196a63a3378d 257 tft.setCursor(tft.width()/2, 35);
OHstin 6:196a63a3378d 258 tft.setTextColor(textColor);
OHstin 6:196a63a3378d 259 tft.printf("%+6.3f",newCurrent);
OHstin 6:196a63a3378d 260 }
OHstin 6:196a63a3378d 261
OHstin 6:196a63a3378d 262 if (newPower == power)
OHstin 6:196a63a3378d 263 {
OHstin 6:196a63a3378d 264 // do nothing
OHstin 6:196a63a3378d 265 } else {
OHstin 6:196a63a3378d 266
OHstin 6:196a63a3378d 267 tft.setCursor(tft.width()/2, 50);
OHstin 6:196a63a3378d 268 // first delete the previous value
OHstin 6:196a63a3378d 269 tft.setTextColor(backgroundColor);
OHstin 6:196a63a3378d 270 tft.printf("%+6.3f",power);
OHstin 6:196a63a3378d 271
OHstin 6:196a63a3378d 272 tft.setCursor(tft.width()/2, 50);
OHstin 6:196a63a3378d 273 // then write the new voltage values
OHstin 6:196a63a3378d 274 tft.setTextColor(textColor);
OHstin 6:196a63a3378d 275 tft.printf("%+6.3f",newPower);
OHstin 6:196a63a3378d 276 }
OHstin 6:196a63a3378d 277
OHstin 6:196a63a3378d 278
OHstin 6:196a63a3378d 279
OHstin 6:196a63a3378d 280
OHstin 6:196a63a3378d 281 voltage = newVoltage;
OHstin 6:196a63a3378d 282 current = newCurrent;
OHstin 6:196a63a3378d 283 power = newPower;
OHstin 6:196a63a3378d 284 }
OHstin 6:196a63a3378d 285
OHstin 6:196a63a3378d 286
OHstin 6:196a63a3378d 287 void UtilityScreen::deAllocateMemory()
OHstin 6:196a63a3378d 288 {
OHstin 6:196a63a3378d 289 // deallocate memory that was created dynamically
OHstin 6:196a63a3378d 290 delete list;
OHstin 6:196a63a3378d 291 }
OHstin 6:196a63a3378d 292
OHstin 6:196a63a3378d 293 void UtilityScreen::buttonPressed(int button)
OHstin 6:196a63a3378d 294 {
OHstin 6:196a63a3378d 295 switch(button) {
OHstin 6:196a63a3378d 296
OHstin 6:196a63a3378d 297 case BACKBUTTON:
OHstin 6:196a63a3378d 298 // navigate to the previous screen
OHstin 6:196a63a3378d 299 changeScreen = -1;
OHstin 6:196a63a3378d 300 break;
OHstin 6:196a63a3378d 301
OHstin 6:196a63a3378d 302 case SCROLLBUTTON:
OHstin 6:196a63a3378d 303 // scroll to the next option
OHstin 6:196a63a3378d 304 scroll = true;
OHstin 6:196a63a3378d 305 break;
OHstin 6:196a63a3378d 306
OHstin 6:196a63a3378d 307 case ENTERBUTTON:
OHstin 6:196a63a3378d 308 // navigate to the selected option
OHstin 6:196a63a3378d 309 enter = true;
OHstin 6:196a63a3378d 310 break;
OHstin 6:196a63a3378d 311
OHstin 6:196a63a3378d 312 default:
OHstin 6:196a63a3378d 313 // do nothing
OHstin 6:196a63a3378d 314 break;
OHstin 6:196a63a3378d 315 }
OHstin 6:196a63a3378d 316 }
OHstin 6:196a63a3378d 317
OHstin 6:196a63a3378d 318 void UtilityScreen::turnOnUtilityPort()
OHstin 6:196a63a3378d 319 {
OHstin 6:196a63a3378d 320 /**
OHstin 6:196a63a3378d 321 ui_serial_letter *letter2 = ui_serial_mail.alloc();
OHstin 6:196a63a3378d 322 letter2->activateSerial = true;
OHstin 6:196a63a3378d 323 ui_serial_mail.put(letter2);
OHstin 6:196a63a3378d 324
OHstin 6:196a63a3378d 325 // start the thread
OHstin 6:196a63a3378d 326 // launch serial communication with raspberry pi
OHstin 6:196a63a3378d 327
OHstin 6:196a63a3378d 328 thread3.start(raspiSerial);
OHstin 6:196a63a3378d 329
OHstin 6:196a63a3378d 330 **/
OHstin 6:196a63a3378d 331
OHstin 6:196a63a3378d 332 // turn on utility port
OHstin 6:196a63a3378d 333 utilityPin = 1;
OHstin 6:196a63a3378d 334 utilityLED = 1;
OHstin 6:196a63a3378d 335
OHstin 6:196a63a3378d 336 // delete text "UTILITY PORT OFF"
OHstin 6:196a63a3378d 337 tft.setTextColor(backgroundColor);
OHstin 6:196a63a3378d 338 tft.setCursor((tft.width()/2)-50,4);
OHstin 6:196a63a3378d 339 tft.printf("UTILITY PORT OFF");
OHstin 6:196a63a3378d 340
OHstin 6:196a63a3378d 341
OHstin 6:196a63a3378d 342 // print text "UTILITY PORT ON"
OHstin 6:196a63a3378d 343 tft.setTextColor(textColor);
OHstin 6:196a63a3378d 344 tft.setCursor((tft.width()/2)-50,4);
OHstin 6:196a63a3378d 345 tft.printf("UTILITY PORT ON");
OHstin 6:196a63a3378d 346 }
OHstin 6:196a63a3378d 347
OHstin 6:196a63a3378d 348 void UtilityScreen::turnOffUtilityPort()
OHstin 6:196a63a3378d 349 {
OHstin 6:196a63a3378d 350 /**
OHstin 6:196a63a3378d 351 ui_serial_letter *letter2 = ui_serial_mail.alloc();
OHstin 6:196a63a3378d 352 letter2->activateSerial = false;
OHstin 6:196a63a3378d 353 ui_serial_mail.put(letter2);
OHstin 6:196a63a3378d 354 **/
OHstin 6:196a63a3378d 355
OHstin 6:196a63a3378d 356 // turn off utility port
OHstin 6:196a63a3378d 357 utilityPin = 0;
OHstin 6:196a63a3378d 358 utilityLED = 0;
OHstin 6:196a63a3378d 359
OHstin 6:196a63a3378d 360 // delete text "UTILITY PORT ON"
OHstin 6:196a63a3378d 361 tft.setTextColor(backgroundColor);
OHstin 6:196a63a3378d 362 tft.setCursor((tft.width()/2)-50,4);
OHstin 6:196a63a3378d 363 tft.printf("UTILITY PORT ON");
OHstin 6:196a63a3378d 364
OHstin 6:196a63a3378d 365
OHstin 6:196a63a3378d 366 // print text "UTILITY PORT OFF"
OHstin 6:196a63a3378d 367 tft.setTextColor(textColor);
OHstin 6:196a63a3378d 368 tft.setCursor((tft.width()/2)-50,4);
OHstin 6:196a63a3378d 369 tft.printf("UTILITY PORT OFF");
OHstin 6:196a63a3378d 370 }
OHstin 6:196a63a3378d 371
OHstin 6:196a63a3378d 372 #endif