Augustine Kizito / Mbed 2 deprecated Solar_Powered_Smart_Camera

Dependencies:   Adafruit_GFX Adafruit_ST7735 INA219 MODSERIAL MbedJSONValue mbed-rtos mbed

Revision:
3:7666de697752
Parent:
2:104bec169fb6
Child:
4:c7b0670f96b2
--- a/main.cpp	Mon Jul 18 04:31:21 2016 +0000
+++ b/main.cpp	Sat Feb 04 20:17:44 2017 +0000
@@ -1,135 +1,522 @@
 #include "mbed.h"
+#include "rtos.h"
+#include "MbedJSONValue.h"
+#include <string>
 #include "Adafruit_ST7735.h"
+#include "SensorSuite.h"
+
 
-Adafruit_ST7735 tft(p11, p12, p13, p10, p8, p9); // MOSI, MISO, SCLK, SSEL, TFT_DC, TFT_RST
-#include "BatteryIcon.h"
-#include "SolarIcon.h"
-#include "OutputIcon.h"
-#include "SettingsIcon.h"
+DigitalOut myled(LED1);
+DigitalOut myled2(LED2);
+DigitalOut relayOne(p26);
+DigitalOut relayTwo(p25);
+DigitalOut relayThree(p24);
+
+#define OFF 1
+#define ON 0
+
+
+
+
+uint16_t backgroundColor = ST7735_BLACK;
 
 
-//LEDs used to display output
-
-DigitalIn enable1(p21);
-DigitalIn enable2(p22);
-DigitalIn enable3(p23);
+LocalFileSystem local("local");
 
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
+//Adafruit_ST7735 tft(p11, p12, p13, p10, p8, p9); // MOSI, MISO, SCLK, SSEL, TFT_DC, TFT_RST
+Adafruit_ST7735 tft(p11, p12, p13, p16, p14, p15); // MOSI, MISO, SCLK, SSEL, TFT_DC, TFT_RST
+#include "Icons.h"
+#include "Inputs.h"
+#include "Settings.h"
+#include "SettingsTest.h"
+#include "LogManager.h"
+#include "LogRecord.h"
+#include "Screens.h"
+#include "RaspiSerial.h"
 
-AnalogIn ain(p20);
-AnalogIn ain1(p19);
-AnalogIn ain2(p18);
 
-DigitalOut myled(LED1);
+void createScreens();
+void changeBackgroundColor();
+bool settingsExist( const char *fname);
+void uiThread();
+void sensorThread();
+
+uint16_t currentColor;
 
-void testdrawtext(char *text, uint16_t color);
+// ensure that settings are not initialised
+bool Settings::settingsInitialised = false;
+MbedJSONValue Settings:: holder;    // JSON Data structure to store settings read from file
+uint16_t Settings::backgroundColor = 0;
 
-// battery icon coordinates
-int startingWidth = 15;
-int startingHeight = 7;
+// ensure that sensor data is initialised
+
+
 
