emma controller code in production board v1

Dependencies:   ADE7758_v1 Crypto DHT11 MQTT MbedJSONValueEmma SDFileSystem TFT_ILI9341 SWSPI SetRTC TFT_fonts Touch W5500Interface mbed-rtos mbed-src SoftSerial

Fork of emma_controller_energy by Emma

Revision:
58:5f953f303551
Parent:
57:4241a1711069
Child:
59:009596ea660d
--- a/emmaCode.cpp	Thu Sep 17 03:50:47 2015 +0000
+++ b/emmaCode.cpp	Mon Sep 21 12:34:10 2015 +0000
@@ -76,7 +76,7 @@
 REST restObj[NODES_MAX] = {REST(&esp),REST(&esp),REST(&esp),REST(&esp),REST(&esp)};
 NODES nodes[NODES_MAX] = {NODES(&restObj[0]),NODES(&restObj[1]),NODES(&restObj[2]),NODES(&restObj[3]),NODES(&restObj[4])};
 
-//mode box class for main menu
+//mode box class    //need to be deleted
 class modeBox {
 public:
     int xTL;    //TopLeft
@@ -87,6 +87,35 @@
     string name;
 };
 
+//mode circle class
+class modeCircle {
+public:
+    void drawCircle(string textLeft, string textRight) {
+        TFT.fillarc(80, 120, 70, 5, 0, 360, White);
+        TFT.locate(65 - ((textLeft.length()*9)/2),110);
+        TFT.printf("%s",textLeft.c_str());
+        
+        TFT.fillarc(240, 120, 70, 5, 0, 360, White);
+        TFT.locate(225 - ((textRight.length()*9)/2),110);
+        TFT.printf("%s",textRight.c_str());    
+    }
+    
+    void animateCircle(int circleId) {  //leftCirle=0 , rightCirle=1
+        int _x=0;
+        
+        if(circleId == leftCircle) {
+            _x=80;
+        } else {
+            _x=240;    
+        }
+        
+        for(int i=0; i<1440; i+=4) {
+            TFT.fillarc(_x, 120, 70, 5, 1440-i-45, 1440-i+45, Black);
+            TFT.fillarc(_x, 120, 70, 5, 1440-i+45, 1440-i+45+4, White);    
+        }
+    }
+};
+
 //ade7758 variables
 uint32_t AWattHrValue, BWattHrValue, CWattHrValue;
 uint32_t AVAHrValue, BVAHrValue, CVAHrValue;
@@ -124,8 +153,161 @@
 string rxLog;
 string rxLogA;
 
+//color of lcd
+uint16_t colorLightGray = TFT.color565(192,192,192);
+uint16_t colorGray = TFT.color565(127,127,127);
+uint16_t colorDarkGray = TFT.color565(64,64,64);
+
 /*start lcd and touch*/
