ReSpeaker Switch V02

Dependencies:   mbed MbedJSONValue

Revision:
6:696bf69e99d2
Parent:
5:cca17ebe4a1f
Child:
7:9153b6566524
--- a/main.cpp	Sun Apr 08 14:03:17 2018 +0000
+++ b/main.cpp	Mon Apr 09 13:21:08 2018 +0000
@@ -16,7 +16,7 @@
 
 */
 
-/* 
+/*
     Bugs:
     PA_14 and PA_13 are share with stlink - not available as interrupt pins
     Used in polling mode instead
@@ -117,6 +117,8 @@
 // Objects //
 /////////////
 
+// Time stamp timer
+Timer timeStamp;
 // json
 MbedJSONValue guiCmd;
 
@@ -169,6 +171,7 @@
 // mic interrupt flag
 int micInterrupt = 0;
 
+
 // json buffer
 char json[MSG_BUFFER_SIZE];
 
@@ -190,27 +193,27 @@
 // Functions //
 ///////////////
 
-// mic interrupt functions
-void micInt_1()
-{
-    micInterrupt = 1;
-}
-void micInt_2()
-{
-    micInterrupt = 2;
-}
-void micInt_3()
-{
-    micInterrupt = 3;
-}
-void micInt_4()
-{
-    micInterrupt = 4;
-}
-void micInt_5()
-{
-    micInterrupt = 5;
-}
+// mic interrupt functions - not used, Polling mode
+//void micInt_1()
+//{
+//    micInterrupt = 1;
+//}
+//void micInt_2()
+//{
+//    micInterrupt = 2;
+//}
+//void micInt_3()
+//{
+//    micInterrupt = 3;
+//}
+//void micInt_4()
+//{
+//    micInterrupt = 4;
+//}
+//void micInt_5()
+//{
+//    micInterrupt = 5;
+//}
 // Serial Event function
 void rxCallback(void);
 
@@ -228,6 +231,10 @@
 
 // initialize switch
 void initSwitch(void);
+
+// poll mic interrupt gpio
+bool micPolling(void);
+
 ////////////////////////
 //  Main Code Setup : //
 ////////////////////////
@@ -263,7 +270,7 @@
             wait(1);
         }
         // change to interrupt - Problematic as PA_14 and PA_13 are share with stlink
-        if (1) { // polling on digital input from micrphones for automatic mic selection
+        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;
@@ -271,14 +278,25 @@
             if (inter_5.read()) micInterrupt=5;
         }
         // mic interrupts
-        if (micInterrupt > 0 ) {
-            pc.printf("IntMic: %d \r\n",micInterrupt);
+//        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);
+            //wait(0.1);
+            // reset micInterrupt flag
             micInterrupt=0;
         }
-
     }// end main loop
 }// end main
 ///////////////
@@ -290,13 +308,16 @@
 {
     // attach serial event interrupt
     pc.attach(&rxCallback, Serial::RxIrq);
-
-    // attach mic interrupts
+    
+    // initialize timer
+    timeStamp.start();
+    // attach mic interrupts - Not used, polling mode
     //inter_1.rise(&micInt_1);
     //inter_2.rise(&micInt_2);
     //inter_3.rise(&micInt_3);
     //inter_4.rise(&micInt_4);
     //inter_5.rise(&micInt_5);
+
     // reset output / input
     mux_s0.write(0);
     mux_s1.write(0);
@@ -309,6 +330,34 @@
     en_spk_5.write(0);
 }// end init switch
 
+// poll mic interrupt gpio
+bool micPolling(void)
+{
+    bool eventFlag=0;
+    // implementation of rising interrupt in polling mode:
+    if (inter_1.read() && (micInterrupt != 1)) {
+        eventFlag=1;
+        micInterrupt=1;
+    }
+    if (inter_2.read() && (micInterrupt != 2)) {
+        eventFlag=1;
+        micInterrupt=2;
+    }
+    if (inter_3.read() && (micInterrupt != 3)) {
+        eventFlag=1;
+        micInterrupt=3;
+    }
+    if (inter_4.read() && (micInterrupt != 4)) {
+        eventFlag=1;
+        micInterrupt=4;
+    }
+    if (inter_5.read() && (micInterrupt != 5)) {
+        eventFlag=1;
+        micInterrupt=5;
+    }
+    return eventFlag; 
+}
+
 // serial event from DSP
 void rxDspCallback(void)
 {