For MAX32630FTHR Demo Board: Plays piano notes mapped to keyboard keys interfaced through serial port (puTTY or powershell).
Dependencies: SDFileSystem max32630fthr USBDevice
main.cpp@1:78ca0566c062, 2019-07-16 (annotated)
- Committer:
- Lugs
- Date:
- Tue Jul 16 02:07:49 2019 +0000
- Revision:
- 1:78ca0566c062
- Parent:
- 0:ad5ce0aff429
- Child:
- 2:93da96b41127
ver 26,added docu;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lugs | 0:ad5ce0aff429 | 1 | #include "mbed.h" |
Lugs | 0:ad5ce0aff429 | 2 | #include "max32630fthr.h" |
Lugs | 0:ad5ce0aff429 | 3 | #include "USBSerial.h" |
Lugs | 0:ad5ce0aff429 | 4 | #include "stdio.h" |
Lugs | 0:ad5ce0aff429 | 5 | #include "SDFileSystem.h" |
Lugs | 0:ad5ce0aff429 | 6 | |
Lugs | 0:ad5ce0aff429 | 7 | //still needs BITS PER SAMPLE PARSING. |
Lugs | 0:ad5ce0aff429 | 8 | //do this like so: PWM.write(WavValue/2^BPS) |
Lugs | 0:ad5ce0aff429 | 9 | |
Lugs | 0:ad5ce0aff429 | 10 | #define BUFFER_SIZE 128 |
Lugs | 0:ad5ce0aff429 | 11 | #define HALF_BUFFER 64 |
Lugs | 0:ad5ce0aff429 | 12 | |
Lugs | 0:ad5ce0aff429 | 13 | DigitalOut rLED(LED1); |
Lugs | 0:ad5ce0aff429 | 14 | DigitalOut gLED(LED2); |
Lugs | 0:ad5ce0aff429 | 15 | DigitalOut bLED(LED3); |
Lugs | 0:ad5ce0aff429 | 16 | |
Lugs | 0:ad5ce0aff429 | 17 | DigitalIn Button(P2_3); |
Lugs | 0:ad5ce0aff429 | 18 | |
Lugs | 0:ad5ce0aff429 | 19 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
Lugs | 0:ad5ce0aff429 | 20 | PwmOut PWM(P5_6); |
Lugs | 0:ad5ce0aff429 | 21 | AnalogIn POT(AIN_0); |
Lugs | 0:ad5ce0aff429 | 22 | volatile int bufferPOS = 0; |
Lugs | 0:ad5ce0aff429 | 23 | volatile unsigned int g=0; |
Lugs | 0:ad5ce0aff429 | 24 | |
Lugs | 0:ad5ce0aff429 | 25 | Serial daplink(P2_1,P2_0); |
Lugs | 0:ad5ce0aff429 | 26 | USBSerial microUSB; |
Lugs | 0:ad5ce0aff429 | 27 | SDFileSystem sd(P0_5, P0_6, P0_4, P0_7, "sd"); |
Lugs | 0:ad5ce0aff429 | 28 | |
Lugs | 0:ad5ce0aff429 | 29 | float audioDataBuffer[BUFFER_SIZE]; |
Lugs | 0:ad5ce0aff429 | 30 | |
Lugs | 0:ad5ce0aff429 | 31 | struct WavFile |
Lugs | 0:ad5ce0aff429 | 32 | { |
Lugs | 0:ad5ce0aff429 | 33 | long int size; |
Lugs | 0:ad5ce0aff429 | 34 | int channels; |
Lugs | 0:ad5ce0aff429 | 35 | int sampleRate; |
Lugs | 0:ad5ce0aff429 | 36 | int bitsPerSample; |
Lugs | 0:ad5ce0aff429 | 37 | }; |
Lugs | 0:ad5ce0aff429 | 38 | |
Lugs | 0:ad5ce0aff429 | 39 | float potval,reading; |
Lugs | 0:ad5ce0aff429 | 40 | |
Lugs | 0:ad5ce0aff429 | 41 | void placeNewSample(void) |
Lugs | 0:ad5ce0aff429 | 42 | { |
Lugs | 0:ad5ce0aff429 | 43 | PWM.write(audioDataBuffer[bufferPOS++]*!Button); //multiply by POT value for volume. |
Lugs | 0:ad5ce0aff429 | 44 | bufferPOS = (bufferPOS+1) & 0x07F; |
Lugs | 0:ad5ce0aff429 | 45 | } |
Lugs | 0:ad5ce0aff429 | 46 | |
Lugs | 0:ad5ce0aff429 | 47 | int main() |
Lugs | 0:ad5ce0aff429 | 48 | { |
Lugs | 0:ad5ce0aff429 | 49 | WavFile Track; |
Lugs | 0:ad5ce0aff429 | 50 | Ticker SampleTime; |
Lugs | 0:ad5ce0aff429 | 51 | |
Lugs | 1:78ca0566c062 | 52 | daplink.printf("\f---DAPLINK SERIAL PORT---\r\n\r\nPLAY TONE VER 26 \r\n (CHANGEABLE TONE) MAX FREQUENCY: 1680. \r\n AFTER 1680Hz INPUTS CANNOT BE PROCESSED.\r\n\r\n"); |
Lugs | 0:ad5ce0aff429 | 53 | microUSB.printf("micro USB serial port\r\n"); |
Lugs | 0:ad5ce0aff429 | 54 | rLED = LED_ON; |
Lugs | 0:ad5ce0aff429 | 55 | wait_ms(500); |
Lugs | 0:ad5ce0aff429 | 56 | rLED = LED_OFF; |
Lugs | 0:ad5ce0aff429 | 57 | gLED = LED_ON; |
Lugs | 0:ad5ce0aff429 | 58 | wait_ms(500); |
Lugs | 0:ad5ce0aff429 | 59 | bLED = LED_ON; |
Lugs | 0:ad5ce0aff429 | 60 | gLED = LED_OFF; |
Lugs | 0:ad5ce0aff429 | 61 | |
Lugs | 0:ad5ce0aff429 | 62 | printf("Generating sine...\r\n"); |
Lugs | 0:ad5ce0aff429 | 63 | int i; |
Lugs | 0:ad5ce0aff429 | 64 | for(i=0; i<128;i++) |
Lugs | 0:ad5ce0aff429 | 65 | { |
Lugs | 0:ad5ce0aff429 | 66 | audioDataBuffer[i] =((1.0 + sin((double(i)/16.0*6.28318530717959)))/2.0); //formula copied from mbed example |
Lugs | 0:ad5ce0aff429 | 67 | } |
Lugs | 0:ad5ce0aff429 | 68 | |
Lugs | 0:ad5ce0aff429 | 69 | printf("Playing tone...\r\n"); |
Lugs | 0:ad5ce0aff429 | 70 | |
Lugs | 0:ad5ce0aff429 | 71 | int PlayingFreq = 500; |
Lugs | 0:ad5ce0aff429 | 72 | |
Lugs | 0:ad5ce0aff429 | 73 | while(1) |
Lugs | 0:ad5ce0aff429 | 74 | { |
Lugs | 0:ad5ce0aff429 | 75 | |
Lugs | 0:ad5ce0aff429 | 76 | Track.sampleRate = PlayingFreq * 16; //TONE FREQ = SAMPLE RATE / SAMPLES PER CYCLE |
Lugs | 0:ad5ce0aff429 | 77 | |
Lugs | 0:ad5ce0aff429 | 78 | PWM.period_us(1); //1MHz |
Lugs | 0:ad5ce0aff429 | 79 | |
Lugs | 0:ad5ce0aff429 | 80 | float ticker_period = (float) 1/(Track.sampleRate); |
Lugs | 0:ad5ce0aff429 | 81 | |
Lugs | 0:ad5ce0aff429 | 82 | printf("\r\nTicker Period: %f\tTicker Freq: %f\r\nTarget Freq: %i\r\n\r\n",ticker_period, 1/ticker_period, PlayingFreq); |
Lugs | 0:ad5ce0aff429 | 83 | |
Lugs | 0:ad5ce0aff429 | 84 | SampleTime.attach(&placeNewSample,ticker_period); |
Lugs | 0:ad5ce0aff429 | 85 | bLED = LED_OFF; |
Lugs | 0:ad5ce0aff429 | 86 | |
Lugs | 0:ad5ce0aff429 | 87 | char c; |
Lugs | 0:ad5ce0aff429 | 88 | char number[10]; |
Lugs | 0:ad5ce0aff429 | 89 | for(i=0;i<10 && c != '\r' && c != '\n'; i++) |
Lugs | 0:ad5ce0aff429 | 90 | { |
Lugs | 0:ad5ce0aff429 | 91 | c = daplink.getc(); |
Lugs | 0:ad5ce0aff429 | 92 | number[i] = c; |
Lugs | 0:ad5ce0aff429 | 93 | } |
Lugs | 1:78ca0566c062 | 94 | |
Lugs | 0:ad5ce0aff429 | 95 | gLED = LED_ON; |
Lugs | 0:ad5ce0aff429 | 96 | PlayingFreq = strtol(number,NULL,0); |
Lugs | 0:ad5ce0aff429 | 97 | gLED = LED_OFF; |
Lugs | 0:ad5ce0aff429 | 98 | rLED = !rLED; |
Lugs | 0:ad5ce0aff429 | 99 | SampleTime.detach(); |
Lugs | 0:ad5ce0aff429 | 100 | } |
Lugs | 0:ad5ce0aff429 | 101 | }; |
Lugs | 0:ad5ce0aff429 | 102 |