Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: USBMSD_BD SDFileSystem max32630fthr USBDevice
main.cpp@8:ce16aa4cdb6a, 2019-07-23 (annotated)
- Committer:
- Lugs
- Date:
- Tue Jul 23 01:42:34 2019 +0000
- Revision:
- 8:ce16aa4cdb6a
- Parent:
- 7:412761dab446
- Child:
- 9:17de551d2208
cleaned up code. skipToNextEntry needs either skiptype error (0) or skiptype normal (1).
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 | 4:24086b80928e | 6 | #include "ctype.h" |
Lugs | 3:fcf745cd4f6d | 7 | |
Lugs | 0:ad5ce0aff429 | 8 | #define BUFFER_SIZE 128 |
Lugs | 0:ad5ce0aff429 | 9 | #define HALF_BUFFER 64 |
Lugs | 8:ce16aa4cdb6a | 10 | #define OS_MAINSTKSIZE 1024 |
Lugs | 8:ce16aa4cdb6a | 11 | |
Lugs | 8:ce16aa4cdb6a | 12 | |
Lugs | 8:ce16aa4cdb6a | 13 | bool debugState = 1; |
Lugs | 0:ad5ce0aff429 | 14 | |
Lugs | 0:ad5ce0aff429 | 15 | DigitalOut rLED(LED1); |
Lugs | 0:ad5ce0aff429 | 16 | DigitalOut gLED(LED2); |
Lugs | 0:ad5ce0aff429 | 17 | DigitalOut bLED(LED3); |
Lugs | 0:ad5ce0aff429 | 18 | |
Lugs | 0:ad5ce0aff429 | 19 | DigitalIn Button(P2_3); |
Lugs | 0:ad5ce0aff429 | 20 | |
Lugs | 0:ad5ce0aff429 | 21 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
Lugs | 0:ad5ce0aff429 | 22 | PwmOut PWM(P5_6); |
Lugs | 0:ad5ce0aff429 | 23 | AnalogIn POT(AIN_0); |
Lugs | 0:ad5ce0aff429 | 24 | volatile int bufferPOS = 0; |
Lugs | 8:ce16aa4cdb6a | 25 | int i; |
Lugs | 0:ad5ce0aff429 | 26 | |
Lugs | 0:ad5ce0aff429 | 27 | Serial daplink(P2_1,P2_0); |
Lugs | 0:ad5ce0aff429 | 28 | USBSerial microUSB; |
Lugs | 0:ad5ce0aff429 | 29 | |
Lugs | 0:ad5ce0aff429 | 30 | float audioDataBuffer[BUFFER_SIZE]; |
Lugs | 0:ad5ce0aff429 | 31 | |
Lugs | 4:24086b80928e | 32 | struct WavFile { |
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 | 8:ce16aa4cdb6a | 47 | void presence() |
Lugs | 4:24086b80928e | 48 | { |
Lugs | 0:ad5ce0aff429 | 49 | rLED = LED_ON; |
Lugs | 0:ad5ce0aff429 | 50 | wait_ms(500); |
Lugs | 0:ad5ce0aff429 | 51 | rLED = LED_OFF; |
Lugs | 0:ad5ce0aff429 | 52 | gLED = LED_ON; |
Lugs | 0:ad5ce0aff429 | 53 | wait_ms(500); |
Lugs | 0:ad5ce0aff429 | 54 | bLED = LED_ON; |
Lugs | 0:ad5ce0aff429 | 55 | gLED = LED_OFF; |
Lugs | 8:ce16aa4cdb6a | 56 | } |
Lugs | 4:24086b80928e | 57 | |
Lugs | 8:ce16aa4cdb6a | 58 | typedef enum : unsigned char { |
Lugs | 8:ce16aa4cdb6a | 59 | C2,Cs2,D2,Ds2,E2,F2,Fs2,G2,Gs2,A2,As2,B2, //C2:0 |
Lugs | 8:ce16aa4cdb6a | 60 | C3,Cs3,D3,Ds3,E3,F3,Fs3,G3,Gs3,A3,As3,B3, //C3:12 |
Lugs | 8:ce16aa4cdb6a | 61 | C4,Cs4,D4,Ds4,E4,F4,Fs4,G4,Gs4,A4,As4,B4, //C4:24 |
Lugs | 8:ce16aa4cdb6a | 62 | C5,Cs5,D5,Ds5,E5,F5,Fs5,G5,Gs5,A5,As5,B5, //C5:36 |
Lugs | 8:ce16aa4cdb6a | 63 | C6,Cs6,D6,Ds6,E6,F6,Fs6,G6,Gs6, //C6:48 |
Lugs | 8:ce16aa4cdb6a | 64 | rest, |
Lugs | 8:ce16aa4cdb6a | 65 | END |
Lugs | 8:ce16aa4cdb6a | 66 | } pitchname; |
Lugs | 8:ce16aa4cdb6a | 67 | |
Lugs | 8:ce16aa4cdb6a | 68 | typedef struct { |
Lugs | 8:ce16aa4cdb6a | 69 | unsigned char length; |
Lugs | 8:ce16aa4cdb6a | 70 | pitchname pitch; |
Lugs | 8:ce16aa4cdb6a | 71 | } note; |
Lugs | 8:ce16aa4cdb6a | 72 | |
Lugs | 8:ce16aa4cdb6a | 73 | char *skipToNextEntry(char *songpos, bool skiptype) |
Lugs | 8:ce16aa4cdb6a | 74 | { |
Lugs | 8:ce16aa4cdb6a | 75 | if(skiptype = 0) { |
Lugs | 8:ce16aa4cdb6a | 76 | do { |
Lugs | 8:ce16aa4cdb6a | 77 | songpos++; |
Lugs | 8:ce16aa4cdb6a | 78 | } while(!isspace(*(songpos))); |
Lugs | 8:ce16aa4cdb6a | 79 | do { |
Lugs | 8:ce16aa4cdb6a | 80 | songpos++; |
Lugs | 8:ce16aa4cdb6a | 81 | } while(!isalnum(*(songpos))); |
Lugs | 8:ce16aa4cdb6a | 82 | return songpos; |
Lugs | 8:ce16aa4cdb6a | 83 | } else if(skiptype = 1) { |
Lugs | 8:ce16aa4cdb6a | 84 | while(!isspace(*(songpos))) { |
Lugs | 8:ce16aa4cdb6a | 85 | songpos++; |
Lugs | 8:ce16aa4cdb6a | 86 | } |
Lugs | 8:ce16aa4cdb6a | 87 | while(isspace(*(songpos))) { |
Lugs | 8:ce16aa4cdb6a | 88 | songpos++; |
Lugs | 8:ce16aa4cdb6a | 89 | }; |
Lugs | 8:ce16aa4cdb6a | 90 | } |
Lugs | 8:ce16aa4cdb6a | 91 | } |
Lugs | 8:ce16aa4cdb6a | 92 | |
Lugs | 8:ce16aa4cdb6a | 93 | bool songParse(note *song,char *buffer) |
Lugs | 8:ce16aa4cdb6a | 94 | { |
Lugs | 8:ce16aa4cdb6a | 95 | int candidate; |
Lugs | 8:ce16aa4cdb6a | 96 | char *songpos=buffer+1; //song position |
Lugs | 8:ce16aa4cdb6a | 97 | int pn_det; |
Lugs | 8:ce16aa4cdb6a | 98 | int i; |
Lugs | 8:ce16aa4cdb6a | 99 | for(i=0; i<256; i++) { |
Lugs | 8:ce16aa4cdb6a | 100 | //take the first base 10 integer you see. |
Lugs | 8:ce16aa4cdb6a | 101 | //this initializes songpos ALWAYS. lol. |
Lugs | 8:ce16aa4cdb6a | 102 | candidate = strtol(songpos-1,&songpos,10); |
Lugs | 8:ce16aa4cdb6a | 103 | |
Lugs | 8:ce16aa4cdb6a | 104 | if(debugState) { |
Lugs | 8:ce16aa4cdb6a | 105 | printf("Character:%i\r\n",candidate); |
Lugs | 8:ce16aa4cdb6a | 106 | } |
Lugs | 8:ce16aa4cdb6a | 107 | |
Lugs | 8:ce16aa4cdb6a | 108 | if(candidate == 0) { |
Lugs | 8:ce16aa4cdb6a | 109 | printf("Found invalid NOTE LENGTH value [%c] at position %li. Skipping until next whitespace...\r\n",*songpos,songpos-buffer); |
Lugs | 8:ce16aa4cdb6a | 110 | songpos = skipToNextEntry(songpos,0); |
Lugs | 8:ce16aa4cdb6a | 111 | i--; |
Lugs | 8:ce16aa4cdb6a | 112 | continue; |
Lugs | 8:ce16aa4cdb6a | 113 | } else { |
Lugs | 8:ce16aa4cdb6a | 114 | song[i].length = candidate; |
Lugs | 8:ce16aa4cdb6a | 115 | if(debugState) { |
Lugs | 8:ce16aa4cdb6a | 116 | printf("Entered candidate [%i] into song[%i].length\r\n",candidate,i); |
Lugs | 8:ce16aa4cdb6a | 117 | } |
Lugs | 8:ce16aa4cdb6a | 118 | } |
Lugs | 8:ce16aa4cdb6a | 119 | |
Lugs | 8:ce16aa4cdb6a | 120 | //parse next character |
Lugs | 8:ce16aa4cdb6a | 121 | if(debugState) { |
Lugs | 8:ce16aa4cdb6a | 122 | printf("Character:%c\r\n",*songpos); |
Lugs | 8:ce16aa4cdb6a | 123 | } |
Lugs | 8:ce16aa4cdb6a | 124 | |
Lugs | 8:ce16aa4cdb6a | 125 | pn_det=0; |
Lugs | 4:24086b80928e | 126 | |
Lugs | 8:ce16aa4cdb6a | 127 | if(!(*songpos=='#'||'a'<=*songpos<='z'||*songpos=='-')) { |
Lugs | 8:ce16aa4cdb6a | 128 | printf("Found invalid PITCH NAME value [%c] at position %li. Skipping word...\r\n",*songpos,songpos-buffer); |
Lugs | 8:ce16aa4cdb6a | 129 | songpos = skipToNextEntry(songpos,0); |
Lugs | 8:ce16aa4cdb6a | 130 | i--; |
Lugs | 8:ce16aa4cdb6a | 131 | continue; |
Lugs | 8:ce16aa4cdb6a | 132 | } |
Lugs | 8:ce16aa4cdb6a | 133 | if(*songpos=='#') { //watch out for sharps |
Lugs | 8:ce16aa4cdb6a | 134 | pn_det+=1; |
Lugs | 8:ce16aa4cdb6a | 135 | songpos++; |
Lugs | 8:ce16aa4cdb6a | 136 | printf("Sharp found.\r\n"); |
Lugs | 8:ce16aa4cdb6a | 137 | } |
Lugs | 8:ce16aa4cdb6a | 138 | switch(*songpos) { |
Lugs | 8:ce16aa4cdb6a | 139 | case 'c': |
Lugs | 8:ce16aa4cdb6a | 140 | pn_det+=0; |
Lugs | 8:ce16aa4cdb6a | 141 | break; |
Lugs | 8:ce16aa4cdb6a | 142 | case 'd': |
Lugs | 8:ce16aa4cdb6a | 143 | pn_det+=2; |
Lugs | 8:ce16aa4cdb6a | 144 | break; |
Lugs | 8:ce16aa4cdb6a | 145 | case 'e': |
Lugs | 8:ce16aa4cdb6a | 146 | pn_det+=4; |
Lugs | 8:ce16aa4cdb6a | 147 | break; |
Lugs | 8:ce16aa4cdb6a | 148 | case 'f': |
Lugs | 8:ce16aa4cdb6a | 149 | pn_det+=5; |
Lugs | 8:ce16aa4cdb6a | 150 | break; |
Lugs | 8:ce16aa4cdb6a | 151 | case 'g': |
Lugs | 8:ce16aa4cdb6a | 152 | pn_det+=7; |
Lugs | 8:ce16aa4cdb6a | 153 | break; |
Lugs | 8:ce16aa4cdb6a | 154 | case 'a': |
Lugs | 8:ce16aa4cdb6a | 155 | pn_det+=9; |
Lugs | 8:ce16aa4cdb6a | 156 | break; |
Lugs | 8:ce16aa4cdb6a | 157 | case 'b': |
Lugs | 8:ce16aa4cdb6a | 158 | pn_det+=11; |
Lugs | 8:ce16aa4cdb6a | 159 | break; |
Lugs | 8:ce16aa4cdb6a | 160 | case '-': |
Lugs | 8:ce16aa4cdb6a | 161 | pn_det=57; |
Lugs | 8:ce16aa4cdb6a | 162 | goto skipoctaveparse; |
Lugs | 8:ce16aa4cdb6a | 163 | } |
Lugs | 8:ce16aa4cdb6a | 164 | |
Lugs | 8:ce16aa4cdb6a | 165 | songpos++; |
Lugs | 8:ce16aa4cdb6a | 166 | |
Lugs | 8:ce16aa4cdb6a | 167 | //parse octaves |
Lugs | 8:ce16aa4cdb6a | 168 | if('0'<=*songpos<='9') { |
Lugs | 8:ce16aa4cdb6a | 169 | int num = strtol(songpos,&songpos,10); |
Lugs | 8:ce16aa4cdb6a | 170 | num+=3;//shift up thrice. board can't handle things this low. |
Lugs | 8:ce16aa4cdb6a | 171 | if(debugState) { |
Lugs | 8:ce16aa4cdb6a | 172 | printf("octave parsed: %i \t-> adder:%i\r\n",num,((num-2)*12)); |
Lugs | 8:ce16aa4cdb6a | 173 | } |
Lugs | 8:ce16aa4cdb6a | 174 | pn_det=pn_det+((num-2)*12); |
Lugs | 8:ce16aa4cdb6a | 175 | } else { |
Lugs | 8:ce16aa4cdb6a | 176 | printf("Found invalid OCTAVE value [%c] at position %li. Skipping word...\r\n",*songpos,songpos-buffer); |
Lugs | 8:ce16aa4cdb6a | 177 | songpos = skipToNextEntry(songpos,0); |
Lugs | 8:ce16aa4cdb6a | 178 | i--; //rewrite current note |
Lugs | 8:ce16aa4cdb6a | 179 | continue; |
Lugs | 8:ce16aa4cdb6a | 180 | } |
Lugs | 8:ce16aa4cdb6a | 181 | skipoctaveparse: |
Lugs | 8:ce16aa4cdb6a | 182 | |
Lugs | 8:ce16aa4cdb6a | 183 | song[i].pitch = (pitchname)pn_det; |
Lugs | 8:ce16aa4cdb6a | 184 | |
Lugs | 8:ce16aa4cdb6a | 185 | if(debugState) { |
Lugs | 8:ce16aa4cdb6a | 186 | printf("Entered pitch [%i] into song[%i].pitch\r\n",song[i].pitch,i); |
Lugs | 8:ce16aa4cdb6a | 187 | printf("Final: {%i,%i} at i=%i\r\n",song[i].length,song[i].pitch,i); |
Lugs | 8:ce16aa4cdb6a | 188 | printf("-------------------\r\n"); |
Lugs | 8:ce16aa4cdb6a | 189 | } |
Lugs | 8:ce16aa4cdb6a | 190 | |
Lugs | 8:ce16aa4cdb6a | 191 | songpos = skipToNextEntry(songpos,1); |
Lugs | 8:ce16aa4cdb6a | 192 | |
Lugs | 8:ce16aa4cdb6a | 193 | //check for buffer end |
Lugs | 8:ce16aa4cdb6a | 194 | if(*(songpos) == '\0') { |
Lugs | 8:ce16aa4cdb6a | 195 | i++; |
Lugs | 8:ce16aa4cdb6a | 196 | song[i].length = 1; |
Lugs | 8:ce16aa4cdb6a | 197 | song[i].pitch = END; |
Lugs | 8:ce16aa4cdb6a | 198 | printf("Parsing done. i = %i\r\n",i); |
Lugs | 8:ce16aa4cdb6a | 199 | return 1; |
Lugs | 8:ce16aa4cdb6a | 200 | } |
Lugs | 8:ce16aa4cdb6a | 201 | if(songpos-buffer > 2048) { |
Lugs | 8:ce16aa4cdb6a | 202 | printf("songpos exceeded buffer size.\r\n"); |
Lugs | 8:ce16aa4cdb6a | 203 | return 0; |
Lugs | 8:ce16aa4cdb6a | 204 | } |
Lugs | 8:ce16aa4cdb6a | 205 | } |
Lugs | 8:ce16aa4cdb6a | 206 | printf("Song exceeded maximum number of notes.\r\n"); |
Lugs | 8:ce16aa4cdb6a | 207 | return 0; |
Lugs | 8:ce16aa4cdb6a | 208 | } |
Lugs | 8:ce16aa4cdb6a | 209 | |
Lugs | 8:ce16aa4cdb6a | 210 | void printSong(note *song) |
Lugs | 8:ce16aa4cdb6a | 211 | { |
Lugs | 8:ce16aa4cdb6a | 212 | for(i=0; song[i].pitch != END; i++) { |
Lugs | 8:ce16aa4cdb6a | 213 | printf("{%i,%i},",song[i].length,song[i].pitch); |
Lugs | 8:ce16aa4cdb6a | 214 | } |
Lugs | 8:ce16aa4cdb6a | 215 | printf("{%i,%i},",song[i].length,song[i].pitch); |
Lugs | 8:ce16aa4cdb6a | 216 | printf("\r\nPrinting done.\r\n"); |
Lugs | 8:ce16aa4cdb6a | 217 | } |
Lugs | 8:ce16aa4cdb6a | 218 | |
Lugs | 8:ce16aa4cdb6a | 219 | int main() |
Lugs | 8:ce16aa4cdb6a | 220 | { |
Lugs | 8:ce16aa4cdb6a | 221 | WavFile Track; |
Lugs | 8:ce16aa4cdb6a | 222 | Ticker SampleTime; |
Lugs | 8:ce16aa4cdb6a | 223 | |
Lugs | 8:ce16aa4cdb6a | 224 | daplink.printf("\f---DAPLINK SERIAL PORT---\r\n\r\nMINI PIANO PLAYER ver 2 \r\n\r\n\r\n"); |
Lugs | 8:ce16aa4cdb6a | 225 | microUSB.printf("micro USB serial port\r\n"); |
Lugs | 8:ce16aa4cdb6a | 226 | presence(); |
Lugs | 4:24086b80928e | 227 | |
Lugs | 4:24086b80928e | 228 | //input iterator vars |
Lugs | 4:24086b80928e | 229 | int i; |
Lugs | 4:24086b80928e | 230 | char c; |
Lugs | 8:ce16aa4cdb6a | 231 | //input holder array |
Lugs | 8:ce16aa4cdb6a | 232 | char *buffer = new char[1024]; //ver 2 update: placed in stack. |
Lugs | 4:24086b80928e | 233 | |
Lugs | 3:fcf745cd4f6d | 234 | //open file |
Lugs | 4:24086b80928e | 235 | toolarge: |
Lugs | 4:24086b80928e | 236 | printf("Please copy the text file of the song then right click on the PuTTY screen.\r\n"); |
Lugs | 4:24086b80928e | 237 | for(i=0; 1; i++) { //do the loop at least once |
Lugs | 4:24086b80928e | 238 | c=daplink.getc(); |
Lugs | 4:24086b80928e | 239 | daplink.putc(c); |
Lugs | 4:24086b80928e | 240 | buffer[i]=c; |
Lugs | 8:ce16aa4cdb6a | 241 | if(i>=4096) { |
Lugs | 4:24086b80928e | 242 | printf("Note data is greater than 2kB! Impossible. Try again.\033[A"); |
Lugs | 4:24086b80928e | 243 | goto toolarge; |
Lugs | 4:24086b80928e | 244 | } |
Lugs | 4:24086b80928e | 245 | wait_ms(1); //wait for the next byte to arrive. every byte takes 1.04ms at 9600 baud. |
Lugs | 8:ce16aa4cdb6a | 246 | if(!daplink.readable()) { |
Lugs | 8:ce16aa4cdb6a | 247 | break; |
Lugs | 4:24086b80928e | 248 | } |
Lugs | 4:24086b80928e | 249 | } |
Lugs | 4:24086b80928e | 250 | buffer[i] = ' '; //set EOF marker. |
Lugs | 8:ce16aa4cdb6a | 251 | buffer[i+1] = '\0'; |
Lugs | 8:ce16aa4cdb6a | 252 | buffer[i+2] = 'a'; //make sure to put a character after (will allow skipToNextEntry() to find the null character at [songpos-1]) |
Lugs | 4:24086b80928e | 253 | |
Lugs | 4:24086b80928e | 254 | //kunwari open na yung file |
Lugs | 4:24086b80928e | 255 | printf("\r\nFile recieved. Parsing...\r\n"); |
Lugs | 8:ce16aa4cdb6a | 256 | |
Lugs | 3:fcf745cd4f6d | 257 | //parse file into song in heap(remove sharps, put into enums.) |
Lugs | 8:ce16aa4cdb6a | 258 | note song[1024]; |
Lugs | 8:ce16aa4cdb6a | 259 | if(!songParse(song,buffer)) { |
Lugs | 8:ce16aa4cdb6a | 260 | printf("Song parse unsuccessful.\r\n"); |
Lugs | 4:24086b80928e | 261 | } |
Lugs | 4:24086b80928e | 262 | |
Lugs | 8:ce16aa4cdb6a | 263 | printSong(song); |
Lugs | 4:24086b80928e | 264 | |
Lugs | 0:ad5ce0aff429 | 265 | printf("Generating sine...\r\n"); |
Lugs | 4:24086b80928e | 266 | for(i=0; i<128; i++) { |
Lugs | 0:ad5ce0aff429 | 267 | audioDataBuffer[i] =((1.0 + sin((double(i)/16.0*6.28318530717959)))/2.0); //formula copied from mbed example |
Lugs | 0:ad5ce0aff429 | 268 | } |
Lugs | 4:24086b80928e | 269 | |
Lugs | 4:24086b80928e | 270 | /* HERE'S WHERE THE MUSIC STARTS */ |
Lugs | 4:24086b80928e | 271 | /* LITERALLY. */ |
Lugs | 4:24086b80928e | 272 | |
Lugs | 4:24086b80928e | 273 | //FORMAT: { NUM_BEATS, PITCH } |
Lugs | 4:24086b80928e | 274 | char bpminp[5]; |
Lugs | 4:24086b80928e | 275 | int PlayingFreq; |
Lugs | 4:24086b80928e | 276 | |
Lugs | 4:24086b80928e | 277 | |
Lugs | 4:24086b80928e | 278 | printf("Please enter desired BPM:\r\n"); |
Lugs | 4:24086b80928e | 279 | |
Lugs | 4:24086b80928e | 280 | for(i=0; i<5 && c!='\r'; i++) { |
Lugs | 4:24086b80928e | 281 | c = daplink.getc(); |
Lugs | 4:24086b80928e | 282 | daplink.putc(c); |
Lugs | 4:24086b80928e | 283 | bpminp[i] = c; |
Lugs | 4:24086b80928e | 284 | } |
Lugs | 8:ce16aa4cdb6a | 285 | |
Lugs | 4:24086b80928e | 286 | printf("BPM Recieved.\r\n"); |
Lugs | 4:24086b80928e | 287 | c=NULL; //reset C for next time. |
Lugs | 4:24086b80928e | 288 | int BPM = strtol(bpminp,NULL,0); |
Lugs | 4:24086b80928e | 289 | float SPB = 60*4/(float)BPM; |
Lugs | 4:24086b80928e | 290 | printf("BPM: %i",BPM); |
Lugs | 8:ce16aa4cdb6a | 291 | |
Lugs | 4:24086b80928e | 292 | restart: |
Lugs | 4:24086b80928e | 293 | |
Lugs | 8:ce16aa4cdb6a | 294 | //buffer input cycle |
Lugs | 4:24086b80928e | 295 | for(i = 0; 1; i++) { |
Lugs | 8:ce16aa4cdb6a | 296 | //frequency select |
Lugs | 4:24086b80928e | 297 | switch(song[i].pitch) { |
Lugs | 3:fcf745cd4f6d | 298 | case rest: |
Lugs | 5:8ba2f1e291b9 | 299 | wait((1/(float)song[i].length)*SPB); |
Lugs | 3:fcf745cd4f6d | 300 | continue; |
Lugs | 3:fcf745cd4f6d | 301 | case D2: |
Lugs | 3:fcf745cd4f6d | 302 | PlayingFreq = 73; |
Lugs | 3:fcf745cd4f6d | 303 | break; |
Lugs | 3:fcf745cd4f6d | 304 | case Ds2: |
Lugs | 3:fcf745cd4f6d | 305 | PlayingFreq = 78; |
Lugs | 3:fcf745cd4f6d | 306 | break; |
Lugs | 3:fcf745cd4f6d | 307 | case E2: |
Lugs | 3:fcf745cd4f6d | 308 | PlayingFreq = 82; |
Lugs | 3:fcf745cd4f6d | 309 | break; |
Lugs | 3:fcf745cd4f6d | 310 | case F2: |
Lugs | 3:fcf745cd4f6d | 311 | PlayingFreq = 87; |
Lugs | 3:fcf745cd4f6d | 312 | break; |
Lugs | 3:fcf745cd4f6d | 313 | case Fs2: |
Lugs | 3:fcf745cd4f6d | 314 | PlayingFreq = 92; |
Lugs | 3:fcf745cd4f6d | 315 | break; |
Lugs | 3:fcf745cd4f6d | 316 | case G2: |
Lugs | 2:93da96b41127 | 317 | PlayingFreq = 98; |
Lugs | 2:93da96b41127 | 318 | break; |
Lugs | 3:fcf745cd4f6d | 319 | case Gs2: |
Lugs | 3:fcf745cd4f6d | 320 | PlayingFreq = 104; |
Lugs | 2:93da96b41127 | 321 | break; |
Lugs | 3:fcf745cd4f6d | 322 | case A2: |
Lugs | 2:93da96b41127 | 323 | PlayingFreq = 110; |
Lugs | 2:93da96b41127 | 324 | break; |
Lugs | 3:fcf745cd4f6d | 325 | case As2: |
Lugs | 2:93da96b41127 | 326 | PlayingFreq = 117; |
Lugs | 2:93da96b41127 | 327 | break; |
Lugs | 3:fcf745cd4f6d | 328 | case B2: |
Lugs | 2:93da96b41127 | 329 | PlayingFreq = 123; |
Lugs | 2:93da96b41127 | 330 | break; |
Lugs | 3:fcf745cd4f6d | 331 | case C3: |
Lugs | 2:93da96b41127 | 332 | PlayingFreq = 131; |
Lugs | 2:93da96b41127 | 333 | break; |
Lugs | 3:fcf745cd4f6d | 334 | case Cs3: |
Lugs | 2:93da96b41127 | 335 | PlayingFreq = 139; |
Lugs | 2:93da96b41127 | 336 | break; |
Lugs | 3:fcf745cd4f6d | 337 | case D3: |
Lugs | 2:93da96b41127 | 338 | PlayingFreq = 147; |
Lugs | 2:93da96b41127 | 339 | break; |
Lugs | 3:fcf745cd4f6d | 340 | case Ds3: |
Lugs | 2:93da96b41127 | 341 | PlayingFreq = 156; |
Lugs | 2:93da96b41127 | 342 | break; |
Lugs | 3:fcf745cd4f6d | 343 | case E3: |
Lugs | 2:93da96b41127 | 344 | PlayingFreq = 165; |
Lugs | 2:93da96b41127 | 345 | break; |
Lugs | 3:fcf745cd4f6d | 346 | case F3: |
Lugs | 2:93da96b41127 | 347 | PlayingFreq = 175; |
Lugs | 2:93da96b41127 | 348 | break; |
Lugs | 3:fcf745cd4f6d | 349 | case Fs3: |
Lugs | 2:93da96b41127 | 350 | PlayingFreq = 185; |
Lugs | 2:93da96b41127 | 351 | break; |
Lugs | 3:fcf745cd4f6d | 352 | case G3: |
Lugs | 2:93da96b41127 | 353 | PlayingFreq = 196; |
Lugs | 2:93da96b41127 | 354 | break; |
Lugs | 3:fcf745cd4f6d | 355 | case Gs3: |
Lugs | 2:93da96b41127 | 356 | PlayingFreq = 208; |
Lugs | 2:93da96b41127 | 357 | break; |
Lugs | 3:fcf745cd4f6d | 358 | case A3: |
Lugs | 2:93da96b41127 | 359 | PlayingFreq = 220; |
Lugs | 2:93da96b41127 | 360 | break; |
Lugs | 3:fcf745cd4f6d | 361 | case As3: |
Lugs | 2:93da96b41127 | 362 | PlayingFreq = 233; |
Lugs | 2:93da96b41127 | 363 | break; |
Lugs | 3:fcf745cd4f6d | 364 | case B3: |
Lugs | 2:93da96b41127 | 365 | PlayingFreq = 247; |
Lugs | 2:93da96b41127 | 366 | break; |
Lugs | 3:fcf745cd4f6d | 367 | case C4: |
Lugs | 2:93da96b41127 | 368 | PlayingFreq = 262; |
Lugs | 2:93da96b41127 | 369 | break; |
Lugs | 3:fcf745cd4f6d | 370 | case Cs4: |
Lugs | 2:93da96b41127 | 371 | PlayingFreq = 277; |
Lugs | 2:93da96b41127 | 372 | break; |
Lugs | 3:fcf745cd4f6d | 373 | case D4: |
Lugs | 2:93da96b41127 | 374 | PlayingFreq = 294; |
Lugs | 2:93da96b41127 | 375 | break; |
Lugs | 3:fcf745cd4f6d | 376 | case Ds4: |
Lugs | 2:93da96b41127 | 377 | PlayingFreq = 311; |
Lugs | 2:93da96b41127 | 378 | break; |
Lugs | 3:fcf745cd4f6d | 379 | case E4: |
Lugs | 2:93da96b41127 | 380 | PlayingFreq = 330; |
Lugs | 2:93da96b41127 | 381 | break; |
Lugs | 3:fcf745cd4f6d | 382 | case F4: |
Lugs | 2:93da96b41127 | 383 | PlayingFreq = 349; |
Lugs | 2:93da96b41127 | 384 | break; |
Lugs | 3:fcf745cd4f6d | 385 | case Fs4: |
Lugs | 2:93da96b41127 | 386 | PlayingFreq = 370; |
Lugs | 2:93da96b41127 | 387 | break; |
Lugs | 3:fcf745cd4f6d | 388 | case G4: |
Lugs | 2:93da96b41127 | 389 | PlayingFreq = 392; |
Lugs | 2:93da96b41127 | 390 | break; |
Lugs | 3:fcf745cd4f6d | 391 | case Gs4: |
Lugs | 2:93da96b41127 | 392 | PlayingFreq = 415; |
Lugs | 2:93da96b41127 | 393 | break; |
Lugs | 3:fcf745cd4f6d | 394 | case A4: |
Lugs | 2:93da96b41127 | 395 | PlayingFreq = 440; |
Lugs | 2:93da96b41127 | 396 | break; |
Lugs | 3:fcf745cd4f6d | 397 | case As4: |
Lugs | 2:93da96b41127 | 398 | PlayingFreq = 466; |
Lugs | 2:93da96b41127 | 399 | break; |
Lugs | 3:fcf745cd4f6d | 400 | case B4: |
Lugs | 2:93da96b41127 | 401 | PlayingFreq = 494; |
Lugs | 2:93da96b41127 | 402 | break; |
Lugs | 3:fcf745cd4f6d | 403 | case C5: |
Lugs | 2:93da96b41127 | 404 | PlayingFreq = 523; |
Lugs | 2:93da96b41127 | 405 | break; |
Lugs | 3:fcf745cd4f6d | 406 | case Cs5: |
Lugs | 2:93da96b41127 | 407 | PlayingFreq = 554; |
Lugs | 2:93da96b41127 | 408 | break; |
Lugs | 3:fcf745cd4f6d | 409 | case D5: |
Lugs | 2:93da96b41127 | 410 | PlayingFreq = 587; |
Lugs | 2:93da96b41127 | 411 | break; |
Lugs | 3:fcf745cd4f6d | 412 | case Ds5: |
Lugs | 2:93da96b41127 | 413 | PlayingFreq = 622; |
Lugs | 2:93da96b41127 | 414 | break; |
Lugs | 3:fcf745cd4f6d | 415 | case E5: |
Lugs | 2:93da96b41127 | 416 | PlayingFreq = 659; |
Lugs | 2:93da96b41127 | 417 | break; |
Lugs | 3:fcf745cd4f6d | 418 | case F5: |
Lugs | 2:93da96b41127 | 419 | PlayingFreq = 698; |
Lugs | 2:93da96b41127 | 420 | break; |
Lugs | 3:fcf745cd4f6d | 421 | case Fs5: |
Lugs | 2:93da96b41127 | 422 | PlayingFreq = 740; |
Lugs | 2:93da96b41127 | 423 | break; |
Lugs | 3:fcf745cd4f6d | 424 | case G5: |
Lugs | 2:93da96b41127 | 425 | PlayingFreq = 784; |
Lugs | 2:93da96b41127 | 426 | break; |
Lugs | 3:fcf745cd4f6d | 427 | case Gs5: |
Lugs | 2:93da96b41127 | 428 | PlayingFreq = 831; |
Lugs | 2:93da96b41127 | 429 | break; |
Lugs | 3:fcf745cd4f6d | 430 | case A5: |
Lugs | 2:93da96b41127 | 431 | PlayingFreq = 880; |
Lugs | 2:93da96b41127 | 432 | break; |
Lugs | 3:fcf745cd4f6d | 433 | case As5: |
Lugs | 2:93da96b41127 | 434 | PlayingFreq = 932; |
Lugs | 2:93da96b41127 | 435 | break; |
Lugs | 3:fcf745cd4f6d | 436 | case B5: |
Lugs | 2:93da96b41127 | 437 | PlayingFreq = 988; |
Lugs | 2:93da96b41127 | 438 | break; |
Lugs | 3:fcf745cd4f6d | 439 | case C6: |
Lugs | 2:93da96b41127 | 440 | PlayingFreq = 1047; |
Lugs | 2:93da96b41127 | 441 | break; |
Lugs | 3:fcf745cd4f6d | 442 | case Cs6: |
Lugs | 2:93da96b41127 | 443 | PlayingFreq = 1109; |
Lugs | 2:93da96b41127 | 444 | break; |
Lugs | 3:fcf745cd4f6d | 445 | case D6: |
Lugs | 2:93da96b41127 | 446 | PlayingFreq = 1175; |
Lugs | 2:93da96b41127 | 447 | break; |
Lugs | 3:fcf745cd4f6d | 448 | case Ds6: |
Lugs | 2:93da96b41127 | 449 | PlayingFreq = 1245; |
Lugs | 2:93da96b41127 | 450 | break; |
Lugs | 3:fcf745cd4f6d | 451 | case E6: |
Lugs | 2:93da96b41127 | 452 | PlayingFreq = 1319; |
Lugs | 2:93da96b41127 | 453 | break; |
Lugs | 3:fcf745cd4f6d | 454 | case F6: |
Lugs | 2:93da96b41127 | 455 | PlayingFreq = 1397; |
Lugs | 2:93da96b41127 | 456 | break; |
Lugs | 3:fcf745cd4f6d | 457 | case Fs6: |
Lugs | 2:93da96b41127 | 458 | PlayingFreq = 1480; |
Lugs | 2:93da96b41127 | 459 | break; |
Lugs | 3:fcf745cd4f6d | 460 | case G6: |
Lugs | 2:93da96b41127 | 461 | PlayingFreq = 1568; |
Lugs | 2:93da96b41127 | 462 | break; |
Lugs | 3:fcf745cd4f6d | 463 | case Gs6: |
Lugs | 2:93da96b41127 | 464 | PlayingFreq = 1661; |
Lugs | 2:93da96b41127 | 465 | break; |
Lugs | 3:fcf745cd4f6d | 466 | case END: |
Lugs | 3:fcf745cd4f6d | 467 | i = 0; |
Lugs | 4:24086b80928e | 468 | printf("SONG END. PRESS BUTTON TO RESTART."); |
Lugs | 8:ce16aa4cdb6a | 469 | while(Button) { |
Lugs | 5:8ba2f1e291b9 | 470 | wait_ms(2); |
Lugs | 4:24086b80928e | 471 | } |
Lugs | 3:fcf745cd4f6d | 472 | goto restart; |
Lugs | 2:93da96b41127 | 473 | } |
Lugs | 4:24086b80928e | 474 | |
Lugs | 8:ce16aa4cdb6a | 475 | //calculate ticker, attach ticker. |
Lugs | 3:fcf745cd4f6d | 476 | Track.sampleRate = PlayingFreq * 16; //TONE FREQ = SAMPLE RATE / SAMPLES PER CYCLE |
Lugs | 3:fcf745cd4f6d | 477 | PWM.period_us(1); //1MHz |
Lugs | 3:fcf745cd4f6d | 478 | float ticker_period = (float) 1/(Track.sampleRate); |
Lugs | 3:fcf745cd4f6d | 479 | printf("\r\nTicker Period: %f\tTicker Freq: %f\r\nTarget Freq: %i \r\n\r\n",ticker_period, 1/ticker_period, PlayingFreq); |
Lugs | 2:93da96b41127 | 480 | |
Lugs | 3:fcf745cd4f6d | 481 | SampleTime.attach(&placeNewSample,ticker_period); |
Lugs | 4:24086b80928e | 482 | wait( (1/(float)song[i].length) *SPB); |
Lugs | 3:fcf745cd4f6d | 483 | SampleTime.detach(); |
Lugs | 8:ce16aa4cdb6a | 484 | |
Lugs | 8:ce16aa4cdb6a | 485 | |
Lugs | 4:24086b80928e | 486 | |
Lugs | 2:93da96b41127 | 487 | printf("\033[A\033[A\033[A\033[A"); |
Lugs | 0:ad5ce0aff429 | 488 | } |
Lugs | 3:fcf745cd4f6d | 489 | } |
Lugs | 0:ad5ce0aff429 | 490 |