Augustine Kizito / Mbed 2 deprecated Solar_Powered_Smart_Camera

Dependencies:   Adafruit_GFX Adafruit_ST7735 INA219 MODSERIAL MbedJSONValue mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Settings.h Source File

Settings.h

00001 #ifndef SETTINGS_H
00002 #define SETTINGS_H
00003 
00004 class Settings
00005 {
00006 
00007 public:
00008     Settings();
00009     void setBackgroundColor(int);
00010     uint16_t getBackgroundColor();
00011     void setLCDBrightness(double);
00012     float getLCDBrightness();
00013 
00014 private:
00015     void initialiseSettings();  // intialises the settings
00016     
00017     bool settingsExist(const char *fname);  // checks whether the setting file exists
00018     
00019     static MbedJSONValue holder;    // JSON Data structure to store settings read from file
00020     static uint16_t backgroundColor;  //
00021     static float LCDBrightness; // tracks the LCDBrightness
00022     static bool settingsInitialised;    // tracks if settings have already been created
00023 };
00024 
00025 
00026 Settings::Settings()
00027 {
00028     // initialise the settings first
00029 
00030     // remember to set the varaible to false
00031     if (!settingsInitialised) {
00032         initialiseSettings();
00033         settingsInitialised = true;
00034     }
00035 }
00036 
00037 void Settings::initialiseSettings()
00038 {
00039     // pointer to the settings file
00040     FILE *ptr_myfile;
00041 
00042     // buffer to store settings data
00043     char read_string [256];
00044 
00045     // if the settings file does not exit, create it
00046     if (!settingsExist("/local/settings.txt")) {
00047 
00048         // create the settings file
00049         FILE *fp;
00050         // prepare file for writing
00051         fp = fopen("/local/settings.txt", "w");
00052 
00053         // create temporary JSON object
00054         MbedJSONValue temp;
00055 
00056         // construct the settings data
00057         temp["LCDBrightness"] = 1;
00058         temp["LCDColor"] = 1;
00059         temp["Data"] = 1;
00060 
00061         // convert json object to string
00062         std::string s;
00063         s = temp.serialize();
00064 
00065         // write settings to file
00066         fprintf(fp, "%s", s.c_str());
00067 
00068         // close the file
00069         fclose(fp);
00070     }
00071 
00072     // prepare the settings file for reading
00073     ptr_myfile = fopen("/local/settings.txt", "r");
00074 
00075     // read the settings string into buffer
00076     fgets(read_string,256,ptr_myfile);
00077 
00078     // convert string into JSON Data
00079     parse(holder,read_string);
00080 
00081     // initialise all the settings
00082 
00083     // LCD Background Color
00084     int color = holder["LCDColor"].get<int>();
00085     if (color == 1)
00086         backgroundColor = ST7735_BLACK;
00087     else
00088         backgroundColor = ST7735_WHITE;
00089 
00090     // LCD Brightness
00091 
00092     // close the file
00093     fclose(ptr_myfile);
00094 
00095 
00096 }
00097 
00098 bool Settings::settingsExist( const char *fname)
00099 {
00100     FILE *file;
00101     if (file = fopen(fname, "r")) {
00102         fclose(file);
00103         return true;
00104     }
00105     return false;
00106 }
00107 
00108 void Settings::setBackgroundColor( int num )
00109 {
00110     // change the background color apropriately
00111     if (num == 1)
00112         backgroundColor = ST7735_BLACK;
00113     else
00114         backgroundColor = ST7735_WHITE;
00115 
00116     // pointer to access file
00117     FILE *ptr_myfile;
00118 
00119     // prepare file for writing
00120     ptr_myfile = fopen("/local/settings.txt", "w");
00121 
00122     // write the new LCD background value in JSON Object
00123     holder["LCDColor"] = num;
00124 
00125     // convert json data to string
00126     std::string s;
00127     s = holder.serialize();
00128 
00129     // write it to the file
00130     fprintf(ptr_myfile, "%s", s.c_str());
00131 
00132     fclose(ptr_myfile);
00133 
00134 
00135 }
00136 
00137 uint16_t Settings::getBackgroundColor()
00138 {
00139     // returns the background color
00140     if (settingsInitialised){
00141         return backgroundColor;
00142     }
00143     
00144     // blue background is a sign of an error
00145     return ST7735_BLUE;
00146 }
00147 
00148 #endif