-// solar icon coordinates
-int xCoord = 10;
-int yCoord = 65;
-
-// output icon coordinates
-int xCoord2 = 100;
-int yCoord2 = 10;
-
-// settings icon coordinates
-int xCoord3 = 100;
-int yCoord3 = 60;
+MainMenuScreen *menu;
+BatteriesScreen *batteriesScreen;
+BatteryScreen *battScreen;
+SolarScreen *solarScreen;
+OutputScreen *outputScreen;
+SettingsScreen *settingsScreen;
+SolarGraphScreen *solarGraphScreen;
+SolarValueScreen *solarValueScreen;
+TestScreen *test;
+LogScreen *logg;
+LogSelectScreen *logSelect2;
+LogSelectScreen *logSelect1;
+LogDurationScreen *logDuration;
+LogDurationScreen *logDuration2;
+ViewLogScreen *viewLog;
+DeleteLogScreen *deleteLog;
+LogManager *log1;
+//LogManager *log2;
 
 int main()
 {
+    // launch the sensors
+    Thread thread(osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25), NULL); 
+    thread.start(sensorThread);
+    
+    // launch the user interface
+    Thread thread2(osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25), NULL); 
+    thread2.start(uiThread);
+    
+    // launch serial communication with raspberry pi
+    Thread thread3(osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25), NULL); 
+    thread3.start(raspiSerial);
+    
+    
+    while(true) {
+
+ 
+    }
+
+
+}
+
+void sensorThread()
+{
+    SensorSuite suite;
+    // launch the sensor suite
+    suite.begin();
+}
+
+void uiThread()
+{
+    // set the current time
+    set_time(1475252667);
     // Use this initializer if you're using a 1.8" TFT
     tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab
 
-    uint16_t backgroundColor = ST7735_BLACK;
+
+
+    // create the settings object
+    Settings settings;
+    currentColor = settings.getBackgroundColor();
+    backgroundColor = currentColor;
+
     tft.fillScreen(backgroundColor);
-
     // landscape view
     tft.setRotation(1);
-    char current_text[40];
-    
-    int batteryLevel = ain2*32.5;
-    int percentage = ain2*100;
-    bool radiationReceived = false;
-    bool outputStatus = false;
+    createScreens();
 
-    sprintf(current_text, "W: %ldp H: %ldp ", tft.height(), tft.width());
-    //testdrawtext(current_text, ST7735_WHITE);
+    //struct setting lcdBackground;
+    //lcdBackground.value = 1;
+    //lcdBackground.key = 1;
+    //rewind(ptr_myfile);
+    //fseek(ptr_myfile,sizeof(struct setting)*0,SEEK_SET);
+    //fwrite(&emptySetting, sizeof(struct setting), 1, ptr_myfile);
+    //fread(&lcdBackground, sizeof(struct setting), 1, ptr_myfile);
+
+
 
-    // determine if the battery is charging
-    // and if solar radiation is received
-    bool batteryCharging;
-    if (ain1 > 0.5) {
-        batteryCharging = true;
-        radiationReceived = true;
-    } else {
-        batteryCharging = false;
-        radiationReceived = false;
+    /**
+    wait(5);
+    relayOne = OFF;
+    relayTwo = OFF;
+    relayThree = OFF;
+    wait(5);
+    while(1) {
+
+        relayTwo = ON;
+        relayOne = ON;
+        relayThree = ON;
+
+        wait(5);
+        relayOne = OFF;
+        relayTwo = OFF;
+        relayThree = OFF;
+        wait(5);
     }
 
-    // create and initialise battery icon project
-    BatteryIcon batteryIcon(startingWidth, startingHeight,backgroundColor,percentage,batteryCharging);
-    // draw the battery icon
-    batteryIcon.drawBatteryIcon();
+    **/
+
+
+
+
+    bool backToFirstLayer = false;
+    bool backToSecondLayer = false;
+    bool backToThirdLayer = false;
+    bool backToFourthLayer = false;
+
+    while(1) {
+
+        int val = menu->start();
+
+        if (val == 1) {
+
+            backToFirstLayer = false;
+
+            while(!backToFirstLayer) {
+
+                switch(batteriesScreen->start()) {
+
+                    case 1:
+                        // clear screen
+                        tft.fillScreen(backgroundColor);
+                        backToSecondLayer = false;
+
+                        while(!backToSecondLayer) {
+                            switch(battScreen->start(BATTERY_ONE)) {
+                                case -1:
+                                    // clear screen
+                                    tft.fillScreen(backgroundColor);
+                                    backToSecondLayer = true;
+                                    break;
+                                default:
+                                    // do nothing
+                                    break;
+                            }
+                        }
+                        break;
+
+                    case 2:
+                        // clear screen
+                        tft.fillScreen(backgroundColor);
+                        backToSecondLayer = false;
+
+                        while(!backToSecondLayer) {
+                            switch(battScreen->start(BATTERY_TWO)) {
+                                case -1:
+                                    // clear screen
+                                    tft.fillScreen(backgroundColor);
+                                    backToSecondLayer = true;
+                                    break;
+                                default:
+                                    // do nothing
+                                    break;
+                            }
+                        }
+                        break;
+
+                    case -1:
+                        // clear screen
+                        tft.fillScreen(backgroundColor);
+                        backToFirstLayer = true;
+                        break;
+
+                    default:
+
+                        break;
+                }
+            }
+
+        } else  if (val == 2) {
+            outputScreen->start();
+        } else if (val == 3) {
+
+            backToFirstLayer = false;
+
+            while(!backToFirstLayer) {
+
+                switch(solarScreen->start()) {
+
+                    case 1:
+                        // clear screen
+                        tft.fillScreen(backgroundColor);
+                        backToSecondLayer = false;
 
-    // create and initialise solar icon
-    SolarIcon solarIcon(xCoord, yCoord, backgroundColor);
-    // draw solar icon
-    solarIcon.drawSolarIcon();
-    
-    // create and initalise output icon
-    OutputIcon outputIcon(xCoord2, yCoord2, backgroundColor);
-    // draw output icon
-    outputIcon.drawOutputIcon();
+                        while(!backToSecondLayer) {
+                            switch(solarValueScreen->start()) {
+                                case -1:
+                                    // clear screen
+                                    tft.fillScreen(backgroundColor);
+                                    backToSecondLayer = true;
+                                    break;
+                                default:
+                                    // do nothing
+                                    break;
+
+                            }
+                        }
+                        break;
+
+                    case 2:
+                        // clear screen
+                        tft.fillScreen(backgroundColor);
+                        backToSecondLayer = false;
+
+                        while(!backToSecondLayer) {
+                            switch(solarGraphScreen->start()) {
+                                case -1:
+                                    // clear screen
+                                    tft.fillScreen(backgroundColor);
+                                    backToSecondLayer = true;
+                                    break;
+                                default:
+                                    // do nothing
+                                    break;
+
+                            }
+                        }
+                        break;
+                    case -1:
+                        // clear screen
+                        tft.fillScreen(backgroundColor);
+                        backToFirstLayer = true;
+                        break;
 
-    // create and initalise output icon
-    SettingsIcon settingsIcon(xCoord3, yCoord3, backgroundColor);
-    // draw settings icon
-    settingsIcon.drawSettingsIcon();
+                    default:
+                        // do nothing
+                        break;
+                }
+            }
+
+        } else if (val == 4) {
+
+            backToFirstLayer = false;
+
+            while (!backToFirstLayer) {
+
+                switch(settingsScreen->start()) {
+                    case 1:
+
+                        // clear screen
+                        tft.fillScreen(backgroundColor);
+                        backToSecondLayer = false;
+
+                        while (!backToSecondLayer) {
 
-    while (1) {
+                            switch(test->start()) {
+                                case -1:
+                                    // clear screen
+                                    // first check if the background color was changed
+                                    // then change the background colors
+                                    changeBackgroundColor();
+                                    tft.fillScreen(backgroundColor);
+                                    backToSecondLayer = true;
+                                    break;
+                            }
+                        }
+                        break;
+                    case 2:
+
+                        // clear the screen
+                        tft.fillScreen(backgroundColor);
+                        backToSecondLayer = false;
+
+                        while (!backToSecondLayer) {
+
+                            switch(logg->start()) {
+
+                                case 1:
+                                    // clear the screen
+                                    tft.fillScreen(backgroundColor);
+                                    backToThirdLayer = false;
 
-        // determine if the battery is charging
-        percentage = ain2*100;
-        if (ain1 > 0.5) {
-            radiationReceived = true;
-            batteryCharging = true;
-        } else {
-            batteryCharging = false;
-            radiationReceived = false;
+                                    while(!backToThirdLayer) {
+
+                                        switch(viewLog->start()) {
+
+                                            case -1:
+                                                tft.fillScreen(backgroundColor);
+                                                backToThirdLayer = true;
+                                                break;
+
+                                        }
+
+                                    }
+
+                                    break;
+
+                                case 2:
+
+                                    // clear the screen
+                                    tft.fillScreen(backgroundColor);
+                                    backToThirdLayer = false;
+
+                                    while(!backToThirdLayer) {
+
+                                        switch(logSelect2->start()) {
+
+                                            case 1:
+                                                // clear the screen
+                                                tft.fillScreen(backgroundColor);
+                                                backToFourthLayer = false;
+
+                                                while(!backToFourthLayer) {
+                                                    switch(logDuration->start()) {
+
+                                                        case 1:
+                                                            // start logging for 30 seconds
+                                                            tft.fillScreen(backgroundColor);
+
+
+                                                            // navigate all the way back to log screen
+                                                            backToFourthLayer = true;
+                                                            backToThirdLayer = true;
+                                                            break;
+                                                        case 2:
+                                                            // start logging for 1 minute
+                                                            tft.fillScreen(backgroundColor);
+
+
+                                                            // navigate all the way to log screen
+                                                            backToFourthLayer = true;
+                                                            backToThirdLayer = true;
+                                                            break;
+
+                                                        case -1:
+                                                            // clear the screen
+                                                            tft.fillScreen(backgroundColor);
+                                                            backToFourthLayer = true;
+                                                    }
+                                                }
+                                                break;
+                                            case 2:
+                                                // clear the screen
+                                                tft.fillScreen(backgroundColor);
+                                                backToFourthLayer = false;
+
+                                                while(!backToFourthLayer) {
+                                                    switch(logDuration->start()) {
+
+                                                        case 1:
+                                                            // start logging for 30 seconds
+                                                            log1 = new LogManager();
+                                                            log1->createNewLog(POWER_CONSUMPTION,THIRTY_SECONDS);
+                                                            tft.fillScreen(backgroundColor);
+
+                                                            // navigate all the way back to log screen
+                                                            backToFourthLayer = true;
+                                                            backToThirdLayer = true;
+                                                            break;
+                                                        case 2:
+                                                            // start logging for 1 minute
+                                                            //log1 = new LogManager();
+                                                            //log1.createNewLog(ONE_MINUTE,SOLAR_POWER);
+                                                            tft.fillScreen(backgroundColor);
+
+                                                            // navigate all the way to log screen
+                                                            backToFourthLayer = true;
+                                                            backToThirdLayer = true;
+                                                            break;
+
+                                                        case -1:
+                                                            // clear the screen
+                                                            tft.fillScreen(backgroundColor);
+                                                            backToFourthLayer = true;
+                                                    }
+                                                }
+                                                break;
+
+                                            case -1:
+                                                tft.fillScreen(backgroundColor);
+                                                backToThirdLayer = true;
+                                                break;
+
+                                            default:
+                                                break;
+                                        }
+                                    }
+                                    break;
+                                case 3:
+                                    // clear the screen
+                                    tft.fillScreen(backgroundColor);
+                                    backToThirdLayer = false;
+
+                                    while(!backToThirdLayer) {
+
+                                        switch(deleteLog->start()) {
+                                            case -1:
+                                                // clear the screen
+                                                tft.fillScreen(backgroundColor);
+                                                backToThirdLayer = true;
+                                        }
+                                    }
+                                    break;
+                                case -1:
+
+                                    tft.fillScreen(backgroundColor);
+                                    backToSecondLayer = true;
+                                    break;
+                                default:
+                                    break;
+                            }
+                        }
+                        break;
+                    case -1:
+                        // clear screen
+                        tft.fillScreen(backgroundColor);
+                        backToFirstLayer = true;
+                        break;
+                }
+
+            }
+
         }
-        
-        if (ain > 0.5){
-            outputStatus = true;    
-        } else {
-            outputStatus =  false;    
-        }
+        tft.fillScreen(backgroundColor);
+    }
+
+
+}
+
+
+void changeBackgroundColor()
+{
+    Settings settings;
+    if (currentColor != settings.getBackgroundColor()) {
+        currentColor = settings.getBackgroundColor();
+        backgroundColor = currentColor;
 
-        // set the battery percentage accordingly
-        batteryIcon.setBatteryPercentage(percentage,batteryCharging);
+        // delete all the pointer
+        delete menu;
+        delete batteriesScreen;
+        delete solarScreen;
+        delete outputScreen;
+        delete settingsScreen;
+        delete solarGraphScreen;
+        delete test;
+        delete logg;
+        delete logSelect2;
+        delete logDuration;
+        delete viewLog;
+        delete deleteLog;
+        delete battScreen;
+        delete solarValueScreen;
 
-        // animate the solar icon accordingly
-        solarIcon.animateSolarIcon(radiationReceived);
-        
-        // animate the output icon accordingly
-        outputIcon.animateOutputIcon(outputStatus);
+        // create new screens
+        createScreens();
 
-        // wait half a second
-        wait_ms(500);
     }
 
 }
 
-void testdrawtext(char *text, uint16_t color)
+void createScreens()
 {
-    tft.setCursor(0, 0);
-    tft.setTextColor(color);
-    tft.setTextWrap(true);
-    tft.printf("%s",text);
-}
\ No newline at end of file
+    menu  = new MainMenuScreen(backgroundColor);
+    batteriesScreen = new BatteriesScreen(backgroundColor);
+    solarScreen = new SolarScreen(backgroundColor);
+    outputScreen = new OutputScreen(backgroundColor);
+    settingsScreen = new SettingsScreen(backgroundColor);
+    solarGraphScreen = new SolarGraphScreen(backgroundColor);
+    test = new TestScreen(backgroundColor);
+    logg = new LogScreen(backgroundColor);
+    logSelect2 = new LogSelectScreen(backgroundColor);
+    logDuration = new LogDurationScreen(backgroundColor);
+    viewLog = new ViewLogScreen(backgroundColor);
+    deleteLog = new DeleteLogScreen(backgroundColor);
+    battScreen = new BatteryScreen(backgroundColor);
+    solarValueScreen = new SolarValueScreen(backgroundColor);
+}
+