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
Parent:
4:c7b0670f96b2
Final Code for mbed operation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
OHstin 4:c7b0670f96b2 1 #ifndef RASPBERRYPISCREEN_H
OHstin 4:c7b0670f96b2 2 #define RASPBERRYPISCREEN_H
OHstin 4:c7b0670f96b2 3
OHstin 4:c7b0670f96b2 4 #include "RaspiSerial.h"
OHstin 4:c7b0670f96b2 5 Thread thread3(osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25), NULL);
OHstin 4:c7b0670f96b2 6
OHstin 4:c7b0670f96b2 7 int piCxn = 0; // tracks the connection of the raspberry pi
OHstin 4:c7b0670f96b2 8 int prevpiCxn = 0; // tracks the previous connection of the raspberry pi
OHstin 4:c7b0670f96b2 9
OHstin 4:c7b0670f96b2 10 class RaspberryPiScreen: public Button <RaspberryPiScreen>
OHstin 4:c7b0670f96b2 11 {
OHstin 4:c7b0670f96b2 12 public:
OHstin 4:c7b0670f96b2 13 RaspberryPiScreen(uint16_t backgroundColor);
OHstin 4:c7b0670f96b2 14 int start();
OHstin 4:c7b0670f96b2 15 void buttonPressed(int button);
OHstin 4:c7b0670f96b2 16
OHstin 4:c7b0670f96b2 17 private:
OHstin 4:c7b0670f96b2 18 void drawRaspberryPiScreen();
OHstin 4:c7b0670f96b2 19 void drawData();
OHstin 4:c7b0670f96b2 20 void updateData();
OHstin 4:c7b0670f96b2 21 void deAllocateMemory();
OHstin 4:c7b0670f96b2 22 void connectPi();
OHstin 4:c7b0670f96b2 23 void disconnectPi();
OHstin 4:c7b0670f96b2 24
OHstin 4:c7b0670f96b2 25 // list
OHstin 4:c7b0670f96b2 26 ListController *list;
OHstin 4:c7b0670f96b2 27 // updated when a scroll event occurs
OHstin 4:c7b0670f96b2 28 bool scroll;
OHstin 4:c7b0670f96b2 29 // updated when an enter event occurs
OHstin 4:c7b0670f96b2 30 bool enter;
OHstin 4:c7b0670f96b2 31 // background color
OHstin 4:c7b0670f96b2 32 uint16_t bgColor;
OHstin 4:c7b0670f96b2 33 // text color;
OHstin 4:c7b0670f96b2 34 uint16_t textColor;
OHstin 4:c7b0670f96b2 35
OHstin 4:c7b0670f96b2 36 int changeScreen;
OHstin 4:c7b0670f96b2 37 bool piConnection; // tracks whether connection with the rpi is initiated
OHstin 4:c7b0670f96b2 38 bool camera; // contains the state of the camera
OHstin 4:c7b0670f96b2 39 bool sensor; // holds the state of the sensor
OHstin 4:c7b0670f96b2 40 bool internet; // holds the state of the internet
OHstin 4:c7b0670f96b2 41
OHstin 4:c7b0670f96b2 42 };
OHstin 4:c7b0670f96b2 43
OHstin 4:c7b0670f96b2 44 RaspberryPiScreen::RaspberryPiScreen(uint16_t backgroundColor)
OHstin 4:c7b0670f96b2 45 {
OHstin 4:c7b0670f96b2 46 bgColor = backgroundColor;
OHstin 4:c7b0670f96b2 47 // determine the color of text
OHstin 4:c7b0670f96b2 48 if (bgColor == ST7735_WHITE ) {
OHstin 4:c7b0670f96b2 49 textColor = ST7735_RED;
OHstin 4:c7b0670f96b2 50 } else if (bgColor == ST7735_BLACK) {
OHstin 4:c7b0670f96b2 51 textColor = ST7735_CYAN;
OHstin 4:c7b0670f96b2 52 }
OHstin 4:c7b0670f96b2 53 }
OHstin 4:c7b0670f96b2 54
OHstin 4:c7b0670f96b2 55 int RaspberryPiScreen::start()
OHstin 4:c7b0670f96b2 56 {
OHstin 4:c7b0670f96b2 57 // draw the screen
OHstin 4:c7b0670f96b2 58 drawRaspberryPiScreen();
OHstin 4:c7b0670f96b2 59
OHstin 4:c7b0670f96b2 60 // attach interrupts to buttons
OHstin 4:c7b0670f96b2 61 Button<RaspberryPiScreen>::activateButtons(this);
OHstin 4:c7b0670f96b2 62
OHstin 4:c7b0670f96b2 63 changeScreen = 0;
OHstin 4:c7b0670f96b2 64 scroll = false;
OHstin 4:c7b0670f96b2 65 enter = false;
OHstin 4:c7b0670f96b2 66
OHstin 4:c7b0670f96b2 67 while(changeScreen==0) {
OHstin 4:c7b0670f96b2 68
OHstin 4:c7b0670f96b2 69 // update the raspberyy pi data
OHstin 4:c7b0670f96b2 70 updateData();
OHstin 4:c7b0670f96b2 71
OHstin 4:c7b0670f96b2 72 // scroll event occured
OHstin 4:c7b0670f96b2 73 if(scroll) {
OHstin 4:c7b0670f96b2 74 // perform scroll on the list
OHstin 4:c7b0670f96b2 75 list->scroll();
OHstin 4:c7b0670f96b2 76 // reset variable
OHstin 4:c7b0670f96b2 77 scroll = false;
OHstin 4:c7b0670f96b2 78 }
OHstin 4:c7b0670f96b2 79
OHstin 4:c7b0670f96b2 80 // enter event occured
OHstin 4:c7b0670f96b2 81 if (enter) {
OHstin 4:c7b0670f96b2 82 // read the current option on the list
OHstin 4:c7b0670f96b2 83 int option = list->getCurrentOption();
OHstin 4:c7b0670f96b2 84
OHstin 4:c7b0670f96b2 85 if (option == 1) {
OHstin 4:c7b0670f96b2 86 // make connection with pi
OHstin 4:c7b0670f96b2 87 //myled = 1;
OHstin 4:c7b0670f96b2 88
OHstin 4:c7b0670f96b2 89 // first check connection with the pi
OHstin 4:c7b0670f96b2 90 //piCxn = 1;
OHstin 4:c7b0670f96b2 91 // pi is not yet connected
OHstin 4:c7b0670f96b2 92 if (piCxn == 0) {
OHstin 4:c7b0670f96b2 93 // pi attempts to connect
OHstin 4:c7b0670f96b2 94 piCxn = 1;
OHstin 6:196a63a3378d 95
OHstin 6:196a63a3378d 96 // turn on pi LED
OHstin 4:c7b0670f96b2 97
OHstin 4:c7b0670f96b2 98 // initiate connection here
OHstin 4:c7b0670f96b2 99 connectPi();
OHstin 4:c7b0670f96b2 100
OHstin 4:c7b0670f96b2 101 } else {
OHstin 4:c7b0670f96b2 102 // do nothing
OHstin 4:c7b0670f96b2 103 }
OHstin 4:c7b0670f96b2 104
OHstin 4:c7b0670f96b2 105
OHstin 4:c7b0670f96b2 106 } else {
OHstin 4:c7b0670f96b2 107 // disconnect with pi
OHstin 4:c7b0670f96b2 108 //piCxn = 3;
OHstin 4:c7b0670f96b2 109
OHstin 4:c7b0670f96b2 110 // first check the pi connection
OHstin 4:c7b0670f96b2 111 if (piCxn == 2) { // pi connection is active
OHstin 4:c7b0670f96b2 112 // pi attempts to disconnect
OHstin 4:c7b0670f96b2 113 piCxn = 3;
OHstin 4:c7b0670f96b2 114 disconnectPi();
OHstin 4:c7b0670f96b2 115
OHstin 4:c7b0670f96b2 116 } else {
OHstin 4:c7b0670f96b2 117 // do nothing
OHstin 4:c7b0670f96b2 118 }
OHstin 4:c7b0670f96b2 119 //myled = 0;
OHstin 4:c7b0670f96b2 120 }
OHstin 4:c7b0670f96b2 121 enter = false;
OHstin 4:c7b0670f96b2 122 }
OHstin 4:c7b0670f96b2 123
OHstin 4:c7b0670f96b2 124 Thread::wait(200);
OHstin 4:c7b0670f96b2 125 }
OHstin 4:c7b0670f96b2 126
OHstin 4:c7b0670f96b2 127 // detach interrupts to buttons
OHstin 4:c7b0670f96b2 128 Button<RaspberryPiScreen>::deActivateButtons();
OHstin 4:c7b0670f96b2 129
OHstin 4:c7b0670f96b2 130 // free up any memory
OHstin 4:c7b0670f96b2 131 deAllocateMemory();
OHstin 4:c7b0670f96b2 132
OHstin 4:c7b0670f96b2 133 return changeScreen;
OHstin 4:c7b0670f96b2 134 }
OHstin 4:c7b0670f96b2 135
OHstin 4:c7b0670f96b2 136 void RaspberryPiScreen::updateData()
OHstin 4:c7b0670f96b2 137 {
OHstin 4:c7b0670f96b2 138
OHstin 4:c7b0670f96b2 139 // open the mail and check for messages
OHstin 4:c7b0670f96b2 140
OHstin 4:c7b0670f96b2 141
OHstin 4:c7b0670f96b2 142 osEvent evt = serial_ui_mail.get(0);
OHstin 4:c7b0670f96b2 143 if(evt.status == osEventMail) {
OHstin 4:c7b0670f96b2 144 serial_ui_letter *letter = (serial_ui_letter*)evt.value.p;
OHstin 4:c7b0670f96b2 145 piConnection = letter->piStat;
OHstin 4:c7b0670f96b2 146 camera = letter->camStat;
OHstin 4:c7b0670f96b2 147 internet = letter->cloudStat;
OHstin 4:c7b0670f96b2 148 sensor = letter->detectionStat;
OHstin 4:c7b0670f96b2 149 // delete the message
OHstin 4:c7b0670f96b2 150 serial_ui_mail.free(letter);
OHstin 4:c7b0670f96b2 151 }
OHstin 4:c7b0670f96b2 152
OHstin 4:c7b0670f96b2 153 // if the pi is in transition
OHstin 4:c7b0670f96b2 154 if (piCxn == 1 && piConnection == true)
OHstin 4:c7b0670f96b2 155 {
OHstin 4:c7b0670f96b2 156 piCxn = 2;
OHstin 4:c7b0670f96b2 157 }
OHstin 4:c7b0670f96b2 158 if (piCxn == 3 && piConnection == false)
OHstin 4:c7b0670f96b2 159 {
OHstin 4:c7b0670f96b2 160 piCxn = 0;
OHstin 4:c7b0670f96b2 161 }
OHstin 4:c7b0670f96b2 162
OHstin 4:c7b0670f96b2 163
OHstin 4:c7b0670f96b2 164
OHstin 4:c7b0670f96b2 165
OHstin 4:c7b0670f96b2 166
OHstin 4:c7b0670f96b2 167
OHstin 4:c7b0670f96b2 168 // update the title
OHstin 4:c7b0670f96b2 169 if(piCxn == prevpiCxn) {
OHstin 4:c7b0670f96b2 170 // do nothing
OHstin 4:c7b0670f96b2 171 } else {
OHstin 4:c7b0670f96b2 172
OHstin 4:c7b0670f96b2 173
OHstin 4:c7b0670f96b2 174 tft.setTextColor(bgColor);
OHstin 4:c7b0670f96b2 175 tft.setCursor((tft.width()/2)-30,4);
OHstin 4:c7b0670f96b2 176 // first delete the previous connection words
OHstin 4:c7b0670f96b2 177 switch(prevpiCxn) {
OHstin 4:c7b0670f96b2 178
OHstin 4:c7b0670f96b2 179 // delete "DISCONNECTED"
OHstin 4:c7b0670f96b2 180 case 0:
OHstin 4:c7b0670f96b2 181 tft.printf("DISCONNECTED");
OHstin 4:c7b0670f96b2 182 break;
OHstin 4:c7b0670f96b2 183 // delete "CONNECTING..."
OHstin 4:c7b0670f96b2 184 case 1:
OHstin 4:c7b0670f96b2 185 tft.printf("CONNECTING...");
OHstin 4:c7b0670f96b2 186 break;
OHstin 4:c7b0670f96b2 187 // delete "CONNECTED"
OHstin 4:c7b0670f96b2 188 case 2:
OHstin 4:c7b0670f96b2 189 tft.printf("CONNECTED");
OHstin 4:c7b0670f96b2 190 break;
OHstin 4:c7b0670f96b2 191 case 3:
OHstin 4:c7b0670f96b2 192 // delete "DISCONNECTING..."
OHstin 4:c7b0670f96b2 193 tft.printf("DISCONNECTING...");
OHstin 4:c7b0670f96b2 194 break;
OHstin 4:c7b0670f96b2 195
OHstin 4:c7b0670f96b2 196 default:
OHstin 4:c7b0670f96b2 197 break;
OHstin 4:c7b0670f96b2 198 }
OHstin 4:c7b0670f96b2 199
OHstin 4:c7b0670f96b2 200 tft.setTextColor(textColor);
OHstin 4:c7b0670f96b2 201 tft.setCursor((tft.width()/2)-30,4);
OHstin 4:c7b0670f96b2 202 // secondly write the new connection words
OHstin 4:c7b0670f96b2 203 switch(piCxn) {
OHstin 4:c7b0670f96b2 204
OHstin 6:196a63a3378d 205 // write "DISCONNECTED"
OHstin 4:c7b0670f96b2 206 case 0:
OHstin 4:c7b0670f96b2 207 tft.printf("DISCONNECTED");
OHstin 6:196a63a3378d 208 // turn off PI LED
OHstin 6:196a63a3378d 209 RPiLED = 0;
OHstin 4:c7b0670f96b2 210 break;
OHstin 6:196a63a3378d 211 // write "CONNECTING..."
OHstin 4:c7b0670f96b2 212 case 1:
OHstin 4:c7b0670f96b2 213 tft.printf("CONNECTING...");
OHstin 6:196a63a3378d 214 // turn on PI LED
OHstin 6:196a63a3378d 215 RPiLED = 1;
OHstin 4:c7b0670f96b2 216 break;
OHstin 6:196a63a3378d 217 // write "CONNECTED"
OHstin 4:c7b0670f96b2 218 case 2:
OHstin 4:c7b0670f96b2 219 tft.printf("CONNECTED");
OHstin 4:c7b0670f96b2 220 break;
OHstin 4:c7b0670f96b2 221 case 3:
OHstin 6:196a63a3378d 222 // write "DISCONNECTING..."
OHstin 4:c7b0670f96b2 223 tft.printf("DISCONNECTING...");
OHstin 4:c7b0670f96b2 224 break;
OHstin 4:c7b0670f96b2 225
OHstin 4:c7b0670f96b2 226 default:
OHstin 4:c7b0670f96b2 227 break;
OHstin 4:c7b0670f96b2 228 }
OHstin 4:c7b0670f96b2 229
OHstin 4:c7b0670f96b2 230 }
OHstin 4:c7b0670f96b2 231
OHstin 4:c7b0670f96b2 232
OHstin 4:c7b0670f96b2 233
OHstin 4:c7b0670f96b2 234 // update the prevCxnStatus
OHstin 4:c7b0670f96b2 235 prevpiCxn = piCxn;
OHstin 4:c7b0670f96b2 236
OHstin 4:c7b0670f96b2 237 }
OHstin 4:c7b0670f96b2 238
OHstin 4:c7b0670f96b2 239 void RaspberryPiScreen::drawRaspberryPiScreen()
OHstin 4:c7b0670f96b2 240 {
OHstin 4:c7b0670f96b2 241 // draw static data here
OHstin 4:c7b0670f96b2 242 tft.setTextColor(textColor);
OHstin 4:c7b0670f96b2 243
OHstin 4:c7b0670f96b2 244 // display title
OHstin 4:c7b0670f96b2 245 tft.setCursor((tft.width()/2)-50,4);
OHstin 4:c7b0670f96b2 246 tft.printf("PI:");
OHstin 4:c7b0670f96b2 247
OHstin 4:c7b0670f96b2 248 // display camera
OHstin 4:c7b0670f96b2 249 tft.setCursor(0,20);
OHstin 4:c7b0670f96b2 250 tft.printf("camera:");
OHstin 4:c7b0670f96b2 251
OHstin 4:c7b0670f96b2 252 // display cloud
OHstin 4:c7b0670f96b2 253 tft.setCursor(0,35);
OHstin 4:c7b0670f96b2 254 tft.printf("internet:");
OHstin 4:c7b0670f96b2 255
OHstin 4:c7b0670f96b2 256 // display sensor
OHstin 4:c7b0670f96b2 257 tft.setCursor(0,50);
OHstin 4:c7b0670f96b2 258 tft.printf("sensor:");
OHstin 4:c7b0670f96b2 259
OHstin 4:c7b0670f96b2 260
OHstin 4:c7b0670f96b2 261
OHstin 4:c7b0670f96b2 262 // construct the list
OHstin 4:c7b0670f96b2 263 list = new ListController( 0,
OHstin 4:c7b0670f96b2 264 63,
OHstin 4:c7b0670f96b2 265 "Turn Pi on",
OHstin 4:c7b0670f96b2 266 "Yes",
OHstin 4:c7b0670f96b2 267 "No",
OHstin 4:c7b0670f96b2 268 "",
OHstin 4:c7b0670f96b2 269 "",
OHstin 4:c7b0670f96b2 270 bgColor,
OHstin 4:c7b0670f96b2 271 2);
OHstin 4:c7b0670f96b2 272
OHstin 4:c7b0670f96b2 273 // draw the list
OHstin 4:c7b0670f96b2 274 list->drawList();
OHstin 4:c7b0670f96b2 275
OHstin 4:c7b0670f96b2 276 // initialise dynamic data
OHstin 4:c7b0670f96b2 277 tft.setTextColor(textColor);
OHstin 4:c7b0670f96b2 278 tft.setCursor((tft.width()/2)-30,4);
OHstin 4:c7b0670f96b2 279 // first delete the previous connection words
OHstin 4:c7b0670f96b2 280 switch(prevpiCxn) {
OHstin 4:c7b0670f96b2 281
OHstin 4:c7b0670f96b2 282 // delete "DISCONNECTED"
OHstin 4:c7b0670f96b2 283 case 0:
OHstin 4:c7b0670f96b2 284 tft.printf("DISCONNECTED");
OHstin 4:c7b0670f96b2 285 break;
OHstin 4:c7b0670f96b2 286 // delete "CONNECTING..."
OHstin 4:c7b0670f96b2 287 case 1:
OHstin 4:c7b0670f96b2 288 tft.printf("CONNECTING...");
OHstin 4:c7b0670f96b2 289 break;
OHstin 4:c7b0670f96b2 290 // delete "CONNECTED"
OHstin 4:c7b0670f96b2 291 case 2:
OHstin 4:c7b0670f96b2 292 tft.printf("CONNECTED");
OHstin 4:c7b0670f96b2 293 break;
OHstin 4:c7b0670f96b2 294 case 3:
OHstin 4:c7b0670f96b2 295 // delete "DISCONNECTING..."
OHstin 4:c7b0670f96b2 296 tft.printf("DISCONNECTING...");
OHstin 4:c7b0670f96b2 297 break;
OHstin 4:c7b0670f96b2 298
OHstin 4:c7b0670f96b2 299 default:
OHstin 4:c7b0670f96b2 300 break;
OHstin 4:c7b0670f96b2 301 }
OHstin 4:c7b0670f96b2 302 }
OHstin 4:c7b0670f96b2 303
OHstin 4:c7b0670f96b2 304 void RaspberryPiScreen::deAllocateMemory()
OHstin 4:c7b0670f96b2 305 {
OHstin 4:c7b0670f96b2 306 // deallocate memory that was created dynamically
OHstin 4:c7b0670f96b2 307 delete list;
OHstin 4:c7b0670f96b2 308 }
OHstin 4:c7b0670f96b2 309
OHstin 4:c7b0670f96b2 310 void RaspberryPiScreen::buttonPressed(int button)
OHstin 4:c7b0670f96b2 311 {
OHstin 4:c7b0670f96b2 312 switch(button) {
OHstin 4:c7b0670f96b2 313
OHstin 4:c7b0670f96b2 314 case BACKBUTTON:
OHstin 4:c7b0670f96b2 315 // navigate to the previous screen
OHstin 4:c7b0670f96b2 316 changeScreen = -1;
OHstin 4:c7b0670f96b2 317 break;
OHstin 4:c7b0670f96b2 318
OHstin 4:c7b0670f96b2 319 case SCROLLBUTTON:
OHstin 4:c7b0670f96b2 320 // scroll to the next option
OHstin 4:c7b0670f96b2 321 scroll = true;
OHstin 4:c7b0670f96b2 322 break;
OHstin 4:c7b0670f96b2 323
OHstin 4:c7b0670f96b2 324 case ENTERBUTTON:
OHstin 4:c7b0670f96b2 325 // navigate to the selected option
OHstin 4:c7b0670f96b2 326 enter = true;
OHstin 4:c7b0670f96b2 327 break;
OHstin 4:c7b0670f96b2 328
OHstin 4:c7b0670f96b2 329 default:
OHstin 4:c7b0670f96b2 330 // do nothing
OHstin 4:c7b0670f96b2 331 break;
OHstin 4:c7b0670f96b2 332 }
OHstin 4:c7b0670f96b2 333 }
OHstin 4:c7b0670f96b2 334
OHstin 4:c7b0670f96b2 335 void RaspberryPiScreen::connectPi()
OHstin 4:c7b0670f96b2 336 {
OHstin 4:c7b0670f96b2 337 ui_serial_letter *letter2 = ui_serial_mail.alloc();
OHstin 4:c7b0670f96b2 338 letter2->activateSerial = true;
OHstin 4:c7b0670f96b2 339 ui_serial_mail.put(letter2);
OHstin 4:c7b0670f96b2 340
OHstin 4:c7b0670f96b2 341 // start the thread
OHstin 4:c7b0670f96b2 342 // launch serial communication with raspberry pi
OHstin 4:c7b0670f96b2 343
OHstin 4:c7b0670f96b2 344 thread3.start(raspiSerial);
OHstin 4:c7b0670f96b2 345 }
OHstin 4:c7b0670f96b2 346
OHstin 4:c7b0670f96b2 347 void RaspberryPiScreen::disconnectPi()
OHstin 4:c7b0670f96b2 348 {
OHstin 4:c7b0670f96b2 349 ui_serial_letter *letter2 = ui_serial_mail.alloc();
OHstin 4:c7b0670f96b2 350 letter2->activateSerial = false;
OHstin 4:c7b0670f96b2 351 ui_serial_mail.put(letter2);
OHstin 4:c7b0670f96b2 352 }
OHstin 4:c7b0670f96b2 353
OHstin 4:c7b0670f96b2 354 #endif