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
Screens/BatteriesScreen.h@3:7666de697752, 2017-02-04 (annotated)
- Committer:
- OHstin
- Date:
- Sat Feb 04 20:17:44 2017 +0000
- Revision:
- 3:7666de697752
- Child:
- 6:196a63a3378d
This version before adding serial communication and pathways
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:7666de697752 | 96 | int batteryPercentage = getBatteryPercentage(); |
OHstin | 3:7666de697752 | 97 | |
OHstin | 3:7666de697752 | 98 | // batteryStatus |
OHstin | 3:7666de697752 | 99 | int batteryStatus = getBatteryStatus(); |
OHstin | 3:7666de697752 | 100 | |
OHstin | 3:7666de697752 | 101 | // track state of individual battery |
OHstin | 3:7666de697752 | 102 | bool batteryOneCharging = false; |
OHstin | 3:7666de697752 | 103 | bool batteryTwoCharging = false; |
OHstin | 3:7666de697752 | 104 | |
OHstin | 3:7666de697752 | 105 | if (batteryStatus == 1 || batteryStatus == 3) { |
OHstin | 3:7666de697752 | 106 | batteryOneCharging = true; |
OHstin | 3:7666de697752 | 107 | } |
OHstin | 3:7666de697752 | 108 | if (batteryStatus == 2 || batteryStatus == 3) { |
OHstin | 3:7666de697752 | 109 | batteryTwoCharging = true; |
OHstin | 3:7666de697752 | 110 | } |
OHstin | 3:7666de697752 | 111 | |
OHstin | 3:7666de697752 | 112 | batteryOne->setBatteryPercentage(batteryPercentage, batteryOneCharging); |
OHstin | 3:7666de697752 | 113 | batteryTwo->setBatteryPercentage(batteryPercentage, batteryTwoCharging); |
OHstin | 3:7666de697752 | 114 | } |
OHstin | 3:7666de697752 | 115 | |
OHstin | 3:7666de697752 | 116 | |
OHstin | 3:7666de697752 | 117 | void BatteriesScreen::drawBatteriesScreen() |
OHstin | 3:7666de697752 | 118 | { |
OHstin | 3:7666de697752 | 119 | |
OHstin | 3:7666de697752 | 120 | // draw the tile |
OHstin | 3:7666de697752 | 121 | drawTile(); |
OHstin | 3:7666de697752 | 122 | |
OHstin | 3:7666de697752 | 123 | // construct the list |
OHstin | 3:7666de697752 | 124 | list = new ListController( 0, |
OHstin | 3:7666de697752 | 125 | 63, |
OHstin | 3:7666de697752 | 126 | "Batteries", |
OHstin | 3:7666de697752 | 127 | "Battery One", |
OHstin | 3:7666de697752 | 128 | "Battery Two", |
OHstin | 3:7666de697752 | 129 | "", |
OHstin | 3:7666de697752 | 130 | "", |
OHstin | 3:7666de697752 | 131 | bgColor, |
OHstin | 3:7666de697752 | 132 | 2); |
OHstin | 3:7666de697752 | 133 | |
OHstin | 3:7666de697752 | 134 | // draw the list |
OHstin | 3:7666de697752 | 135 | list->drawList(); |
OHstin | 3:7666de697752 | 136 | |
OHstin | 3:7666de697752 | 137 | } |
OHstin | 3:7666de697752 | 138 | |
OHstin | 3:7666de697752 | 139 | void BatteriesScreen::drawTile() |
OHstin | 3:7666de697752 | 140 | { |
OHstin | 3:7666de697752 | 141 | // first draw the blue background |
OHstin | 3:7666de697752 | 142 | tft.fillRect(0,0,tft.width(),63,ST7735_BLUE); |
OHstin | 3:7666de697752 | 143 | |
OHstin | 3:7666de697752 | 144 | // retrieve batteryPercentage |
OHstin | 3:7666de697752 | 145 | int batteryPercentage = getBatteryPercentage(); |
OHstin | 3:7666de697752 | 146 | |
OHstin | 3:7666de697752 | 147 | // batteryStatus |
OHstin | 3:7666de697752 | 148 | int batteryStatus = getBatteryStatus(); |
OHstin | 3:7666de697752 | 149 | |
OHstin | 3:7666de697752 | 150 | // track state of individual battery |
OHstin | 3:7666de697752 | 151 | bool batteryOneCharging = false; |
OHstin | 3:7666de697752 | 152 | bool batteryTwoCharging = false; |
OHstin | 3:7666de697752 | 153 | |
OHstin | 3:7666de697752 | 154 | if (batteryStatus == 1 || batteryStatus == 3) { |
OHstin | 3:7666de697752 | 155 | batteryOneCharging = true; |
OHstin | 3:7666de697752 | 156 | } |
OHstin | 3:7666de697752 | 157 | if (batteryStatus == 2 || batteryStatus == 3) { |
OHstin | 3:7666de697752 | 158 | batteryTwoCharging = true; |
OHstin | 3:7666de697752 | 159 | } |
OHstin | 3:7666de697752 | 160 | |
OHstin | 3:7666de697752 | 161 | // Coordinates for battery one |
OHstin | 3:7666de697752 | 162 | int x = 5; |
OHstin | 3:7666de697752 | 163 | int y = 5; |
OHstin | 3:7666de697752 | 164 | |
OHstin | 3:7666de697752 | 165 | // initialise the first battery icon |
OHstin | 3:7666de697752 | 166 | batteryOne = new BatteryIcon(x,y,ST7735_BLUE,batteryPercentage,batteryOneCharging); |
OHstin | 3:7666de697752 | 167 | |
OHstin | 3:7666de697752 | 168 | // Coordinates for battery two |
OHstin | 3:7666de697752 | 169 | int x1 = 83; |
OHstin | 3:7666de697752 | 170 | int y1 = 5; |
OHstin | 3:7666de697752 | 171 | |
OHstin | 3:7666de697752 | 172 | // Initialise the second battery icon |
OHstin | 3:7666de697752 | 173 | batteryTwo = new BatteryIcon(x1,y1,ST7735_BLUE,batteryPercentage,batteryTwoCharging); |
OHstin | 3:7666de697752 | 174 | |
OHstin | 3:7666de697752 | 175 | // draw the batteryIcons |
OHstin | 3:7666de697752 | 176 | batteryOne->drawBatteryIcon(); |
OHstin | 3:7666de697752 | 177 | batteryTwo->drawBatteryIcon(); |
OHstin | 3:7666de697752 | 178 | |
OHstin | 3:7666de697752 | 179 | // draw underlying text |
OHstin | 3:7666de697752 | 180 | tft.setCursor(13,50); |
OHstin | 3:7666de697752 | 181 | tft.setTextColor(ST7735_WHITE); |
OHstin | 3:7666de697752 | 182 | tft.printf("B1"); |
OHstin | 3:7666de697752 | 183 | |
OHstin | 3:7666de697752 | 184 | tft.setCursor(91,50); |
OHstin | 3:7666de697752 | 185 | tft.setTextColor(ST7735_WHITE); |
OHstin | 3:7666de697752 | 186 | tft.printf("B2"); |
OHstin | 3:7666de697752 | 187 | |
OHstin | 3:7666de697752 | 188 | } |
OHstin | 3:7666de697752 | 189 | |
OHstin | 3:7666de697752 | 190 | void BatteriesScreen::deAllocateMemory() |
OHstin | 3:7666de697752 | 191 | { |
OHstin | 3:7666de697752 | 192 | // deallocate memory that was created dynamically |
OHstin | 3:7666de697752 | 193 | delete batteryOne; |
OHstin | 3:7666de697752 | 194 | delete batteryTwo; |
OHstin | 3:7666de697752 | 195 | delete list; |
OHstin | 3:7666de697752 | 196 | |
OHstin | 3:7666de697752 | 197 | } |
OHstin | 3:7666de697752 | 198 | |
OHstin | 3:7666de697752 | 199 | void BatteriesScreen::buttonPressed(int button) |
OHstin | 3:7666de697752 | 200 | { |
OHstin | 3:7666de697752 | 201 | switch(button) { |
OHstin | 3:7666de697752 | 202 | |
OHstin | 3:7666de697752 | 203 | case BACKBUTTON: |
OHstin | 3:7666de697752 | 204 | // navigate to the previous screen |
OHstin | 3:7666de697752 | 205 | changeScreen = -1; |
OHstin | 3:7666de697752 | 206 | break; |
OHstin | 3:7666de697752 | 207 | |
OHstin | 3:7666de697752 | 208 | case SCROLLBUTTON: |
OHstin | 3:7666de697752 | 209 | // scroll to the next option |
OHstin | 3:7666de697752 | 210 | scroll = true; |
OHstin | 3:7666de697752 | 211 | break; |
OHstin | 3:7666de697752 | 212 | |
OHstin | 3:7666de697752 | 213 | case ENTERBUTTON: |
OHstin | 3:7666de697752 | 214 | // navigate to the selected option |
OHstin | 3:7666de697752 | 215 | enter = true; |
OHstin | 3:7666de697752 | 216 | break; |
OHstin | 3:7666de697752 | 217 | |
OHstin | 3:7666de697752 | 218 | default: |
OHstin | 3:7666de697752 | 219 | // do nothing |
OHstin | 3:7666de697752 | 220 | break; |
OHstin | 3:7666de697752 | 221 | } |
OHstin | 3:7666de697752 | 222 | } |
OHstin | 3:7666de697752 | 223 | |
OHstin | 3:7666de697752 | 224 | |
OHstin | 3:7666de697752 | 225 | #endif |
OHstin | 3:7666de697752 | 226 |