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

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?

UserRevisionLine numberNew contents of line
OHstin 3:7666de697752 1 #ifndef GRAPHCONTROLLER_H
OHstin 3:7666de697752 2 #define GRAPHCONTROLLER_H
OHstin 3:7666de697752 3
OHstin 3:7666de697752 4 class GraphController
OHstin 3:7666de697752 5 {
OHstin 3:7666de697752 6
OHstin 3:7666de697752 7 public:
OHstin 3:7666de697752 8 GraphController(uint16_t backgroundColor, char title[]);
OHstin 3:7666de697752 9 void plotData(); // plots the data on the graph
OHstin 3:7666de697752 10 void drawAxes(); // draws the axes on the map
OHstin 3:7666de697752 11
OHstin 3:7666de697752 12 private:
OHstin 3:7666de697752 13 int pointNumber; // the total number of X Coordintes
OHstin 3:7666de697752 14 int originWidth; // where the x axis starts
OHstin 3:7666de697752 15 int originHeight; // where the y axis starts
OHstin 3:7666de697752 16 int previousValue; // previous x coordinate
OHstin 3:7666de697752 17 int position;
OHstin 3:7666de697752 18 int counter;
OHstin 3:7666de697752 19 uint16_t bgColor;
OHstin 3:7666de697752 20 uint16_t textColor;
OHstin 3:7666de697752 21 uint16_t plotColor;
OHstin 3:7666de697752 22 char t[32];
OHstin 3:7666de697752 23
OHstin 3:7666de697752 24 int valStore[130]; // stores all avalialble x Cordinates i.e before plotting
OHstin 3:7666de697752 25 int oldValStore[130]; // stores outdated x Coordinates i.e after plotting
OHstin 3:7666de697752 26
OHstin 3:7666de697752 27 void drawLeftToRight(); // plots data from left to right
OHstin 3:7666de697752 28 void drawRightToLeft(); // plots data from right to left
OHstin 3:7666de697752 29 void printXAxis(int direction); // prints labels on the X Axis depending on direction
OHstin 3:7666de697752 30 void increment(int position, int val); // plots data when before splace on the x axis runs out
OHstin 3:7666de697752 31 void shift(int val); // intenlligently plots more data on the graph by moving plots to the left
OHstin 3:7666de697752 32
OHstin 3:7666de697752 33 };
OHstin 3:7666de697752 34
OHstin 3:7666de697752 35 GraphController::GraphController(uint16_t backgroundColor, char title[])
OHstin 3:7666de697752 36 {
OHstin 3:7666de697752 37 pointNumber = 130; // the total number of X Coordintes
OHstin 3:7666de697752 38 originWidth = 15; // where the x axis starts
OHstin 3:7666de697752 39 originHeight = 116; // where the y axis starts
OHstin 3:7666de697752 40
OHstin 3:7666de697752 41 strcpy(t,title);
OHstin 3:7666de697752 42
OHstin 3:7666de697752 43 bgColor = backgroundColor;
OHstin 3:7666de697752 44
OHstin 3:7666de697752 45 if (bgColor == ST7735_BLACK) {
OHstin 3:7666de697752 46 textColor = ST7735_WHITE;
OHstin 3:7666de697752 47 plotColor = ST7735_YELLOW;
OHstin 3:7666de697752 48 } else if (bgColor == ST7735_WHITE) {
OHstin 3:7666de697752 49 textColor = ST7735_BLACK;
OHstin 3:7666de697752 50 plotColor = ST7735_BLUE;
OHstin 3:7666de697752 51 }
OHstin 3:7666de697752 52 }
OHstin 3:7666de697752 53
OHstin 3:7666de697752 54 void GraphController::drawAxes()
OHstin 3:7666de697752 55 {
OHstin 3:7666de697752 56 tft.setTextColor(textColor);
OHstin 3:7666de697752 57
OHstin 3:7666de697752 58 // first draw the title
OHstin 3:7666de697752 59 tft.setCursor(25,0);
OHstin 3:7666de697752 60 tft.printf("%s",t);
OHstin 3:7666de697752 61
OHstin 3:7666de697752 62 // draw the axes
OHstin 3:7666de697752 63 tft.drawFastHLine(originWidth-1-3,originHeight,pointNumber+10,textColor);
OHstin 3:7666de697752 64 tft.drawFastVLine(originWidth-1,originHeight+1-100,100+3,textColor);
OHstin 3:7666de697752 65
OHstin 3:7666de697752 66 // draw the demarcations
OHstin 3:7666de697752 67 // y axis
OHstin 3:7666de697752 68 tft.drawFastHLine(originWidth-1-3,originHeight-30,3,textColor);
OHstin 3:7666de697752 69 tft.drawFastHLine(originWidth-1-3,originHeight-60,3,textColor);
OHstin 3:7666de697752 70 tft.drawFastHLine(originWidth-1-3,originHeight-90,3,textColor);
OHstin 3:7666de697752 71
OHstin 3:7666de697752 72 // x axis
OHstin 3:7666de697752 73 tft.drawFastVLine(originWidth-1+26,originHeight+1,3,textColor);
OHstin 3:7666de697752 74 tft.drawFastVLine(originWidth-1+(26*2),originHeight+1,3,textColor);
OHstin 3:7666de697752 75 tft.drawFastVLine(originWidth-1+(26*3),originHeight+1,3,textColor);
OHstin 3:7666de697752 76 tft.drawFastVLine(originWidth-1+(26*4),originHeight+1,3,textColor);
OHstin 3:7666de697752 77 tft.drawFastVLine(originWidth-1+(26*5),originHeight+1,3,textColor);
OHstin 3:7666de697752 78
OHstin 3:7666de697752 79
OHstin 3:7666de697752 80 // label the X Axis left to right direction
OHstin 3:7666de697752 81 printXAxis(0);
OHstin 3:7666de697752 82
OHstin 3:7666de697752 83
OHstin 3:7666de697752 84 // change the rotation
OHstin 3:7666de697752 85 tft.setCursor(originWidth-10,originHeight-3);
OHstin 3:7666de697752 86 tft.printf("0");
OHstin 3:7666de697752 87
OHstin 3:7666de697752 88 tft.setCursor(originWidth-10,originHeight-2-30);
OHstin 3:7666de697752 89 tft.printf("3");
OHstin 3:7666de697752 90
OHstin 3:7666de697752 91 tft.setCursor(originWidth-10,originHeight-2-(30*2));
OHstin 3:7666de697752 92 tft.printf("6");
OHstin 3:7666de697752 93
OHstin 3:7666de697752 94 tft.setCursor(originWidth-10,originHeight-2-(30*3));
OHstin 3:7666de697752 95 tft.printf("9");
OHstin 3:7666de697752 96
OHstin 3:7666de697752 97 position = 0;
OHstin 3:7666de697752 98 counter = 0;
OHstin 3:7666de697752 99 previousValue = -1; // previous x coordinate
OHstin 3:7666de697752 100 }
OHstin 3:7666de697752 101
OHstin 3:7666de697752 102 void GraphController::printXAxis(int direction)
OHstin 3:7666de697752 103 {
OHstin 3:7666de697752 104 // direction of zero means left to right
OHstin 3:7666de697752 105
OHstin 3:7666de697752 106 // direction one means right to left
OHstin 3:7666de697752 107
OHstin 3:7666de697752 108 // first set text color to deleting color
OHstin 3:7666de697752 109 tft.setTextColor(bgColor);
OHstin 3:7666de697752 110
OHstin 3:7666de697752 111 if (direction == 0) {
OHstin 3:7666de697752 112
OHstin 3:7666de697752 113 // first clear all the previous right to left numbers
OHstin 3:7666de697752 114 drawRightToLeft();
OHstin 3:7666de697752 115
OHstin 3:7666de697752 116 // draw numbers form left to right
OHstin 3:7666de697752 117 tft.setTextColor(textColor);
OHstin 3:7666de697752 118 drawLeftToRight();
OHstin 3:7666de697752 119
OHstin 3:7666de697752 120 } else if(direction == 1) {
OHstin 3:7666de697752 121
OHstin 3:7666de697752 122 // first clear all the previous left to right numbers
OHstin 3:7666de697752 123 drawLeftToRight();
OHstin 3:7666de697752 124
OHstin 3:7666de697752 125 // draw numbers from left to right
OHstin 3:7666de697752 126 tft.setTextColor(textColor);
OHstin 3:7666de697752 127 drawRightToLeft();
OHstin 3:7666de697752 128 tft.setTextColor(plotColor);
OHstin 3:7666de697752 129
OHstin 3:7666de697752 130 }
OHstin 3:7666de697752 131
OHstin 3:7666de697752 132 // reset to the drawing color
OHstin 3:7666de697752 133
OHstin 3:7666de697752 134 }
OHstin 3:7666de697752 135
OHstin 3:7666de697752 136 void GraphController::drawLeftToRight()
OHstin 3:7666de697752 137 {
OHstin 3:7666de697752 138 tft.setCursor(originWidth-3,originHeight+5);
OHstin 3:7666de697752 139 tft.printf("0");
OHstin 3:7666de697752 140
OHstin 3:7666de697752 141 tft.setCursor(originWidth-8+(25),originHeight+5);
OHstin 3:7666de697752 142 tft.printf("2.5");
OHstin 3:7666de697752 143
OHstin 3:7666de697752 144 tft.setCursor(originWidth-1+(25*2),originHeight+5);
OHstin 3:7666de697752 145 tft.printf("5");
OHstin 3:7666de697752 146
OHstin 3:7666de697752 147 tft.setCursor(originWidth-8+(25*3),originHeight+5);
OHstin 3:7666de697752 148 tft.printf("7.5");
OHstin 3:7666de697752 149
OHstin 3:7666de697752 150 tft.setCursor(originWidth-3+(25*4),originHeight+5);
OHstin 3:7666de697752 151 tft.printf("10");
OHstin 3:7666de697752 152
OHstin 3:7666de697752 153 tft.setCursor(originWidth-8+(25*5),originHeight+5);
OHstin 3:7666de697752 154 tft.printf("12.5");
OHstin 3:7666de697752 155 }
OHstin 3:7666de697752 156
OHstin 3:7666de697752 157 void GraphController::drawRightToLeft()
OHstin 3:7666de697752 158 {
OHstin 3:7666de697752 159
OHstin 3:7666de697752 160 tft.setCursor(originWidth-15,originHeight+5);
OHstin 3:7666de697752 161 tft.printf("12.5");
OHstin 3:7666de697752 162
OHstin 3:7666de697752 163 tft.setCursor(originWidth-5+(25),originHeight+5);
OHstin 3:7666de697752 164 tft.printf("10");
OHstin 3:7666de697752 165
OHstin 3:7666de697752 166 tft.setCursor(originWidth-8+(25*2),originHeight+5);
OHstin 3:7666de697752 167 tft.printf("7.5");
OHstin 3:7666de697752 168
OHstin 3:7666de697752 169 tft.setCursor(originWidth+(25*3),originHeight+5);
OHstin 3:7666de697752 170 tft.printf("5");
OHstin 3:7666de697752 171
OHstin 3:7666de697752 172 tft.setCursor(originWidth-5+(25*4),originHeight+5);
OHstin 3:7666de697752 173 tft.printf("2.5");
OHstin 3:7666de697752 174
OHstin 3:7666de697752 175 tft.setCursor(originWidth+2+(25*5),originHeight+5);
OHstin 3:7666de697752 176 tft.printf("0");
OHstin 3:7666de697752 177 }
OHstin 3:7666de697752 178
OHstin 3:7666de697752 179 void GraphController::plotData()
OHstin 3:7666de697752 180 {
OHstin 3:7666de697752 181 //static int position = 0;
OHstin 3:7666de697752 182 //static int counter = 0;
OHstin 3:7666de697752 183 // first check how many much space is left
OHstin 3:7666de697752 184 //numStore[position-1]= position;
OHstin 3:7666de697752 185 int val = getSolarPower();
OHstin 3:7666de697752 186
OHstin 3:7666de697752 187 // read the val
OHstin 3:7666de697752 188
OHstin 3:7666de697752 189 //counter makes sure the value is only displayed
OHstin 3:7666de697752 190 // only once every 5 times the graph is updated
OHstin 3:7666de697752 191
OHstin 3:7666de697752 192 if (counter == 0) {
OHstin 3:7666de697752 193 if (val != previousValue) {
OHstin 3:7666de697752 194 // first delete the previous value
OHstin 3:7666de697752 195 tft.setCursor(2,9);
OHstin 3:7666de697752 196 tft.setTextColor(bgColor);
OHstin 3:7666de697752 197 tft.printf("%dW",previousValue);
OHstin 3:7666de697752 198
OHstin 3:7666de697752 199 // then write the new value
OHstin 3:7666de697752 200 tft.setCursor(2,9);
OHstin 3:7666de697752 201 tft.setTextColor(plotColor);
OHstin 3:7666de697752 202 tft.printf("%dW",val);
OHstin 3:7666de697752 203 } else {
OHstin 3:7666de697752 204 // do nothing
OHstin 3:7666de697752 205 }
OHstin 3:7666de697752 206
OHstin 3:7666de697752 207 previousValue = val;
OHstin 3:7666de697752 208
OHstin 3:7666de697752 209 }
OHstin 3:7666de697752 210
OHstin 3:7666de697752 211
OHstin 3:7666de697752 212 // esure the counter is reset accordingly i.e every 5 times
OHstin 3:7666de697752 213 counter++;
OHstin 3:7666de697752 214 if (counter > 5) {
OHstin 3:7666de697752 215 counter = 0;
OHstin 3:7666de697752 216 }
OHstin 3:7666de697752 217
OHstin 3:7666de697752 218
OHstin 3:7666de697752 219 // if the space on the X Axis is enough
OHstin 3:7666de697752 220 if (position < pointNumber) {
OHstin 3:7666de697752 221 // update the array with the new value
OHstin 3:7666de697752 222 valStore[position] = val;
OHstin 3:7666de697752 223
OHstin 3:7666de697752 224 // plot the value in the available position
OHstin 3:7666de697752 225 //increment(position, val);
OHstin 3:7666de697752 226 tft.drawFastVLine(originWidth+(position*1),originHeight-val,val,plotColor);
OHstin 3:7666de697752 227
OHstin 3:7666de697752 228 // plot in the following position the next time
OHstin 3:7666de697752 229 position++;
OHstin 3:7666de697752 230 } else {
OHstin 3:7666de697752 231
OHstin 3:7666de697752 232 // intelligently plot the new value on the graph
OHstin 3:7666de697752 233 shift(val);
OHstin 3:7666de697752 234 }
OHstin 3:7666de697752 235
OHstin 3:7666de697752 236 // ensure change of X Axis direction only occurs once
OHstin 3:7666de697752 237 if (position==pointNumber) {
OHstin 3:7666de697752 238 printXAxis(1);
OHstin 3:7666de697752 239 position++;
OHstin 3:7666de697752 240 }
OHstin 3:7666de697752 241 }
OHstin 3:7666de697752 242
OHstin 3:7666de697752 243 void GraphController::increment(int position, int val)
OHstin 3:7666de697752 244 {
OHstin 3:7666de697752 245
OHstin 3:7666de697752 246 }
OHstin 3:7666de697752 247
OHstin 3:7666de697752 248 void GraphController::shift(int val)
OHstin 3:7666de697752 249 {
OHstin 3:7666de697752 250 // all coordinates in the graph are shifted to the left
OHstin 3:7666de697752 251 // therefore creating space for one more coordinate to be plotted
OHstin 3:7666de697752 252
OHstin 3:7666de697752 253
OHstin 3:7666de697752 254 // first, store previous values in the apprpriate array array
OHstin 3:7666de697752 255 for(int i = 0; i < pointNumber; i++) {
OHstin 3:7666de697752 256 oldValStore[i] = valStore[i];
OHstin 3:7666de697752 257 }
OHstin 3:7666de697752 258
OHstin 3:7666de697752 259
OHstin 3:7666de697752 260 // second, shift all the values in the array to the left
OHstin 3:7666de697752 261
OHstin 3:7666de697752 262 int bufferVal = -1;
OHstin 3:7666de697752 263
OHstin 3:7666de697752 264 for (int i = (pointNumber-1); i >= 0; i--) {
OHstin 3:7666de697752 265
OHstin 3:7666de697752 266 int currentSpot = i;
OHstin 3:7666de697752 267 int nextSpot = i-1;
OHstin 3:7666de697752 268
OHstin 3:7666de697752 269 int nextVal = 0;
OHstin 3:7666de697752 270
OHstin 3:7666de697752 271 if(bufferVal != -1) {
OHstin 3:7666de697752 272 nextVal = bufferVal;
OHstin 3:7666de697752 273 } else {
OHstin 3:7666de697752 274 nextVal = valStore[currentSpot];
OHstin 3:7666de697752 275 }
OHstin 3:7666de697752 276
OHstin 3:7666de697752 277 bufferVal = valStore[nextSpot];
OHstin 3:7666de697752 278 valStore[nextSpot] = nextVal;
OHstin 3:7666de697752 279 }
OHstin 3:7666de697752 280
OHstin 3:7666de697752 281 valStore[pointNumber-1] = val;
OHstin 3:7666de697752 282
OHstin 3:7666de697752 283 // third, intelligently plot the data onto the graph
OHstin 3:7666de697752 284 int deltaVal;
OHstin 3:7666de697752 285 for (int i = 0; i < pointNumber; i++) {
OHstin 3:7666de697752 286
OHstin 3:7666de697752 287 // first calculate difference between the new value and the old value
OHstin 3:7666de697752 288 deltaVal = valStore[i] - oldValStore[i];
OHstin 3:7666de697752 289
OHstin 3:7666de697752 290 // if new value is bigger the old value
OHstin 3:7666de697752 291 if (deltaVal > 0) {
OHstin 3:7666de697752 292 // only add the extra height required
OHstin 3:7666de697752 293 tft.drawFastVLine(originWidth+(i*1),originHeight-oldValStore[i]-deltaVal,deltaVal,plotColor);
OHstin 3:7666de697752 294 }
OHstin 3:7666de697752 295 // if new value is smaller than the old value
OHstin 3:7666de697752 296 else if(deltaVal < 0) {
OHstin 3:7666de697752 297 // only remmove the excess height
OHstin 3:7666de697752 298 tft.drawFastVLine(originWidth+(i*1),originHeight-oldValStore[i],(deltaVal*-1),bgColor);
OHstin 3:7666de697752 299 }
OHstin 3:7666de697752 300 // if the values are the same
OHstin 3:7666de697752 301 else {
OHstin 3:7666de697752 302 // do nothing
OHstin 3:7666de697752 303 }
OHstin 3:7666de697752 304 }
OHstin 3:7666de697752 305 }
OHstin 3:7666de697752 306
OHstin 3:7666de697752 307
OHstin 3:7666de697752 308 #endif