For MAX32630FTHR Demo Board: Plays piano notes mapped to keyboard keys interfaced through serial port (puTTY or powershell).

Dependencies:   SDFileSystem max32630fthr USBDevice

Committer:
Lugs
Date:
Mon Jul 29 01:08:41 2019 +0000
Revision:
4:06115dc377f7
Parent:
3:3a4d2d10e298
presentation version

Who changed what in which revision?

UserRevisionLine numberNew 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 3:3a4d2d10e298 6 #include "ctype.h"
Lugs 0:ad5ce0aff429 7
Lugs 0:ad5ce0aff429 8 //still needs BITS PER SAMPLE PARSING.
Lugs 0:ad5ce0aff429 9 //do this like so: PWM.write(WavValue/2^BPS)
Lugs 0:ad5ce0aff429 10
Lugs 3:3a4d2d10e298 11 //MAPPING IS WRONG. P = 440 PLZ. Y = MIDDLE D.
Lugs 3:3a4d2d10e298 12
Lugs 0:ad5ce0aff429 13 #define BUFFER_SIZE 128
Lugs 0:ad5ce0aff429 14 #define HALF_BUFFER 64
Lugs 0:ad5ce0aff429 15
Lugs 0:ad5ce0aff429 16 DigitalOut rLED(LED1);
Lugs 0:ad5ce0aff429 17 DigitalOut gLED(LED2);
Lugs 0:ad5ce0aff429 18 DigitalOut bLED(LED3);
Lugs 0:ad5ce0aff429 19
Lugs 0:ad5ce0aff429 20 DigitalIn Button(P2_3);
Lugs 0:ad5ce0aff429 21
Lugs 0:ad5ce0aff429 22 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
Lugs 4:06115dc377f7 23 PwmOut PWM(P4_0);
Lugs 0:ad5ce0aff429 24 AnalogIn POT(AIN_0);
Lugs 0:ad5ce0aff429 25 volatile int bufferPOS = 0;
Lugs 0:ad5ce0aff429 26 volatile unsigned int g=0;
Lugs 0:ad5ce0aff429 27
Lugs 0:ad5ce0aff429 28 Serial daplink(P2_1,P2_0);
Lugs 0:ad5ce0aff429 29 USBSerial microUSB;
Lugs 0:ad5ce0aff429 30 SDFileSystem sd(P0_5, P0_6, P0_4, P0_7, "sd");
Lugs 0:ad5ce0aff429 31
Lugs 0:ad5ce0aff429 32 float audioDataBuffer[BUFFER_SIZE];
Lugs 0:ad5ce0aff429 33
Lugs 0:ad5ce0aff429 34 struct WavFile
Lugs 0:ad5ce0aff429 35 {
Lugs 0:ad5ce0aff429 36 long int size;
Lugs 0:ad5ce0aff429 37 int channels;
Lugs 0:ad5ce0aff429 38 int sampleRate;
Lugs 0:ad5ce0aff429 39 int bitsPerSample;
Lugs 0:ad5ce0aff429 40 };
Lugs 0:ad5ce0aff429 41
Lugs 0:ad5ce0aff429 42 float potval,reading;
Lugs 0:ad5ce0aff429 43
Lugs 0:ad5ce0aff429 44 void placeNewSample(void)
Lugs 0:ad5ce0aff429 45 {
Lugs 2:93da96b41127 46 PWM.write(audioDataBuffer[bufferPOS++]); //multiply by POT value for volume.
Lugs 0:ad5ce0aff429 47 bufferPOS = (bufferPOS+1) & 0x07F;
Lugs 0:ad5ce0aff429 48 }
Lugs 0:ad5ce0aff429 49
Lugs 0:ad5ce0aff429 50 int main()
Lugs 0:ad5ce0aff429 51 {
Lugs 0:ad5ce0aff429 52 WavFile Track;
Lugs 0:ad5ce0aff429 53 Ticker SampleTime;
Lugs 0:ad5ce0aff429 54
Lugs 2:93da96b41127 55 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 56 microUSB.printf("micro USB serial port\r\n");
Lugs 0:ad5ce0aff429 57 rLED = LED_ON;
Lugs 0:ad5ce0aff429 58 wait_ms(500);
Lugs 0:ad5ce0aff429 59 rLED = LED_OFF;
Lugs 0:ad5ce0aff429 60 gLED = LED_ON;
Lugs 0:ad5ce0aff429 61 wait_ms(500);
Lugs 0:ad5ce0aff429 62 bLED = LED_ON;
Lugs 0:ad5ce0aff429 63 gLED = LED_OFF;
Lugs 0:ad5ce0aff429 64
Lugs 0:ad5ce0aff429 65 printf("Generating sine...\r\n");
Lugs 0:ad5ce0aff429 66 int i;
Lugs 0:ad5ce0aff429 67 for(i=0; i<128;i++)
Lugs 0:ad5ce0aff429 68 {
Lugs 0:ad5ce0aff429 69 audioDataBuffer[i] =((1.0 + sin((double(i)/16.0*6.28318530717959)))/2.0); //formula copied from mbed example
Lugs 0:ad5ce0aff429 70 }
Lugs 0:ad5ce0aff429 71
Lugs 4:06115dc377f7 72 printf("Ready for input.\r\n");
Lugs 0:ad5ce0aff429 73
Lugs 2:93da96b41127 74 int PlayingFreq = 440, attached = 0;
Lugs 0:ad5ce0aff429 75
Lugs 0:ad5ce0aff429 76 while(1)
Lugs 0:ad5ce0aff429 77 {
Lugs 0:ad5ce0aff429 78
Lugs 0:ad5ce0aff429 79 Track.sampleRate = PlayingFreq * 16; //TONE FREQ = SAMPLE RATE / SAMPLES PER CYCLE
Lugs 0:ad5ce0aff429 80
Lugs 0:ad5ce0aff429 81 PWM.period_us(1); //1MHz
Lugs 0:ad5ce0aff429 82
Lugs 0:ad5ce0aff429 83 float ticker_period = (float) 1/(Track.sampleRate);
Lugs 0:ad5ce0aff429 84
Lugs 3:3a4d2d10e298 85 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 86
Lugs 2:93da96b41127 87 if(attached)
Lugs 2:93da96b41127 88 {
Lugs 2:93da96b41127 89 SampleTime.attach(&placeNewSample,ticker_period);
Lugs 2:93da96b41127 90 }
Lugs 2:93da96b41127 91
Lugs 0:ad5ce0aff429 92 bLED = LED_OFF;
Lugs 0:ad5ce0aff429 93
Lugs 0:ad5ce0aff429 94 char c;
Lugs 2:93da96b41127 95 c = daplink.getc();
Lugs 2:93da96b41127 96
Lugs 2:93da96b41127 97 gLED = LED_ON;
Lugs 2:93da96b41127 98
Lugs 2:93da96b41127 99 switch(c)
Lugs 0:ad5ce0aff429 100 {
Lugs 2:93da96b41127 101 case '1':
Lugs 3:3a4d2d10e298 102 PlayingFreq = 73;
Lugs 2:93da96b41127 103 break;
Lugs 2:93da96b41127 104 case '!':
Lugs 3:3a4d2d10e298 105 PlayingFreq = 78;
Lugs 2:93da96b41127 106 break;
Lugs 2:93da96b41127 107 case '2':
Lugs 3:3a4d2d10e298 108 PlayingFreq = 82;
Lugs 2:93da96b41127 109 break;
Lugs 2:93da96b41127 110 case '3':
Lugs 3:3a4d2d10e298 111 PlayingFreq = 87;
Lugs 3:3a4d2d10e298 112 break;
Lugs 3:3a4d2d10e298 113 case '#':
Lugs 3:3a4d2d10e298 114 PlayingFreq = 92;
Lugs 2:93da96b41127 115 break;
Lugs 2:93da96b41127 116 case '4':
Lugs 3:3a4d2d10e298 117 PlayingFreq = 98;
Lugs 2:93da96b41127 118 break;
Lugs 2:93da96b41127 119 case '$':
Lugs 3:3a4d2d10e298 120 PlayingFreq = 104;
Lugs 2:93da96b41127 121 break;
Lugs 2:93da96b41127 122 case '5':
Lugs 3:3a4d2d10e298 123 PlayingFreq = 110;
Lugs 2:93da96b41127 124 break;
Lugs 2:93da96b41127 125 case '%':
Lugs 3:3a4d2d10e298 126 PlayingFreq = 117;
Lugs 2:93da96b41127 127 break;
Lugs 2:93da96b41127 128 case '6':
Lugs 3:3a4d2d10e298 129 PlayingFreq = 123;
Lugs 2:93da96b41127 130 break;
Lugs 2:93da96b41127 131 case '7':
Lugs 3:3a4d2d10e298 132 PlayingFreq = 131;
Lugs 2:93da96b41127 133 break;
Lugs 2:93da96b41127 134 case '8':
Lugs 3:3a4d2d10e298 135 PlayingFreq = 139;
Lugs 2:93da96b41127 136 break;
Lugs 2:93da96b41127 137 case '9':
Lugs 3:3a4d2d10e298 138 PlayingFreq = 147;
Lugs 2:93da96b41127 139 break;
Lugs 2:93da96b41127 140 case '(':
Lugs 3:3a4d2d10e298 141 PlayingFreq = 156;
Lugs 2:93da96b41127 142 break;
Lugs 2:93da96b41127 143 case '0':
Lugs 3:3a4d2d10e298 144 PlayingFreq = 165;
Lugs 2:93da96b41127 145 break;
Lugs 2:93da96b41127 146 case 'q':
Lugs 3:3a4d2d10e298 147 PlayingFreq = 175;
Lugs 2:93da96b41127 148 break;
Lugs 2:93da96b41127 149 case 'Q':
Lugs 3:3a4d2d10e298 150 PlayingFreq = 185;
Lugs 2:93da96b41127 151 break;
Lugs 2:93da96b41127 152 case 'w':
Lugs 3:3a4d2d10e298 153 PlayingFreq = 196;
Lugs 2:93da96b41127 154 break;
Lugs 2:93da96b41127 155 case 'W':
Lugs 3:3a4d2d10e298 156 PlayingFreq = 208;
Lugs 2:93da96b41127 157 break;
Lugs 2:93da96b41127 158 case 'e':
Lugs 3:3a4d2d10e298 159 PlayingFreq = 220;
Lugs 3:3a4d2d10e298 160 break;
Lugs 3:3a4d2d10e298 161 case 'E':
Lugs 3:3a4d2d10e298 162 PlayingFreq = 233;
Lugs 3:3a4d2d10e298 163 break;
Lugs 3:3a4d2d10e298 164 case 'r':
Lugs 3:3a4d2d10e298 165 PlayingFreq = 247;
Lugs 3:3a4d2d10e298 166 break;
Lugs 3:3a4d2d10e298 167 case 't':
Lugs 3:3a4d2d10e298 168 PlayingFreq = 262;
Lugs 3:3a4d2d10e298 169 break;
Lugs 3:3a4d2d10e298 170 case 'T':
Lugs 3:3a4d2d10e298 171 PlayingFreq = 277;
Lugs 3:3a4d2d10e298 172 break;
Lugs 3:3a4d2d10e298 173 case 'y':
Lugs 3:3a4d2d10e298 174 PlayingFreq = 294;
Lugs 3:3a4d2d10e298 175 break;
Lugs 3:3a4d2d10e298 176 case 'Y':
Lugs 3:3a4d2d10e298 177 PlayingFreq = 311;
Lugs 3:3a4d2d10e298 178 break;
Lugs 3:3a4d2d10e298 179 case 'u':
Lugs 2:93da96b41127 180 PlayingFreq = 330;
Lugs 2:93da96b41127 181 break;
Lugs 3:3a4d2d10e298 182 case 'i':
Lugs 2:93da96b41127 183 PlayingFreq = 349;
Lugs 2:93da96b41127 184 break;
Lugs 3:3a4d2d10e298 185 case 'I':
Lugs 2:93da96b41127 186 PlayingFreq = 370;
Lugs 2:93da96b41127 187 break;
Lugs 3:3a4d2d10e298 188 case 'o':
Lugs 2:93da96b41127 189 PlayingFreq = 392;
Lugs 2:93da96b41127 190 break;
Lugs 3:3a4d2d10e298 191 case 'O':
Lugs 2:93da96b41127 192 PlayingFreq = 415;
Lugs 2:93da96b41127 193 break;
Lugs 3:3a4d2d10e298 194 case 'p':
Lugs 2:93da96b41127 195 PlayingFreq = 440;
Lugs 2:93da96b41127 196 break;
Lugs 3:3a4d2d10e298 197 case 'P':
Lugs 2:93da96b41127 198 PlayingFreq = 466;
Lugs 2:93da96b41127 199 break;
Lugs 3:3a4d2d10e298 200 case 'a':
Lugs 2:93da96b41127 201 PlayingFreq = 494;
Lugs 2:93da96b41127 202 break;
Lugs 3:3a4d2d10e298 203 case 's':
Lugs 2:93da96b41127 204 PlayingFreq = 523;
Lugs 2:93da96b41127 205 break;
Lugs 2:93da96b41127 206 case 'S':
Lugs 3:3a4d2d10e298 207 PlayingFreq = 554;
Lugs 2:93da96b41127 208 break;
Lugs 2:93da96b41127 209 case 'd':
Lugs 3:3a4d2d10e298 210 PlayingFreq = 587;
Lugs 2:93da96b41127 211 break;
Lugs 2:93da96b41127 212 case 'D':
Lugs 3:3a4d2d10e298 213 PlayingFreq = 622;
Lugs 2:93da96b41127 214 break;
Lugs 2:93da96b41127 215 case 'f':
Lugs 3:3a4d2d10e298 216 PlayingFreq = 659;
Lugs 2:93da96b41127 217 break;
Lugs 2:93da96b41127 218 case 'g':
Lugs 3:3a4d2d10e298 219 PlayingFreq = 698;
Lugs 2:93da96b41127 220 break;
Lugs 2:93da96b41127 221 case 'G':
Lugs 3:3a4d2d10e298 222 PlayingFreq = 740;
Lugs 2:93da96b41127 223 break;
Lugs 2:93da96b41127 224 case 'h':
Lugs 3:3a4d2d10e298 225 PlayingFreq = 784;
Lugs 2:93da96b41127 226 break;
Lugs 2:93da96b41127 227 case 'H':
Lugs 3:3a4d2d10e298 228 PlayingFreq = 831;
Lugs 2:93da96b41127 229 break;
Lugs 2:93da96b41127 230 case 'j':
Lugs 3:3a4d2d10e298 231 PlayingFreq = 880;
Lugs 3:3a4d2d10e298 232 break;
Lugs 3:3a4d2d10e298 233 case 'J':
Lugs 3:3a4d2d10e298 234 PlayingFreq = 932;
Lugs 2:93da96b41127 235 break;
Lugs 2:93da96b41127 236 case 'k':
Lugs 3:3a4d2d10e298 237 PlayingFreq = 988;
Lugs 3:3a4d2d10e298 238 break;
Lugs 3:3a4d2d10e298 239 case 'l':
Lugs 3:3a4d2d10e298 240 PlayingFreq = 1047;
Lugs 3:3a4d2d10e298 241 break;
Lugs 3:3a4d2d10e298 242 case 'L':
Lugs 3:3a4d2d10e298 243 PlayingFreq = 1109;
Lugs 3:3a4d2d10e298 244 break;
Lugs 3:3a4d2d10e298 245 case 'z':
Lugs 3:3a4d2d10e298 246 PlayingFreq = 1175;
Lugs 3:3a4d2d10e298 247 break;
Lugs 3:3a4d2d10e298 248 case 'Z':
Lugs 3:3a4d2d10e298 249 PlayingFreq = 1245;
Lugs 3:3a4d2d10e298 250 break;
Lugs 3:3a4d2d10e298 251 case 'x':
Lugs 3:3a4d2d10e298 252 PlayingFreq = 1319;
Lugs 3:3a4d2d10e298 253 break;
Lugs 3:3a4d2d10e298 254 case 'c':
Lugs 2:93da96b41127 255 PlayingFreq = 1397;
Lugs 2:93da96b41127 256 break;
Lugs 3:3a4d2d10e298 257 case 'C':
Lugs 2:93da96b41127 258 PlayingFreq = 1480;
Lugs 2:93da96b41127 259 break;
Lugs 3:3a4d2d10e298 260 case 'v':
Lugs 2:93da96b41127 261 PlayingFreq = 1568;
Lugs 2:93da96b41127 262 break;
Lugs 3:3a4d2d10e298 263 case 'V':
Lugs 2:93da96b41127 264 PlayingFreq = 1661;
Lugs 2:93da96b41127 265 break;
Lugs 2:93da96b41127 266 case '\r':
Lugs 2:93da96b41127 267 attached = !attached;
Lugs 3:3a4d2d10e298 268 gLED = !gLED;
Lugs 2:93da96b41127 269 break;
Lugs 2:93da96b41127 270 }
Lugs 2:93da96b41127 271
Lugs 2:93da96b41127 272 gLED = LED_OFF;
Lugs 2:93da96b41127 273 rLED = !rLED;
Lugs 3:3a4d2d10e298 274 SampleTime.detach();
Lugs 1:78ca0566c062 275
Lugs 2:93da96b41127 276 printf("\033[A\033[A\033[A\033[A");
Lugs 0:ad5ce0aff429 277 }
Lugs 0:ad5ce0aff429 278 };
Lugs 0:ad5ce0aff429 279