Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Adafruit_GFX Adafruit_ST7735 INA219 MODSERIAL MbedJSONValue mbed-rtos mbed
main.cpp@1:1354a7ebd0c6, 2016-07-10 (annotated)
- Committer:
- OHstin
- Date:
- Sun Jul 10 12:24:41 2016 +0000
- Revision:
- 1:1354a7ebd0c6
- Parent:
- 0:5139f11cfc62
- Child:
- 2:104bec169fb6
Battery Icon Class Complete
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| OHstin | 0:5139f11cfc62 | 1 | #include "mbed.h" |
| OHstin | 0:5139f11cfc62 | 2 | #include "Adafruit_ST7735.h" |
| OHstin | 0:5139f11cfc62 | 3 | |
| OHstin | 1:1354a7ebd0c6 | 4 | Adafruit_ST7735 tft(p11, p12, p13, p10, p8, p9); // MOSI, MISO, SCLK, SSEL, TFT_DC, TFT_RST |
| OHstin | 1:1354a7ebd0c6 | 5 | #include "BatteryIcon.h" |
| OHstin | 0:5139f11cfc62 | 6 | |
| OHstin | 1:1354a7ebd0c6 | 7 | |
| OHstin | 0:5139f11cfc62 | 8 | //LEDs used to display output |
| OHstin | 0:5139f11cfc62 | 9 | |
| OHstin | 0:5139f11cfc62 | 10 | DigitalIn enable1(p21); |
| OHstin | 0:5139f11cfc62 | 11 | DigitalIn enable2(p22); |
| OHstin | 0:5139f11cfc62 | 12 | DigitalIn enable3(p23); |
| OHstin | 0:5139f11cfc62 | 13 | |
| OHstin | 0:5139f11cfc62 | 14 | DigitalOut led1(LED1); |
| OHstin | 0:5139f11cfc62 | 15 | DigitalOut led2(LED2); |
| OHstin | 0:5139f11cfc62 | 16 | DigitalOut led3(LED3); |
| OHstin | 0:5139f11cfc62 | 17 | |
| OHstin | 0:5139f11cfc62 | 18 | AnalogIn ain(p20); |
| OHstin | 0:5139f11cfc62 | 19 | AnalogIn ain1(p19); |
| OHstin | 0:5139f11cfc62 | 20 | AnalogIn ain2(p18); |
| OHstin | 0:5139f11cfc62 | 21 | |
| OHstin | 0:5139f11cfc62 | 22 | DigitalOut myled(LED1); |
| OHstin | 0:5139f11cfc62 | 23 | |
| OHstin | 1:1354a7ebd0c6 | 24 | |
| OHstin | 1:1354a7ebd0c6 | 25 | |
| OHstin | 0:5139f11cfc62 | 26 | void testdrawtext(char *text, uint16_t color); |
| OHstin | 1:1354a7ebd0c6 | 27 | void testdrawrects(uint16_t color); |
| OHstin | 1:1354a7ebd0c6 | 28 | void testfastlines(uint16_t color, int batteryLevel); |
| OHstin | 1:1354a7ebd0c6 | 29 | void testDrawPercentage(char *percentage, uint16_t color); |
| OHstin | 1:1354a7ebd0c6 | 30 | void displayBatteryPercentage(int percentage, uint16_t color); |
| OHstin | 1:1354a7ebd0c6 | 31 | void testDrawThunderbolt(); |
| OHstin | 1:1354a7ebd0c6 | 32 | uint16_t determineColor(int batteryLevel); |
| OHstin | 0:5139f11cfc62 | 33 | |
| OHstin | 0:5139f11cfc62 | 34 | float currentFloat = 1.0; |
| OHstin | 1:1354a7ebd0c6 | 35 | int currentBatteryLevel = 0; |
| OHstin | 1:1354a7ebd0c6 | 36 | int previousBatteryLevel = 0; |
| OHstin | 1:1354a7ebd0c6 | 37 | int previousBatteryColor = 0; |
| OHstin | 1:1354a7ebd0c6 | 38 | |
| OHstin | 1:1354a7ebd0c6 | 39 | int startingWidth = 20; |
| OHstin | 1:1354a7ebd0c6 | 40 | int startingHeight = 10; |
| OHstin | 0:5139f11cfc62 | 41 | |
| OHstin | 0:5139f11cfc62 | 42 | |
| OHstin | 0:5139f11cfc62 | 43 | int main() |
| OHstin | 0:5139f11cfc62 | 44 | { |
| OHstin | 0:5139f11cfc62 | 45 | // Use this initializer if you're using a 1.8" TFT |
| OHstin | 0:5139f11cfc62 | 46 | tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab |
| OHstin | 0:5139f11cfc62 | 47 | |
| OHstin | 1:1354a7ebd0c6 | 48 | uint16_t backgroundColor = ST7735_WHITE; |
| OHstin | 1:1354a7ebd0c6 | 49 | tft.fillScreen(backgroundColor); |
| OHstin | 1:1354a7ebd0c6 | 50 | |
| OHstin | 1:1354a7ebd0c6 | 51 | // landscape view |
| OHstin | 0:5139f11cfc62 | 52 | tft.setRotation(1); |
| OHstin | 0:5139f11cfc62 | 53 | char current_text[40]; |
| OHstin | 1:1354a7ebd0c6 | 54 | |
| OHstin | 1:1354a7ebd0c6 | 55 | int batteryLevel = ain2*32.5; |
| OHstin | 1:1354a7ebd0c6 | 56 | int percentage = ain2*100; |
| OHstin | 1:1354a7ebd0c6 | 57 | |
| OHstin | 1:1354a7ebd0c6 | 58 | sprintf(current_text, "W: %ldp H: %ldp ", tft.height(), tft.width()); |
| OHstin | 1:1354a7ebd0c6 | 59 | testdrawtext(current_text, ST7735_WHITE); |
| OHstin | 0:5139f11cfc62 | 60 | |
| OHstin | 1:1354a7ebd0c6 | 61 | // determine if the battery is charging |
| OHstin | 1:1354a7ebd0c6 | 62 | bool batteryCharging; |
| OHstin | 1:1354a7ebd0c6 | 63 | if (ain1 > 0.5) { |
| OHstin | 1:1354a7ebd0c6 | 64 | batteryCharging = true; |
| OHstin | 1:1354a7ebd0c6 | 65 | } else { |
| OHstin | 1:1354a7ebd0c6 | 66 | batteryCharging = false; |
| OHstin | 1:1354a7ebd0c6 | 67 | } |
| OHstin | 1:1354a7ebd0c6 | 68 | |
| OHstin | 1:1354a7ebd0c6 | 69 | // create and initialise battery icon project |
| OHstin | 1:1354a7ebd0c6 | 70 | BatteryIcon batteryIcon(startingWidth, startingHeight,backgroundColor,percentage,batteryCharging); |
| OHstin | 1:1354a7ebd0c6 | 71 | // draw the battery icon |
| OHstin | 1:1354a7ebd0c6 | 72 | batteryIcon.drawBatteryIcon(); |
| OHstin | 1:1354a7ebd0c6 | 73 | |
| OHstin | 1:1354a7ebd0c6 | 74 | while (1) { |
| OHstin | 0:5139f11cfc62 | 75 | |
| OHstin | 1:1354a7ebd0c6 | 76 | // determine if the battery is charging |
| OHstin | 1:1354a7ebd0c6 | 77 | percentage = ain2*100; |
| OHstin | 1:1354a7ebd0c6 | 78 | if (ain1 > 0.5) { |
| OHstin | 1:1354a7ebd0c6 | 79 | batteryCharging = true; |
| OHstin | 1:1354a7ebd0c6 | 80 | } else { |
| OHstin | 1:1354a7ebd0c6 | 81 | batteryCharging = false; |
| OHstin | 0:5139f11cfc62 | 82 | } |
| OHstin | 1:1354a7ebd0c6 | 83 | // set the battery percentage accordingly |
| OHstin | 1:1354a7ebd0c6 | 84 | batteryIcon.setBatteryPercentage(percentage,batteryCharging); |
| OHstin | 0:5139f11cfc62 | 85 | |
| OHstin | 1:1354a7ebd0c6 | 86 | // wait half a second |
| OHstin | 1:1354a7ebd0c6 | 87 | wait_ms(500); |
| OHstin | 0:5139f11cfc62 | 88 | } |
| OHstin | 1:1354a7ebd0c6 | 89 | |
| OHstin | 0:5139f11cfc62 | 90 | } |
| OHstin | 0:5139f11cfc62 | 91 | |
| OHstin | 0:5139f11cfc62 | 92 | void testdrawtext(char *text, uint16_t color) |
| OHstin | 0:5139f11cfc62 | 93 | { |
| OHstin | 1:1354a7ebd0c6 | 94 | tft.setCursor(0, 100); |
| OHstin | 0:5139f11cfc62 | 95 | tft.setTextColor(color); |
| OHstin | 0:5139f11cfc62 | 96 | tft.setTextWrap(true); |
| OHstin | 0:5139f11cfc62 | 97 | tft.printf("%s",text); |
| OHstin | 0:5139f11cfc62 | 98 | } |