ReSpeaker Switch V02

Dependencies:   mbed MbedJSONValue

Revision:
7:9153b6566524
Parent:
6:696bf69e99d2
Child:
8:5f6b8bffe9b9
--- a/main.cpp	Mon Apr 09 13:21:08 2018 +0000
+++ b/main.cpp	Mon Apr 09 14:37:12 2018 +0000
@@ -1,26 +1,16 @@
 ////////////////////////////////////////
 //      Tau_ReSpeaker_Sitch_V01       //
-//  Arkadiraf@gmail.com - 05/06/2017  //
+//  Arkadiraf@gmail.com - 09/04/2018  //
 ////////////////////////////////////////
 /*
- Receive byte from pc to reroute input outputs
- b1xxxxxxx (0x8x) - define input port; b10000000 - port 0,  b10000001 - port 1, b10000010 - port 2 etc , b10000011 - port 3 etc.
- b0xxxxxxx - define output ports; b10000001 - port 1,  b10000011 - port 1&2, b10000000 - non.
- off all:   0x00
- All ports: 0x1f
- speaker 5: 0x10
- speaker 1: 0x01
-
- Alternative json format:
- Json Format: {"mic":0, "spk": [0,1,0,0,0]}
-
+ json format: json:{"name":"auto"}                               - automatic mode
+ Json Format: json:{"name":"switch","mic":0, "spk": [0,1,0,0,0]} - manual mode
 */
 
 /*
     Bugs:
     PA_14 and PA_13 are share with stlink - not available as interrupt pins
     Used in polling mode instead
-
 */
 /*
    Board : Nucleo STM32F446RE
@@ -106,9 +96,8 @@
 //#define DEBUG_MOD1  // json packet recognise
 //#define DEBUG_MOD2   // json parse
 //#define DEBUG_MOD3   // switch handler
-#define DEBUG_MOD4   // serial with dsp module
-#define DEBUG_MOD5   // Microphone interrupt debug
-#define DEBUG_MOD10   // responsivity msges to gui
+//#define DEBUG_MOD4   // serial with dsp module
+//#define DEBUG_MOD10   // responsivity msges to gui
 
 #define MSG_BUFFER_SIZE 512
 #define HEADER_SIZE 5
@@ -170,7 +159,7 @@
 
 // mic interrupt flag
 int micInterrupt = 0;
-
+bool autoFlag = 0; // automatic mode flag
 
 // json buffer
 char json[MSG_BUFFER_SIZE];
@@ -235,6 +224,9 @@
 // poll mic interrupt gpio
 bool micPolling(void);
 
+// Atuomatic Switch Handler
+void autoSwitch(int micEvent);
+
 ////////////////////////
 //  Main Code Setup : //
 ////////////////////////
@@ -259,7 +251,7 @@
     ///////////////////////
     //  Main Code Loop : //
     ///////////////////////
-    while(1) { // does nothing. everything is currently in the callbacks
+    while(1) { // GPIO polling for mic events
         if(0) { // no analog data to read (not implemented yet
             micAData[0] = a_data_1.read_u16();
             micAData[1] = a_data_2.read_u16();
@@ -270,32 +262,23 @@
             wait(1);
         }
         // change to interrupt - Problematic as PA_14 and PA_13 are share with stlink
-        if (0) { // polling on digital input from micrphones for automatic mic selection
-            if (inter_1.read()) micInterrupt=1;
-            if (inter_2.read()) micInterrupt=2;
-            if (inter_3.read()) micInterrupt=3;
-            if (inter_4.read()) micInterrupt=4;
-            if (inter_5.read()) micInterrupt=5;
-        }
-        // mic interrupts
-//        if (micInterrupt > 0 ) {
-//            pc.printf("IntMic: %d \r\n",micInterrupt);
-//            // led blink
-//            led = !led;
-//            wait(0.1);
-//            micInterrupt=0;
-//        }
         if (micPolling()) { // simple polling of interrupt signals
-            float callTime = timeStamp.read();
-            //pc.printf("IntMic: %d time: %.3f \r\n",micInterrupt,callTime); // Printf generates delay of about 5 millis
-            // send json formatted
-            pc.printf("{\"event\":\"micInt\",\"mic\":%d,\"time\":%.3f}\r\n",micInterrupt,callTime); // Printf generates delay of about 5 millis
-            
-            // led blink
-            led = !led;
-            //wait(0.1);
-            // reset micInterrupt flag
-            micInterrupt=0;
+            if (autoFlag)  { // update switch based on automatic selection
+                autoSwitch(micInterrupt);
+            }
+
+            if (1) { // send to pc event time
+                float callTime = timeStamp.read();
+                //pc.printf("IntMic: %d time: %.3f \r\n",micInterrupt,callTime); // Printf generates delay of about 5 millis
+                // send json formatted
+                pc.printf("{\"event\":\"micInt\",\"mic\":%d,\"time\":%.3f}\r\n",micInterrupt,callTime); // Printf generates delay of about 5 millis
+
+                // led blink
+                led = !led;
+                //wait(0.1);
+                // reset micInterrupt flag
+                micInterrupt=0;
+            }
         }
     }// end main loop
 }// end main
@@ -303,14 +286,49 @@
 // Functions //
 ///////////////
 
+// Atuomatic Switch Handler
+void autoSwitch(int micEvent)
+{
+    int micChannle = 0;
+    bool spkChannle[5]= {0};
+    
+    // speaker selection:
+    
+    // simple selection mode
+    micChannle = micEvent;
+    if ((micChannle >= 0) && (micChannle <= 4)) {// verify mic is in the limit
+        spkChannle[micChannle-1] = 1;
+    }
+    
+    // update hardware
+    // update mic select mux
+    uint8_t micByte = (uint8_t) micChannle;
+    mux_s0.write((bool)(micByte & 0b00000001));
+    mux_s1.write((bool)(micByte & 0b00000010));
+    mux_s2.write((bool)(micByte & 0b00000100));
+    mux_s3.write((bool)(micByte & 0b00001000));
+
+    // update speakers:
+    en_spk_1.write(spkChannle[0]);
+    en_spk_2.write(spkChannle[1]);
+    en_spk_3.write(spkChannle[2]);
+    en_spk_4.write(spkChannle[3]);
+    en_spk_5.write(spkChannle[4]);
+
+    // send updated values json formatted
+    pc.printf("{\"event\":\"switch\",\"mic\":%d,\"spk\":[%d,%d,%d,%d,%d]}\r\n",micChannle,spkChannle[0],spkChannle[1],spkChannle[2],spkChannle[3],spkChannle[4]); // Printf generates delay of about 5 millis
+
+}// end autoSwitch
+
 // initialize switch
 void initSwitch(void)
 {
     // attach serial event interrupt
     pc.attach(&rxCallback, Serial::RxIrq);
-    
+
     // initialize timer
     timeStamp.start();
+
     // attach mic interrupts - Not used, polling mode
     //inter_1.rise(&micInt_1);
     //inter_2.rise(&micInt_2);
@@ -355,7 +373,7 @@
         eventFlag=1;
         micInterrupt=5;
     }
-    return eventFlag; 
+    return eventFlag;
 }
 
 // serial event from DSP
@@ -500,6 +518,9 @@
 
     // select handler based on target mcu
     if (targetName == "switch") {
+        // disable automatic mode
+        autoFlag=0;
+        // update controls
         switchPacket();
     } else if (targetName == "dsp") {
         // send msg to dsp
@@ -507,7 +528,11 @@
 #ifdef DEBUG_MOD2
         pc.printf("json:%s", json);
 #endif
-    } else {
+    }
+    if (targetName == "auto") {
+        // automatic mode
+        autoFlag=1;
+    }  else {
 #ifdef DEBUG_MOD2
         // unrecognised target
         pc.printf("unrecognised target: %s \r\n", targetName.c_str());
@@ -548,31 +573,4 @@
     en_spk_5.write(spkChannle[4]);
     // end switch target parse
 
-}// end switch packet
-
-
-//// Serial Event function  simple byte parser
-//void rxCallback(void)
-//{
-//    // incoming buffer
-//    uint8_t in_byte;
-//    // read icoming
-//    led = !led;
-//    in_byte = pc.getc();
-//#ifdef DEBUG_MOD1
-//    pc.putc(in_byte);
-//#endif
-//    // check if command of input or output
-//    if ((bool)(in_byte & 0b10000000)) { // set input
-//        mux_s0.write((bool)(in_byte & 0b00000001));
-//        mux_s1.write((bool)(in_byte & 0b00000010));
-//        mux_s2.write((bool)(in_byte & 0b00000100));
-//        mux_s3.write((bool)(in_byte & 0b00001000));
-//    } else { // set output
-//        en_spk_1.write((bool)(in_byte & 0b00000001));
-//        en_spk_2.write((bool)(in_byte & 0b00000010));
-//        en_spk_3.write((bool)(in_byte & 0b00000100));
-//        en_spk_4.write((bool)(in_byte & 0b00001000));
-//        en_spk_5.write((bool)(in_byte & 0b00010000));
-//    }
-//} // end rxCallback
\ No newline at end of file
+}// end switch packet
\ No newline at end of file