ReSpeaker Switch V02

Dependencies:   mbed MbedJSONValue

Revision:
11:db4e51a318c2
Parent:
10:e4e3f5af6b0a
Child:
12:1b8560db3dff
--- a/main.cpp	Sun Aug 26 09:55:45 2018 +0000
+++ b/main.cpp	Mon Aug 27 10:32:28 2018 +0000
@@ -62,7 +62,7 @@
 
     DSP Interrupt
     PA_10 --> DSP_Trigger
-    
+
     MIC Interrupts:
     PC_15 --> INTER_1
     PC_14 --> INTER_2
@@ -103,7 +103,7 @@
 //#define DEBUG_MOD10   // responsivity msges to gui
 bool debuggingEn = 0;
 
-#define MSG_BUFFER_SIZE 512
+#define MSG_BUFFER_SIZE 1024
 #define HEADER_SIZE 5
 #define FOOTER_SIZE 2
 
@@ -272,10 +272,12 @@
     ///////////////////////
     while(1) { // GPIO polling for mic events
         // frquency counter
-        if (dspIntFlag){
+        if (dspIntFlag) {
             double calculatedFrequencyDouble = (double) (TICKS2TOGGLE * 2.0 ) / freqTimeInterval; // Calculated in MHz
             int calculatedFrequency = (int) (calculatedFrequencyDouble * 1000000); // convert to hz
-            pc.printf("{\"event\":\"dspFreq\",\"Freq\":%d}\r\n",calculatedFrequency); // Printf 
+            if (calculatedFrequency < 10000000) { // cannot be larger
+                pc.printf("{\"event\":\"dspFreq\",\"Freq\":%d}\r\n",calculatedFrequency); // Printf
+            }
             dspIntFlag=0;
         }
         if(0) { // no analog data to read (not implemented yet
@@ -313,8 +315,9 @@
 ///////////////
 
 // Interrup event from the dsp (frequency counter)
-void dspInterrupt(){
-    freqTimeInterval = freqTimer.read_us() + 1; // compensate the time takes to read / reset 
+void dspInterrupt()
+{
+    freqTimeInterval = freqTimer.read_us() + 1; // compensate the time takes to read / reset
     freqTimer.reset();
     dspIntFlag = 1;
 }//end dspInterrupt
@@ -324,15 +327,15 @@
 {
     int micChannle = 0;
     bool spkChannle[5]= {0};
-    
+
     // speaker selection:
-    
+
     // simple selection mode
     micChannle = micEvent;
     if ((micChannle >= 1) && (micChannle <= 5)) {// verify mic is in the limit
         spkChannle[micChannle-1] = 1;
     }
-    
+
     // update hardware
     // update mic select mux
     uint8_t micByte = (uint8_t) micChannle;
@@ -358,13 +361,13 @@
 {
     // attach serial event interrupt
     pc.attach(&rxCallback, Serial::RxIrq);
-    
+
     // attach interrupt event for frequency counting
     dsp_trigger.rise(&dspInterrupt);
-    
+
     // start timer
     freqTimer.start();
-    
+
     // initialize timer
     timeStamp.start();
 
@@ -420,9 +423,7 @@
 {
     while (dsp.readable()) {
         uint8_t in_byte = dsp.getc();
-#ifdef DEBUG_MOD4
         pc.putc(in_byte);
-#endif
     }
 } // end rxDspCallback
 
@@ -543,41 +544,46 @@
     //pc.printf("%d, %.*s", packetMSG.bufferIndex ,packetMSG.bufferIndex, packetMSG.buffer );
     pc.printf("%s", json);
 #endif
-
-    // GUI message format Switch: {"name":"switch","mic":0, "spk": [0,1,0,0,0]}
-    parse(guiCmd, json);
-    if (debuggingEn){
-        pc.printf("%s", json);
-    }
-    // get target:
-    targetName = guiCmd["name"].get<string>(); // switch / dsp
+    // verification its a full json
+    if ((json[0] == '{') &&  (json[strlen(json)-3] == '}')) {
+        // GUI message format Switch: {"name":"switch","mic":0, "spk": [0,1,0,0,0]}
+        parse(guiCmd, json);
+        if (debuggingEn) {
+            pc.printf("%s", json);
+        }
+        // get target:
+        targetName = guiCmd["name"].get<string>(); // switch / dsp
 
 #ifdef DEBUG_MOD2
-    // send parsed values
-    pc.printf("targetName: %s \r\n", targetName.c_str());
+        // send parsed values
+        pc.printf("targetName: %s \r\n", targetName.c_str());
 #endif
 
-    // select handler based on target mcu
-    if (targetName == "switch") {
-        // disable automatic mode
-        autoFlag=0;
-        // update controls
-        switchPacket();
-    } else if ((targetName == "dsp") || (targetName == "dspParam")) {
-        // send msg to dsp
-        dsp.printf("json:%s", json);
+        // select handler based on target mcu
+        if (targetName == "switch") {
+            // disable automatic mode
+            autoFlag=0;
+            // update controls
+            switchPacket();
+        } else if ((targetName == "dsp") || (targetName == "dspParam") || (targetName == "dspFilter")) {
+            // send msg to dsp
+            dsp.printf("json:%s", json);
 #ifdef DEBUG_MOD2
-        pc.printf("json:%s", json);
+            pc.printf("json:%s", json);
 #endif
-    }
-    if (targetName == "auto") {
-        // automatic mode
-        autoFlag=1;
-    }  else {
+        }
+        if (targetName == "auto") {
+            // automatic mode
+            autoFlag=1;
+            pc.printf("{\"Ack\":\"switch\",\"auto\":%d}\r\n",autoFlag); // Printf
+        }  else {
 #ifdef DEBUG_MOD2
-        // unrecognised target
-        pc.printf("unrecognised target: %s \r\n", targetName.c_str());
+            // unrecognised target
+            pc.printf("unrecognised target: %s \r\n", targetName.c_str());
 #endif
+        }
+    } else { // not a full json message
+        pc.printf("Error: %s", json);
     }
     // led blink
     led = !led;
@@ -588,13 +594,13 @@
 {
     int micChannle = 0;
     bool spkChannle[5]= {0};
-    
+
     // get debbug status
     debuggingEn = guiCmd["debug"].get<int>();
-    
+
     // get mic channle
     micChannle = guiCmd["mic"].get<int>();
-    
+
     // get speakers output
     for (int ii=0 ; ii < 5 ; ii++) {
         spkChannle[ii] = guiCmd["spk"][ii].get<bool>();
@@ -617,5 +623,5 @@
     en_spk_4.write(spkChannle[3]);
     en_spk_5.write(spkChannle[4]);
     // end switch target parse
-
+    pc.printf("{\"Ack\":\"switch\",\"mic\":%d,\"spk\":[%d,%d,%d,%d,%d]}\r\n",micChannle,spkChannle[0],spkChannle[1],spkChannle[2],spkChannle[3],spkChannle[4]);
 }// end switch packet
\ No newline at end of file