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:
3:7666de697752
Final Code for mbed operation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
OHstin 3:7666de697752 1 #ifndef BATTERIESSCREEN_H
OHstin 3:7666de697752 2 #define BATTERIESSCREEN_H
OHstin 3:7666de697752 3
OHstin 3:7666de697752 4 #include "ListController.h"
OHstin 3:7666de697752 5 #include "SensorSuite.h"
OHstin 3:7666de697752 6 #include "SensorModel.h"
OHstin 3:7666de697752 7
OHstin 3:7666de697752 8 class BatteriesScreen : public Button <BatteriesScreen>
OHstin 3:7666de697752 9 {
OHstin 3:7666de697752 10 public:
OHstin 3:7666de697752 11 BatteriesScreen(uint16_t backgroundColor);
OHstin 3:7666de697752 12 int start();
OHstin 3:7666de697752 13 void buttonPressed(int button);
OHstin 3:7666de697752 14
OHstin 3:7666de697752 15 private:
OHstin 3:7666de697752 16
OHstin 3:7666de697752 17 void drawBatteriesScreen();
OHstin 3:7666de697752 18 void drawTile();
OHstin 3:7666de697752 19 void updateTile();
OHstin 3:7666de697752 20 void deAllocateMemory();
OHstin 3:7666de697752 21
OHstin 3:7666de697752 22 // Icons
OHstin 3:7666de697752 23 BatteryIcon *batteryOne;
OHstin 3:7666de697752 24 BatteryIcon *batteryTwo;
OHstin 3:7666de697752 25
OHstin 3:7666de697752 26 // list
OHstin 3:7666de697752 27 ListController *list;
OHstin 3:7666de697752 28
OHstin 3:7666de697752 29 // updated when a scroll event occurs
OHstin 3:7666de697752 30 bool scroll;
OHstin 3:7666de697752 31 // updated when an enter event occurs
OHstin 3:7666de697752 32 bool enter;
OHstin 3:7666de697752 33 // background color
OHstin 3:7666de697752 34 uint16_t bgColor;
OHstin 3:7666de697752 35
OHstin 3:7666de697752 36 int changeScreen;
OHstin 3:7666de697752 37 };
OHstin 3:7666de697752 38
OHstin 3:7666de697752 39 BatteriesScreen::BatteriesScreen(uint16_t backgroundColor)
OHstin 3:7666de697752 40 {
OHstin 3:7666de697752 41
OHstin 3:7666de697752 42 bgColor = backgroundColor;
OHstin 3:7666de697752 43 }
OHstin 3:7666de697752 44
OHstin 3:7666de697752 45 int BatteriesScreen::start()
OHstin 3:7666de697752 46 {
OHstin 3:7666de697752 47
OHstin 3:7666de697752 48
OHstin 3:7666de697752 49
OHstin 3:7666de697752 50 // draw the screen
OHstin 3:7666de697752 51 drawBatteriesScreen();
OHstin 3:7666de697752 52
OHstin 3:7666de697752 53 // attach interrupts to buttons
OHstin 3:7666de697752 54 Button<BatteriesScreen>::activateButtons(this);
OHstin 3:7666de697752 55
OHstin 3:7666de697752 56 changeScreen = 0;
OHstin 3:7666de697752 57 scroll = false;
OHstin 3:7666de697752 58 enter = false;
OHstin 3:7666de697752 59
OHstin 3:7666de697752 60
OHstin 3:7666de697752 61 while (changeScreen == 0) {
OHstin 3:7666de697752 62
OHstin 3:7666de697752 63 // update the title accordingly
OHstin 3:7666de697752 64 updateTile();
OHstin 3:7666de697752 65
OHstin 3:7666de697752 66 // scroll event occured
OHstin 3:7666de697752 67 if (scroll) {
OHstin 3:7666de697752 68 // perform scroll on the list
OHstin 3:7666de697752 69 list->scroll();
OHstin 3:7666de697752 70 // reset variable
OHstin 3:7666de697752 71 scroll = false;
OHstin 3:7666de697752 72 }
OHstin 3:7666de697752 73
OHstin 3:7666de697752 74 // enter event occured
OHstin 3:7666de697752 75 if (enter) {
OHstin 3:7666de697752 76 // read the current option on the list
OHstin 3:7666de697752 77 changeScreen = list->getCurrentOption();
OHstin 3:7666de697752 78 }
OHstin 3:7666de697752 79 //wait_ms(200);
OHstin 3:7666de697752 80 Thread::wait(200);
OHstin 3:7666de697752 81 }
OHstin 3:7666de697752 82
OHstin 3:7666de697752 83 // dettach interrupts to buttons
OHstin 3:7666de697752 84 Button<BatteriesScreen>::deActivateButtons();
OHstin 3:7666de697752 85
OHstin 3:7666de697752 86 // free up some memory
OHstin 3:7666de697752 87 deAllocateMemory();
OHstin 3:7666de697752 88
OHstin 3:7666de697752 89 return changeScreen;
OHstin 3:7666de697752 90 }
OHstin 3:7666de697752 91
OHstin 3:7666de697752 92 void BatteriesScreen::updateTile()
OHstin 3:7666de697752 93 {
OHstin 3:7666de697752 94
OHstin 3:7666de697752 95 // percentage
OHstin 6:196a63a3378d 96 int batteryOnePercentage = getBatteryOnePercentage();
OHstin 6:196a63a3378d 97 int batteryTwoPercentage = getBatteryTwoPercentage();
OHstin 3:7666de697752 98
OHstin 3:7666de697752 99 // batteryStatus
OHstin 3:7666de697752 100 int batteryStatus = getBatteryStatus();
OHstin 3:7666de697752 101
OHstin 3:7666de697752 102 // track state of individual battery
OHstin 3:7666de697752 103 bool batteryOneCharging = false;
OHstin 3:7666de697752 104 bool batteryTwoCharging = false;
OHstin 3:7666de697752 105
OHstin 3:7666de697752 106 if (batteryStatus == 1 || batteryStatus == 3) {
OHstin 3:7666de697752 107 batteryOneCharging = true;
OHstin 3:7666de697752 108 }
OHstin 3:7666de697752 109 if (batteryStatus == 2 || batteryStatus == 3) {
OHstin 3:7666de697752 110 batteryTwoCharging = true;
OHstin 3:7666de697752 111 }
OHstin 3:7666de697752 112
OHstin 6:196a63a3378d 113 batteryOne->setBatteryPercentage(batteryOnePercentage, batteryOneCharging);
OHstin 6:196a63a3378d 114 batteryTwo->setBatteryPercentage(batteryTwoPercentage, batteryTwoCharging);
OHstin 3:7666de697752 115 }
OHstin 3:7666de697752 116
OHstin 3:7666de697752 117
OHstin 3:7666de697752 118 void BatteriesScreen::drawBatteriesScreen()
OHstin 3:7666de697752 119 {
OHstin 3:7666de697752 120
OHstin 3:7666de697752 121 // draw the tile
OHstin 3:7666de697752 122 drawTile();
OHstin 3:7666de697752 123
OHstin 3:7666de697752 124 // construct the list
OHstin 3:7666de697752 125 list = new ListController( 0,
OHstin 3:7666de697752 126 63,
OHstin 3:7666de697752 127 "Batteries",
OHstin 3:7666de697752 128 "Battery One",
OHstin 3:7666de697752 129 "Battery Two",
OHstin 3:7666de697752 130 "",
OHstin 3:7666de697752 131 "",
OHstin 3:7666de697752 132 bgColor,
OHstin 3:7666de697752 133 2);
OHstin 3:7666de697752 134
OHstin 3:7666de697752 135 // draw the list
OHstin 3:7666de697752 136 list->drawList();
OHstin 3:7666de697752 137
OHstin 3:7666de697752 138 }
OHstin 3:7666de697752 139
OHstin 3:7666de697752 140 void BatteriesScreen::drawTile()
OHstin 3:7666de697752 141 {
OHstin 3:7666de697752 142 // first draw the blue background
OHstin 3:7666de697752 143 tft.fillRect(0,0,tft.width(),63,ST7735_BLUE);
OHstin 3:7666de697752 144
OHstin 3:7666de697752 145 // retrieve batteryPercentage
OHstin 3:7666de697752 146 int batteryPercentage = getBatteryPercentage();
OHstin 3:7666de697752 147
OHstin 3:7666de697752 148 // batteryStatus
OHstin 3:7666de697752 149 int batteryStatus = getBatteryStatus();
OHstin 3:7666de697752 150
OHstin 3:7666de697752 151 // track state of individual battery
OHstin 3:7666de697752 152 bool batteryOneCharging = false;
OHstin 3:7666de697752 153 bool batteryTwoCharging = false;
OHstin 3:7666de697752 154
OHstin 3:7666de697752 155 if (batteryStatus == 1 || batteryStatus == 3) {
OHstin 3:7666de697752 156 batteryOneCharging = true;
OHstin 3:7666de697752 157 }
OHstin 3:7666de697752 158 if (batteryStatus == 2 || batteryStatus == 3) {
OHstin 3:7666de697752 159 batteryTwoCharging = true;
OHstin 3:7666de697752 160 }
OHstin 3:7666de697752 161
OHstin 3:7666de697752 162 // Coordinates for battery one
OHstin 3:7666de697752 163 int x = 5;
OHstin 3:7666de697752 164 int y = 5;
OHstin 3:7666de697752 165
OHstin 3:7666de697752 166 // initialise the first battery icon
OHstin 3:7666de697752 167 batteryOne = new BatteryIcon(x,y,ST7735_BLUE,batteryPercentage,batteryOneCharging);
OHstin 3:7666de697752 168
OHstin 3:7666de697752 169 // Coordinates for battery two
OHstin 3:7666de697752 170 int x1 = 83;
OHstin 3:7666de697752 171 int y1 = 5;
OHstin 3:7666de697752 172
OHstin 3:7666de697752 173 // Initialise the second battery icon
OHstin 3:7666de697752 174 batteryTwo = new BatteryIcon(x1,y1,ST7735_BLUE,batteryPercentage,batteryTwoCharging);
OHstin 3:7666de697752 175
OHstin 3:7666de697752 176 // draw the batteryIcons
OHstin 3:7666de697752 177 batteryOne->drawBatteryIcon();
OHstin 3:7666de697752 178 batteryTwo->drawBatteryIcon();
OHstin 3:7666de697752 179
OHstin 3:7666de697752 180 // draw underlying text
OHstin 3:7666de697752 181 tft.setCursor(13,50);
OHstin 3:7666de697752 182 tft.setTextColor(ST7735_WHITE);
OHstin 3:7666de697752 183 tft.printf("B1");
OHstin 3:7666de697752 184
OHstin 3:7666de697752 185 tft.setCursor(91,50);
OHstin 3:7666de697752 186 tft.setTextColor(ST7735_WHITE);
OHstin 3:7666de697752 187 tft.printf("B2");
OHstin 3:7666de697752 188
OHstin 3:7666de697752 189 }
OHstin 3:7666de697752 190
OHstin 3:7666de697752 191 void BatteriesScreen::deAllocateMemory()
OHstin 3:7666de697752 192 {
OHstin 3:7666de697752 193 // deallocate memory that was created dynamically
OHstin 3:7666de697752 194 delete batteryOne;
OHstin 3:7666de697752 195 delete batteryTwo;
OHstin 3:7666de697752 196 delete list;
OHstin 3:7666de697752 197
OHstin 3:7666de697752 198 }
OHstin 3:7666de697752 199
OHstin 3:7666de697752 200 void BatteriesScreen::buttonPressed(int button)
OHstin 3:7666de697752 201 {
OHstin 3:7666de697752 202 switch(button) {
OHstin 3:7666de697752 203
OHstin 3:7666de697752 204 case BACKBUTTON:
OHstin 3:7666de697752 205 // navigate to the previous screen
OHstin 3:7666de697752 206 changeScreen = -1;
OHstin 3:7666de697752 207 break;
OHstin 3:7666de697752 208
OHstin 3:7666de697752 209 case SCROLLBUTTON:
OHstin 3:7666de697752 210 // scroll to the next option
OHstin 3:7666de697752 211 scroll = true;
OHstin 3:7666de697752 212 break;
OHstin 3:7666de697752 213
OHstin 3:7666de697752 214 case ENTERBUTTON:
OHstin 3:7666de697752 215 // navigate to the selected option
OHstin 3:7666de697752 216 enter = true;
OHstin 3:7666de697752 217 break;
OHstin 3:7666de697752 218
OHstin 3:7666de697752 219 default:
OHstin 3:7666de697752 220 // do nothing
OHstin 3:7666de697752 221 break;
OHstin 3:7666de697752 222 }
OHstin 3:7666de697752 223 }
OHstin 3:7666de697752 224
OHstin 3:7666de697752 225
OHstin 3:7666de697752 226 #endif
OHstin 3:7666de697752 227