For MAX32630FTHR Demo Board: Plays piano notes mapped to keyboard keys interfaced through serial port (puTTY or powershell).
Dependencies: SDFileSystem max32630fthr USBDevice
Diff: main.cpp
- Revision:
- 0:ad5ce0aff429
- Child:
- 1:78ca0566c062
diff -r 000000000000 -r ad5ce0aff429 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Jul 16 02:02:52 2019 +0000 @@ -0,0 +1,101 @@ +#include "mbed.h" +#include "max32630fthr.h" +#include "USBSerial.h" +#include "stdio.h" +#include "SDFileSystem.h" + +//still needs BITS PER SAMPLE PARSING. +//do this like so: PWM.write(WavValue/2^BPS) + +#define BUFFER_SIZE 128 +#define HALF_BUFFER 64 + +DigitalOut rLED(LED1); +DigitalOut gLED(LED2); +DigitalOut bLED(LED3); + +DigitalIn Button(P2_3); + +MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); +PwmOut PWM(P5_6); +AnalogIn POT(AIN_0); +volatile int bufferPOS = 0; +volatile unsigned int g=0; + +Serial daplink(P2_1,P2_0); +USBSerial microUSB; +SDFileSystem sd(P0_5, P0_6, P0_4, P0_7, "sd"); + +float audioDataBuffer[BUFFER_SIZE]; + +struct WavFile +{ + long int size; + int channels; + int sampleRate; + int bitsPerSample; +}; + +float potval,reading; + +void placeNewSample(void) +{ + PWM.write(audioDataBuffer[bufferPOS++]*!Button); //multiply by POT value for volume. + bufferPOS = (bufferPOS+1) & 0x07F; +} + +int main() +{ + WavFile Track; + Ticker SampleTime; + + daplink.printf("\f---DAPLINK SERIAL PORT---\r\n\r\nPLAY TONE VER 26 \r\n (CHANGEABLE TONE)\r\n\r\n"); + microUSB.printf("micro USB serial port\r\n"); + rLED = LED_ON; + wait_ms(500); + rLED = LED_OFF; + gLED = LED_ON; + wait_ms(500); + bLED = LED_ON; + gLED = LED_OFF; + + printf("Generating sine...\r\n"); + int i; + for(i=0; i<128;i++) + { + audioDataBuffer[i] =((1.0 + sin((double(i)/16.0*6.28318530717959)))/2.0); //formula copied from mbed example + } + + printf("Playing tone...\r\n"); + + int PlayingFreq = 500; + + while(1) + { + + Track.sampleRate = PlayingFreq * 16; //TONE FREQ = SAMPLE RATE / SAMPLES PER CYCLE + + PWM.period_us(1); //1MHz + + float ticker_period = (float) 1/(Track.sampleRate); + + printf("\r\nTicker Period: %f\tTicker Freq: %f\r\nTarget Freq: %i\r\n\r\n",ticker_period, 1/ticker_period, PlayingFreq); + + SampleTime.attach(&placeNewSample,ticker_period); + bLED = LED_OFF; + + char c; + char number[10]; + for(i=0;i<10 && c != '\r' && c != '\n'; i++) + { + c = daplink.getc(); + number[i] = c; + } + gLED = LED_ON; + PlayingFreq = strtol(number,NULL,0); + gLED = LED_OFF; + rLED = !rLED; + SampleTime.detach(); + } +}; +