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:
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?

UserRevisionLine numberNew contents of line
OHstin 3:7666de697752 1 #ifndef LOGRECORD_H
OHstin 3:7666de697752 2 #define LOGRECORD_H
OHstin 3:7666de697752 3
OHstin 3:7666de697752 4 #include "mbed.h"
OHstin 3:7666de697752 5
OHstin 3:7666de697752 6 // logging parameters
OHstin 3:7666de697752 7 #define SOLAR_POWER 1
OHstin 3:7666de697752 8 #define POWER_CONSUMPTION 2
OHstin 3:7666de697752 9
OHstin 3:7666de697752 10 // durations
OHstin 3:7666de697752 11 #define THIRTY_SECONDS 1
OHstin 3:7666de697752 12 #define ONE_MINUTE 2
OHstin 3:7666de697752 13
OHstin 3:7666de697752 14 class LogRecord
OHstin 3:7666de697752 15 {
OHstin 3:7666de697752 16 public:
OHstin 3:7666de697752 17 LogRecord();
OHstin 3:7666de697752 18 LogRecord( char iD[]);
OHstin 3:7666de697752 19 void initialiseRecord(MbedJSONValue &temp, int paramter, int duration);
OHstin 3:7666de697752 20 void initialiseRecord(MbedJSONValue &temp, int ID);
OHstin 3:7666de697752 21 bool updateRecord(MbedJSONValue &temp); // returns true if update successful, false if completed
OHstin 3:7666de697752 22 int getParameter(); // get the current parameter
OHstin 3:7666de697752 23 int getID(); // get the ID of the record
OHstin 3:7666de697752 24 char *getName(); // get the name of the parameter
OHstin 3:7666de697752 25 float getMaximum(); // get the maximum value
OHstin 3:7666de697752 26 float getMinimum(); // get the minimum value
OHstin 3:7666de697752 27 float getAverage(); // get the average value
OHstin 3:7666de697752 28 int getStatus(); // get the status
OHstin 3:7666de697752 29 int getTimeStamp(); // get the time of recording
OHstin 3:7666de697752 30 int getDuration(); // get the duration of the log
OHstin 3:7666de697752 31 float getPercentage(); // get the percentage time of the log completed
OHstin 3:7666de697752 32
OHstin 3:7666de697752 33 private:
OHstin 3:7666de697752 34 int parameter;
OHstin 3:7666de697752 35 int status;
OHstin 3:7666de697752 36 char timeleft[35];
OHstin 3:7666de697752 37 float maximum;
OHstin 3:7666de697752 38 float minimum;
OHstin 3:7666de697752 39 float average;
OHstin 3:7666de697752 40 float percentage;
OHstin 3:7666de697752 41 char name[15];
OHstin 3:7666de697752 42 char logID[35];
OHstin 3:7666de697752 43 int id;
OHstin 3:7666de697752 44 int count; // the number of times the record is updated
OHstin 3:7666de697752 45 int totalCount; // the total number of data points to be measured
OHstin 3:7666de697752 46 int viewed; // checks if the log has been viewed
OHstin 3:7666de697752 47 int timeStamp; // time the log was taken
OHstin 3:7666de697752 48 int duration; // the time it took
OHstin 3:7666de697752 49 };
OHstin 3:7666de697752 50
OHstin 3:7666de697752 51 LogRecord::LogRecord()
OHstin 3:7666de697752 52 {
OHstin 3:7666de697752 53 // set the name to be empty
OHstin 3:7666de697752 54 strcpy(name,"");
OHstin 3:7666de697752 55 }
OHstin 3:7666de697752 56
OHstin 3:7666de697752 57 LogRecord::LogRecord( char iD[])
OHstin 3:7666de697752 58 {
OHstin 3:7666de697752 59 // copy iD string into logID
OHstin 3:7666de697752 60 strcpy(logID,iD);
OHstin 3:7666de697752 61 }
OHstin 3:7666de697752 62
OHstin 3:7666de697752 63 void LogRecord::initialiseRecord(MbedJSONValue &temp, int ID)
OHstin 3:7666de697752 64 {
OHstin 3:7666de697752 65 // copy all the data into the record
OHstin 3:7666de697752 66
OHstin 3:7666de697752 67 // id used to determine its location
OHstin 3:7666de697752 68 id = ID;
OHstin 3:7666de697752 69
OHstin 3:7666de697752 70 timeStamp = temp[logID]["t"].get<int>();
OHstin 3:7666de697752 71
OHstin 3:7666de697752 72 // parameter
OHstin 3:7666de697752 73 parameter = temp[logID]["p"].get<int>();
OHstin 3:7666de697752 74
OHstin 3:7666de697752 75 if(parameter == SOLAR_POWER)
OHstin 3:7666de697752 76 {
OHstin 3:7666de697752 77 strcpy(name,"solar");
OHstin 3:7666de697752 78 }else if(parameter == POWER_CONSUMPTION){
OHstin 3:7666de697752 79 strcpy(name,"consumption");
OHstin 3:7666de697752 80 }
OHstin 3:7666de697752 81
OHstin 3:7666de697752 82 // maximum
OHstin 3:7666de697752 83 int maximumInt = temp[logID]["mx"].get<int>();
OHstin 3:7666de697752 84 maximum = maximumInt/100;
OHstin 3:7666de697752 85
OHstin 3:7666de697752 86 // minimum
OHstin 3:7666de697752 87 int minimumInt = temp[logID]["mn"].get<int>();
OHstin 3:7666de697752 88 minimum = minimumInt/100;
OHstin 3:7666de697752 89
OHstin 3:7666de697752 90 // average
OHstin 3:7666de697752 91 int averageInt = temp[logID]["av"].get<int>();
OHstin 3:7666de697752 92 average = averageInt/100;
OHstin 3:7666de697752 93
OHstin 3:7666de697752 94 // status
OHstin 3:7666de697752 95 status = temp[logID]["s"].get<int>();
OHstin 3:7666de697752 96 // duration
OHstin 3:7666de697752 97 duration = temp[logID]["d"].get<int>();
OHstin 3:7666de697752 98 // percentage
OHstin 3:7666de697752 99 int percentageInt = temp[logID]["%"].get<int>();
OHstin 3:7666de697752 100 percentage = percentageInt/10;
OHstin 3:7666de697752 101 // viewed
OHstin 3:7666de697752 102 viewed = temp[logID]["v"].get<int>();
OHstin 3:7666de697752 103
OHstin 3:7666de697752 104
OHstin 3:7666de697752 105
OHstin 3:7666de697752 106 }
OHstin 3:7666de697752 107
OHstin 3:7666de697752 108 void LogRecord::initialiseRecord(MbedJSONValue &temp, int param, int durn)
OHstin 3:7666de697752 109 {
OHstin 3:7666de697752 110 // set counter to zero
OHstin 3:7666de697752 111 count = 0;
OHstin 3:7666de697752 112 // set the parameter
OHstin 3:7666de697752 113 temp[logID]["p"] = param;
OHstin 3:7666de697752 114 // also determine which value/sensor to be stored
OHstin 3:7666de697752 115
OHstin 3:7666de697752 116 // set the duration
OHstin 3:7666de697752 117 temp[logID]["d"] = durn;
OHstin 3:7666de697752 118 // determine the total data points
OHstin 3:7666de697752 119 switch(durn) {
OHstin 3:7666de697752 120 // the interrupt that logs data is called every 10s
OHstin 3:7666de697752 121 case THIRTY_SECONDS:
OHstin 3:7666de697752 122 totalCount = 3;
OHstin 3:7666de697752 123 break;
OHstin 3:7666de697752 124 case ONE_MINUTE:
OHstin 3:7666de697752 125 totalCount = 6;
OHstin 3:7666de697752 126 break;
OHstin 3:7666de697752 127 default:
OHstin 3:7666de697752 128 totalCount = 0;
OHstin 3:7666de697752 129 break;
OHstin 3:7666de697752 130 }
OHstin 3:7666de697752 131
OHstin 3:7666de697752 132 // set the time logging started
OHstin 3:7666de697752 133 temp[logID]["t"] = static_cast<int>(time(NULL));;
OHstin 3:7666de697752 134
OHstin 3:7666de697752 135 // set the status to logging
OHstin 3:7666de697752 136 temp[logID]["s"] = 1;
OHstin 3:7666de697752 137
OHstin 3:7666de697752 138 // set default max
OHstin 3:7666de697752 139 maximum = 0.0;
OHstin 3:7666de697752 140 // set default min
OHstin 3:7666de697752 141 minimum = 0.0;
OHstin 3:7666de697752 142 // set default average
OHstin 3:7666de697752 143 average = 0.0;
OHstin 3:7666de697752 144 }
OHstin 3:7666de697752 145
OHstin 3:7666de697752 146 bool LogRecord::updateRecord(MbedJSONValue &temp)
OHstin 3:7666de697752 147 {
OHstin 3:7666de697752 148 // increment counter
OHstin 3:7666de697752 149 count++;
OHstin 3:7666de697752 150
OHstin 3:7666de697752 151 if (count > totalCount) {
OHstin 3:7666de697752 152 // change status to complete
OHstin 3:7666de697752 153 temp[logID]["s"] = 2;
OHstin 3:7666de697752 154 return false; // logging complete
OHstin 3:7666de697752 155 }
OHstin 3:7666de697752 156
OHstin 3:7666de697752 157 // value used to store the read value
OHstin 3:7666de697752 158 float value = 30;
OHstin 3:7666de697752 159
OHstin 3:7666de697752 160 // calculate maximum
OHstin 3:7666de697752 161 if( value > maximum)
OHstin 3:7666de697752 162 maximum = value;
OHstin 3:7666de697752 163 // calculate minimum
OHstin 3:7666de697752 164 if( value < minimum)
OHstin 3:7666de697752 165 minimum = value;
OHstin 3:7666de697752 166 // calclute average
OHstin 3:7666de697752 167 average = (average + value )/ count;
OHstin 3:7666de697752 168 // calculate %age complete
OHstin 3:7666de697752 169 float percentage = count/totalCount;
OHstin 3:7666de697752 170
OHstin 3:7666de697752 171 // write data to JSON Object
OHstin 3:7666de697752 172 temp[logID]["mx"] = (int)(maximum*100);
OHstin 3:7666de697752 173 temp[logID]["mn"] = (int)(minimum*100);
OHstin 3:7666de697752 174 temp[logID]["av"] = (int)(average*100);
OHstin 3:7666de697752 175 temp[logID]["%"] = (int)(percentage*100);
OHstin 3:7666de697752 176
OHstin 3:7666de697752 177 return true;
OHstin 3:7666de697752 178 }
OHstin 3:7666de697752 179
OHstin 3:7666de697752 180 int LogRecord::getParameter()
OHstin 3:7666de697752 181 {
OHstin 3:7666de697752 182 return parameter;
OHstin 3:7666de697752 183 }
OHstin 3:7666de697752 184
OHstin 3:7666de697752 185 char *LogRecord::getName()
OHstin 3:7666de697752 186 {
OHstin 3:7666de697752 187 return name;
OHstin 3:7666de697752 188 }
OHstin 3:7666de697752 189
OHstin 3:7666de697752 190 int LogRecord::getID()
OHstin 3:7666de697752 191 {
OHstin 3:7666de697752 192 return id;
OHstin 3:7666de697752 193 }
OHstin 3:7666de697752 194
OHstin 3:7666de697752 195 float LogRecord::getMaximum()
OHstin 3:7666de697752 196 {
OHstin 3:7666de697752 197 return maximum;
OHstin 3:7666de697752 198 }
OHstin 3:7666de697752 199
OHstin 3:7666de697752 200 float LogRecord::getMinimum()
OHstin 3:7666de697752 201 {
OHstin 3:7666de697752 202 return minimum;
OHstin 3:7666de697752 203 }
OHstin 3:7666de697752 204
OHstin 3:7666de697752 205 float LogRecord::getAverage()
OHstin 3:7666de697752 206 {
OHstin 3:7666de697752 207 return average;
OHstin 3:7666de697752 208 }
OHstin 3:7666de697752 209
OHstin 3:7666de697752 210 int LogRecord::getStatus()
OHstin 3:7666de697752 211 {
OHstin 3:7666de697752 212 return status;
OHstin 3:7666de697752 213 }
OHstin 3:7666de697752 214
OHstin 3:7666de697752 215 int LogRecord::getTimeStamp()
OHstin 3:7666de697752 216 {
OHstin 3:7666de697752 217 return timeStamp;
OHstin 3:7666de697752 218 }
OHstin 3:7666de697752 219
OHstin 3:7666de697752 220 int LogRecord::getDuration()
OHstin 3:7666de697752 221 {
OHstin 3:7666de697752 222 return duration;
OHstin 3:7666de697752 223 }
OHstin 3:7666de697752 224
OHstin 3:7666de697752 225 float LogRecord::getPercentage()
OHstin 3:7666de697752 226 {
OHstin 3:7666de697752 227 return percentage;
OHstin 3:7666de697752 228 }
OHstin 3:7666de697752 229 #endif