slave2

Dependencies:   PinDetect dsp mbed-rtos mbed

Revision:
1:caeb5792f292
Parent:
0:7ee25b048596
Child:
2:919431d2e1a7
--- a/main.cpp	Sun Apr 24 17:37:33 2016 +0000
+++ b/main.cpp	Mon Apr 25 01:43:46 2016 +0000
@@ -2,46 +2,90 @@
 #include "PinDetect.h"
 #include "rtos.h"
 
-DigitalOut myled(LED1);
+DigitalOut myled(LED1); //LEDS for debug purposes
 DigitalOut myled2(LED2);
 DigitalOut myled4(LED4);
-AnalogOut outfunction(p18);
-DigitalOut masterSignalOut(p26);
-InterruptIn trigger(p25);
+AnalogIn audioIn(p20);//pin for sound capture
+//AnalogOut outfunction(p18);
+//DigitalOut masterSignalOut(p26);
+InterruptIn startButton(p21);
+InterruptIn printButton(p22);
+//InterruptIn trigger(p25);
+Serial pc(USBTX,USBRX);//serial to computer
+Serial master(p9,p10);//serial to slave 1
+#define samples 300 //how many samples to take
+float* data; //set up the recording array
+float* dataSlave1; //set up array for slave1's data
+Timer ti; //Timer to determine sampling rate
+int startTime;
+int endTime;
+int sampleRate;
 int counter;
-
-Timer ti;
+int start;
+int print;
 
 void analogScope(void const *args){
     counter = 0;
     myled = 0;
     while(1){
     //outfunction = (ti.read_us()%1000)*0.001;
-    //myled= (((ti.read_ms())>>10)%1);
-    outfunction = (counter%10)*0.1;
+    //myled = (((int)(ti.read_ms())>>10)%1);
+    //outfunction = (counter%10)*0.1;
     counter++;
     myled = !myled;
+    //Thread::wait_us(50);
     }
 }
 void sendinterrupt(void){
-    masterSignalOut = 1;
+    //masterSignalOut = 1;
 }
 void interruptrecv(void){
     ti.reset();
     myled4 = !myled4;
     counter = 0;
-    masterSignalOut = 0;
+    //masterSignalOut = 0;
+}
+void startRecording(void){
+    start = 1;
+    myled2 = 1;
+}
+void startPrint(void){
+    print = 1;
+    myled4 = 1;
 }
 int main() {
     ti.reset();
+    start = 0;
     myled2 = 0;
     myled4 = 0;
-    masterSignalOut = 0;
-    Thread threadanalog(analogScope);
-    threadanalog.set_priority(osPriorityLow);
-    trigger.rise(&interruptrecv);
+    //allocate memory to the data arrays
+    data = (float*) malloc(sizeof(float)*samples);
+    dataSlave1 = (float*) malloc(sizeof(float)*samples);
+    //masterSignalOut = 0;
+    //startButton.mode(PullUp);
+    startButton.fall(&startRecording);
+    //printButton.mode(PullUp);
+    printButton.fall(&startPrint);
+    //trigger.rise(&interruptrecv);
+    //Thread threadanalog(analogScope);
+    //threadanalog.set_priority(osPriorityLow);
     while(1) {
-        myled2 = !myled2;
-        Thread::wait(10000);
+        //myled2 = !myled2;
+        if(start){ //once button is pressed
+            startTime = ti.read_us(); //get start time
+            for(int i = 0;i<samples;i++){ //record 300 samples
+                data[i] = audioIn;
+            }
+            endTime = ti.read_us(); //get end time
+            sampleRate = (int)(samples*1000000/(endTime-startTime)); //calculate sample rate
+            start = 0; //stop
+        }
+        if(print){
+               for(int i = 0;i<samples;i++){
+                    pc.printf("%5f ",data[i]);
+                }
+                pc.printf("\n %5d",sampleRate);
+                print = 0;
+        }
     }
 }