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: AD5206 USBDevice mbed
Fork of USBAUDIO_speaker by
main.cpp@10:352d3dbfd6ec, 2015-10-17 (annotated)
- Committer:
 - taoqiuyang
 - Date:
 - Sat Oct 17 22:14:31 2015 +0000
 - Revision:
 - 10:352d3dbfd6ec
 - Parent:
 - 7:ba4f65ce69f2
 
Working demo;
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| samux | 3:41b10e311100 | 1 | // USBAudio speaker example | 
| samux | 0:5176b3dfccd6 | 2 | |
| samux | 0:5176b3dfccd6 | 3 | #include "mbed.h" | 
| samux | 0:5176b3dfccd6 | 4 | #include "USBAudio.h" | 
| taoqiuyang | 10:352d3dbfd6ec | 5 | #include "AD5206.h" | 
| taoqiuyang | 10:352d3dbfd6ec | 6 | |
| taoqiuyang | 10:352d3dbfd6ec | 7 | AD5206 digipot(p5, p6, p7,p8); | 
| taoqiuyang | 10:352d3dbfd6ec | 8 | int val=150; | 
| taoqiuyang | 10:352d3dbfd6ec | 9 | DigitalOut led1(LED1); | 
| taoqiuyang | 10:352d3dbfd6ec | 10 | DigitalOut led2(LED2); | 
| taoqiuyang | 10:352d3dbfd6ec | 11 | DigitalIn but1(p22); | 
| taoqiuyang | 10:352d3dbfd6ec | 12 | DigitalIn but2(p21); | 
| taoqiuyang | 10:352d3dbfd6ec | 13 | |
| samux | 0:5176b3dfccd6 | 14 | |
| samux | 0:5176b3dfccd6 | 15 | // frequency: 48 kHz | 
| samux | 0:5176b3dfccd6 | 16 | #define FREQ 48000 | 
| samux | 0:5176b3dfccd6 | 17 | |
| samux | 0:5176b3dfccd6 | 18 | // 1 channel: mono | 
| samux | 0:5176b3dfccd6 | 19 | #define NB_CHA 1 | 
| samux | 0:5176b3dfccd6 | 20 | |
| samux | 0:5176b3dfccd6 | 21 | // length of an audio packet: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there is one channel, the length will be 48 * 2 * 1 | 
| samux | 2:c3edc567ae33 | 22 | #define AUDIO_LENGTH_PACKET 48 * 2 * 1 | 
| samux | 0:5176b3dfccd6 | 23 | |
| samux | 7:ba4f65ce69f2 | 24 | // USBAudio (we just use audio packets received, we don't send audio packets to the computer in this example) | 
| samux | 7:ba4f65ce69f2 | 25 | USBAudio audio(FREQ, NB_CHA, 8000, 1, 0x7180, 0x7500); | 
| samux | 0:5176b3dfccd6 | 26 | |
| samux | 0:5176b3dfccd6 | 27 | // speaker connected to the AnalogOut output. The audio stream received over USb will be sent to the speaker | 
| samux | 0:5176b3dfccd6 | 28 | AnalogOut speaker(p18); | 
| samux | 0:5176b3dfccd6 | 29 | |
| samux | 0:5176b3dfccd6 | 30 | // ticker to send data to the speaker at the good frequency | 
| samux | 0:5176b3dfccd6 | 31 | Ticker tic; | 
| taoqiuyang | 10:352d3dbfd6ec | 32 | Ticker tic2; | 
| taoqiuyang | 10:352d3dbfd6ec | 33 | |
| samux | 0:5176b3dfccd6 | 34 | |
| samux | 1:0335b5c18618 | 35 | // buffer where will be store one audio packet (LENGTH_AUDIO_PACKET/2 because we are storing int16 and not uint8) | 
| samux | 2:c3edc567ae33 | 36 | int16_t buf[AUDIO_LENGTH_PACKET/2]; | 
| samux | 0:5176b3dfccd6 | 37 | |
| samux | 0:5176b3dfccd6 | 38 | // show if an audio packet is available | 
| samux | 0:5176b3dfccd6 | 39 | volatile bool available = false; | 
| samux | 0:5176b3dfccd6 | 40 | |
| samux | 0:5176b3dfccd6 | 41 | // index of the value which will be send to the speaker | 
| samux | 0:5176b3dfccd6 | 42 | int index_buf = 0; | 
| samux | 0:5176b3dfccd6 | 43 | |
| samux | 0:5176b3dfccd6 | 44 | // previous value sent to the speaker | 
| samux | 0:5176b3dfccd6 | 45 | uint16_t p_val = 0; | 
| samux | 0:5176b3dfccd6 | 46 | |
| samux | 0:5176b3dfccd6 | 47 | // function executed each 1/FREQ s | 
| samux | 0:5176b3dfccd6 | 48 | void tic_handler() { | 
| samux | 0:5176b3dfccd6 | 49 | float speaker_value; | 
| samux | 0:5176b3dfccd6 | 50 | |
| samux | 0:5176b3dfccd6 | 51 | if (available) { | 
| samux | 0:5176b3dfccd6 | 52 | //convert 2 bytes in float | 
| samux | 1:0335b5c18618 | 53 | speaker_value = (float)(buf[index_buf]); | 
| samux | 0:5176b3dfccd6 | 54 | |
| samux | 0:5176b3dfccd6 | 55 | // speaker_value between 0 and 65535 | 
| samux | 0:5176b3dfccd6 | 56 | speaker_value += 32768.0; | 
| samux | 0:5176b3dfccd6 | 57 | |
| samux | 0:5176b3dfccd6 | 58 | // adjust according to current volume | 
| samux | 0:5176b3dfccd6 | 59 | speaker_value *= audio.getVolume(); | 
| samux | 0:5176b3dfccd6 | 60 | |
| samux | 0:5176b3dfccd6 | 61 | // as two bytes has been read, we move the index of two bytes | 
| samux | 1:0335b5c18618 | 62 | index_buf++; | 
| samux | 0:5176b3dfccd6 | 63 | |
| samux | 0:5176b3dfccd6 | 64 | // if we have read all the buffer, no more data available | 
| samux | 2:c3edc567ae33 | 65 | if (index_buf == AUDIO_LENGTH_PACKET/2) { | 
| samux | 0:5176b3dfccd6 | 66 | index_buf = 0; | 
| samux | 0:5176b3dfccd6 | 67 | available = false; | 
| samux | 0:5176b3dfccd6 | 68 | } | 
| samux | 0:5176b3dfccd6 | 69 | } else { | 
| samux | 0:5176b3dfccd6 | 70 | speaker_value = p_val; | 
| samux | 0:5176b3dfccd6 | 71 | } | 
| samux | 0:5176b3dfccd6 | 72 | |
| samux | 0:5176b3dfccd6 | 73 | p_val = speaker_value; | 
| samux | 0:5176b3dfccd6 | 74 | |
| samux | 0:5176b3dfccd6 | 75 | // send value to the speaker | 
| samux | 0:5176b3dfccd6 | 76 | speaker.write_u16((uint16_t)speaker_value); | 
| samux | 0:5176b3dfccd6 | 77 | } | 
| samux | 0:5176b3dfccd6 | 78 | |
| taoqiuyang | 10:352d3dbfd6ec | 79 | void tic_handler2() { | 
| taoqiuyang | 10:352d3dbfd6ec | 80 | led1=0; | 
| taoqiuyang | 10:352d3dbfd6ec | 81 | led2=0; | 
| taoqiuyang | 10:352d3dbfd6ec | 82 | if (!but1){led1=1;val+=20;} | 
| taoqiuyang | 10:352d3dbfd6ec | 83 | if (!but2){led2=1;val-=20;} | 
| taoqiuyang | 10:352d3dbfd6ec | 84 | digipot.write_AD5206(0,val); | 
| taoqiuyang | 10:352d3dbfd6ec | 85 | |
| taoqiuyang | 10:352d3dbfd6ec | 86 | if (val>255){val=255;} | 
| taoqiuyang | 10:352d3dbfd6ec | 87 | if (val<0) {val=0;} | 
| taoqiuyang | 10:352d3dbfd6ec | 88 | } | 
| taoqiuyang | 10:352d3dbfd6ec | 89 | |
| samux | 0:5176b3dfccd6 | 90 | int main() { | 
| samux | 0:5176b3dfccd6 | 91 | |
| samux | 0:5176b3dfccd6 | 92 | // attach a function executed each 1/FREQ s | 
| samux | 0:5176b3dfccd6 | 93 | tic.attach_us(tic_handler, 1000000.0/(float)FREQ); | 
| taoqiuyang | 10:352d3dbfd6ec | 94 | tic2.attach(tic_handler2, 0.2); | 
| taoqiuyang | 10:352d3dbfd6ec | 95 | |
| samux | 0:5176b3dfccd6 | 96 | while (1) { | 
| samux | 0:5176b3dfccd6 | 97 | // read an audio packet | 
| samux | 1:0335b5c18618 | 98 | audio.read((uint8_t *)buf); | 
| samux | 0:5176b3dfccd6 | 99 | available = true; | 
| samux | 0:5176b3dfccd6 | 100 | } | 
| samux | 0:5176b3dfccd6 | 101 | } | 
