For MAX323630FTHR: Plays a WAV file in the SD card. Interfaced through serial port using puTTY or powershell.

Dependencies:   USBMSD_BD SDFileSystem max32630fthr USBDevice

Committer:
Lugs
Date:
Sat Nov 09 01:33:17 2019 +0000
Revision:
3:2ddba0146fd8
Parent:
2:d4f9c8c25fa6
Removed demoboard library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Lugs 0:12c56a256ee1 1 #include "mbed.h"
Lugs 0:12c56a256ee1 2 #include "max32630fthr.h"
Lugs 0:12c56a256ee1 3 #include "stdio.h"
Lugs 1:7884bc0fb012 4 #include "ctype.h"
Lugs 1:7884bc0fb012 5 #include "generalinterface.h"
Lugs 0:12c56a256ee1 6 #define BUFFER_SIZE 1024
Lugs 0:12c56a256ee1 7 #define HALF_BUFFER 512
Lugs 0:12c56a256ee1 8
Lugs 1:7884bc0fb012 9 bool debugState = 1;
Lugs 1:7884bc0fb012 10
Lugs 0:12c56a256ee1 11 DigitalOut rLED(LED1);
Lugs 0:12c56a256ee1 12 DigitalOut gLED(LED2);
Lugs 0:12c56a256ee1 13 DigitalOut bLED(LED3);
Lugs 0:12c56a256ee1 14
Lugs 0:12c56a256ee1 15 DigitalIn Button(P2_3);
Lugs 0:12c56a256ee1 16
Lugs 0:12c56a256ee1 17 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
Lugs 1:7884bc0fb012 18 PwmOut PWM(P4_0);
Lugs 0:12c56a256ee1 19 AnalogIn POT(AIN_0);
Lugs 1:7884bc0fb012 20 int i;
Lugs 0:12c56a256ee1 21 volatile int bufferPOS = 0;
Lugs 1:7884bc0fb012 22 short audioDataBuffer[BUFFER_SIZE];
Lugs 1:7884bc0fb012 23 struct WavFile {
Lugs 0:12c56a256ee1 24 long int size;
Lugs 0:12c56a256ee1 25 int channels;
Lugs 0:12c56a256ee1 26 int sampleRate;
Lugs 0:12c56a256ee1 27 int bitsPerSample;
Lugs 0:12c56a256ee1 28 };
Lugs 0:12c56a256ee1 29
Lugs 1:7884bc0fb012 30 float maxSampleVal=1;
Lugs 0:12c56a256ee1 31 WavFile Track;
Lugs 1:7884bc0fb012 32 Ticker SampleTime;
Lugs 1:7884bc0fb012 33
Lugs 1:7884bc0fb012 34 DigitalIn db1(P3_3);
Lugs 2:d4f9c8c25fa6 35 float potval;
Lugs 0:12c56a256ee1 36
Lugs 0:12c56a256ee1 37 void placeNewSample(void)
Lugs 0:12c56a256ee1 38 {
Lugs 2:d4f9c8c25fa6 39 PWM.write((((float)audioDataBuffer[bufferPOS]*potval)/maxSampleVal)+0.5); //multiply by POT value for volume.
Lugs 1:7884bc0fb012 40 bufferPOS = (bufferPOS+2)%BUFFER_SIZE;
Lugs 1:7884bc0fb012 41 //if(!Button)
Lugs 1:7884bc0fb012 42 //{
Lugs 1:7884bc0fb012 43 // SampleTime.detach();
Lugs 1:7884bc0fb012 44 //}
Lugs 1:7884bc0fb012 45 //printf("%i/%f = %f\r\n",audioDataBuffer[bufferPOS],(maxSampleVal),((float)audioDataBuffer[bufferPOS])/maxSampleVal)+1;
Lugs 0:12c56a256ee1 46 }
Lugs 0:12c56a256ee1 47
Lugs 1:7884bc0fb012 48 void presence()
Lugs 1:7884bc0fb012 49 {
Lugs 0:12c56a256ee1 50 rLED = LED_ON;
Lugs 0:12c56a256ee1 51 wait_ms(500);
Lugs 0:12c56a256ee1 52 rLED = LED_OFF;
Lugs 0:12c56a256ee1 53 gLED = LED_ON;
Lugs 0:12c56a256ee1 54 wait_ms(500);
Lugs 0:12c56a256ee1 55 bLED = LED_ON;
Lugs 0:12c56a256ee1 56 gLED = LED_OFF;
Lugs 1:7884bc0fb012 57 }
Lugs 1:7884bc0fb012 58
Lugs 1:7884bc0fb012 59 int main()
Lugs 1:7884bc0fb012 60 {
Lugs 1:7884bc0fb012 61 startFileSystem();
Lugs 1:7884bc0fb012 62
Lugs 1:7884bc0fb012 63 daplink.printf("\f---DAPLINK SERIAL PORT---\r\n\r\nWAV PLAYER VER 2\r\n\r\n");
Lugs 1:7884bc0fb012 64 microUSB.printf("micro USB serial port\r\n");
Lugs 1:7884bc0fb012 65
Lugs 1:7884bc0fb012 66 presence();
Lugs 1:7884bc0fb012 67
Lugs 1:7884bc0fb012 68 restart:
Lugs 1:7884bc0fb012 69
Lugs 1:7884bc0fb012 70 char *title = new char[24];
Lugs 1:7884bc0fb012 71 title[0] = '/';
Lugs 1:7884bc0fb012 72 title[1] = 'f';
Lugs 1:7884bc0fb012 73 title[2] = 's';
Lugs 1:7884bc0fb012 74 title[3] = '/';
Lugs 1:7884bc0fb012 75 FILE *audio;
Lugs 1:7884bc0fb012 76 while(1) {
Lugs 1:7884bc0fb012 77 printf("Please input filename: ");
Lugs 1:7884bc0fb012 78 char *inputptr = title+4;
Lugs 1:7884bc0fb012 79 if(!getInput(24,inputptr)) {
Lugs 3:2ddba0146fd8 80 printf("Filenames cannot be more than 20 characters.\r\n");
Lugs 1:7884bc0fb012 81 }
Lugs 1:7884bc0fb012 82 audio = fopen(title,"r");
Lugs 1:7884bc0fb012 83 if(audio == NULL) {
Lugs 3:2ddba0146fd8 84 printf("File not found. Please append filetype at the end of filename.\r\n");
Lugs 1:7884bc0fb012 85 continue;
Lugs 1:7884bc0fb012 86 }
Lugs 1:7884bc0fb012 87 break;
Lugs 0:12c56a256ee1 88 }
Lugs 1:7884bc0fb012 89 printf("\r\nFile opened.\r\n\r\n");
Lugs 1:7884bc0fb012 90
Lugs 0:12c56a256ee1 91 //find file size
Lugs 0:12c56a256ee1 92 fseek(audio,0L,SEEK_END);
Lugs 0:12c56a256ee1 93 Track.size = ftell(audio);
Lugs 1:7884bc0fb012 94
Lugs 0:12c56a256ee1 95 printf("File Size:%li \r\n",Track.size);
Lugs 1:7884bc0fb012 96
Lugs 1:7884bc0fb012 97 //read sample rate and channels of wav file
Lugs 0:12c56a256ee1 98 fseek(audio,22,SEEK_SET);
Lugs 1:7884bc0fb012 99 fread(&Track.channels, 2, 1,audio);
Lugs 1:7884bc0fb012 100 fread(&Track.sampleRate, 2, 1,audio);
Lugs 0:12c56a256ee1 101 printf("Sample Rate: %i\r\n",Track.sampleRate);
Lugs 0:12c56a256ee1 102 printf("%i channels \r\n",Track.channels);
Lugs 1:7884bc0fb012 103 if(Track.sampleRate >= 20000) {
Lugs 1:7884bc0fb012 104 printf("WARNING: Microcontroller may not be able to play sample rates higher than 20kHz properly.\r\n");
Lugs 1:7884bc0fb012 105 }
Lugs 1:7884bc0fb012 106
Lugs 0:12c56a256ee1 107 //read bits per sample
Lugs 0:12c56a256ee1 108 fseek(audio,34,SEEK_SET);
Lugs 0:12c56a256ee1 109 fread(audioDataBuffer, 2, 1,audio);
Lugs 0:12c56a256ee1 110 Track.bitsPerSample = (audioDataBuffer[1] << 8) + audioDataBuffer[0];
Lugs 0:12c56a256ee1 111 printf("Bits Per Sample: %i\r\n",Track.bitsPerSample);
Lugs 1:7884bc0fb012 112 for(int i=0;i<Track.bitsPerSample;i++)
Lugs 1:7884bc0fb012 113 {
Lugs 1:7884bc0fb012 114 maxSampleVal *= 2;
Lugs 1:7884bc0fb012 115 }
Lugs 1:7884bc0fb012 116 maxSampleVal/=2;
Lugs 1:7884bc0fb012 117
Lugs 0:12c56a256ee1 118 int flag;
Lugs 0:12c56a256ee1 119 //find start of actual audio
Lugs 0:12c56a256ee1 120 fseek(audio,44,SEEK_SET);
Lugs 1:7884bc0fb012 121
Lugs 1:7884bc0fb012 122 printf("Size: %i bytes\r\n",Track.size);
Lugs 1:7884bc0fb012 123
Lugs 3:2ddba0146fd8 124 PWM.period_us(20);
Lugs 1:7884bc0fb012 125 short size = sizeof(short);
Lugs 0:12c56a256ee1 126
Lugs 1:7884bc0fb012 127 fread((void *)audioDataBuffer,size,HALF_BUFFER,audio);
Lugs 1:7884bc0fb012 128 printf("Sample time will be attached with period %f.",1.0/(double)(Track.sampleRate));
Lugs 1:7884bc0fb012 129 SampleTime.attach(&placeNewSample,1.0/(double)(Track.sampleRate));
Lugs 1:7884bc0fb012 130 //microcontroller may not be strong enough to call fread after attaching a 44.1kHz wav file.
Lugs 1:7884bc0fb012 131 printf("\r\nTicker attached.\r\nRunning buffer loop...\r\n\r\n");
Lugs 0:12c56a256ee1 132
Lugs 1:7884bc0fb012 133 //take the first block of audio data into the buffer (buffer size is two blocks)
Lugs 1:7884bc0fb012 134 flag = 0;
Lugs 1:7884bc0fb012 135 while(ftell(audio) < Track.size) {
Lugs 2:d4f9c8c25fa6 136 potval = POT.read()*2;
Lugs 1:7884bc0fb012 137 if((bufferPOS < HALF_BUFFER) && flag == 0) {
Lugs 1:7884bc0fb012 138 fread((void *)(audioDataBuffer + HALF_BUFFER),size,HALF_BUFFER,audio);
Lugs 1:7884bc0fb012 139 flag = !flag;
Lugs 1:7884bc0fb012 140 } else if((bufferPOS >= HALF_BUFFER) && flag == 1) {
Lugs 1:7884bc0fb012 141 fread((void *)audioDataBuffer,size,HALF_BUFFER,audio);
Lugs 1:7884bc0fb012 142 flag = !flag;
Lugs 1:7884bc0fb012 143 }
Lugs 1:7884bc0fb012 144 }
Lugs 1:7884bc0fb012 145 SampleTime.detach();
Lugs 1:7884bc0fb012 146 bLED=LED_OFF;
Lugs 1:7884bc0fb012 147 rLED=LED_ON;
Lugs 1:7884bc0fb012 148 while(1)
Lugs 0:12c56a256ee1 149 {
Lugs 1:7884bc0fb012 150 if(!db1)
Lugs 0:12c56a256ee1 151 {
Lugs 1:7884bc0fb012 152 goto restart;
Lugs 0:12c56a256ee1 153 }
Lugs 0:12c56a256ee1 154 }
Lugs 0:12c56a256ee1 155 };
Lugs 0:12c56a256ee1 156