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/SolarScreen.h@3:7666de697752, 2017-02-04 (annotated)
- Committer:
- OHstin
- Date:
- Sat Feb 04 20:17:44 2017 +0000
- Revision:
- 3:7666de697752
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 SOLARSCREEN_H |
OHstin | 3:7666de697752 | 2 | #define SOLARSCREEN_H |
OHstin | 3:7666de697752 | 3 | |
OHstin | 3:7666de697752 | 4 | #include "ListController.h" |
OHstin | 3:7666de697752 | 5 | |
OHstin | 3:7666de697752 | 6 | class SolarScreen: public Button <SolarScreen> |
OHstin | 3:7666de697752 | 7 | { |
OHstin | 3:7666de697752 | 8 | |
OHstin | 3:7666de697752 | 9 | public: |
OHstin | 3:7666de697752 | 10 | SolarScreen(uint16_t backgroundColor); |
OHstin | 3:7666de697752 | 11 | int start(); |
OHstin | 3:7666de697752 | 12 | void buttonPressed(int button); |
OHstin | 3:7666de697752 | 13 | |
OHstin | 3:7666de697752 | 14 | private: |
OHstin | 3:7666de697752 | 15 | |
OHstin | 3:7666de697752 | 16 | void drawSolarScreen(); |
OHstin | 3:7666de697752 | 17 | void drawTile(); |
OHstin | 3:7666de697752 | 18 | void updateTile(); |
OHstin | 3:7666de697752 | 19 | //void deAllocateMemory(); |
OHstin | 3:7666de697752 | 20 | void drawWattage(float wattage, uint16_t color); |
OHstin | 3:7666de697752 | 21 | |
OHstin | 3:7666de697752 | 22 | int changeScreen; |
OHstin | 3:7666de697752 | 23 | float previousWattage; |
OHstin | 3:7666de697752 | 24 | |
OHstin | 3:7666de697752 | 25 | ListController *list; // manages the list |
OHstin | 3:7666de697752 | 26 | bool scroll; // updated when a scroll event occurs |
OHstin | 3:7666de697752 | 27 | bool enter; // updated when an enter event occurs |
OHstin | 3:7666de697752 | 28 | uint16_t bgColor; // background color |
OHstin | 3:7666de697752 | 29 | |
OHstin | 3:7666de697752 | 30 | |
OHstin | 3:7666de697752 | 31 | |
OHstin | 3:7666de697752 | 32 | }; |
OHstin | 3:7666de697752 | 33 | |
OHstin | 3:7666de697752 | 34 | |
OHstin | 3:7666de697752 | 35 | SolarScreen::SolarScreen(uint16_t backgroundColor) |
OHstin | 3:7666de697752 | 36 | { |
OHstin | 3:7666de697752 | 37 | |
OHstin | 3:7666de697752 | 38 | bgColor = backgroundColor; |
OHstin | 3:7666de697752 | 39 | } |
OHstin | 3:7666de697752 | 40 | |
OHstin | 3:7666de697752 | 41 | int SolarScreen::start(){ |
OHstin | 3:7666de697752 | 42 | |
OHstin | 3:7666de697752 | 43 | previousWattage = -1; |
OHstin | 3:7666de697752 | 44 | |
OHstin | 3:7666de697752 | 45 | // attach interrupts to buttons |
OHstin | 3:7666de697752 | 46 | Button<SolarScreen>::activateButtons(this); |
OHstin | 3:7666de697752 | 47 | |
OHstin | 3:7666de697752 | 48 | // draw the screen |
OHstin | 3:7666de697752 | 49 | drawSolarScreen(); |
OHstin | 3:7666de697752 | 50 | |
OHstin | 3:7666de697752 | 51 | |
OHstin | 3:7666de697752 | 52 | changeScreen = 0; |
OHstin | 3:7666de697752 | 53 | scroll = false; |
OHstin | 3:7666de697752 | 54 | enter = false; |
OHstin | 3:7666de697752 | 55 | |
OHstin | 3:7666de697752 | 56 | |
OHstin | 3:7666de697752 | 57 | |
OHstin | 3:7666de697752 | 58 | while (changeScreen == 0){ |
OHstin | 3:7666de697752 | 59 | updateTile(); |
OHstin | 3:7666de697752 | 60 | |
OHstin | 3:7666de697752 | 61 | // scroll event occured |
OHstin | 3:7666de697752 | 62 | if (scroll) { |
OHstin | 3:7666de697752 | 63 | // perform scroll on the list |
OHstin | 3:7666de697752 | 64 | list->scroll(); |
OHstin | 3:7666de697752 | 65 | // reset variable |
OHstin | 3:7666de697752 | 66 | scroll = false; |
OHstin | 3:7666de697752 | 67 | } |
OHstin | 3:7666de697752 | 68 | |
OHstin | 3:7666de697752 | 69 | // enter event occured |
OHstin | 3:7666de697752 | 70 | if (enter) { |
OHstin | 3:7666de697752 | 71 | // read the current option on the list |
OHstin | 3:7666de697752 | 72 | changeScreen = list->getCurrentOption(); |
OHstin | 3:7666de697752 | 73 | } |
OHstin | 3:7666de697752 | 74 | //wait_ms(500); |
OHstin | 3:7666de697752 | 75 | Thread::wait(200); |
OHstin | 3:7666de697752 | 76 | } |
OHstin | 3:7666de697752 | 77 | |
OHstin | 3:7666de697752 | 78 | // relese memory occupied by list controller |
OHstin | 3:7666de697752 | 79 | delete list; |
OHstin | 3:7666de697752 | 80 | |
OHstin | 3:7666de697752 | 81 | // dettach interrupts to buttons |
OHstin | 3:7666de697752 | 82 | Button<SolarScreen>::deActivateButtons(); |
OHstin | 3:7666de697752 | 83 | |
OHstin | 3:7666de697752 | 84 | return changeScreen; |
OHstin | 3:7666de697752 | 85 | } |
OHstin | 3:7666de697752 | 86 | |
OHstin | 3:7666de697752 | 87 | |
OHstin | 3:7666de697752 | 88 | void SolarScreen::drawSolarScreen(){ |
OHstin | 3:7666de697752 | 89 | |
OHstin | 3:7666de697752 | 90 | // draw the tile |
OHstin | 3:7666de697752 | 91 | drawTile(); |
OHstin | 3:7666de697752 | 92 | |
OHstin | 3:7666de697752 | 93 | // construct the list |
OHstin | 3:7666de697752 | 94 | list = new ListController( 0, |
OHstin | 3:7666de697752 | 95 | 63, |
OHstin | 3:7666de697752 | 96 | "Solar", |
OHstin | 3:7666de697752 | 97 | "Values", |
OHstin | 3:7666de697752 | 98 | "Graph", |
OHstin | 3:7666de697752 | 99 | "", |
OHstin | 3:7666de697752 | 100 | "", |
OHstin | 3:7666de697752 | 101 | backgroundColor, |
OHstin | 3:7666de697752 | 102 | 2); |
OHstin | 3:7666de697752 | 103 | |
OHstin | 3:7666de697752 | 104 | // draw the list |
OHstin | 3:7666de697752 | 105 | list->drawList(); |
OHstin | 3:7666de697752 | 106 | } |
OHstin | 3:7666de697752 | 107 | |
OHstin | 3:7666de697752 | 108 | void SolarScreen::drawTile() |
OHstin | 3:7666de697752 | 109 | { |
OHstin | 3:7666de697752 | 110 | // draw the green background |
OHstin | 3:7666de697752 | 111 | tft.fillRect(0,0,tft.width(),63,ST7735_GREEN); |
OHstin | 3:7666de697752 | 112 | |
OHstin | 3:7666de697752 | 113 | // draw the text |
OHstin | 3:7666de697752 | 114 | tft.setTextSize(1); |
OHstin | 3:7666de697752 | 115 | tft.setCursor(5,5); |
OHstin | 3:7666de697752 | 116 | tft.setTextColor(ST7735_RED); |
OHstin | 3:7666de697752 | 117 | tft.printf("Power:"); |
OHstin | 3:7666de697752 | 118 | |
OHstin | 3:7666de697752 | 119 | // update the power values |
OHstin | 3:7666de697752 | 120 | updateTile(); |
OHstin | 3:7666de697752 | 121 | } |
OHstin | 3:7666de697752 | 122 | |
OHstin | 3:7666de697752 | 123 | void SolarScreen::updateTile() |
OHstin | 3:7666de697752 | 124 | { |
OHstin | 3:7666de697752 | 125 | // read the current power |
OHstin | 3:7666de697752 | 126 | float wattage = getSolarPower(); |
OHstin | 3:7666de697752 | 127 | |
OHstin | 3:7666de697752 | 128 | // temporary storage |
OHstin | 3:7666de697752 | 129 | char wattageString[5]; |
OHstin | 3:7666de697752 | 130 | |
OHstin | 3:7666de697752 | 131 | // tracks previously measured power |
OHstin | 3:7666de697752 | 132 | // ensure first value is ridiculous |
OHstin | 3:7666de697752 | 133 | // to enable print on startup |
OHstin | 3:7666de697752 | 134 | //static float previousWattage = -1; |
OHstin | 3:7666de697752 | 135 | |
OHstin | 3:7666de697752 | 136 | // convert float to char* |
OHstin | 3:7666de697752 | 137 | // to round off to 1 decimal place |
OHstin | 3:7666de697752 | 138 | sprintf(wattageString,"%.1f",wattage); |
OHstin | 3:7666de697752 | 139 | |
OHstin | 3:7666de697752 | 140 | // convert back to float |
OHstin | 3:7666de697752 | 141 | wattage = atof(wattageString); |
OHstin | 3:7666de697752 | 142 | |
OHstin | 3:7666de697752 | 143 | // multiply by 10 |
OHstin | 3:7666de697752 | 144 | wattage = wattage*10; |
OHstin | 3:7666de697752 | 145 | |
OHstin | 3:7666de697752 | 146 | // convert float to integer |
OHstin | 3:7666de697752 | 147 | float currentWattage = wattage; |
OHstin | 3:7666de697752 | 148 | |
OHstin | 3:7666de697752 | 149 | if(currentWattage != previousWattage){ |
OHstin | 3:7666de697752 | 150 | |
OHstin | 3:7666de697752 | 151 | // first delete the previous wattage |
OHstin | 3:7666de697752 | 152 | drawWattage(previousWattage,ST7735_GREEN); |
OHstin | 3:7666de697752 | 153 | // then write the new wattage |
OHstin | 3:7666de697752 | 154 | drawWattage(currentWattage,ST7735_BLUE); |
OHstin | 3:7666de697752 | 155 | |
OHstin | 3:7666de697752 | 156 | |
OHstin | 3:7666de697752 | 157 | }else{ |
OHstin | 3:7666de697752 | 158 | // do nothing |
OHstin | 3:7666de697752 | 159 | } |
OHstin | 3:7666de697752 | 160 | |
OHstin | 3:7666de697752 | 161 | // update the previous wattage |
OHstin | 3:7666de697752 | 162 | previousWattage = currentWattage; |
OHstin | 3:7666de697752 | 163 | } |
OHstin | 3:7666de697752 | 164 | |
OHstin | 3:7666de697752 | 165 | void SolarScreen::drawWattage(float wattage, uint16_t color) |
OHstin | 3:7666de697752 | 166 | { |
OHstin | 3:7666de697752 | 167 | // convert float to integer |
OHstin | 3:7666de697752 | 168 | int wattageInt = wattage; |
OHstin | 3:7666de697752 | 169 | |
OHstin | 3:7666de697752 | 170 | // divide by 10 |
OHstin | 3:7666de697752 | 171 | int firstInt = wattageInt/10; |
OHstin | 3:7666de697752 | 172 | |
OHstin | 3:7666de697752 | 173 | // print the quotient as the first value |
OHstin | 3:7666de697752 | 174 | int secondInt = wattageInt%10; |
OHstin | 3:7666de697752 | 175 | |
OHstin | 3:7666de697752 | 176 | // the coordinates of the first large digit |
OHstin | 3:7666de697752 | 177 | int startWidth = 40; |
OHstin | 3:7666de697752 | 178 | int startHeight = 15; |
OHstin | 3:7666de697752 | 179 | |
OHstin | 3:7666de697752 | 180 | // The first Large Digit |
OHstin | 3:7666de697752 | 181 | tft.setTextSize(6); |
OHstin | 3:7666de697752 | 182 | tft.setCursor(startWidth, startHeight); |
OHstin | 3:7666de697752 | 183 | tft.setTextColor(color); |
OHstin | 3:7666de697752 | 184 | tft.printf("%d",firstInt); |
OHstin | 3:7666de697752 | 185 | |
OHstin | 3:7666de697752 | 186 | // the decimal point |
OHstin | 3:7666de697752 | 187 | tft.setCursor(startWidth+ 28, startHeight + 21); |
OHstin | 3:7666de697752 | 188 | tft.setTextSize(3); |
OHstin | 3:7666de697752 | 189 | tft.printf("."); |
OHstin | 3:7666de697752 | 190 | |
OHstin | 3:7666de697752 | 191 | // The second Large Digit |
OHstin | 3:7666de697752 | 192 | tft.setCursor(startWidth + 45, startHeight); |
OHstin | 3:7666de697752 | 193 | tft.setTextSize(6); |
OHstin | 3:7666de697752 | 194 | tft.printf("%d", secondInt); |
OHstin | 3:7666de697752 | 195 | |
OHstin | 3:7666de697752 | 196 | // The power units |
OHstin | 3:7666de697752 | 197 | tft.setCursor(startWidth + 80, startHeight + 15); |
OHstin | 3:7666de697752 | 198 | tft.setTextSize(3); |
OHstin | 3:7666de697752 | 199 | tft.printf("W"); |
OHstin | 3:7666de697752 | 200 | |
OHstin | 3:7666de697752 | 201 | // reset the text size to smallest |
OHstin | 3:7666de697752 | 202 | tft.setTextSize(1); |
OHstin | 3:7666de697752 | 203 | } |
OHstin | 3:7666de697752 | 204 | |
OHstin | 3:7666de697752 | 205 | void SolarScreen::buttonPressed(int button) |
OHstin | 3:7666de697752 | 206 | { |
OHstin | 3:7666de697752 | 207 | switch(button){ |
OHstin | 3:7666de697752 | 208 | |
OHstin | 3:7666de697752 | 209 | case BACKBUTTON: |
OHstin | 3:7666de697752 | 210 | // navigate to the previous screen |
OHstin | 3:7666de697752 | 211 | changeScreen = -1; |
OHstin | 3:7666de697752 | 212 | break; |
OHstin | 3:7666de697752 | 213 | |
OHstin | 3:7666de697752 | 214 | case SCROLLBUTTON: |
OHstin | 3:7666de697752 | 215 | // scroll to the next option |
OHstin | 3:7666de697752 | 216 | scroll = true; |
OHstin | 3:7666de697752 | 217 | break; |
OHstin | 3:7666de697752 | 218 | |
OHstin | 3:7666de697752 | 219 | case ENTERBUTTON: |
OHstin | 3:7666de697752 | 220 | // navigate to the selected option |
OHstin | 3:7666de697752 | 221 | enter = true; |
OHstin | 3:7666de697752 | 222 | |
OHstin | 3:7666de697752 | 223 | default: |
OHstin | 3:7666de697752 | 224 | // do nothing |
OHstin | 3:7666de697752 | 225 | break; |
OHstin | 3:7666de697752 | 226 | } |
OHstin | 3:7666de697752 | 227 | } |
OHstin | 3:7666de697752 | 228 | |
OHstin | 3:7666de697752 | 229 | #endif |