For MAX32630FTHR Demo Board: Plays piano notes mapped to keyboard keys interfaced through serial port (puTTY or powershell).
Dependencies: SDFileSystem max32630fthr USBDevice
main.cpp@2:93da96b41127, 2019-07-16 (annotated)
- Committer:
- Lugs
- Date:
- Tue Jul 16 09:37:41 2019 +0000
- Revision:
- 2:93da96b41127
- Parent:
- 1:78ca0566c062
- Child:
- 3:3a4d2d10e298
1 to L. Can turn on and off with enter.
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 | 2:93da96b41127 | 43 | PWM.write(audioDataBuffer[bufferPOS++]); //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 | 2:93da96b41127 | 52 | daplink.printf("\f---DAPLINK SERIAL PORT---\r\n\r\nMINI PIANO ver 1 \r\nMAKE SURE YOUR PUTTY IS SET TO NOT WAIT FOR [ENTER] \r\nMIDDLE A (A4) (440Hz) IS Y, [WHITE] KEYS VALID FROM 1 to L. \r\nPRESS SHIFT FOR SHARP\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 | 2:93da96b41127 | 71 | int PlayingFreq = 440, attached = 0; |
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 | 2:93da96b41127 | 84 | if(attached) |
Lugs | 2:93da96b41127 | 85 | { |
Lugs | 2:93da96b41127 | 86 | SampleTime.attach(&placeNewSample,ticker_period); |
Lugs | 2:93da96b41127 | 87 | } |
Lugs | 2:93da96b41127 | 88 | |
Lugs | 0:ad5ce0aff429 | 89 | bLED = LED_OFF; |
Lugs | 0:ad5ce0aff429 | 90 | |
Lugs | 0:ad5ce0aff429 | 91 | char c; |
Lugs | 2:93da96b41127 | 92 | c = daplink.getc(); |
Lugs | 2:93da96b41127 | 93 | |
Lugs | 2:93da96b41127 | 94 | gLED = LED_ON; |
Lugs | 2:93da96b41127 | 95 | |
Lugs | 2:93da96b41127 | 96 | switch(c) |
Lugs | 0:ad5ce0aff429 | 97 | { |
Lugs | 2:93da96b41127 | 98 | case '1': |
Lugs | 2:93da96b41127 | 99 | PlayingFreq = 98; |
Lugs | 2:93da96b41127 | 100 | break; |
Lugs | 2:93da96b41127 | 101 | case '!': |
Lugs | 2:93da96b41127 | 102 | PlayingFreq = 404; |
Lugs | 2:93da96b41127 | 103 | break; |
Lugs | 2:93da96b41127 | 104 | case '2': |
Lugs | 2:93da96b41127 | 105 | PlayingFreq = 110; |
Lugs | 2:93da96b41127 | 106 | break; |
Lugs | 2:93da96b41127 | 107 | case '@': |
Lugs | 2:93da96b41127 | 108 | PlayingFreq = 117; |
Lugs | 2:93da96b41127 | 109 | break; |
Lugs | 2:93da96b41127 | 110 | case '3': |
Lugs | 2:93da96b41127 | 111 | PlayingFreq = 123; |
Lugs | 2:93da96b41127 | 112 | break; |
Lugs | 2:93da96b41127 | 113 | case '4': |
Lugs | 2:93da96b41127 | 114 | PlayingFreq = 131; |
Lugs | 2:93da96b41127 | 115 | break; |
Lugs | 2:93da96b41127 | 116 | case '$': |
Lugs | 2:93da96b41127 | 117 | PlayingFreq = 139; |
Lugs | 2:93da96b41127 | 118 | break; |
Lugs | 2:93da96b41127 | 119 | case '5': |
Lugs | 2:93da96b41127 | 120 | PlayingFreq = 147; |
Lugs | 2:93da96b41127 | 121 | break; |
Lugs | 2:93da96b41127 | 122 | case '%': |
Lugs | 2:93da96b41127 | 123 | PlayingFreq = 156; |
Lugs | 2:93da96b41127 | 124 | break; |
Lugs | 2:93da96b41127 | 125 | case '6': |
Lugs | 2:93da96b41127 | 126 | PlayingFreq = 165; |
Lugs | 2:93da96b41127 | 127 | break; |
Lugs | 2:93da96b41127 | 128 | case '7': |
Lugs | 2:93da96b41127 | 129 | PlayingFreq = 175; |
Lugs | 2:93da96b41127 | 130 | break; |
Lugs | 2:93da96b41127 | 131 | case '&': |
Lugs | 2:93da96b41127 | 132 | PlayingFreq = 185; |
Lugs | 2:93da96b41127 | 133 | break; |
Lugs | 2:93da96b41127 | 134 | case '8': |
Lugs | 2:93da96b41127 | 135 | PlayingFreq = 196; |
Lugs | 2:93da96b41127 | 136 | break; |
Lugs | 2:93da96b41127 | 137 | case '*': |
Lugs | 2:93da96b41127 | 138 | PlayingFreq = 208; |
Lugs | 2:93da96b41127 | 139 | break; |
Lugs | 2:93da96b41127 | 140 | case '9': |
Lugs | 2:93da96b41127 | 141 | PlayingFreq = 220; |
Lugs | 2:93da96b41127 | 142 | break; |
Lugs | 2:93da96b41127 | 143 | case '(': |
Lugs | 2:93da96b41127 | 144 | PlayingFreq = 233; |
Lugs | 2:93da96b41127 | 145 | break; |
Lugs | 2:93da96b41127 | 146 | case '0': |
Lugs | 2:93da96b41127 | 147 | PlayingFreq = 247; |
Lugs | 2:93da96b41127 | 148 | break; |
Lugs | 2:93da96b41127 | 149 | case 'q': |
Lugs | 2:93da96b41127 | 150 | PlayingFreq = 262; |
Lugs | 2:93da96b41127 | 151 | break; |
Lugs | 2:93da96b41127 | 152 | case 'Q': |
Lugs | 2:93da96b41127 | 153 | PlayingFreq = 277; |
Lugs | 2:93da96b41127 | 154 | break; |
Lugs | 2:93da96b41127 | 155 | case 'w': |
Lugs | 2:93da96b41127 | 156 | PlayingFreq = 294; |
Lugs | 2:93da96b41127 | 157 | break; |
Lugs | 2:93da96b41127 | 158 | case 'W': |
Lugs | 2:93da96b41127 | 159 | PlayingFreq = 311; |
Lugs | 2:93da96b41127 | 160 | break; |
Lugs | 2:93da96b41127 | 161 | case 'e': |
Lugs | 2:93da96b41127 | 162 | PlayingFreq = 330; |
Lugs | 2:93da96b41127 | 163 | break; |
Lugs | 2:93da96b41127 | 164 | case 'r': |
Lugs | 2:93da96b41127 | 165 | PlayingFreq = 349; |
Lugs | 2:93da96b41127 | 166 | break; |
Lugs | 2:93da96b41127 | 167 | case 'R': |
Lugs | 2:93da96b41127 | 168 | PlayingFreq = 370; |
Lugs | 2:93da96b41127 | 169 | break; |
Lugs | 2:93da96b41127 | 170 | case 't': |
Lugs | 2:93da96b41127 | 171 | PlayingFreq = 392; |
Lugs | 2:93da96b41127 | 172 | break; |
Lugs | 2:93da96b41127 | 173 | case 'T': |
Lugs | 2:93da96b41127 | 174 | PlayingFreq = 415; |
Lugs | 2:93da96b41127 | 175 | break; |
Lugs | 2:93da96b41127 | 176 | case 'y': |
Lugs | 2:93da96b41127 | 177 | PlayingFreq = 440; |
Lugs | 2:93da96b41127 | 178 | break; |
Lugs | 2:93da96b41127 | 179 | case 'Y': |
Lugs | 2:93da96b41127 | 180 | PlayingFreq = 466; |
Lugs | 2:93da96b41127 | 181 | break; |
Lugs | 2:93da96b41127 | 182 | case 'u': |
Lugs | 2:93da96b41127 | 183 | PlayingFreq = 494; |
Lugs | 2:93da96b41127 | 184 | break; |
Lugs | 2:93da96b41127 | 185 | case 'i': |
Lugs | 2:93da96b41127 | 186 | PlayingFreq = 523; |
Lugs | 2:93da96b41127 | 187 | break; |
Lugs | 2:93da96b41127 | 188 | case 'I': |
Lugs | 2:93da96b41127 | 189 | PlayingFreq = 554; |
Lugs | 2:93da96b41127 | 190 | break; |
Lugs | 2:93da96b41127 | 191 | case 'o': |
Lugs | 2:93da96b41127 | 192 | PlayingFreq = 587; |
Lugs | 2:93da96b41127 | 193 | break; |
Lugs | 2:93da96b41127 | 194 | case 'O': |
Lugs | 2:93da96b41127 | 195 | PlayingFreq = 622; |
Lugs | 2:93da96b41127 | 196 | break; |
Lugs | 2:93da96b41127 | 197 | case 'p': |
Lugs | 2:93da96b41127 | 198 | PlayingFreq = 659; |
Lugs | 2:93da96b41127 | 199 | break; |
Lugs | 2:93da96b41127 | 200 | case 'a': |
Lugs | 2:93da96b41127 | 201 | PlayingFreq = 698; |
Lugs | 2:93da96b41127 | 202 | break; |
Lugs | 2:93da96b41127 | 203 | case 'A': |
Lugs | 2:93da96b41127 | 204 | PlayingFreq = 740; |
Lugs | 2:93da96b41127 | 205 | break; |
Lugs | 2:93da96b41127 | 206 | case 's': |
Lugs | 2:93da96b41127 | 207 | PlayingFreq = 784; |
Lugs | 2:93da96b41127 | 208 | break; |
Lugs | 2:93da96b41127 | 209 | case 'S': |
Lugs | 2:93da96b41127 | 210 | PlayingFreq = 831; |
Lugs | 2:93da96b41127 | 211 | break; |
Lugs | 2:93da96b41127 | 212 | case 'd': |
Lugs | 2:93da96b41127 | 213 | PlayingFreq = 880; |
Lugs | 2:93da96b41127 | 214 | break; |
Lugs | 2:93da96b41127 | 215 | case 'D': |
Lugs | 2:93da96b41127 | 216 | PlayingFreq = 932; |
Lugs | 2:93da96b41127 | 217 | break; |
Lugs | 2:93da96b41127 | 218 | case 'f': |
Lugs | 2:93da96b41127 | 219 | PlayingFreq = 988; |
Lugs | 2:93da96b41127 | 220 | break; |
Lugs | 2:93da96b41127 | 221 | case 'g': |
Lugs | 2:93da96b41127 | 222 | PlayingFreq = 1047; |
Lugs | 2:93da96b41127 | 223 | break; |
Lugs | 2:93da96b41127 | 224 | case 'G': |
Lugs | 2:93da96b41127 | 225 | PlayingFreq = 1109; |
Lugs | 2:93da96b41127 | 226 | break; |
Lugs | 2:93da96b41127 | 227 | case 'h': |
Lugs | 2:93da96b41127 | 228 | PlayingFreq = 1175; |
Lugs | 2:93da96b41127 | 229 | break; |
Lugs | 2:93da96b41127 | 230 | case 'H': |
Lugs | 2:93da96b41127 | 231 | PlayingFreq = 1245; |
Lugs | 2:93da96b41127 | 232 | break; |
Lugs | 2:93da96b41127 | 233 | case 'j': |
Lugs | 2:93da96b41127 | 234 | PlayingFreq = 1319; |
Lugs | 2:93da96b41127 | 235 | break; |
Lugs | 2:93da96b41127 | 236 | case 'k': |
Lugs | 2:93da96b41127 | 237 | PlayingFreq = 1397; |
Lugs | 2:93da96b41127 | 238 | break; |
Lugs | 2:93da96b41127 | 239 | case 'K': |
Lugs | 2:93da96b41127 | 240 | PlayingFreq = 1480; |
Lugs | 2:93da96b41127 | 241 | break; |
Lugs | 2:93da96b41127 | 242 | case 'l': |
Lugs | 2:93da96b41127 | 243 | PlayingFreq = 1568; |
Lugs | 2:93da96b41127 | 244 | break; |
Lugs | 2:93da96b41127 | 245 | case 'L': |
Lugs | 2:93da96b41127 | 246 | PlayingFreq = 1661; |
Lugs | 2:93da96b41127 | 247 | break; |
Lugs | 2:93da96b41127 | 248 | case '\r': |
Lugs | 2:93da96b41127 | 249 | attached = !attached; |
Lugs | 2:93da96b41127 | 250 | break; |
Lugs | 2:93da96b41127 | 251 | } |
Lugs | 2:93da96b41127 | 252 | |
Lugs | 2:93da96b41127 | 253 | gLED = LED_OFF; |
Lugs | 2:93da96b41127 | 254 | rLED = !rLED; |
Lugs | 2:93da96b41127 | 255 | if(attached) |
Lugs | 2:93da96b41127 | 256 | { |
Lugs | 2:93da96b41127 | 257 | SampleTime.detach(); |
Lugs | 0:ad5ce0aff429 | 258 | } |
Lugs | 1:78ca0566c062 | 259 | |
Lugs | 2:93da96b41127 | 260 | printf("\033[A\033[A\033[A\033[A"); |
Lugs | 0:ad5ce0aff429 | 261 | } |
Lugs | 0:ad5ce0aff429 | 262 | }; |
Lugs | 0:ad5ce0aff429 | 263 |