IISEC / Mbed 2 deprecated echo_machine

Dependencies:   mbed

Committer:
titanium
Date:
Fri Apr 19 04:11:04 2019 +0000
Revision:
2:718d996170c2
Parent:
1:5972dbd390c8
Child:
3:8ed0851755d7
recursive echo;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
titanium 2:718d996170c2 1 /* voiceecho.cpp */
titanium 2:718d996170c2 2 // voice input to ADC is delayed for 0.1 to 0.5 sec and output to DAC and PWM
titanium 2:718d996170c2 3 // Copyright Toshihiro Matsui, IISEC, April 2019
titanium 2:718d996170c2 4 // Any use/copy/modify of this program is fully granted to anyone by the author.
titanium 2:718d996170c2 5 //
titanium 2:718d996170c2 6
titanium 2:718d996170c2 7
titanium 0:f99e355ba60d 8 #include "mbed.h"
titanium 0:f99e355ba60d 9
titanium 0:f99e355ba60d 10 DigitalOut led1(LED1);
titanium 0:f99e355ba60d 11 PwmOut pwm(p21);
titanium 0:f99e355ba60d 12 AnalogIn mic(p20);
titanium 0:f99e355ba60d 13 AnalogOut spk(p18);
titanium 1:5972dbd390c8 14 Ticker voicetick;
titanium 0:f99e355ba60d 15
titanium 0:f99e355ba60d 16 #define WAVMAX 7000 /*buffer size; SAMPFREQ*DEALY<WAVMAX*/
titanium 0:f99e355ba60d 17 #define SAMPFREQ 16000.0
titanium 0:f99e355ba60d 18 #define DELAY 0.25 /*s*/
titanium 0:f99e355ba60d 19
titanium 0:f99e355ba60d 20 unsigned short wave[WAVMAX];
titanium 0:f99e355ba60d 21 int readindex=0, writeindex=0;
titanium 0:f99e355ba60d 22
titanium 0:f99e355ba60d 23 class KBline : public Serial
titanium 0:f99e355ba60d 24 {
titanium 0:f99e355ba60d 25 public:
titanium 0:f99e355ba60d 26 char line[100];
titanium 0:f99e355ba60d 27 int index;
titanium 0:f99e355ba60d 28 int receiving; //status 0=not started, 1=waiting, 2=receiving, 3=end
titanium 0:f99e355ba60d 29 private:
titanium 0:f99e355ba60d 30 void serialirq()
titanium 0:f99e355ba60d 31 {
titanium 0:f99e355ba60d 32 char ch;
titanium 0:f99e355ba60d 33 if (this->readable()) {
titanium 0:f99e355ba60d 34 ch=this->getc();
titanium 0:f99e355ba60d 35 switch(ch) { //simple line editing
titanium 0:f99e355ba60d 36 case '\n':
titanium 0:f99e355ba60d 37 case '\r': //LF and CR terminate reading
titanium 0:f99e355ba60d 38 line[index++]=ch;
titanium 0:f99e355ba60d 39 line[index++]=0;
titanium 0:f99e355ba60d 40 this->printf("\r\n");
titanium 0:f99e355ba60d 41 receiving=3;
titanium 0:f99e355ba60d 42 break;
titanium 0:f99e355ba60d 43 case 21: /* control-U cancels entire line*/
titanium 0:f99e355ba60d 44 this->printf("\n\r");
titanium 0:f99e355ba60d 45 index=0;
titanium 0:f99e355ba60d 46 break;
titanium 0:f99e355ba60d 47 case 0x08:
titanium 0:f99e355ba60d 48 case 0x7f: /*BS and DEL delete the last character*/
titanium 0:f99e355ba60d 49 if (index>0) {
titanium 0:f99e355ba60d 50 index--;
titanium 0:f99e355ba60d 51 this->putc(0x08);
titanium 0:f99e355ba60d 52 this->putc(0x20);
titanium 0:f99e355ba60d 53 this->putc(0x08);
titanium 0:f99e355ba60d 54 }
titanium 0:f99e355ba60d 55 break;
titanium 0:f99e355ba60d 56 default:
titanium 0:f99e355ba60d 57 receiving=2;
titanium 0:f99e355ba60d 58 line[index++]=ch;
titanium 0:f99e355ba60d 59 this->putc(ch);
titanium 0:f99e355ba60d 60 break;
titanium 1:5972dbd390c8 61 } } }
titanium 0:f99e355ba60d 62 public:
titanium 0:f99e355ba60d 63 KBline(PinName tx, PinName rx) : Serial(tx, rx)
titanium 0:f99e355ba60d 64 {
titanium 0:f99e355ba60d 65 receiving=0;
titanium 0:f99e355ba60d 66 }
titanium 0:f99e355ba60d 67 // KBline(Serial *pc) { term=pc; }; //constructor
titanium 0:f99e355ba60d 68 void start_getline()
titanium 0:f99e355ba60d 69 {
titanium 0:f99e355ba60d 70 index=0;
titanium 0:f99e355ba60d 71 receiving=1;
titanium 0:f99e355ba60d 72 this->attach(this, &KBline::serialirq);
titanium 0:f99e355ba60d 73 }
titanium 0:f99e355ba60d 74 // int receiving() { return(receiving);}
titanium 0:f99e355ba60d 75 char *getline()
titanium 0:f99e355ba60d 76 {
titanium 0:f99e355ba60d 77 while (receiving!=3) wait(0.1);
titanium 0:f99e355ba60d 78 receiving=0;
titanium 0:f99e355ba60d 79 return(line);
titanium 0:f99e355ba60d 80 }
titanium 0:f99e355ba60d 81 } ;
titanium 0:f99e355ba60d 82
titanium 1:5972dbd390c8 83 int newindex(int ix)
titanium 1:5972dbd390c8 84 { if (ix<0) ix += WAVMAX;
titanium 1:5972dbd390c8 85 return(ix % WAVMAX); }
titanium 1:5972dbd390c8 86
titanium 1:5972dbd390c8 87 void voice_io() /*timer driven*/
titanium 1:5972dbd390c8 88 { unsigned short val;
titanium 2:718d996170c2 89 wave[readindex]= wave[readindex]*0.4+mic.read_u16()*0.6; //mic.read()*30000;
titanium 1:5972dbd390c8 90 val=wave[writeindex];
titanium 1:5972dbd390c8 91 pwm.write((float)val/65536.0);
titanium 1:5972dbd390c8 92 spk.write_u16(val); // (val/30000.0 );
titanium 1:5972dbd390c8 93 readindex=newindex(readindex+1);
titanium 1:5972dbd390c8 94 writeindex=newindex(writeindex+1);
titanium 0:f99e355ba60d 95 }
titanium 0:f99e355ba60d 96
titanium 0:f99e355ba60d 97 int main()
titanium 0:f99e355ba60d 98 {
titanium 0:f99e355ba60d 99 KBline pc(USBTX, USBRX);
titanium 0:f99e355ba60d 100 float delay=DELAY;
titanium 0:f99e355ba60d 101
titanium 0:f99e355ba60d 102 pc.baud(9600);
titanium 1:5972dbd390c8 103 pwm.period_us(32); /* 62.5us*/
titanium 0:f99e355ba60d 104 delay=DELAY;
titanium 0:f99e355ba60d 105 readindex=0;
titanium 1:5972dbd390c8 106 writeindex=newindex(readindex-delay*SAMPFREQ);
titanium 1:5972dbd390c8 107 voicetick.attach(voice_io, 1.0/SAMPFREQ);
titanium 0:f99e355ba60d 108 pc.printf("echo machine delay=%fs\n\r", delay);
titanium 0:f99e355ba60d 109 while (true) {
titanium 0:f99e355ba60d 110 pc.start_getline();
titanium 0:f99e355ba60d 111 while (pc.receiving!=3) {
titanium 0:f99e355ba60d 112 led1 = !led1;
titanium 0:f99e355ba60d 113 wait(0.2);
titanium 0:f99e355ba60d 114 }
titanium 1:5972dbd390c8 115 sscanf(pc.line,"%f", &delay); /* multiple of 0.1s */
titanium 1:5972dbd390c8 116 writeindex=newindex(readindex-delay*SAMPFREQ);
titanium 1:5972dbd390c8 117 pc.printf("new delay %5.2fs r=%d w=%d val=%d\n\r", delay, readindex, writeindex, wave[writeindex]);
titanium 0:f99e355ba60d 118 }
titanium 0:f99e355ba60d 119 }
titanium 0:f99e355ba60d 120