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