IISEC / Mbed 2 deprecated echo_machine

Dependencies:   mbed

Revision:
1:5972dbd390c8
Parent:
0:f99e355ba60d
Child:
2:718d996170c2
--- a/voiceecho.cpp	Wed Apr 17 03:23:25 2019 +0000
+++ b/voiceecho.cpp	Wed Apr 17 04:53:40 2019 +0000
@@ -1,12 +1,10 @@
 #include "mbed.h"
 
-/* IO pin defines*/
 DigitalOut led1(LED1);
 PwmOut pwm(p21);
 AnalogIn mic(p20);
 AnalogOut spk(p18);
-Ticker readtick, writetick;
-// Serial pc(USBTX, USBRX);
+Ticker voicetick;
 
 #define WAVMAX 7000  /*buffer size; SAMPFREQ*DEALY<WAVMAX*/
 #define SAMPFREQ 16000.0
@@ -53,9 +51,7 @@
                     line[index++]=ch;
                     this->putc(ch);
                     break;
-            }
-        }
-    }
+            }        }    }
 public:
     KBline(PinName tx, PinName rx) : Serial(tx, rx)
     {
@@ -77,41 +73,31 @@
     }
 } ;
 
-void read_wave()    /*timer driven*/
-{
-    wave[readindex++]= mic.read_u16();  //mic.read()*30000;
-    if (readindex>=WAVMAX) readindex=0;
+int newindex(int ix)
+{   if (ix<0) ix += WAVMAX;
+    return(ix % WAVMAX); }
+
+void voice_io()    /*timer driven*/
+{   unsigned short val;
+    wave[readindex]= mic.read_u16();  //mic.read()*30000;
+    val=wave[writeindex];
+    pwm.write((float)val/65536.0);
+    spk.write_u16(val); // (val/30000.0  );
+    readindex=newindex(readindex+1);
+    writeindex=newindex(writeindex+1);
 }
 
-void new_writeindex(int wi)
-{
-    if (wi<0) wi += WAVMAX;
-    writeindex=wi % WAVMAX;
-}
-
-void write_wave()
-{
-    unsigned short val=wave[writeindex];
-    pwm.write(val*0.001);
-    spk.write_u16(val); // (val/30000.0  );
-    new_writeindex(writeindex+1);
-    // wave[index] +=val<<1;
-}
-
-// main() runs in its own thread in the OS
 int main()
 {
     KBline pc(USBTX, USBRX);
     float delay=DELAY;
 
     pc.baud(9600);
-    pwm.period_us(50);
+    pwm.period_us(32);  /* 62.5us*/
     delay=DELAY;
     readindex=0;
-    new_writeindex(readindex-delay*SAMPFREQ);
-
-    readtick.attach(read_wave, 1.0/SAMPFREQ);
-    writetick.attach(write_wave, 1.0/SAMPFREQ);
+    writeindex=newindex(readindex-delay*SAMPFREQ);
+    voicetick.attach(voice_io, 1.0/SAMPFREQ);
     pc.printf("echo machine  delay=%fs\n\r", delay);
     while (true) {
         pc.start_getline();
@@ -119,9 +105,9 @@
             led1 = !led1;
             wait(0.2);
         }
-        delay=(pc.line[0]-'0') *0.1;  /* multiple of 0.1s */
-        new_writeindex(readindex-delay*SAMPFREQ);
-        pc.printf("new delay %5.2fs  %d \n\r", delay, (readindex-writeindex)% WAVMAX);
+        sscanf(pc.line,"%f", &delay);  /* multiple of 0.1s */
+        writeindex=newindex(readindex-delay*SAMPFREQ);
+        pc.printf("new delay %5.2fs  r=%d  w=%d  val=%d\n\r", delay, readindex, writeindex, wave[writeindex]);
     }
 }