IISEC / Mbed 2 deprecated echo_machine

Dependencies:   mbed

Revision:
0:f99e355ba60d
Child:
1:5972dbd390c8
diff -r 000000000000 -r f99e355ba60d voiceecho.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/voiceecho.cpp	Wed Apr 17 03:23:25 2019 +0000
@@ -0,0 +1,127 @@
+#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);
+
+#define WAVMAX 7000  /*buffer size; SAMPFREQ*DEALY<WAVMAX*/
+#define SAMPFREQ 16000.0
+#define DELAY 0.25 /*s*/
+
+unsigned short wave[WAVMAX];
+int readindex=0, writeindex=0;
+
+class KBline : public Serial
+{
+public:
+    char line[100];
+    int  index;
+    int  receiving; //status 0=not started, 1=waiting, 2=receiving, 3=end
+private:
+    void serialirq()
+    {
+        char ch;
+        if (this->readable()) {
+            ch=this->getc();
+            switch(ch) { //simple line editing
+                case '\n':
+                case '\r':  //LF and CR terminate reading
+                    line[index++]=ch;
+                    line[index++]=0;
+                    this->printf("\r\n");
+                    receiving=3;
+                    break;
+                case 21:  /* control-U cancels entire line*/
+                    this->printf("\n\r");
+                    index=0;
+                    break;
+                case 0x08:
+                case 0x7f: /*BS and DEL delete the last character*/
+                    if (index>0) {
+                        index--;
+                        this->putc(0x08);
+                        this->putc(0x20);
+                        this->putc(0x08);
+                    }
+                    break;
+                default:
+                    receiving=2;
+                    line[index++]=ch;
+                    this->putc(ch);
+                    break;
+            }
+        }
+    }
+public:
+    KBline(PinName tx, PinName rx) : Serial(tx, rx)
+    {
+        receiving=0;
+    }
+    //  KBline(Serial *pc) { term=pc; }; //constructor
+    void start_getline()
+    {
+        index=0;
+        receiving=1;
+        this->attach(this, &KBline::serialirq);
+    }
+    // int receiving() { return(receiving);}
+    char *getline()
+    {
+        while (receiving!=3) wait(0.1);
+        receiving=0;
+        return(line);
+    }
+} ;
+
+void read_wave()    /*timer driven*/
+{
+    wave[readindex++]= mic.read_u16();  //mic.read()*30000;
+    if (readindex>=WAVMAX) readindex=0;
+}
+
+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);
+    delay=DELAY;
+    readindex=0;
+    new_writeindex(readindex-delay*SAMPFREQ);
+
+    readtick.attach(read_wave, 1.0/SAMPFREQ);
+    writetick.attach(write_wave, 1.0/SAMPFREQ);
+    pc.printf("echo machine  delay=%fs\n\r", delay);
+    while (true) {
+        pc.start_getline();
+        while (pc.receiving!=3) {
+            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);
+    }
+}
+