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 SETTINGS_H
OHstin 3:7666de697752 2 #define SETTINGS_H
OHstin 3:7666de697752 3
OHstin 3:7666de697752 4 class Settings
OHstin 3:7666de697752 5 {
OHstin 3:7666de697752 6
OHstin 3:7666de697752 7 public:
OHstin 3:7666de697752 8 Settings();
OHstin 3:7666de697752 9 void setBackgroundColor(int);
OHstin 3:7666de697752 10 uint16_t getBackgroundColor();
OHstin 3:7666de697752 11 void setLCDBrightness(double);
OHstin 3:7666de697752 12 float getLCDBrightness();
OHstin 3:7666de697752 13
OHstin 3:7666de697752 14 private:
OHstin 3:7666de697752 15 void initialiseSettings(); // intialises the settings
OHstin 3:7666de697752 16
OHstin 3:7666de697752 17 bool settingsExist(const char *fname); // checks whether the setting file exists
OHstin 3:7666de697752 18
OHstin 3:7666de697752 19 static MbedJSONValue holder; // JSON Data structure to store settings read from file
OHstin 3:7666de697752 20 static uint16_t backgroundColor; //
OHstin 3:7666de697752 21 static float LCDBrightness; // tracks the LCDBrightness
OHstin 3:7666de697752 22 static bool settingsInitialised; // tracks if settings have already been created
OHstin 3:7666de697752 23 };
OHstin 3:7666de697752 24
OHstin 3:7666de697752 25
OHstin 3:7666de697752 26 Settings::Settings()
OHstin 3:7666de697752 27 {
OHstin 3:7666de697752 28 // initialise the settings first
OHstin 3:7666de697752 29
OHstin 3:7666de697752 30 // remember to set the varaible to false
OHstin 3:7666de697752 31 if (!settingsInitialised) {
OHstin 3:7666de697752 32 initialiseSettings();
OHstin 3:7666de697752 33 settingsInitialised = true;
OHstin 3:7666de697752 34 }
OHstin 3:7666de697752 35 }
OHstin 3:7666de697752 36
OHstin 3:7666de697752 37 void Settings::initialiseSettings()
OHstin 3:7666de697752 38 {
OHstin 3:7666de697752 39 // pointer to the settings file
OHstin 3:7666de697752 40 FILE *ptr_myfile;
OHstin 3:7666de697752 41
OHstin 3:7666de697752 42 // buffer to store settings data
OHstin 3:7666de697752 43 char read_string [256];
OHstin 3:7666de697752 44
OHstin 3:7666de697752 45 // if the settings file does not exit, create it
OHstin 3:7666de697752 46 if (!settingsExist("/local/settings.txt")) {
OHstin 3:7666de697752 47
OHstin 3:7666de697752 48 // create the settings file
OHstin 3:7666de697752 49 FILE *fp;
OHstin 3:7666de697752 50 // prepare file for writing
OHstin 3:7666de697752 51 fp = fopen("/local/settings.txt", "w");
OHstin 3:7666de697752 52
OHstin 3:7666de697752 53 // create temporary JSON object
OHstin 3:7666de697752 54 MbedJSONValue temp;
OHstin 3:7666de697752 55
OHstin 3:7666de697752 56 // construct the settings data
OHstin 3:7666de697752 57 temp["LCDBrightness"] = 1;
OHstin 3:7666de697752 58 temp["LCDColor"] = 1;
OHstin 3:7666de697752 59 temp["Data"] = 1;
OHstin 3:7666de697752 60
OHstin 3:7666de697752 61 // convert json object to string
OHstin 3:7666de697752 62 std::string s;
OHstin 3:7666de697752 63 s = temp.serialize();
OHstin 3:7666de697752 64
OHstin 3:7666de697752 65 // write settings to file
OHstin 3:7666de697752 66 fprintf(fp, "%s", s.c_str());
OHstin 3:7666de697752 67
OHstin 3:7666de697752 68 // close the file
OHstin 3:7666de697752 69 fclose(fp);
OHstin 3:7666de697752 70 }
OHstin 3:7666de697752 71
OHstin 3:7666de697752 72 // prepare the settings file for reading
OHstin 3:7666de697752 73 ptr_myfile = fopen("/local/settings.txt", "r");
OHstin 3:7666de697752 74
OHstin 3:7666de697752 75 // read the settings string into buffer
OHstin 3:7666de697752 76 fgets(read_string,256,ptr_myfile);
OHstin 3:7666de697752 77
OHstin 3:7666de697752 78 // convert string into JSON Data
OHstin 3:7666de697752 79 parse(holder,read_string);
OHstin 3:7666de697752 80
OHstin 3:7666de697752 81 // initialise all the settings
OHstin 3:7666de697752 82
OHstin 3:7666de697752 83 // LCD Background Color
OHstin 3:7666de697752 84 int color = holder["LCDColor"].get<int>();
OHstin 3:7666de697752 85 if (color == 1)
OHstin 3:7666de697752 86 backgroundColor = ST7735_BLACK;
OHstin 3:7666de697752 87 else
OHstin 3:7666de697752 88 backgroundColor = ST7735_WHITE;
OHstin 3:7666de697752 89
OHstin 3:7666de697752 90 // LCD Brightness
OHstin 3:7666de697752 91
OHstin 3:7666de697752 92 // close the file
OHstin 3:7666de697752 93 fclose(ptr_myfile);
OHstin 3:7666de697752 94
OHstin 3:7666de697752 95
OHstin 3:7666de697752 96 }
OHstin 3:7666de697752 97
OHstin 3:7666de697752 98 bool Settings::settingsExist( const char *fname)
OHstin 3:7666de697752 99 {
OHstin 3:7666de697752 100 FILE *file;
OHstin 3:7666de697752 101 if (file = fopen(fname, "r")) {
OHstin 3:7666de697752 102 fclose(file);
OHstin 3:7666de697752 103 return true;
OHstin 3:7666de697752 104 }
OHstin 3:7666de697752 105 return false;
OHstin 3:7666de697752 106 }
OHstin 3:7666de697752 107
OHstin 3:7666de697752 108 void Settings::setBackgroundColor( int num )
OHstin 3:7666de697752 109 {
OHstin 3:7666de697752 110 // change the background color apropriately
OHstin 3:7666de697752 111 if (num == 1)
OHstin 3:7666de697752 112 backgroundColor = ST7735_BLACK;
OHstin 3:7666de697752 113 else
OHstin 3:7666de697752 114 backgroundColor = ST7735_WHITE;
OHstin 3:7666de697752 115
OHstin 3:7666de697752 116 // pointer to access file
OHstin 3:7666de697752 117 FILE *ptr_myfile;
OHstin 3:7666de697752 118
OHstin 3:7666de697752 119 // prepare file for writing
OHstin 3:7666de697752 120 ptr_myfile = fopen("/local/settings.txt", "w");
OHstin 3:7666de697752 121
OHstin 3:7666de697752 122 // write the new LCD background value in JSON Object
OHstin 3:7666de697752 123 holder["LCDColor"] = num;
OHstin 3:7666de697752 124
OHstin 3:7666de697752 125 // convert json data to string
OHstin 3:7666de697752 126 std::string s;
OHstin 3:7666de697752 127 s = holder.serialize();
OHstin 3:7666de697752 128
OHstin 3:7666de697752 129 // write it to the file
OHstin 3:7666de697752 130 fprintf(ptr_myfile, "%s", s.c_str());
OHstin 3:7666de697752 131
OHstin 3:7666de697752 132 fclose(ptr_myfile);
OHstin 3:7666de697752 133
OHstin 3:7666de697752 134
OHstin 3:7666de697752 135 }
OHstin 3:7666de697752 136
OHstin 3:7666de697752 137 uint16_t Settings::getBackgroundColor()
OHstin 3:7666de697752 138 {
OHstin 3:7666de697752 139 // returns the background color
OHstin 3:7666de697752 140 if (settingsInitialised){
OHstin 3:7666de697752 141 return backgroundColor;
OHstin 3:7666de697752 142 }
OHstin 3:7666de697752 143
OHstin 3:7666de697752 144 // blue background is a sign of an error
OHstin 3:7666de697752 145 return ST7735_BLUE;
OHstin 3:7666de697752 146 }
OHstin 3:7666de697752 147
OHstin 3:7666de697752 148 #endif