For MAX323630FTHR: Plays a WAV file in the SD card. Interfaced through serial port using puTTY or powershell.
Dependencies: USBMSD_BD SDFileSystem max32630fthr USBDevice
main.cpp@0:12c56a256ee1, 2019-07-16 (annotated)
- Committer:
- Lugs
- Date:
- Tue Jul 16 08:11:48 2019 +0000
- Revision:
- 0:12c56a256ee1
- Child:
- 1:7884bc0fb012
still caching version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lugs | 0:12c56a256ee1 | 1 | #include "mbed.h" |
Lugs | 0:12c56a256ee1 | 2 | #include "max32630fthr.h" |
Lugs | 0:12c56a256ee1 | 3 | #include "USBSerial.h" |
Lugs | 0:12c56a256ee1 | 4 | #include "stdio.h" |
Lugs | 0:12c56a256ee1 | 5 | #include "SDFileSystem.h" |
Lugs | 0:12c56a256ee1 | 6 | |
Lugs | 0:12c56a256ee1 | 7 | //still needs BITS PER SAMPLE PARSING. |
Lugs | 0:12c56a256ee1 | 8 | //do this like so: PWM.write(WavValue/2^BPS) |
Lugs | 0:12c56a256ee1 | 9 | |
Lugs | 0:12c56a256ee1 | 10 | #define BUFFER_SIZE 1024 |
Lugs | 0:12c56a256ee1 | 11 | #define HALF_BUFFER 512 |
Lugs | 0:12c56a256ee1 | 12 | |
Lugs | 0:12c56a256ee1 | 13 | DigitalOut rLED(LED1); |
Lugs | 0:12c56a256ee1 | 14 | DigitalOut gLED(LED2); |
Lugs | 0:12c56a256ee1 | 15 | DigitalOut bLED(LED3); |
Lugs | 0:12c56a256ee1 | 16 | |
Lugs | 0:12c56a256ee1 | 17 | DigitalIn Button(P2_3); |
Lugs | 0:12c56a256ee1 | 18 | |
Lugs | 0:12c56a256ee1 | 19 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
Lugs | 0:12c56a256ee1 | 20 | PwmOut PWM(P5_6); |
Lugs | 0:12c56a256ee1 | 21 | AnalogIn POT(AIN_0); |
Lugs | 0:12c56a256ee1 | 22 | volatile int bufferPOS = 0; |
Lugs | 0:12c56a256ee1 | 23 | |
Lugs | 0:12c56a256ee1 | 24 | Serial daplink(P2_1,P2_0); |
Lugs | 0:12c56a256ee1 | 25 | USBSerial microUSB; |
Lugs | 0:12c56a256ee1 | 26 | SDFileSystem sd(P0_5, P0_6, P0_4, P0_7, "sd"); |
Lugs | 0:12c56a256ee1 | 27 | |
Lugs | 0:12c56a256ee1 | 28 | int audioDataBuffer[BUFFER_SIZE]; |
Lugs | 0:12c56a256ee1 | 29 | |
Lugs | 0:12c56a256ee1 | 30 | struct WavFile |
Lugs | 0:12c56a256ee1 | 31 | { |
Lugs | 0:12c56a256ee1 | 32 | long int size; |
Lugs | 0:12c56a256ee1 | 33 | int channels; |
Lugs | 0:12c56a256ee1 | 34 | int sampleRate; |
Lugs | 0:12c56a256ee1 | 35 | int bitsPerSample; |
Lugs | 0:12c56a256ee1 | 36 | }; |
Lugs | 0:12c56a256ee1 | 37 | |
Lugs | 0:12c56a256ee1 | 38 | WavFile Track; |
Lugs | 0:12c56a256ee1 | 39 | |
Lugs | 0:12c56a256ee1 | 40 | void placeNewSample(void) |
Lugs | 0:12c56a256ee1 | 41 | { |
Lugs | 0:12c56a256ee1 | 42 | //POT.read(); //COMMENTED OUT FOR TESTING |
Lugs | 0:12c56a256ee1 | 43 | PWM = (float) audioDataBuffer[bufferPOS]/Track.bitsPerSample; //multiply by POT value for volume. |
Lugs | 0:12c56a256ee1 | 44 | bufferPOS = (bufferPOS+1) & 0x07F; |
Lugs | 0:12c56a256ee1 | 45 | } |
Lugs | 0:12c56a256ee1 | 46 | |
Lugs | 0:12c56a256ee1 | 47 | |
Lugs | 0:12c56a256ee1 | 48 | int main() |
Lugs | 0:12c56a256ee1 | 49 | { |
Lugs | 0:12c56a256ee1 | 50 | |
Lugs | 0:12c56a256ee1 | 51 | daplink.printf("\f---DAPLINK SERIAL PORT---\r\n\r\nWAV PLAYER VER 2\r\n\r\n"); |
Lugs | 0:12c56a256ee1 | 52 | microUSB.printf("micro USB serial port\r\n"); |
Lugs | 0:12c56a256ee1 | 53 | rLED = LED_ON; |
Lugs | 0:12c56a256ee1 | 54 | wait_ms(500); |
Lugs | 0:12c56a256ee1 | 55 | rLED = LED_OFF; |
Lugs | 0:12c56a256ee1 | 56 | gLED = LED_ON; |
Lugs | 0:12c56a256ee1 | 57 | wait_ms(500); |
Lugs | 0:12c56a256ee1 | 58 | bLED = LED_ON; |
Lugs | 0:12c56a256ee1 | 59 | gLED = LED_OFF; |
Lugs | 0:12c56a256ee1 | 60 | |
Lugs | 0:12c56a256ee1 | 61 | Ticker SampleTime; |
Lugs | 0:12c56a256ee1 | 62 | |
Lugs | 0:12c56a256ee1 | 63 | FILE *audio = fopen("/sd/1.wav","r"); |
Lugs | 0:12c56a256ee1 | 64 | |
Lugs | 0:12c56a256ee1 | 65 | if(!audio) |
Lugs | 0:12c56a256ee1 | 66 | { |
Lugs | 0:12c56a256ee1 | 67 | printf("Failed to open file. [Exit]\r\n"); |
Lugs | 0:12c56a256ee1 | 68 | return 0; |
Lugs | 0:12c56a256ee1 | 69 | } |
Lugs | 0:12c56a256ee1 | 70 | |
Lugs | 0:12c56a256ee1 | 71 | printf("File opened.\r\n\r\n"); |
Lugs | 0:12c56a256ee1 | 72 | |
Lugs | 0:12c56a256ee1 | 73 | //find file size |
Lugs | 0:12c56a256ee1 | 74 | fseek(audio,0L,SEEK_END); |
Lugs | 0:12c56a256ee1 | 75 | Track.size = ftell(audio); |
Lugs | 0:12c56a256ee1 | 76 | |
Lugs | 0:12c56a256ee1 | 77 | printf("File Size:%li \r\n",Track.size); |
Lugs | 0:12c56a256ee1 | 78 | |
Lugs | 0:12c56a256ee1 | 79 | //read sample rate of wav file |
Lugs | 0:12c56a256ee1 | 80 | fseek(audio,22,SEEK_SET); |
Lugs | 0:12c56a256ee1 | 81 | fread(audioDataBuffer, 4, 1,audio); |
Lugs | 0:12c56a256ee1 | 82 | |
Lugs | 0:12c56a256ee1 | 83 | printf("Raw Sample Rate Data: %i\r\n",audioDataBuffer[4]); |
Lugs | 0:12c56a256ee1 | 84 | |
Lugs | 0:12c56a256ee1 | 85 | Track.channels = audioDataBuffer[0]; |
Lugs | 0:12c56a256ee1 | 86 | Track.sampleRate =(audioDataBuffer[3] << 8) + audioDataBuffer[2]; //needs to be done due to little-endian encoding of wave file headers |
Lugs | 0:12c56a256ee1 | 87 | |
Lugs | 0:12c56a256ee1 | 88 | printf("Sample Rate: %i\r\n",Track.sampleRate); |
Lugs | 0:12c56a256ee1 | 89 | printf("%i channels \r\n",Track.channels); |
Lugs | 0:12c56a256ee1 | 90 | |
Lugs | 0:12c56a256ee1 | 91 | //read bits per sample |
Lugs | 0:12c56a256ee1 | 92 | fseek(audio,34,SEEK_SET); |
Lugs | 0:12c56a256ee1 | 93 | fread(audioDataBuffer, 2, 1,audio); |
Lugs | 0:12c56a256ee1 | 94 | Track.bitsPerSample = (audioDataBuffer[1] << 8) + audioDataBuffer[0]; |
Lugs | 0:12c56a256ee1 | 95 | |
Lugs | 0:12c56a256ee1 | 96 | printf("Bits Per Sample: %i\r\n",Track.bitsPerSample); |
Lugs | 0:12c56a256ee1 | 97 | |
Lugs | 0:12c56a256ee1 | 98 | int flag; |
Lugs | 0:12c56a256ee1 | 99 | //find start of actual audio |
Lugs | 0:12c56a256ee1 | 100 | fseek(audio,44,SEEK_SET); |
Lugs | 0:12c56a256ee1 | 101 | int bytecount; |
Lugs | 0:12c56a256ee1 | 102 | bytecount = Track.size; |
Lugs | 0:12c56a256ee1 | 103 | |
Lugs | 0:12c56a256ee1 | 104 | printf("Size: %i bytes\r\nPlay time: %f minutes",Track.size,(float)((Track.size-44)*8)/Track.bitsPerSample); |
Lugs | 0:12c56a256ee1 | 105 | |
Lugs | 0:12c56a256ee1 | 106 | //initialize PWM to 1us period [1MHz] |
Lugs | 0:12c56a256ee1 | 107 | PWM.period_us(1); |
Lugs | 0:12c56a256ee1 | 108 | SampleTime.attach(&placeNewSample,1/(Track.sampleRate)); |
Lugs | 0:12c56a256ee1 | 109 | //microcontroller may not be strong enough to call fread after attaching a 44.1kHz wav file. |
Lugs | 0:12c56a256ee1 | 110 | |
Lugs | 0:12c56a256ee1 | 111 | printf("\r\nTicker attached.\r\nRunning Loop...\r\n\r\n"); |
Lugs | 0:12c56a256ee1 | 112 | |
Lugs | 0:12c56a256ee1 | 113 | //take the first block of audio data into the buffer (buffer size is two blocks) |
Lugs | 0:12c56a256ee1 | 114 | fread(audioDataBuffer,HALF_BUFFER,1,audio); |
Lugs | 0:12c56a256ee1 | 115 | |
Lugs | 0:12c56a256ee1 | 116 | int g=0; |
Lugs | 0:12c56a256ee1 | 117 | |
Lugs | 0:12c56a256ee1 | 118 | while(ftell(audio) != Track.size) |
Lugs | 0:12c56a256ee1 | 119 | { |
Lugs | 0:12c56a256ee1 | 120 | printf("Loop %i\f",g++); |
Lugs | 0:12c56a256ee1 | 121 | if(bufferPOS < HALF_BUFFER && flag == 0) |
Lugs | 0:12c56a256ee1 | 122 | { |
Lugs | 0:12c56a256ee1 | 123 | fread(audioDataBuffer + HALF_BUFFER,HALF_BUFFER,1,audio); |
Lugs | 0:12c56a256ee1 | 124 | bytecount -= HALF_BUFFER; |
Lugs | 0:12c56a256ee1 | 125 | flag = 1; |
Lugs | 0:12c56a256ee1 | 126 | } |
Lugs | 0:12c56a256ee1 | 127 | else if(bufferPOS >= HALF_BUFFER && flag == 1) |
Lugs | 0:12c56a256ee1 | 128 | { |
Lugs | 0:12c56a256ee1 | 129 | fread(audioDataBuffer,HALF_BUFFER,1,audio); |
Lugs | 0:12c56a256ee1 | 130 | bytecount -= HALF_BUFFER; |
Lugs | 0:12c56a256ee1 | 131 | flag = 0; |
Lugs | 0:12c56a256ee1 | 132 | } |
Lugs | 0:12c56a256ee1 | 133 | } |
Lugs | 0:12c56a256ee1 | 134 | }; |
Lugs | 0:12c56a256ee1 | 135 |