
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
Icons/BatteryIcon.h@6:196a63a3378d, 2017-04-30 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
OHstin | 3:7666de697752 | 1 | #ifndef BATTERYICON_H |
OHstin | 3:7666de697752 | 2 | #define BATTERYICON_H |
OHstin | 3:7666de697752 | 3 | |
OHstin | 3:7666de697752 | 4 | #include "mbed.h" |
OHstin | 3:7666de697752 | 5 | |
OHstin | 3:7666de697752 | 6 | class BatteryIcon |
OHstin | 3:7666de697752 | 7 | { |
OHstin | 3:7666de697752 | 8 | public: |
OHstin | 3:7666de697752 | 9 | //BatteryIcon(); |
OHstin | 3:7666de697752 | 10 | BatteryIcon(uint8_t xCoordinate, uint8_t yCoordinate, uint16_t backgroundColor, int batteryPercentage, bool chargeStatus); |
OHstin | 3:7666de697752 | 11 | void setBatteryPercentage(int batteryPercentage, bool chargeStatus); |
OHstin | 3:7666de697752 | 12 | void drawBatteryIcon(); |
OHstin | 3:7666de697752 | 13 | void removeBatteryIcon(); |
OHstin | 3:7666de697752 | 14 | |
OHstin | 3:7666de697752 | 15 | private: |
OHstin | 3:7666de697752 | 16 | uint8_t xCoord; |
OHstin | 3:7666de697752 | 17 | uint8_t yCoord; |
OHstin | 3:7666de697752 | 18 | uint16_t bgColor; |
OHstin | 3:7666de697752 | 19 | int previousPercentage; |
OHstin | 3:7666de697752 | 20 | int percentage; |
OHstin | 3:7666de697752 | 21 | int batteryLevel; |
OHstin | 3:7666de697752 | 22 | int previousBatteryLevel; |
OHstin | 3:7666de697752 | 23 | int previousBatteryColor; |
OHstin | 3:7666de697752 | 24 | bool chargeStat; |
OHstin | 3:7666de697752 | 25 | bool thunderboltShowing; |
OHstin | 3:7666de697752 | 26 | bool battInitialised; |
OHstin | 3:7666de697752 | 27 | uint16_t deleteColor; |
OHstin | 3:7666de697752 | 28 | uint16_t textColor; |
OHstin | 3:7666de697752 | 29 | |
OHstin | 3:7666de697752 | 30 | |
OHstin | 3:7666de697752 | 31 | uint16_t determineBatteryColor(); |
OHstin | 3:7666de697752 | 32 | void drawBatteryOutline(); |
OHstin | 3:7666de697752 | 33 | void displayPercentage(int pc, uint16_t tc); |
OHstin | 3:7666de697752 | 34 | void fillBattery(); |
OHstin | 3:7666de697752 | 35 | void drawThunderbolt(); |
OHstin | 3:7666de697752 | 36 | void removeThunderbolt(); |
OHstin | 3:7666de697752 | 37 | |
OHstin | 3:7666de697752 | 38 | }; |
OHstin | 3:7666de697752 | 39 | |
OHstin | 3:7666de697752 | 40 | /** |
OHstin | 3:7666de697752 | 41 | BatteryIcon::BatteryIcon(){ |
OHstin | 3:7666de697752 | 42 | |
OHstin | 3:7666de697752 | 43 | } |
OHstin | 3:7666de697752 | 44 | **/ |
OHstin | 3:7666de697752 | 45 | |
OHstin | 3:7666de697752 | 46 | BatteryIcon::BatteryIcon(uint8_t xCoordinate, uint8_t yCoordinate, uint16_t backgroundColor, int batteryPercentage, bool chargeStatus) |
OHstin | 3:7666de697752 | 47 | : previousPercentage(-1),previousBatteryLevel(0), previousBatteryColor(0),thunderboltShowing(false) |
OHstin | 3:7666de697752 | 48 | { |
OHstin | 3:7666de697752 | 49 | // make all necessary initialisations |
OHstin | 3:7666de697752 | 50 | xCoord = xCoordinate; |
OHstin | 3:7666de697752 | 51 | yCoord = yCoordinate; |
OHstin | 3:7666de697752 | 52 | bgColor = backgroundColor; |
OHstin | 3:7666de697752 | 53 | chargeStat = chargeStatus; |
OHstin | 3:7666de697752 | 54 | percentage = batteryPercentage; |
OHstin | 3:7666de697752 | 55 | |
OHstin | 3:7666de697752 | 56 | // determine the color to delete stuff |
OHstin | 3:7666de697752 | 57 | deleteColor = bgColor; |
OHstin | 3:7666de697752 | 58 | |
OHstin | 3:7666de697752 | 59 | // determine the color of text |
OHstin | 3:7666de697752 | 60 | if (bgColor == ST7735_WHITE ) { |
OHstin | 3:7666de697752 | 61 | textColor = ST7735_BLACK; |
OHstin | 3:7666de697752 | 62 | } else if (bgColor == ST7735_BLACK) { |
OHstin | 3:7666de697752 | 63 | textColor = ST7735_WHITE; |
OHstin | 3:7666de697752 | 64 | } else if (bgColor == ST7735_BLUE) { |
OHstin | 3:7666de697752 | 65 | textColor = ST7735_WHITE; |
OHstin | 3:7666de697752 | 66 | } |
OHstin | 3:7666de697752 | 67 | } |
OHstin | 3:7666de697752 | 68 | |
OHstin | 3:7666de697752 | 69 | void BatteryIcon::drawBatteryIcon() |
OHstin | 3:7666de697752 | 70 | { |
OHstin | 3:7666de697752 | 71 | |
OHstin | 3:7666de697752 | 72 | // draw the outline first |
OHstin | 3:7666de697752 | 73 | drawBatteryOutline(); |
OHstin | 3:7666de697752 | 74 | |
OHstin | 3:7666de697752 | 75 | // dispaly the battery Percentage |
OHstin | 3:7666de697752 | 76 | setBatteryPercentage(percentage,chargeStat); |
OHstin | 3:7666de697752 | 77 | |
OHstin | 3:7666de697752 | 78 | } |
OHstin | 3:7666de697752 | 79 | |
OHstin | 3:7666de697752 | 80 | void BatteryIcon::removeBatteryIcon() |
OHstin | 3:7666de697752 | 81 | { |
OHstin | 3:7666de697752 | 82 | |
OHstin | 3:7666de697752 | 83 | } |
OHstin | 3:7666de697752 | 84 | |
OHstin | 3:7666de697752 | 85 | void BatteryIcon::setBatteryPercentage(int percentage, bool chargeStatus) |
OHstin | 3:7666de697752 | 86 | { |
OHstin | 3:7666de697752 | 87 | |
OHstin | 3:7666de697752 | 88 | // calculate batter level; |
OHstin | 3:7666de697752 | 89 | float batteryLevelFloat = (percentage*32.0)/100; |
OHstin | 3:7666de697752 | 90 | batteryLevel = batteryLevelFloat; |
OHstin | 3:7666de697752 | 91 | |
OHstin | 3:7666de697752 | 92 | // write the battery percentage |
OHstin | 3:7666de697752 | 93 | if (previousPercentage == -1) { |
OHstin | 3:7666de697752 | 94 | |
OHstin | 3:7666de697752 | 95 | // write a new percentage |
OHstin | 3:7666de697752 | 96 | displayPercentage(percentage, textColor); |
OHstin | 3:7666de697752 | 97 | |
OHstin | 3:7666de697752 | 98 | } else if (percentage == previousPercentage) { |
OHstin | 3:7666de697752 | 99 | // do nothing |
OHstin | 3:7666de697752 | 100 | } else { |
OHstin | 3:7666de697752 | 101 | |
OHstin | 3:7666de697752 | 102 | // first delete the current percentage |
OHstin | 3:7666de697752 | 103 | displayPercentage(previousPercentage, bgColor); |
OHstin | 3:7666de697752 | 104 | |
OHstin | 3:7666de697752 | 105 | // write a new percentage |
OHstin | 3:7666de697752 | 106 | displayPercentage(percentage, textColor); |
OHstin | 3:7666de697752 | 107 | } |
OHstin | 3:7666de697752 | 108 | |
OHstin | 3:7666de697752 | 109 | |
OHstin | 3:7666de697752 | 110 | |
OHstin | 3:7666de697752 | 111 | // fill the battery |
OHstin | 3:7666de697752 | 112 | fillBattery(); |
OHstin | 3:7666de697752 | 113 | |
OHstin | 3:7666de697752 | 114 | |
OHstin | 3:7666de697752 | 115 | |
OHstin | 3:7666de697752 | 116 | // draw Thunderbolt at the end |
OHstin | 3:7666de697752 | 117 | |
OHstin | 3:7666de697752 | 118 | // first check if battery is charging |
OHstin | 3:7666de697752 | 119 | if (chargeStatus) { |
OHstin | 3:7666de697752 | 120 | |
OHstin | 3:7666de697752 | 121 | // check if thunderbolt is not displayed |
OHstin | 3:7666de697752 | 122 | if (!thunderboltShowing) { |
OHstin | 3:7666de697752 | 123 | // draw the thundebolt |
OHstin | 3:7666de697752 | 124 | drawThunderbolt(); |
OHstin | 3:7666de697752 | 125 | } else { |
OHstin | 3:7666de697752 | 126 | // do nothing |
OHstin | 3:7666de697752 | 127 | } |
OHstin | 3:7666de697752 | 128 | } else { // battery is not charging |
OHstin | 3:7666de697752 | 129 | |
OHstin | 3:7666de697752 | 130 | // check if thunderbolt is showing |
OHstin | 3:7666de697752 | 131 | if (thunderboltShowing) { |
OHstin | 3:7666de697752 | 132 | // remove the thunderbolt |
OHstin | 3:7666de697752 | 133 | removeThunderbolt(); |
OHstin | 3:7666de697752 | 134 | } else { |
OHstin | 3:7666de697752 | 135 | // do nothing |
OHstin | 3:7666de697752 | 136 | } |
OHstin | 3:7666de697752 | 137 | |
OHstin | 3:7666de697752 | 138 | } |
OHstin | 3:7666de697752 | 139 | |
OHstin | 3:7666de697752 | 140 | // record the previous data |
OHstin | 3:7666de697752 | 141 | previousPercentage = percentage; |
OHstin | 3:7666de697752 | 142 | previousBatteryLevel = batteryLevel; |
OHstin | 3:7666de697752 | 143 | |
OHstin | 3:7666de697752 | 144 | |
OHstin | 3:7666de697752 | 145 | |
OHstin | 3:7666de697752 | 146 | } |
OHstin | 3:7666de697752 | 147 | |
OHstin | 3:7666de697752 | 148 | void BatteryIcon::displayPercentage(int pc,uint16_t tc ) |
OHstin | 3:7666de697752 | 149 | { |
OHstin | 3:7666de697752 | 150 | // dispalay the battery percentage |
OHstin | 3:7666de697752 | 151 | tft.setCursor(xCoord+28, yCoord+20); |
OHstin | 3:7666de697752 | 152 | tft.setTextColor(tc); |
OHstin | 3:7666de697752 | 153 | tft.setTextSize(2); |
OHstin | 3:7666de697752 | 154 | tft.printf("%ld%%",pc); |
OHstin | 3:7666de697752 | 155 | tft.setTextSize(1); |
OHstin | 3:7666de697752 | 156 | } |
OHstin | 3:7666de697752 | 157 | |
OHstin | 3:7666de697752 | 158 | void BatteryIcon::drawThunderbolt() |
OHstin | 3:7666de697752 | 159 | { |
OHstin | 3:7666de697752 | 160 | |
OHstin | 3:7666de697752 | 161 | int16_t height = yCoord; |
OHstin | 3:7666de697752 | 162 | int16_t width = xCoord + 40; |
OHstin | 3:7666de697752 | 163 | |
OHstin | 3:7666de697752 | 164 | // draw first diagonal lines |
OHstin | 3:7666de697752 | 165 | for (int n = 0; n < 3; n++ ) { |
OHstin | 3:7666de697752 | 166 | tft.drawLine(width+n+4, height+1,width+n+1,height+7,ST7735_YELLOW); |
OHstin | 3:7666de697752 | 167 | } |
OHstin | 3:7666de697752 | 168 | // draw horizontal lines |
OHstin | 3:7666de697752 | 169 | for (int n = 0; n < 2; n++) { |
OHstin | 3:7666de697752 | 170 | tft.drawFastHLine(width+3,height+6+n,4,ST7735_YELLOW); |
OHstin | 3:7666de697752 | 171 | } |
OHstin | 3:7666de697752 | 172 | // draw second horizontal lines |
OHstin | 3:7666de697752 | 173 | for (int n = 0; n < 3; n++ ) { |
OHstin | 3:7666de697752 | 174 | tft.drawLine(width+n+6, height+6,width+3+n, height+11,ST7735_YELLOW); |
OHstin | 3:7666de697752 | 175 | } |
OHstin | 3:7666de697752 | 176 | // draw filled triangle |
OHstin | 3:7666de697752 | 177 | tft.fillTriangle(width, height+12,width+8, height+12,width+4, height+16,ST7735_YELLOW); |
OHstin | 3:7666de697752 | 178 | |
OHstin | 3:7666de697752 | 179 | thunderboltShowing = true; |
OHstin | 3:7666de697752 | 180 | } |
OHstin | 3:7666de697752 | 181 | |
OHstin | 3:7666de697752 | 182 | void BatteryIcon::removeThunderbolt() |
OHstin | 3:7666de697752 | 183 | { |
OHstin | 3:7666de697752 | 184 | |
OHstin | 3:7666de697752 | 185 | // here the thunderbolt lines are replaced by the background color, hence |
OHstin | 3:7666de697752 | 186 | // making the thunderbolt disappear |
OHstin | 3:7666de697752 | 187 | |
OHstin | 3:7666de697752 | 188 | int16_t height = yCoord; |
OHstin | 3:7666de697752 | 189 | int16_t width = xCoord + 40; |
OHstin | 3:7666de697752 | 190 | |
OHstin | 3:7666de697752 | 191 | // draw first diagonal lines |
OHstin | 3:7666de697752 | 192 | for (int n = 0; n < 3; n++ ) { |
OHstin | 3:7666de697752 | 193 | tft.drawLine(width+n+4, height+1,width+n+1,height+7,bgColor); |
OHstin | 3:7666de697752 | 194 | } |
OHstin | 3:7666de697752 | 195 | // draw horizontal lines |
OHstin | 3:7666de697752 | 196 | for (int n = 0; n < 2; n++) { |
OHstin | 3:7666de697752 | 197 | tft.drawFastHLine(width+3,height+6+n,4,bgColor); |
OHstin | 3:7666de697752 | 198 | } |
OHstin | 3:7666de697752 | 199 | // draw second horizontal lines |
OHstin | 3:7666de697752 | 200 | for (int n = 0; n < 3; n++ ) { |
OHstin | 3:7666de697752 | 201 | tft.drawLine(width+n+6, height+6,width+3+n, height+11,bgColor); |
OHstin | 3:7666de697752 | 202 | } |
OHstin | 3:7666de697752 | 203 | // draw filled triangle |
OHstin | 3:7666de697752 | 204 | tft.fillTriangle(width, height+12,width+8, height+12,width+4, height+16,bgColor); |
OHstin | 3:7666de697752 | 205 | |
OHstin | 3:7666de697752 | 206 | thunderboltShowing = false; |
OHstin | 3:7666de697752 | 207 | } |
OHstin | 3:7666de697752 | 208 | |
OHstin | 3:7666de697752 | 209 | void BatteryIcon::fillBattery() |
OHstin | 3:7666de697752 | 210 | { |
OHstin | 3:7666de697752 | 211 | |
OHstin | 3:7666de697752 | 212 | if ( batteryLevel > previousBatteryLevel ) { |
OHstin | 3:7666de697752 | 213 | |
OHstin | 3:7666de697752 | 214 | // first determine the color of lines |
OHstin | 3:7666de697752 | 215 | uint16_t theColor = determineBatteryColor(); |
OHstin | 3:7666de697752 | 216 | |
OHstin | 3:7666de697752 | 217 | // increase the number of lines |
OHstin | 3:7666de697752 | 218 | for (int y = previousBatteryLevel; y < batteryLevel; y++) { |
OHstin | 3:7666de697752 | 219 | tft.drawFastHLine(xCoord + 1, yCoord + 38-y,23,theColor); |
OHstin | 3:7666de697752 | 220 | } |
OHstin | 3:7666de697752 | 221 | } else if (batteryLevel < previousBatteryLevel) { |
OHstin | 3:7666de697752 | 222 | |
OHstin | 3:7666de697752 | 223 | // decrease the number of lines |
OHstin | 3:7666de697752 | 224 | for (int y = previousBatteryLevel; y > batteryLevel; y--) { |
OHstin | 3:7666de697752 | 225 | tft.drawFastHLine(xCoord + 1, yCoord + 38-y,23,bgColor); |
OHstin | 3:7666de697752 | 226 | } |
OHstin | 3:7666de697752 | 227 | |
OHstin | 3:7666de697752 | 228 | // first determine the color of lines |
OHstin | 3:7666de697752 | 229 | uint16_t theColor = determineBatteryColor(); |
OHstin | 3:7666de697752 | 230 | |
OHstin | 3:7666de697752 | 231 | } else { |
OHstin | 3:7666de697752 | 232 | // do nothing |
OHstin | 3:7666de697752 | 233 | } |
OHstin | 3:7666de697752 | 234 | |
OHstin | 3:7666de697752 | 235 | } |
OHstin | 3:7666de697752 | 236 | |
OHstin | 3:7666de697752 | 237 | uint16_t BatteryIcon::determineBatteryColor() |
OHstin | 3:7666de697752 | 238 | { |
OHstin | 3:7666de697752 | 239 | |
OHstin | 3:7666de697752 | 240 | uint16_t color; |
OHstin | 3:7666de697752 | 241 | |
OHstin | 3:7666de697752 | 242 | if (batteryLevel >= 21) { |
OHstin | 3:7666de697752 | 243 | |
OHstin | 3:7666de697752 | 244 | //first check if its already green |
OHstin | 3:7666de697752 | 245 | if (previousBatteryColor != 1) { |
OHstin | 3:7666de697752 | 246 | |
OHstin | 3:7666de697752 | 247 | // make the colors green |
OHstin | 3:7666de697752 | 248 | for (int y = 0; y <= batteryLevel; y++) { |
OHstin | 3:7666de697752 | 249 | tft.drawFastHLine(xCoord + 1,yCoord + 38-y,23, ST7735_GREEN); |
OHstin | 3:7666de697752 | 250 | } |
OHstin | 3:7666de697752 | 251 | |
OHstin | 3:7666de697752 | 252 | previousBatteryColor = 1; |
OHstin | 3:7666de697752 | 253 | } |
OHstin | 3:7666de697752 | 254 | |
OHstin | 3:7666de697752 | 255 | // update the new battery color |
OHstin | 3:7666de697752 | 256 | color = ST7735_GREEN; |
OHstin | 3:7666de697752 | 257 | |
OHstin | 3:7666de697752 | 258 | } else if (batteryLevel < 21 && batteryLevel >= 7 ) { |
OHstin | 3:7666de697752 | 259 | |
OHstin | 3:7666de697752 | 260 | //first check if its already yellow |
OHstin | 3:7666de697752 | 261 | if (previousBatteryColor != 2) { |
OHstin | 3:7666de697752 | 262 | |
OHstin | 3:7666de697752 | 263 | // make the colors yellow |
OHstin | 3:7666de697752 | 264 | for (int y = 0; y <= batteryLevel; y++) { |
OHstin | 3:7666de697752 | 265 | tft.drawFastHLine(xCoord + 1,yCoord + 38-y,23, ST7735_YELLOW); |
OHstin | 3:7666de697752 | 266 | } |
OHstin | 3:7666de697752 | 267 | |
OHstin | 3:7666de697752 | 268 | previousBatteryColor = 2; |
OHstin | 3:7666de697752 | 269 | } |
OHstin | 3:7666de697752 | 270 | |
OHstin | 3:7666de697752 | 271 | // update the new battery color |
OHstin | 3:7666de697752 | 272 | color = ST7735_YELLOW; |
OHstin | 3:7666de697752 | 273 | } else { |
OHstin | 3:7666de697752 | 274 | //first check if its already red |
OHstin | 3:7666de697752 | 275 | if (previousBatteryColor != 3) { |
OHstin | 3:7666de697752 | 276 | |
OHstin | 3:7666de697752 | 277 | // make the colors red |
OHstin | 3:7666de697752 | 278 | for (int y = 0; y <= batteryLevel; y++) { |
OHstin | 3:7666de697752 | 279 | tft.drawFastHLine(xCoord + 1,yCoord + 38-y,23, ST7735_RED); |
OHstin | 3:7666de697752 | 280 | } |
OHstin | 3:7666de697752 | 281 | |
OHstin | 3:7666de697752 | 282 | |
OHstin | 3:7666de697752 | 283 | previousBatteryColor = 3; |
OHstin | 3:7666de697752 | 284 | } |
OHstin | 3:7666de697752 | 285 | |
OHstin | 3:7666de697752 | 286 | // update the new battery color |
OHstin | 3:7666de697752 | 287 | color = ST7735_RED; |
OHstin | 3:7666de697752 | 288 | } |
OHstin | 3:7666de697752 | 289 | |
OHstin | 3:7666de697752 | 290 | return color; |
OHstin | 3:7666de697752 | 291 | |
OHstin | 3:7666de697752 | 292 | } |
OHstin | 3:7666de697752 | 293 | |
OHstin | 3:7666de697752 | 294 | void BatteryIcon::drawBatteryOutline() |
OHstin | 3:7666de697752 | 295 | { |
OHstin | 3:7666de697752 | 296 | // small rectangle |
OHstin | 3:7666de697752 | 297 | tft.drawRect( xCoord + 8, yCoord,10,6,ST7735_GREEN); |
OHstin | 3:7666de697752 | 298 | |
OHstin | 3:7666de697752 | 299 | // big rectangle |
OHstin | 3:7666de697752 | 300 | tft.drawRect( xCoord , yCoord + 5,25,35,ST7735_GREEN); |
OHstin | 3:7666de697752 | 301 | } |
OHstin | 3:7666de697752 | 302 | |
OHstin | 3:7666de697752 | 303 | #endif |