Console Serial IO con display OLED e seriale asincrona

Dependencies:   mbed BufferedSerial AserialIOFuncLib SSD1306 TerminalPlusV2

Fork of SerialIO by Max Scordamaglia

Console Serial IO con display OLED e seriale asincrona

Revision:
41:fb612a3936fb
Parent:
40:48422add4537
Child:
42:8ca8ff1ab387
--- a/choiceFunctions.h	Sun Jan 03 14:32:39 2016 +0000
+++ b/choiceFunctions.h	Sat Jan 09 00:53:41 2016 +0000
@@ -41,8 +41,9 @@
 //---------------------------------------------------------------------------------------------
 }
 
-void writeFunctionVal(string strIn)
+int trovaIndiceFunzione(string strIn)
 {
+    //data la stringa di menu e funzione, restituisce l'indice
     int idx=0;
     bool trovato=false;
     //cerca valore. Per ora facciamo cosi, poi si vede se indicizzarlo
@@ -50,8 +51,16 @@
         if ((strIn.substr(0,1)==fnzAdd.i2s(function_val[idx][0])) && (strIn.substr(2,1)==fnzAdd.i2s(function_val[idx][1]))) trovato=true;
         else idx++;
     }
+    return idx;
+}
+
+
+void writeFunctionVal(string strIn)
+{
+    //scrivi a video i valori funzione
+    int idx=trovaIndiceFunzione(strIn);
     //stampo
-    if (trovato==true) { //non dovrebbe servire
+    if (idx<50) { //non dovrebbe servire
         term.formatPrintf(fnzAdd.string2char(fnzAdd.i2s(function_val[idx][2])),50,16,10); //min
         term.formatPrintf(fnzAdd.string2char(fnzAdd.i2s(function_val[idx][5])),60,16,10); //start
         term.formatPrintf(fnzAdd.string2char(fnzAdd.i2s(function_val[idx][3])),70,16,10); //max
@@ -129,11 +138,12 @@
                 bbold=0;
                 prefix=" ";
             }
-            term.formatPrintf(fnzAdd.string2char(prefix+menu_funcChoice[i]),41,4+i,38,bbold);
+            term.formatPrintf(fnzAdd.string2char(prefix+menu_funcChoice[i]),41,4+i,38,bbold); //stampo funzione in bold
         }
+        writeFunctionVal(menu_funcChoice[funcidx].substr(0,3)); //scrivo valore
     }
 
-   // errp=0; //per ora non fa nulla, vedere se tenere a uso futuro
+    // errp=0; //per ora non fa nulla, vedere se tenere a uso futuro
     return errp;
 }
 
@@ -155,13 +165,16 @@
         funcidx=contoldfun;
         term.formatPrintf(fnzAdd.string2char("*"+menu_funcChoice[funcidx]),41,4+funcidx,38);
         term.resetattrib();
+        //invio ad arduino
+        //ardser.printf(fnzAdd.string2char("f"+fnzAdd.padstr(fnzAdd.i2s(trovaIndiceFunzione(menu_funcChoice[funcidx])),2,' ')+"\r")); // no, questa passa il valore dell'indice
+        ardser.printf(fnzAdd.string2char("f"+funcc+"\r"));
         // scrivo valori della funzione a video
         writeFunctionVal(menu_funcChoice[funcidx].substr(0,3));
         //display
         term.formatPrintf(fnzAdd.string2char(fnzAdd.addEOS("Set Funzione "+funcc+"\n")),61,p52,99,1); //p52
         printDisp(dispType,fnzAdd.string2char(fnzAdd.addEOS("Set Funzione "+funcc+"\r")));
-        //errp=0;
-    } else errp=0; //stampo solo all'ultimo
+        errp=0;
+    }
     return errp;
 }
 
@@ -180,12 +193,15 @@
         menuidx=atoi(fnzAdd.string2char(funcc));
         term.formatPrintf(fnzAdd.string2char("*"+menu_main[menuidx]),1,4+menuidx,19);
         term.resetattrib();
+        //invio ad arduino
+        ardser.printf(fnzAdd.string2char("m"+funcc+"\r"));
         //setto funzioni
         funcfromMenu(menuidx);
         //display
         term.formatPrintf(fnzAdd.string2char(fnzAdd.addEOS("Set Menu "+funcc+"\n")),61,p52,99,1); //p52
         printDisp(dispType,fnzAdd.string2char(fnzAdd.addEOS("Set Menu "+funcc+"\r")));
-    } else errp=0; //stampo solo all'ultimo
+        errp=0;
+    }
     return errp;
 }
 