-int emmaModeSelection(void) {
+int emmaModeSelection(void) {   //circle
+    bool modeSelected = false;
+    int idx=0;
+    int md=0;
+    int TPx;
+    int TPy;
+    Timer t;
+    
+    TFT.background(Blue);
+    TFT.foreground(White);
+    
+    TFT.set_font((unsigned char*) Lato27x27);
+    TFT.set_orientation(1);
+    TFT.cls();
+    
+    TFT.locate(40,25);
+    TFT.printf("Loading Emma...");
+    
+    TFT.locate(0,0);
+    TFT.fillarc(159,149,20,10,0,360, colorLightGray);
+    
+    for(int i=0; i<2880; i+=4) {
+        TFT.fillarc(159,149,20,10,(i>>1)-45,(i>>1)+45,colorDarkGray);
+        TFT.fillarc(159, 149, 20, 10, (i >> 1)-45-4, (i >> 1)-45, colorLightGray);
+        
+        TFT.fillarc(159, 149, 40, 10, 1440-i-45, 1440-i+45, colorDarkGray);
+        TFT.fillarc(159, 149, 40, 10, 1440-i+45, 1440-i+45+4, colorLightGray);    
+    }
+    
+    TFT.cls();
+    
+    Matrix matrix;
+    Coordinate ScreenSample[3];
+    
+    //lcd type 1
+    //matrix.An = 580;
+    //matrix.Bn = 75980;
+    //matrix.Cn = -3410580;
+    //matrix.Dn = 57855;
+    //matrix.En = -2465;
+    //matrix.Fn = -3483515;
+    //matrix.Divider = 209144;
+    
+    //ScreenSample[0].x = 230;
+    //ScreenSample[0].y = 167;
+    //ScreenSample[1].x = 754;
+    //ScreenSample[1].y = 163;
+    //ScreenSample[2].x = 771;
+    //ScreenSample[2].y = 562;
+    
+    //lcd type 2
+    matrix.An = 1305;
+    matrix.Bn = -77430;
+    matrix.Cn = 75296670;
+    matrix.Dn = -57275;
+    matrix.En = -1160;
+    matrix.Fn = 55285220;
+    matrix.Divider = 211002;
+    
+    ScreenSample[0].x = 782;
+    ScreenSample[0].y = 863;
+    ScreenSample[1].x = 248;
+    ScreenSample[1].y = 854;
+    ScreenSample[2].x = 256;
+    ScreenSample[2].y = 459;
+    
+    TP.SetCalibration(&matrix, &ScreenSample[0]);
+    
+    TFT.locate(5,5);
+    TFT.set_font((unsigned char*) Lato19x19);
+    TFT.printf("Hi, kamu mau apa?");
+    wait(2);
+    
+    //menu
+    //1. make two options, operasi or pengaturan
+    //2. registrasi or lanjut
+    //3. controller or lanjut
+    //4. wifi or update firmware
+    string menuText[8] = {"Jalan","Pengaturan","Daftar","Lanjut","Atur EMMA","Lanjut","Atur WiFi","Update"};
+    
+    //init main menu
+    modeCircle menu;
+    
+    menu.drawCircle(menuText[idx],menuText[idx+1]);
+            
+    //read emma settings
+    emmaReadSettings();
+    
+    //mqttDOMAIN is not empty -> has been registered
+    if(!mqttDOMAIN.empty()) {
+        TFT.locate(0,200);
+        TFT.printf(" auto selection");
+        t.start();
+    }
+    
+    while(!modeSelected) {
+        if(!TP._tp_irq) {
+            if(TP.Read_Ads7843()) {
+                TP.getDisplayPoint();
+                TPx = TP.display.x;
+                TPy = TP.display.y;
+                TP.TP_DrawPoint(TPx,TPy, Blue);
+                if((20 < TPx && TPx < 150) && (55 < TPy && TPy < 190)) {            //pick leftCircle
+                    if(idx==0) {
+                        md = MODE_OPERATION;    
+                    } else if(idx==2) {
+                        md = MODE_REGISTER;    
+                    } else if(idx==4) {
+                        md = MODE_SETTINGS;    
+                    } else if(idx==6) {
+                        md = MODE_WIFI_CONFIG;    
+                    }
+                    modeSelected = true;
+                    menu.animateCircle(leftCircle);    
+                } else if((180 < TPx && TPx < 310) && (55 < TPy && TPy < 190)) {    //pick rightCircle
+                    if(idx == 6) {
+                        md = MODE_FIRMWARE_DOWNLOAD;
+                        modeSelected = true;
+                        menu.animateCircle(rightCircle);    
+                    } else {
+                        idx = idx+2;
+                        menu.animateCircle(rightCircle);
+                        wait(0.5);
+                        TFT.cls();
+                        TFT.locate(5,5);
+                        TFT.printf("Hi, kamu mau apa?");
+                        menu.drawCircle(menuText[idx],menuText[idx+1]);    
+                    }
+                }
+            }
+        } else if(t.read()>60) {
+            md = MODE_OPERATION;
+            modeSelected = true;
+            t.stop();
+            t.reset();    
+        }
+    }
+    
+    //TFT.locate(0,200);
+    //TFT.printf("                         ");
+    //TFT.locate(10,200);
+    //TFT.printf("mode: %d is selected",md);
+    //wait(2);
+    TFT.cls();
+    
+    return md;
+}
+/*
+int emmaModeSelection(void) {   //box
     bool modeSelected = false;
     int md=0;
     int TPx;
@@ -296,6 +478,7 @@
     
     return md;
 }
+*/
 /*end lcd and touch*/
 
 /*start emma read settings*/
@@ -382,34 +565,45 @@
     DBG.printf("gprs:%d\r\n",gprsAvailable);
 }
 void emmaModeWiFiConfig(void) {
+    bool findCh;
     string str;
-    TFT.locate(0,0);
-    TFT.printf(" please wait");
+    Timer t;
+        
+    DBG.printf("emmaModeWiFiConfig\r\n");
     
-    if(wifiAvailable) {
-        DBG.printf("emmaModeWiFiConfig\r\n");
+    //waiting
         
-        //set wifi module to configuration
-        _ESP.printf("MODE=C");
-        while(1) {
-            char rcv[128] = {};
-            wifiRcvReply(rcv,3000);
-            str = rcv;
-            if(str.find("SC_STATUS_FIND_CHANNEL") != std::string::npos)
-                break;
+    t.start();
+    //set wifi module to configuration
+    _ESP.printf("MODE=C");
+    while(1) {
+        char rcv[128] = {};
+        wifiRcvReply(rcv,3000);
+        str = rcv;
+        if(str.find("SC_STATUS_FIND_CHANNEL") != std::string::npos) {
+            findCh = true;
+            break;
         }
+        if(t.read() > 60.0f) {
+            t.stop();
+            t.reset();
+            findCh = false;
+            break;    
+        }    
+    }
+    
+    if(findCh) {
+        TFT.locate(10,200);
+        TFT.printf("Silakan sambungkan dg App");
         
-        TFT.locate(0,0);
-        TFT.printf(" emmaModeWiFiConfig");
-        TFT.locate(0,20);
-        TFT.printf(" connect with emma app now");
-        
-        DBG.printf("entering wifi configuration mode\r\n");
+        DBG.printf("enterwifi configuration mode\r\n");
         while(1) {
             char rcv[128] = {};
             wifiRcvReply(rcv,3000);
             str = rcv;
             if(str.find("MODE=C OK") != std::string::npos) {
+                TFT.locate(0,200);
+                TFT.printf("                              ");
                 //save wifiSSID and wifiPASS
                 if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
                     str.erase(str.begin(),str.begin()+str.find("[")+1);
@@ -426,41 +620,43 @@
                             int st = writeSetting(parameter[i],val.c_str());
                             if(st) {
                                 DBG.printf("%s is saved\r\n",parameter[i]);
-                                TFT.locate(0,40);
-                                TFT.printf(" %s is saved",parameter[i]);
-                                wait(3);
-                                TFT.locate(0,40);
+                                TFT.locate(10,200);
+                                TFT.printf(" %s tersimpan",parameter[i]);
+                                wait(1);
+                                TFT.locate(0,200);
                                 TFT.printf("                              ");
                             } else {
                                 DBG.printf("%s is not saved\r\n",parameter[i]);
-                                TFT.locate(0,40);
-                                TFT.printf(" %s is not saved",parameter[i]);
-                                wait(3);
-                                TFT.locate(0,40);
+                                TFT.locate(10,200);
+                                TFT.printf(" %s tak tersimpan",parameter[i]);
+                                wait(1);
+                                TFT.locate(0,200);
                                 TFT.printf("                              ");
                             }
                         }
                     }
                     
                     //wificonfig finish
-                    TFT.locate(0,20);
-                    TFT.printf("                                             ");
-                    TFT.locate(0,20);
-                    TFT.printf(" wificonfig finish. please restart.");
-                    
+                    lcdDrawSmile();
+                    TFT.locate(0,200);
+                    TFT.printf("                              ");
+                    TFT.locate(10,200);
+                    TFT.printf("Sukses! Tolong restart EMMA");
                 } 
             } else if(str.find("SC_STATUS_GETTING_SSID_PSWD") != std::string::npos){
                 DBG.printf("app connected\r\n");
-                TFT.locate(0,20);
+                TFT.locate(0,200);
                 TFT.printf("                              ");
-                TFT.locate(0,20);
-                TFT.printf(" app connected");
+                TFT.locate(10,200);
+                TFT.printf("App tersambung");
             }
-        }    
+        }
     } else {
-        DBG.printf("no wifi found\r\n");
-        TFT.locate(0,20);
-        TFT.printf(" no wifi found");
+        lcdDrawFrown();
+        TFT.locate(0,200);
+        TFT.printf("                              ");
+        TFT.locate(10,200);
+        TFT.printf("Gagal! Tolong restart EMMA");
     }
 }
 void emmaModeSettings(void) {
@@ -4125,6 +4321,24 @@
 }
 /*end emma nodes*/
 
+/*start emma lcd*/
+void lcdDrawSmile(void) {
+    TFT.fillarc(120, 40, 20, 10, 290, 70, Green);   //left eye
+    TFT.fillarc(200, 40, 20, 10, 290, 70, Green);   //right eye
+    TFT.fillarc(160, 60, 85, 10, 120, 240, Green);  //mouth
+}
+
+void lcdDrawFrown(void) {
+    TFT.fillarc(120, 40, 20, 10, 110, 250, Red);    //left eye
+    TFT.fillarc(200, 40, 20, 10, 110, 250, Red);    //right eye
+    TFT.fillarc(160, 200, 85, 10, 315, 45, Red);     //mouth
+}
+
+//void lcdPrintRestart(void) {
+//    
+//}
+/*end emma lcd*/
+
 /*start emma private function*/
 void isEthAvailable(void) {
     //if(ipstack.getEth().linkstatus()) {