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.
soundboard/main.cpp@0:6a73d3dc037e, 2014-07-28 (annotated)
- Committer:
- ShawnHymel
- Date:
- Mon Jul 28 20:29:28 2014 +0000
- Revision:
- 0:6a73d3dc037e
Added main.cpp files from each of the demo programs. Libraries are not included.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ShawnHymel | 0:6a73d3dc037e | 1 | // Soundboard that plays 1 of 4 .wav files stored on the SD card based on 1 of |
| ShawnHymel | 0:6a73d3dc037e | 2 | // 4 buttons pressed |
| ShawnHymel | 0:6a73d3dc037e | 3 | |
| ShawnHymel | 0:6a73d3dc037e | 4 | #include "mbed.h" |
| ShawnHymel | 0:6a73d3dc037e | 5 | #include "wave_player.h" |
| ShawnHymel | 0:6a73d3dc037e | 6 | #include "SDFileSystem.h" |
| ShawnHymel | 0:6a73d3dc037e | 7 | |
| ShawnHymel | 0:6a73d3dc037e | 8 | // .wav files to play |
| ShawnHymel | 0:6a73d3dc037e | 9 | char *filenames[4] = { "/sd/good_morning.wav", \ |
| ShawnHymel | 0:6a73d3dc037e | 10 | "/sd/questions.wav", \ |
| ShawnHymel | 0:6a73d3dc037e | 11 | "/sd/lack_discipline.wav", \ |
| ShawnHymel | 0:6a73d3dc037e | 12 | "/sd/stop_whining.wav"}; |
| ShawnHymel | 0:6a73d3dc037e | 13 | |
| ShawnHymel | 0:6a73d3dc037e | 14 | // Define buttons |
| ShawnHymel | 0:6a73d3dc037e | 15 | DigitalIn button_1(p11); |
| ShawnHymel | 0:6a73d3dc037e | 16 | DigitalIn button_2(p12); |
| ShawnHymel | 0:6a73d3dc037e | 17 | DigitalIn button_3(p13); |
| ShawnHymel | 0:6a73d3dc037e | 18 | DigitalIn button_4(p14); |
| ShawnHymel | 0:6a73d3dc037e | 19 | |
| ShawnHymel | 0:6a73d3dc037e | 20 | // USB serial (tx, rx) |
| ShawnHymel | 0:6a73d3dc037e | 21 | Serial pc(USBTX, USBRX); |
| ShawnHymel | 0:6a73d3dc037e | 22 | |
| ShawnHymel | 0:6a73d3dc037e | 23 | // SD card |
| ShawnHymel | 0:6a73d3dc037e | 24 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
| ShawnHymel | 0:6a73d3dc037e | 25 | |
| ShawnHymel | 0:6a73d3dc037e | 26 | // Audio out (DAC) |
| ShawnHymel | 0:6a73d3dc037e | 27 | AnalogOut DACout(p18); |
| ShawnHymel | 0:6a73d3dc037e | 28 | wave_player waver(&DACout); |
| ShawnHymel | 0:6a73d3dc037e | 29 | |
| ShawnHymel | 0:6a73d3dc037e | 30 | // Play a .wav file |
| ShawnHymel | 0:6a73d3dc037e | 31 | int playSound(int file_num) { |
| ShawnHymel | 0:6a73d3dc037e | 32 | |
| ShawnHymel | 0:6a73d3dc037e | 33 | FILE *file; |
| ShawnHymel | 0:6a73d3dc037e | 34 | |
| ShawnHymel | 0:6a73d3dc037e | 35 | // Open sound file for reading |
| ShawnHymel | 0:6a73d3dc037e | 36 | file = fopen(filenames[file_num], "r"); |
| ShawnHymel | 0:6a73d3dc037e | 37 | if ( file == NULL ) { |
| ShawnHymel | 0:6a73d3dc037e | 38 | error("ERROR: Could not open file for reading!\n"); |
| ShawnHymel | 0:6a73d3dc037e | 39 | return -1; |
| ShawnHymel | 0:6a73d3dc037e | 40 | } |
| ShawnHymel | 0:6a73d3dc037e | 41 | |
| ShawnHymel | 0:6a73d3dc037e | 42 | // Play the sound file |
| ShawnHymel | 0:6a73d3dc037e | 43 | pc.printf("Playing sound clip %i\r\n", (file_num + 1)); |
| ShawnHymel | 0:6a73d3dc037e | 44 | waver.play(file); |
| ShawnHymel | 0:6a73d3dc037e | 45 | |
| ShawnHymel | 0:6a73d3dc037e | 46 | // Reset to beginning of file and close it |
| ShawnHymel | 0:6a73d3dc037e | 47 | fseek(file, 0, SEEK_SET); |
| ShawnHymel | 0:6a73d3dc037e | 48 | fclose(file); |
| ShawnHymel | 0:6a73d3dc037e | 49 | |
| ShawnHymel | 0:6a73d3dc037e | 50 | return 0; |
| ShawnHymel | 0:6a73d3dc037e | 51 | } |
| ShawnHymel | 0:6a73d3dc037e | 52 | |
| ShawnHymel | 0:6a73d3dc037e | 53 | int main() { |
| ShawnHymel | 0:6a73d3dc037e | 54 | |
| ShawnHymel | 0:6a73d3dc037e | 55 | // Use internal pull-up resistors |
| ShawnHymel | 0:6a73d3dc037e | 56 | button_1.mode(PullUp); |
| ShawnHymel | 0:6a73d3dc037e | 57 | button_2.mode(PullUp); |
| ShawnHymel | 0:6a73d3dc037e | 58 | button_3.mode(PullUp); |
| ShawnHymel | 0:6a73d3dc037e | 59 | button_4.mode(PullUp); |
| ShawnHymel | 0:6a73d3dc037e | 60 | |
| ShawnHymel | 0:6a73d3dc037e | 61 | pc.printf("\r\nHardware Soundboard\r\n"); |
| ShawnHymel | 0:6a73d3dc037e | 62 | |
| ShawnHymel | 0:6a73d3dc037e | 63 | while(1) { |
| ShawnHymel | 0:6a73d3dc037e | 64 | |
| ShawnHymel | 0:6a73d3dc037e | 65 | // Figure out which button was pressed and play that file |
| ShawnHymel | 0:6a73d3dc037e | 66 | if ( button_1 == 0 ) { |
| ShawnHymel | 0:6a73d3dc037e | 67 | playSound(0); |
| ShawnHymel | 0:6a73d3dc037e | 68 | } |
| ShawnHymel | 0:6a73d3dc037e | 69 | if ( button_2 == 0 ) { |
| ShawnHymel | 0:6a73d3dc037e | 70 | playSound(1); |
| ShawnHymel | 0:6a73d3dc037e | 71 | } |
| ShawnHymel | 0:6a73d3dc037e | 72 | if ( button_3 == 0 ) { |
| ShawnHymel | 0:6a73d3dc037e | 73 | playSound(2); |
| ShawnHymel | 0:6a73d3dc037e | 74 | } |
| ShawnHymel | 0:6a73d3dc037e | 75 | if ( button_4 == 0 ) { |
| ShawnHymel | 0:6a73d3dc037e | 76 | playSound(3); |
| ShawnHymel | 0:6a73d3dc037e | 77 | } |
| ShawnHymel | 0:6a73d3dc037e | 78 | |
| ShawnHymel | 0:6a73d3dc037e | 79 | // Wait 10ms before sampling the buttons again |
| ShawnHymel | 0:6a73d3dc037e | 80 | wait(0.01); |
| ShawnHymel | 0:6a73d3dc037e | 81 | } |
| ShawnHymel | 0:6a73d3dc037e | 82 | } |