Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
voiceecho.cpp@1:5972dbd390c8, 2019-04-17 (annotated)
- Committer:
- titanium
- Date:
- Wed Apr 17 04:53:40 2019 +0000
- Revision:
- 1:5972dbd390c8
- Parent:
- 0:f99e355ba60d
- Child:
- 2:718d996170c2
echo machine v2 pwm enabled
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| titanium | 0:f99e355ba60d | 1 | #include "mbed.h" |
| titanium | 0:f99e355ba60d | 2 | |
| titanium | 0:f99e355ba60d | 3 | DigitalOut led1(LED1); |
| titanium | 0:f99e355ba60d | 4 | PwmOut pwm(p21); |
| titanium | 0:f99e355ba60d | 5 | AnalogIn mic(p20); |
| titanium | 0:f99e355ba60d | 6 | AnalogOut spk(p18); |
| titanium | 1:5972dbd390c8 | 7 | Ticker voicetick; |
| titanium | 0:f99e355ba60d | 8 | |
| titanium | 0:f99e355ba60d | 9 | #define WAVMAX 7000 /*buffer size; SAMPFREQ*DEALY<WAVMAX*/ |
| titanium | 0:f99e355ba60d | 10 | #define SAMPFREQ 16000.0 |
| titanium | 0:f99e355ba60d | 11 | #define DELAY 0.25 /*s*/ |
| titanium | 0:f99e355ba60d | 12 | |
| titanium | 0:f99e355ba60d | 13 | unsigned short wave[WAVMAX]; |
| titanium | 0:f99e355ba60d | 14 | int readindex=0, writeindex=0; |
| titanium | 0:f99e355ba60d | 15 | |
| titanium | 0:f99e355ba60d | 16 | class KBline : public Serial |
| titanium | 0:f99e355ba60d | 17 | { |
| titanium | 0:f99e355ba60d | 18 | public: |
| titanium | 0:f99e355ba60d | 19 | char line[100]; |
| titanium | 0:f99e355ba60d | 20 | int index; |
| titanium | 0:f99e355ba60d | 21 | int receiving; //status 0=not started, 1=waiting, 2=receiving, 3=end |
| titanium | 0:f99e355ba60d | 22 | private: |
| titanium | 0:f99e355ba60d | 23 | void serialirq() |
| titanium | 0:f99e355ba60d | 24 | { |
| titanium | 0:f99e355ba60d | 25 | char ch; |
| titanium | 0:f99e355ba60d | 26 | if (this->readable()) { |
| titanium | 0:f99e355ba60d | 27 | ch=this->getc(); |
| titanium | 0:f99e355ba60d | 28 | switch(ch) { //simple line editing |
| titanium | 0:f99e355ba60d | 29 | case '\n': |
| titanium | 0:f99e355ba60d | 30 | case '\r': //LF and CR terminate reading |
| titanium | 0:f99e355ba60d | 31 | line[index++]=ch; |
| titanium | 0:f99e355ba60d | 32 | line[index++]=0; |
| titanium | 0:f99e355ba60d | 33 | this->printf("\r\n"); |
| titanium | 0:f99e355ba60d | 34 | receiving=3; |
| titanium | 0:f99e355ba60d | 35 | break; |
| titanium | 0:f99e355ba60d | 36 | case 21: /* control-U cancels entire line*/ |
| titanium | 0:f99e355ba60d | 37 | this->printf("\n\r"); |
| titanium | 0:f99e355ba60d | 38 | index=0; |
| titanium | 0:f99e355ba60d | 39 | break; |
| titanium | 0:f99e355ba60d | 40 | case 0x08: |
| titanium | 0:f99e355ba60d | 41 | case 0x7f: /*BS and DEL delete the last character*/ |
| titanium | 0:f99e355ba60d | 42 | if (index>0) { |
| titanium | 0:f99e355ba60d | 43 | index--; |
| titanium | 0:f99e355ba60d | 44 | this->putc(0x08); |
| titanium | 0:f99e355ba60d | 45 | this->putc(0x20); |
| titanium | 0:f99e355ba60d | 46 | this->putc(0x08); |
| titanium | 0:f99e355ba60d | 47 | } |
| titanium | 0:f99e355ba60d | 48 | break; |
| titanium | 0:f99e355ba60d | 49 | default: |
| titanium | 0:f99e355ba60d | 50 | receiving=2; |
| titanium | 0:f99e355ba60d | 51 | line[index++]=ch; |
| titanium | 0:f99e355ba60d | 52 | this->putc(ch); |
| titanium | 0:f99e355ba60d | 53 | break; |
| titanium | 1:5972dbd390c8 | 54 | } } } |
| titanium | 0:f99e355ba60d | 55 | public: |
| titanium | 0:f99e355ba60d | 56 | KBline(PinName tx, PinName rx) : Serial(tx, rx) |
| titanium | 0:f99e355ba60d | 57 | { |
| titanium | 0:f99e355ba60d | 58 | receiving=0; |
| titanium | 0:f99e355ba60d | 59 | } |
| titanium | 0:f99e355ba60d | 60 | // KBline(Serial *pc) { term=pc; }; //constructor |
| titanium | 0:f99e355ba60d | 61 | void start_getline() |
| titanium | 0:f99e355ba60d | 62 | { |
| titanium | 0:f99e355ba60d | 63 | index=0; |
| titanium | 0:f99e355ba60d | 64 | receiving=1; |
| titanium | 0:f99e355ba60d | 65 | this->attach(this, &KBline::serialirq); |
| titanium | 0:f99e355ba60d | 66 | } |
| titanium | 0:f99e355ba60d | 67 | // int receiving() { return(receiving);} |
| titanium | 0:f99e355ba60d | 68 | char *getline() |
| titanium | 0:f99e355ba60d | 69 | { |
| titanium | 0:f99e355ba60d | 70 | while (receiving!=3) wait(0.1); |
| titanium | 0:f99e355ba60d | 71 | receiving=0; |
| titanium | 0:f99e355ba60d | 72 | return(line); |
| titanium | 0:f99e355ba60d | 73 | } |
| titanium | 0:f99e355ba60d | 74 | } ; |
| titanium | 0:f99e355ba60d | 75 | |
| titanium | 1:5972dbd390c8 | 76 | int newindex(int ix) |
| titanium | 1:5972dbd390c8 | 77 | { if (ix<0) ix += WAVMAX; |
| titanium | 1:5972dbd390c8 | 78 | return(ix % WAVMAX); } |
| titanium | 1:5972dbd390c8 | 79 | |
| titanium | 1:5972dbd390c8 | 80 | void voice_io() /*timer driven*/ |
| titanium | 1:5972dbd390c8 | 81 | { unsigned short val; |
| titanium | 1:5972dbd390c8 | 82 | wave[readindex]= mic.read_u16(); //mic.read()*30000; |
| titanium | 1:5972dbd390c8 | 83 | val=wave[writeindex]; |
| titanium | 1:5972dbd390c8 | 84 | pwm.write((float)val/65536.0); |
| titanium | 1:5972dbd390c8 | 85 | spk.write_u16(val); // (val/30000.0 ); |
| titanium | 1:5972dbd390c8 | 86 | readindex=newindex(readindex+1); |
| titanium | 1:5972dbd390c8 | 87 | writeindex=newindex(writeindex+1); |
| titanium | 0:f99e355ba60d | 88 | } |
| titanium | 0:f99e355ba60d | 89 | |
| titanium | 0:f99e355ba60d | 90 | int main() |
| titanium | 0:f99e355ba60d | 91 | { |
| titanium | 0:f99e355ba60d | 92 | KBline pc(USBTX, USBRX); |
| titanium | 0:f99e355ba60d | 93 | float delay=DELAY; |
| titanium | 0:f99e355ba60d | 94 | |
| titanium | 0:f99e355ba60d | 95 | pc.baud(9600); |
| titanium | 1:5972dbd390c8 | 96 | pwm.period_us(32); /* 62.5us*/ |
| titanium | 0:f99e355ba60d | 97 | delay=DELAY; |
| titanium | 0:f99e355ba60d | 98 | readindex=0; |
| titanium | 1:5972dbd390c8 | 99 | writeindex=newindex(readindex-delay*SAMPFREQ); |
| titanium | 1:5972dbd390c8 | 100 | voicetick.attach(voice_io, 1.0/SAMPFREQ); |
| titanium | 0:f99e355ba60d | 101 | pc.printf("echo machine delay=%fs\n\r", delay); |
| titanium | 0:f99e355ba60d | 102 | while (true) { |
| titanium | 0:f99e355ba60d | 103 | pc.start_getline(); |
| titanium | 0:f99e355ba60d | 104 | while (pc.receiving!=3) { |
| titanium | 0:f99e355ba60d | 105 | led1 = !led1; |
| titanium | 0:f99e355ba60d | 106 | wait(0.2); |
| titanium | 0:f99e355ba60d | 107 | } |
| titanium | 1:5972dbd390c8 | 108 | sscanf(pc.line,"%f", &delay); /* multiple of 0.1s */ |
| titanium | 1:5972dbd390c8 | 109 | writeindex=newindex(readindex-delay*SAMPFREQ); |
| titanium | 1:5972dbd390c8 | 110 | pc.printf("new delay %5.2fs r=%d w=%d val=%d\n\r", delay, readindex, writeindex, wave[writeindex]); |
| titanium | 0:f99e355ba60d | 111 | } |
| titanium | 0:f99e355ba60d | 112 | } |
| titanium | 0:f99e355ba60d | 113 |