@@ -216,6 +232,8 @@
 //********************* Start
 bool StartfuncOut()
 {
+    status=true;
+    term.formatPrintf(fnzAdd.string2char(fnzAdd.addEOS("On")),11,p16,10,1);
     term.formatPrintf("Start\n",21,p51,8,1); //p51
     term.formatPrintf("Start H-Ard\n",61,p52,99,1);   //p52
     ardser.printf("start\r");
@@ -226,6 +244,8 @@
 //********************* Stop
 bool StopfuncOut()
 {
+    status=false;
+    term.formatPrintf(fnzAdd.string2char(fnzAdd.addEOS("Off")),11,p16,10,1);
     term.formatPrintf("Stop\n",21,p51,8,1); //p51
     term.formatPrintf("Stop H-Ard\n",61,p52,99,1);   //p52
     ardser.printf("stop\r");
@@ -233,6 +253,31 @@
     return 0;
 }
 
+//********************* Plus
+bool plusOut(string func, string sstr)
+{
+    //aumenta la funzione
+    bool errp=1;
+    string funcc;
+    funcc=sstr.substr(1,1);
+    if ((funcc>="0") && (funcc<="9")) {
+        errp=0;
+    }
+    return errp;
+}
+
+//********************* Minus
+bool minusOut(string func, string sstr)
+{
+    //aumenta la funzione
+    bool errp=1;
+    string funcc;
+    funcc=sstr.substr(1,1);
+    if ((funcc>="0") && (funcc<="9")) {
+        errp=0;
+    }
+    return errp;
+}
 
 /************************ IN Function *****************************/
 /******************************************************************/
@@ -250,6 +295,7 @@
     if ((funcc>="0") && (funcc<="9")) {
         //trasformo in intero
         funcn=atoi(fnzAdd.string2char(funcc));
+        menuidx=funcn;
         //assegno menu al vettore
         menu_main[funcn]=fnzAdd.addEOS(sstr.substr(3,20));
         //*** tolti in attesa di regolare la sincronizzazione
@@ -296,7 +342,35 @@
     return errp;
 }
 
-//********************* Start
+//********************** Parametri in ingresso
+bool getParamIn(string func, string sstr)
+{
+    //Sintassi: gpx
+    //G:costante
+    //P:0-menu 1-funzione 2-onoff 3-random
+    //X:valore
+    bool errp=1;
+    int ttype=atoi(fnzAdd.string2char(sstr.substr(1,1)));
+    switch (ttype) {
+        case 0: //menu
+            menuOut("m", "m"+sstr.substr(2,1));
+            break;
+        case 1: //function
+            funcOut("f", "f"+sstr.substr(2,1));
+            break;
+        case 22: //onoff
+            if (sstr.substr(2,1)=="1") StartfuncOut();
+            else  StopfuncOut();
+            break;
+        case 3:
+            //////////// fare random!!!!
+            break;
+    }
+    errp=0;
+    return errp;
+}
+
+//********************* Start //!!!!!!!!!!da fare
 bool StartfuncIn()
 {
     term.formatPrintf("*\n",26,p51,8,1); //p51
@@ -305,7 +379,7 @@
     return 1;
 }
 
-//********************* Start
+//********************* Start //!!!!!!!!!!!!!!!!da fare
 bool StopfuncIn()
 {
     term.formatPrintf("*\n",26,p51,8,1); //p51
@@ -327,46 +401,23 @@
     xxxx-valore
     */
     bool errp=1;
+    int vval=atoi(fnzAdd.string2char(sstr.substr(6,4)));
     int idx=atoi(fnzAdd.string2char(sstr.substr(1,2)));
-    int vval=atoi(fnzAdd.string2char(sstr.substr(6,4)));
-    // term.formatPrintf(fnzAdd.string2char("*"+fnzAdd.i2s(idx)),50,16,10);
     if (sstr.substr(0,1)=="v") {
-        function_val[idx][0]=atoi(fnzAdd.string2char(sstr.substr(3,1))); //MENU
-        function_val[idx][1]=atoi(fnzAdd.string2char(sstr.substr(4,1))); //FUNZIONE
+        if (idx!=99) { //se l'indice passato e' ignoto perche' e' un update, cercalo, se un primo giro crealo
+            function_val[idx][0]=atoi(fnzAdd.string2char(sstr.substr(3,1))); //MENU
+            function_val[idx][1]=atoi(fnzAdd.string2char(sstr.substr(4,1))); //FUNZIONE
+        } else {
+            //cerca valore. Per ora facciamo cosi, poi si vede se indicizzarlo
+            idx=trovaIndiceFunzione(sstr.substr(3,1)+"."+sstr.substr(4,1));
+        }
         function_val[idx][2+atoi(fnzAdd.string2char(sstr.substr(5,1)))] =vval; //tipo
         //errp=0;
     }
     return errp;
 }
 
-//********************* ?????????????????????????????
-bool NfuncIn(string func, string funcnum, string param, char* arr)
-{
-    bool errp=1;
-    if ((funcnum>="0") && (funcnum<="9")) {
-        term.formatPrintf(fnzAdd.string2char(fnzAdd.addEOS(param)),21,p51,8,1); //p51
-        // vale la pena creare una funzione di parametri da formalizzare
-        for (int i = 0; i < 6; i++) term.formatPrintf(" ",23+(i*4),12,1,1);
-        term.formatPrintf(fnzAdd.string2char(param.substr(0,2)+"*"),21+(fnzAdd.c2i(arr[1])*4),12,1,1); //p11 //p12 //p21
-        printDisp(dispType,"NFuncIn\n\r");
-        printDisp(dispType,fnzAdd.string2char(fnzAdd.addEOS("  "+func+funcnum+param+"\r")));
-        //ardser.printf(fnzAdd.string2char(fnzAdd.addEOS(func+"-"+funcnum+"-"+param)));
-        errp=0;
-        //    }
-    }
-    return errp;
-}
-
-//********************* Boot
-bool bootfuncIn()
-{
-    term.formatPrintf("*\n",26,p51,8,1); //p51
-    term.formatPrintf("boot\n",21,p51,8,1); //p51
-    term.formatPrintf("Arduino Boot\n",61,p52,99,1);   //p52
-    ardser.printf("rl\r");
-    return 1;
-}
-
+//**************************************** altre
 int contaMenuFunc()
 {
     //funzione che conta le voci di menu in essere