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@7:412761dab446, 2019-07-19 (annotated)
- Committer:
- Lugs
- Date:
- Fri Jul 19 08:55:15 2019 +0000
- Revision:
- 7:412761dab446
- Parent:
- 5:8ba2f1e291b9
- Child:
- 8:ce16aa4cdb6a
new commitment. last commitment was a mistake.
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 | 0:ad5ce0aff429 | 10 | |
Lugs | 0:ad5ce0aff429 | 11 | DigitalOut rLED(LED1); |
Lugs | 0:ad5ce0aff429 | 12 | DigitalOut gLED(LED2); |
Lugs | 0:ad5ce0aff429 | 13 | DigitalOut bLED(LED3); |
Lugs | 0:ad5ce0aff429 | 14 | |
Lugs | 0:ad5ce0aff429 | 15 | DigitalIn Button(P2_3); |
Lugs | 0:ad5ce0aff429 | 16 | |
Lugs | 0:ad5ce0aff429 | 17 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
Lugs | 0:ad5ce0aff429 | 18 | PwmOut PWM(P5_6); |
Lugs | 0:ad5ce0aff429 | 19 | AnalogIn POT(AIN_0); |
Lugs | 0:ad5ce0aff429 | 20 | volatile int bufferPOS = 0; |
Lugs | 0:ad5ce0aff429 | 21 | |
Lugs | 0:ad5ce0aff429 | 22 | Serial daplink(P2_1,P2_0); |
Lugs | 0:ad5ce0aff429 | 23 | USBSerial microUSB; |
Lugs | 0:ad5ce0aff429 | 24 | |
Lugs | 0:ad5ce0aff429 | 25 | float audioDataBuffer[BUFFER_SIZE]; |
Lugs | 0:ad5ce0aff429 | 26 | |
Lugs | 4:24086b80928e | 27 | struct WavFile { |
Lugs | 0:ad5ce0aff429 | 28 | long int size; |
Lugs | 0:ad5ce0aff429 | 29 | int channels; |
Lugs | 0:ad5ce0aff429 | 30 | int sampleRate; |
Lugs | 0:ad5ce0aff429 | 31 | int bitsPerSample; |
Lugs | 0:ad5ce0aff429 | 32 | }; |
Lugs | 0:ad5ce0aff429 | 33 | |
Lugs | 0:ad5ce0aff429 | 34 | float potval,reading; |
Lugs | 0:ad5ce0aff429 | 35 | |
Lugs | 0:ad5ce0aff429 | 36 | void placeNewSample(void) |
Lugs | 0:ad5ce0aff429 | 37 | { |
Lugs | 2:93da96b41127 | 38 | PWM.write(audioDataBuffer[bufferPOS++]); //multiply by POT value for volume. |
Lugs | 0:ad5ce0aff429 | 39 | bufferPOS = (bufferPOS+1) & 0x07F; |
Lugs | 0:ad5ce0aff429 | 40 | } |
Lugs | 0:ad5ce0aff429 | 41 | |
Lugs | 0:ad5ce0aff429 | 42 | int main() |
Lugs | 4:24086b80928e | 43 | { |
Lugs | 0:ad5ce0aff429 | 44 | WavFile Track; |
Lugs | 0:ad5ce0aff429 | 45 | Ticker SampleTime; |
Lugs | 4:24086b80928e | 46 | |
Lugs | 3:fcf745cd4f6d | 47 | daplink.printf("\f---DAPLINK SERIAL PORT---\r\n\r\nMINI PIANO PLAYER ver 1 \r\n\r\n\r\n"); |
Lugs | 0:ad5ce0aff429 | 48 | microUSB.printf("micro USB serial port\r\n"); |
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 | 4:24086b80928e | 56 | |
Lugs | 3:fcf745cd4f6d | 57 | typedef enum { |
Lugs | 3:fcf745cd4f6d | 58 | C2,Cs2,D2,Ds2,E2,F2,Fs2,G2,Gs2,A2,As2,B2, //C2:0 |
Lugs | 3:fcf745cd4f6d | 59 | C3,Cs3,D3,Ds3,E3,F3,Fs3,G3,Gs3,A3,As3,B3, //C3:12 |
Lugs | 3:fcf745cd4f6d | 60 | C4,Cs4,D4,Ds4,E4,F4,Fs4,G4,Gs4,A4,As4,B4, //C4:24 |
Lugs | 3:fcf745cd4f6d | 61 | C5,Cs5,D5,Ds5,E5,F5,Fs5,G5,Gs5,A5,As5,B5, //C5:36 |
Lugs | 3:fcf745cd4f6d | 62 | C6,Cs6,D6,Ds6,E6,F6,Fs6,G6,Gs6, //C6:48 |
Lugs | 3:fcf745cd4f6d | 63 | rest, |
Lugs | 3:fcf745cd4f6d | 64 | END |
Lugs | 4:24086b80928e | 65 | } pitchname; |
Lugs | 4:24086b80928e | 66 | |
Lugs | 3:fcf745cd4f6d | 67 | typedef struct { |
Lugs | 4:24086b80928e | 68 | int length; |
Lugs | 3:fcf745cd4f6d | 69 | pitchname pitch; |
Lugs | 3:fcf745cd4f6d | 70 | } note; |
Lugs | 4:24086b80928e | 71 | |
Lugs | 4:24086b80928e | 72 | //input iterator vars |
Lugs | 4:24086b80928e | 73 | int i; |
Lugs | 4:24086b80928e | 74 | char c; |
Lugs | 3:fcf745cd4f6d | 75 | |
Lugs | 4:24086b80928e | 76 | //input holder arrays |
Lugs | 4:24086b80928e | 77 | char buffer[256]; |
Lugs | 4:24086b80928e | 78 | char inputtype[30]; |
Lugs | 4:24086b80928e | 79 | typedef enum { |
Lugs | 4:24086b80928e | 80 | nokiacomposer, |
Lugs | 4:24086b80928e | 81 | normal |
Lugs | 4:24086b80928e | 82 | } inputtypes; |
Lugs | 4:24086b80928e | 83 | inputtypes current_inputtype; |
Lugs | 4:24086b80928e | 84 | |
Lugs | 4:24086b80928e | 85 | //parse input type |
Lugs | 4:24086b80928e | 86 | int g; |
Lugs | 4:24086b80928e | 87 | tryagain: |
Lugs | 4:24086b80928e | 88 | printf("Please specify the input type.\r\nAvailable types:\r\nnokiacomposer\r\nnormal\r\n\r\n>"); |
Lugs | 4:24086b80928e | 89 | for(i=0; i<30; i++) { |
Lugs | 4:24086b80928e | 90 | c=daplink.getc(); |
Lugs | 4:24086b80928e | 91 | if(c == '\r') |
Lugs | 4:24086b80928e | 92 | { |
Lugs | 4:24086b80928e | 93 | g=i+2; |
Lugs | 4:24086b80928e | 94 | inputtype[i] = '\0'; |
Lugs | 4:24086b80928e | 95 | break; |
Lugs | 4:24086b80928e | 96 | } |
Lugs | 4:24086b80928e | 97 | daplink.putc(c); |
Lugs | 4:24086b80928e | 98 | inputtype[i]=c; |
Lugs | 4:24086b80928e | 99 | } |
Lugs | 4:24086b80928e | 100 | printf("\r\n"); |
Lugs | 4:24086b80928e | 101 | for(i=0;i<=g;i++) |
Lugs | 4:24086b80928e | 102 | { |
Lugs | 4:24086b80928e | 103 | printf("%i\t",inputtype[i]); |
Lugs | 4:24086b80928e | 104 | } |
Lugs | 4:24086b80928e | 105 | printf("\r\nSTRING LITERAL:\r\n"); |
Lugs | 4:24086b80928e | 106 | char test[] = "nokiacomposer"; |
Lugs | 4:24086b80928e | 107 | for(i=0;i<=g;i++) |
Lugs | 4:24086b80928e | 108 | { |
Lugs | 4:24086b80928e | 109 | printf("%i\t",test[i]); |
Lugs | 4:24086b80928e | 110 | } |
Lugs | 4:24086b80928e | 111 | |
Lugs | 4:24086b80928e | 112 | if(!strcmp(inputtype,"nokiacomposer")) { |
Lugs | 4:24086b80928e | 113 | printf("\r\nType chosen: nokiacomposer\r\n"); |
Lugs | 4:24086b80928e | 114 | current_inputtype = nokiacomposer; |
Lugs | 4:24086b80928e | 115 | } else if(!strcmp(inputtype,"normal")) { |
Lugs | 4:24086b80928e | 116 | printf("\r\nType chosen: normal\r\n"); |
Lugs | 4:24086b80928e | 117 | current_inputtype = normal; |
Lugs | 4:24086b80928e | 118 | } else { |
Lugs | 4:24086b80928e | 119 | c=NULL; |
Lugs | 4:24086b80928e | 120 | printf("Invalid input type. Try again."); |
Lugs | 4:24086b80928e | 121 | goto tryagain; //prints message below input area |
Lugs | 4:24086b80928e | 122 | } |
Lugs | 4:24086b80928e | 123 | |
Lugs | 3:fcf745cd4f6d | 124 | //open file |
Lugs | 4:24086b80928e | 125 | toolarge: |
Lugs | 4:24086b80928e | 126 | printf("Please copy the text file of the song then right click on the PuTTY screen.\r\n"); |
Lugs | 4:24086b80928e | 127 | for(i=0; 1; i++) { //do the loop at least once |
Lugs | 4:24086b80928e | 128 | c=daplink.getc(); |
Lugs | 4:24086b80928e | 129 | daplink.putc(c); |
Lugs | 4:24086b80928e | 130 | buffer[i]=c; |
Lugs | 4:24086b80928e | 131 | if(i>=256) { |
Lugs | 4:24086b80928e | 132 | printf("Note data is greater than 2kB! Impossible. Try again.\033[A"); |
Lugs | 4:24086b80928e | 133 | goto toolarge; |
Lugs | 4:24086b80928e | 134 | } |
Lugs | 4:24086b80928e | 135 | wait_ms(1); //wait for the next byte to arrive. every byte takes 1.04ms at 9600 baud. |
Lugs | 4:24086b80928e | 136 | if(!daplink.readable()) |
Lugs | 4:24086b80928e | 137 | { |
Lugs | 4:24086b80928e | 138 | printf("That'sallll\r\n"); |
Lugs | 4:24086b80928e | 139 | break; |
Lugs | 4:24086b80928e | 140 | } |
Lugs | 4:24086b80928e | 141 | } |
Lugs | 4:24086b80928e | 142 | buffer[i] = ' '; //set EOF marker. |
Lugs | 4:24086b80928e | 143 | buffer[i++] = '\0'; |
Lugs | 4:24086b80928e | 144 | |
Lugs | 4:24086b80928e | 145 | //kunwari open na yung file |
Lugs | 4:24086b80928e | 146 | printf("\r\nFile recieved. Parsing...\r\n"); |
Lugs | 3:fcf745cd4f6d | 147 | //parse file into song in heap(remove sharps, put into enums.) |
Lugs | 3:fcf745cd4f6d | 148 | |
Lugs | 4:24086b80928e | 149 | //useful benchmarks to find which characters are valid. |
Lugs | 4:24086b80928e | 150 | int candidate_number; |
Lugs | 4:24086b80928e | 151 | char *songpos=buffer+1; //song position |
Lugs | 4:24086b80928e | 152 | note song[256]; |
Lugs | 4:24086b80928e | 153 | int pn_det; |
Lugs | 7:412761dab446 | 154 | //asdasd |
Lugs | 4:24086b80928e | 155 | |
Lugs | 4:24086b80928e | 156 | if(current_inputtype == nokiacomposer) { |
Lugs | 4:24086b80928e | 157 | //for every [note] slot in [song] |
Lugs | 5:8ba2f1e291b9 | 158 | for(i=0; i<256; i++) { |
Lugs | 4:24086b80928e | 159 | //take the first base 10 integer you see. |
Lugs | 4:24086b80928e | 160 | //this initializes songpos ALWAYS. lol. |
Lugs | 4:24086b80928e | 161 | candidate_number = strtol(songpos-1,&songpos,10); |
Lugs | 4:24086b80928e | 162 | printf("Character:%i\r\n",candidate_number); |
Lugs | 4:24086b80928e | 163 | //if it's 1,2,4,8,16 or 32, VALID. |
Lugs | 4:24086b80928e | 164 | if((candidate_number==1)||candidate_number==2||candidate_number==4||candidate_number==8||candidate_number==16||candidate_number==32) { |
Lugs | 4:24086b80928e | 165 | song[i].length = candidate_number; |
Lugs | 4:24086b80928e | 166 | printf("Entered candidate [%i] into song[%i].length\r\n",candidate_number,i); |
Lugs | 4:24086b80928e | 167 | } else { |
Lugs | 4:24086b80928e | 168 | printf("Found invalid NOTE LENGTH value [%c] at position %li. Skipping until next whitespace...\r\n",*songpos,songpos-buffer); |
Lugs | 4:24086b80928e | 169 | //skip to after next whitespace |
Lugs | 4:24086b80928e | 170 | do { |
Lugs | 4:24086b80928e | 171 | songpos++; |
Lugs | 4:24086b80928e | 172 | } while(!isspace(*(songpos))); |
Lugs | 4:24086b80928e | 173 | do { |
Lugs | 4:24086b80928e | 174 | songpos++; |
Lugs | 4:24086b80928e | 175 | } while(!isalnum(*(songpos))); |
Lugs | 4:24086b80928e | 176 | //then do next iteration of LENGTH,PITCH,OCTAVE parse |
Lugs | 4:24086b80928e | 177 | i--; |
Lugs | 4:24086b80928e | 178 | continue; |
Lugs | 4:24086b80928e | 179 | } |
Lugs | 4:24086b80928e | 180 | //parse next character |
Lugs | 4:24086b80928e | 181 | printf("Character:%c\r\n",*songpos); |
Lugs | 4:24086b80928e | 182 | pn_det=0; |
Lugs | 4:24086b80928e | 183 | //watch out for sharps |
Lugs | 5:8ba2f1e291b9 | 184 | if(!(*songpos=='#'||'a'<=*songpos<='z'||*songpos=='-')) { |
Lugs | 4:24086b80928e | 185 | printf("Found invalid PITCH NAME value [%c] at position %li. Skipping word...\r\n",*songpos,songpos-buffer); |
Lugs | 4:24086b80928e | 186 | //skip to after next whitespace |
Lugs | 4:24086b80928e | 187 | do { |
Lugs | 4:24086b80928e | 188 | songpos++; |
Lugs | 4:24086b80928e | 189 | } while(!isspace(*(songpos))); |
Lugs | 4:24086b80928e | 190 | do { |
Lugs | 4:24086b80928e | 191 | songpos++; |
Lugs | 4:24086b80928e | 192 | } while(!isalnum(*(songpos))); |
Lugs | 4:24086b80928e | 193 | //then do next iteration of LENGTH,PITCH,OCTAVE parse |
Lugs | 4:24086b80928e | 194 | i--; |
Lugs | 4:24086b80928e | 195 | continue; |
Lugs | 4:24086b80928e | 196 | } |
Lugs | 4:24086b80928e | 197 | if(*songpos=='#') { |
Lugs | 4:24086b80928e | 198 | pn_det+=1; |
Lugs | 4:24086b80928e | 199 | songpos++; |
Lugs | 4:24086b80928e | 200 | printf("Sharp found.\r\n"); |
Lugs | 4:24086b80928e | 201 | } |
Lugs | 4:24086b80928e | 202 | switch(*songpos) { |
Lugs | 4:24086b80928e | 203 | case 'c': |
Lugs | 4:24086b80928e | 204 | pn_det+=0; |
Lugs | 4:24086b80928e | 205 | break; |
Lugs | 4:24086b80928e | 206 | case 'd': |
Lugs | 4:24086b80928e | 207 | pn_det+=2; |
Lugs | 4:24086b80928e | 208 | break; |
Lugs | 4:24086b80928e | 209 | case 'e': |
Lugs | 4:24086b80928e | 210 | pn_det+=4; |
Lugs | 4:24086b80928e | 211 | break; |
Lugs | 4:24086b80928e | 212 | case 'f': |
Lugs | 4:24086b80928e | 213 | pn_det+=5; |
Lugs | 4:24086b80928e | 214 | break; |
Lugs | 4:24086b80928e | 215 | case 'g': |
Lugs | 4:24086b80928e | 216 | pn_det+=7; |
Lugs | 4:24086b80928e | 217 | break; |
Lugs | 4:24086b80928e | 218 | case 'a': |
Lugs | 4:24086b80928e | 219 | pn_det+=9; |
Lugs | 4:24086b80928e | 220 | break; |
Lugs | 4:24086b80928e | 221 | case 'b': |
Lugs | 4:24086b80928e | 222 | pn_det+=11; |
Lugs | 4:24086b80928e | 223 | break; |
Lugs | 5:8ba2f1e291b9 | 224 | case '-': |
Lugs | 5:8ba2f1e291b9 | 225 | pn_det=57; |
Lugs | 5:8ba2f1e291b9 | 226 | goto skipoctave; |
Lugs | 4:24086b80928e | 227 | } |
Lugs | 4:24086b80928e | 228 | //check for invalid character at current position |
Lugs | 4:24086b80928e | 229 | //means that character was valid. move on. |
Lugs | 4:24086b80928e | 230 | songpos++; |
Lugs | 4:24086b80928e | 231 | printf("%c\n",*songpos); |
Lugs | 4:24086b80928e | 232 | if('0'<=*songpos<='9') { |
Lugs | 4:24086b80928e | 233 | int num = strtol(songpos,&songpos,10); |
Lugs | 4:24086b80928e | 234 | num+=3;//shift up thrice. board can't handle things this low. |
Lugs | 4:24086b80928e | 235 | printf("octave parsed: %i \t-> adder:%i\r\n",num,((num-2)*12)); |
Lugs | 4:24086b80928e | 236 | pn_det=pn_det+((num-2)*12); |
Lugs | 4:24086b80928e | 237 | } else { |
Lugs | 4:24086b80928e | 238 | printf("Found invalid OCTAVE value [%c] at position %li. Skipping word...\r\n",*songpos,songpos-buffer); |
Lugs | 4:24086b80928e | 239 | //skip to after next whitespace |
Lugs | 4:24086b80928e | 240 | while(!isspace(*(songpos))) { |
Lugs | 4:24086b80928e | 241 | songpos++; |
Lugs | 4:24086b80928e | 242 | } |
Lugs | 4:24086b80928e | 243 | while(!isalnum(*(songpos))) { |
Lugs | 4:24086b80928e | 244 | songpos++; |
Lugs | 4:24086b80928e | 245 | } |
Lugs | 4:24086b80928e | 246 | //then do next iteration of LENGTH,PITCH,OCTAVE parse |
Lugs | 4:24086b80928e | 247 | i--; |
Lugs | 4:24086b80928e | 248 | continue; |
Lugs | 4:24086b80928e | 249 | } |
Lugs | 5:8ba2f1e291b9 | 250 | skipoctave: |
Lugs | 4:24086b80928e | 251 | song[i].pitch = (pitchname)pn_det; |
Lugs | 4:24086b80928e | 252 | printf("Entered pitch [%i] into song[%i].pitch\r\n",song[i].pitch,i); |
Lugs | 4:24086b80928e | 253 | printf("Final: {%i,%i} at i=%i\r\n",song[i].length,song[i].pitch,i); |
Lugs | 4:24086b80928e | 254 | printf("-------------------\r\n"); |
Lugs | 4:24086b80928e | 255 | //make sure you're after the next whitespace |
Lugs | 4:24086b80928e | 256 | while(!isspace(*(songpos))) { |
Lugs | 4:24086b80928e | 257 | songpos++; |
Lugs | 4:24086b80928e | 258 | } |
Lugs | 4:24086b80928e | 259 | while(isspace(*(songpos))) { |
Lugs | 4:24086b80928e | 260 | songpos++; |
Lugs | 4:24086b80928e | 261 | }; |
Lugs | 4:24086b80928e | 262 | if(*songpos == '\0'){ |
Lugs | 4:24086b80928e | 263 | printf("NULL found. Escaping...\r\n"); |
Lugs | 4:24086b80928e | 264 | goto esc; |
Lugs | 4:24086b80928e | 265 | } |
Lugs | 4:24086b80928e | 266 | } |
Lugs | 4:24086b80928e | 267 | } else if(current_inputtype == normal) { |
Lugs | 4:24086b80928e | 268 | //add normaltype parser here. |
Lugs | 4:24086b80928e | 269 | } |
Lugs | 4:24086b80928e | 270 | |
Lugs | 4:24086b80928e | 271 | //add end term |
Lugs | 4:24086b80928e | 272 | |
Lugs | 4:24086b80928e | 273 | //parsing is done |
Lugs | 4:24086b80928e | 274 | esc: |
Lugs | 4:24086b80928e | 275 | |
Lugs | 4:24086b80928e | 276 | printf("at end: i = %i\r\n",i); |
Lugs | 4:24086b80928e | 277 | i++; |
Lugs | 4:24086b80928e | 278 | song[i].length = 1; |
Lugs | 4:24086b80928e | 279 | song[i].pitch = END; |
Lugs | 4:24086b80928e | 280 | |
Lugs | 4:24086b80928e | 281 | printf("Parsing done.\r\n"); |
Lugs | 4:24086b80928e | 282 | |
Lugs | 4:24086b80928e | 283 | //ENDOFPARSER |
Lugs | 4:24086b80928e | 284 | |
Lugs | 4:24086b80928e | 285 | int k; |
Lugs | 4:24086b80928e | 286 | for(k=0; song[k].pitch != END; k++) { |
Lugs | 4:24086b80928e | 287 | printf("{%i,%i},",song[k].length,song[k].pitch); |
Lugs | 4:24086b80928e | 288 | } |
Lugs | 4:24086b80928e | 289 | printf("{%i,%i},",song[k].length,song[k].pitch); |
Lugs | 4:24086b80928e | 290 | printf("\r\nPrinting done.\r\n"); |
Lugs | 4:24086b80928e | 291 | |
Lugs | 0:ad5ce0aff429 | 292 | printf("Generating sine...\r\n"); |
Lugs | 4:24086b80928e | 293 | for(i=0; i<128; i++) { |
Lugs | 0:ad5ce0aff429 | 294 | audioDataBuffer[i] =((1.0 + sin((double(i)/16.0*6.28318530717959)))/2.0); //formula copied from mbed example |
Lugs | 0:ad5ce0aff429 | 295 | } |
Lugs | 4:24086b80928e | 296 | |
Lugs | 4:24086b80928e | 297 | /* HERE'S WHERE THE MUSIC STARTS */ |
Lugs | 4:24086b80928e | 298 | /* LITERALLY. */ |
Lugs | 4:24086b80928e | 299 | |
Lugs | 4:24086b80928e | 300 | //FORMAT: { NUM_BEATS, PITCH } |
Lugs | 4:24086b80928e | 301 | char bpminp[5]; |
Lugs | 4:24086b80928e | 302 | int PlayingFreq; |
Lugs | 4:24086b80928e | 303 | |
Lugs | 4:24086b80928e | 304 | |
Lugs | 4:24086b80928e | 305 | printf("Please enter desired BPM:\r\n"); |
Lugs | 4:24086b80928e | 306 | |
Lugs | 4:24086b80928e | 307 | for(i=0; i<5 && c!='\r'; i++) { |
Lugs | 4:24086b80928e | 308 | c = daplink.getc(); |
Lugs | 4:24086b80928e | 309 | daplink.putc(c); |
Lugs | 4:24086b80928e | 310 | bpminp[i] = c; |
Lugs | 4:24086b80928e | 311 | } |
Lugs | 0:ad5ce0aff429 | 312 | |
Lugs | 4:24086b80928e | 313 | printf("BPM Recieved.\r\n"); |
Lugs | 0:ad5ce0aff429 | 314 | |
Lugs | 4:24086b80928e | 315 | c=NULL; //reset C for next time. |
Lugs | 4:24086b80928e | 316 | int BPM = strtol(bpminp,NULL,0); |
Lugs | 4:24086b80928e | 317 | float SPB = 60*4/(float)BPM; |
Lugs | 3:fcf745cd4f6d | 318 | |
Lugs | 4:24086b80928e | 319 | printf("BPM: %i",BPM); |
Lugs | 0:ad5ce0aff429 | 320 | |
Lugs | 4:24086b80928e | 321 | restart: |
Lugs | 4:24086b80928e | 322 | |
Lugs | 4:24086b80928e | 323 | /* |
Lugs | 4:24086b80928e | 324 | HARVEST MOON SONG IN NORMAL FORMAT |
Lugs | 4:24086b80928e | 325 | |
Lugs | 3:fcf745cd4f6d | 326 | note song[] = { |
Lugs | 3:fcf745cd4f6d | 327 | //batch 1 |
Lugs | 3:fcf745cd4f6d | 328 | {4,(pitchname)38},{4,E5},{4,Fs5},{4,D5},{4,Fs5},{4,G5},{4,A5},{4,Fs5},{4,D5}, |
Lugs | 3:fcf745cd4f6d | 329 | {4,A5},{4,B5},{4,A5},{4,G5},{4,B5},{4,A5}, |
Lugs | 3:fcf745cd4f6d | 330 | {4,rest}, |
Lugs | 3:fcf745cd4f6d | 331 | //batch 2 |
Lugs | 3:fcf745cd4f6d | 332 | {4,D5},{4,E5},{4,Fs5},{4,D5},{4,Fs5},{4,G5},{4,A5},{4,Fs5},{4,D5}, |
Lugs | 3:fcf745cd4f6d | 333 | {4,A5},{4,G5},{4,D5},{4,Fs5},{4,D5},{4,E5}, |
Lugs | 3:fcf745cd4f6d | 334 | {4,rest}, |
Lugs | 3:fcf745cd4f6d | 335 | //batch 1 |
Lugs | 3:fcf745cd4f6d | 336 | {4,D5},{4,E5},{4,Fs5},{4,D5},{4,Fs5},{4,G5},{4,A5},{4,Fs5},{4,D5}, |
Lugs | 3:fcf745cd4f6d | 337 | {4,A5},{4,B5},{4,A5},{4,G5},{4,B5},{4,A5}, |
Lugs | 3:fcf745cd4f6d | 338 | {4,rest}, |
Lugs | 3:fcf745cd4f6d | 339 | //batch 3 |
Lugs | 3:fcf745cd4f6d | 340 | {2,G5},{4,G5},{4,E5},{4,Fs5},{4,E5},{4,D5},{4,Cs5},{4,D5}, |
Lugs | 3:fcf745cd4f6d | 341 | //END |
Lugs | 3:fcf745cd4f6d | 342 | {1,END} |
Lugs | 3:fcf745cd4f6d | 343 | }; |
Lugs | 4:24086b80928e | 344 | */ |
Lugs | 4:24086b80928e | 345 | |
Lugs | 4:24086b80928e | 346 | for(i = 0; 1; i++) { |
Lugs | 4:24086b80928e | 347 | switch(song[i].pitch) { |
Lugs | 3:fcf745cd4f6d | 348 | case rest: |
Lugs | 5:8ba2f1e291b9 | 349 | wait((1/(float)song[i].length)*SPB); |
Lugs | 3:fcf745cd4f6d | 350 | continue; |
Lugs | 3:fcf745cd4f6d | 351 | case D2: |
Lugs | 3:fcf745cd4f6d | 352 | PlayingFreq = 73; |
Lugs | 3:fcf745cd4f6d | 353 | break; |
Lugs | 3:fcf745cd4f6d | 354 | case Ds2: |
Lugs | 3:fcf745cd4f6d | 355 | PlayingFreq = 78; |
Lugs | 3:fcf745cd4f6d | 356 | break; |
Lugs | 3:fcf745cd4f6d | 357 | case E2: |
Lugs | 3:fcf745cd4f6d | 358 | PlayingFreq = 82; |
Lugs | 3:fcf745cd4f6d | 359 | break; |
Lugs | 3:fcf745cd4f6d | 360 | case F2: |
Lugs | 3:fcf745cd4f6d | 361 | PlayingFreq = 87; |
Lugs | 3:fcf745cd4f6d | 362 | break; |
Lugs | 3:fcf745cd4f6d | 363 | case Fs2: |
Lugs | 3:fcf745cd4f6d | 364 | PlayingFreq = 92; |
Lugs | 3:fcf745cd4f6d | 365 | break; |
Lugs | 3:fcf745cd4f6d | 366 | case G2: |
Lugs | 2:93da96b41127 | 367 | PlayingFreq = 98; |
Lugs | 2:93da96b41127 | 368 | break; |
Lugs | 3:fcf745cd4f6d | 369 | case Gs2: |
Lugs | 3:fcf745cd4f6d | 370 | PlayingFreq = 104; |
Lugs | 2:93da96b41127 | 371 | break; |
Lugs | 3:fcf745cd4f6d | 372 | case A2: |
Lugs | 2:93da96b41127 | 373 | PlayingFreq = 110; |
Lugs | 2:93da96b41127 | 374 | break; |
Lugs | 3:fcf745cd4f6d | 375 | case As2: |
Lugs | 2:93da96b41127 | 376 | PlayingFreq = 117; |
Lugs | 2:93da96b41127 | 377 | break; |
Lugs | 3:fcf745cd4f6d | 378 | case B2: |
Lugs | 2:93da96b41127 | 379 | PlayingFreq = 123; |
Lugs | 2:93da96b41127 | 380 | break; |
Lugs | 3:fcf745cd4f6d | 381 | case C3: |
Lugs | 2:93da96b41127 | 382 | PlayingFreq = 131; |
Lugs | 2:93da96b41127 | 383 | break; |
Lugs | 3:fcf745cd4f6d | 384 | case Cs3: |
Lugs | 2:93da96b41127 | 385 | PlayingFreq = 139; |
Lugs | 2:93da96b41127 | 386 | break; |
Lugs | 3:fcf745cd4f6d | 387 | case D3: |
Lugs | 2:93da96b41127 | 388 | PlayingFreq = 147; |
Lugs | 2:93da96b41127 | 389 | break; |
Lugs | 3:fcf745cd4f6d | 390 | case Ds3: |
Lugs | 2:93da96b41127 | 391 | PlayingFreq = 156; |
Lugs | 2:93da96b41127 | 392 | break; |
Lugs | 3:fcf745cd4f6d | 393 | case E3: |
Lugs | 2:93da96b41127 | 394 | PlayingFreq = 165; |
Lugs | 2:93da96b41127 | 395 | break; |
Lugs | 3:fcf745cd4f6d | 396 | case F3: |
Lugs | 2:93da96b41127 | 397 | PlayingFreq = 175; |
Lugs | 2:93da96b41127 | 398 | break; |
Lugs | 3:fcf745cd4f6d | 399 | case Fs3: |
Lugs | 2:93da96b41127 | 400 | PlayingFreq = 185; |
Lugs | 2:93da96b41127 | 401 | break; |
Lugs | 3:fcf745cd4f6d | 402 | case G3: |
Lugs | 2:93da96b41127 | 403 | PlayingFreq = 196; |
Lugs | 2:93da96b41127 | 404 | break; |
Lugs | 3:fcf745cd4f6d | 405 | case Gs3: |
Lugs | 2:93da96b41127 | 406 | PlayingFreq = 208; |
Lugs | 2:93da96b41127 | 407 | break; |
Lugs | 3:fcf745cd4f6d | 408 | case A3: |
Lugs | 2:93da96b41127 | 409 | PlayingFreq = 220; |
Lugs | 2:93da96b41127 | 410 | break; |
Lugs | 3:fcf745cd4f6d | 411 | case As3: |
Lugs | 2:93da96b41127 | 412 | PlayingFreq = 233; |
Lugs | 2:93da96b41127 | 413 | break; |
Lugs | 3:fcf745cd4f6d | 414 | case B3: |
Lugs | 2:93da96b41127 | 415 | PlayingFreq = 247; |
Lugs | 2:93da96b41127 | 416 | break; |
Lugs | 3:fcf745cd4f6d | 417 | case C4: |
Lugs | 2:93da96b41127 | 418 | PlayingFreq = 262; |
Lugs | 2:93da96b41127 | 419 | break; |
Lugs | 3:fcf745cd4f6d | 420 | case Cs4: |
Lugs | 2:93da96b41127 | 421 | PlayingFreq = 277; |
Lugs | 2:93da96b41127 | 422 | break; |
Lugs | 3:fcf745cd4f6d | 423 | case D4: |
Lugs | 2:93da96b41127 | 424 | PlayingFreq = 294; |
Lugs | 2:93da96b41127 | 425 | break; |
Lugs | 3:fcf745cd4f6d | 426 | case Ds4: |
Lugs | 2:93da96b41127 | 427 | PlayingFreq = 311; |
Lugs | 2:93da96b41127 | 428 | break; |
Lugs | 3:fcf745cd4f6d | 429 | case E4: |
Lugs | 2:93da96b41127 | 430 | PlayingFreq = 330; |
Lugs | 2:93da96b41127 | 431 | break; |
Lugs | 3:fcf745cd4f6d | 432 | case F4: |
Lugs | 2:93da96b41127 | 433 | PlayingFreq = 349; |
Lugs | 2:93da96b41127 | 434 | break; |
Lugs | 3:fcf745cd4f6d | 435 | case Fs4: |
Lugs | 2:93da96b41127 | 436 | PlayingFreq = 370; |
Lugs | 2:93da96b41127 | 437 | break; |
Lugs | 3:fcf745cd4f6d | 438 | case G4: |
Lugs | 2:93da96b41127 | 439 | PlayingFreq = 392; |
Lugs | 2:93da96b41127 | 440 | break; |
Lugs | 3:fcf745cd4f6d | 441 | case Gs4: |
Lugs | 2:93da96b41127 | 442 | PlayingFreq = 415; |
Lugs | 2:93da96b41127 | 443 | break; |
Lugs | 3:fcf745cd4f6d | 444 | case A4: |
Lugs | 2:93da96b41127 | 445 | PlayingFreq = 440; |
Lugs | 2:93da96b41127 | 446 | break; |
Lugs | 3:fcf745cd4f6d | 447 | case As4: |
Lugs | 2:93da96b41127 | 448 | PlayingFreq = 466; |
Lugs | 2:93da96b41127 | 449 | break; |
Lugs | 3:fcf745cd4f6d | 450 | case B4: |
Lugs | 2:93da96b41127 | 451 | PlayingFreq = 494; |
Lugs | 2:93da96b41127 | 452 | break; |
Lugs | 3:fcf745cd4f6d | 453 | case C5: |
Lugs | 2:93da96b41127 | 454 | PlayingFreq = 523; |
Lugs | 2:93da96b41127 | 455 | break; |
Lugs | 3:fcf745cd4f6d | 456 | case Cs5: |
Lugs | 2:93da96b41127 | 457 | PlayingFreq = 554; |
Lugs | 2:93da96b41127 | 458 | break; |
Lugs | 3:fcf745cd4f6d | 459 | case D5: |
Lugs | 2:93da96b41127 | 460 | PlayingFreq = 587; |
Lugs | 2:93da96b41127 | 461 | break; |
Lugs | 3:fcf745cd4f6d | 462 | case Ds5: |
Lugs | 2:93da96b41127 | 463 | PlayingFreq = 622; |
Lugs | 2:93da96b41127 | 464 | break; |
Lugs | 3:fcf745cd4f6d | 465 | case E5: |
Lugs | 2:93da96b41127 | 466 | PlayingFreq = 659; |
Lugs | 2:93da96b41127 | 467 | break; |
Lugs | 3:fcf745cd4f6d | 468 | case F5: |
Lugs | 2:93da96b41127 | 469 | PlayingFreq = 698; |
Lugs | 2:93da96b41127 | 470 | break; |
Lugs | 3:fcf745cd4f6d | 471 | case Fs5: |
Lugs | 2:93da96b41127 | 472 | PlayingFreq = 740; |
Lugs | 2:93da96b41127 | 473 | break; |
Lugs | 3:fcf745cd4f6d | 474 | case G5: |
Lugs | 2:93da96b41127 | 475 | PlayingFreq = 784; |
Lugs | 2:93da96b41127 | 476 | break; |
Lugs | 3:fcf745cd4f6d | 477 | case Gs5: |
Lugs | 2:93da96b41127 | 478 | PlayingFreq = 831; |
Lugs | 2:93da96b41127 | 479 | break; |
Lugs | 3:fcf745cd4f6d | 480 | case A5: |
Lugs | 2:93da96b41127 | 481 | PlayingFreq = 880; |
Lugs | 2:93da96b41127 | 482 | break; |
Lugs | 3:fcf745cd4f6d | 483 | case As5: |
Lugs | 2:93da96b41127 | 484 | PlayingFreq = 932; |
Lugs | 2:93da96b41127 | 485 | break; |
Lugs | 3:fcf745cd4f6d | 486 | case B5: |
Lugs | 2:93da96b41127 | 487 | PlayingFreq = 988; |
Lugs | 2:93da96b41127 | 488 | break; |
Lugs | 3:fcf745cd4f6d | 489 | case C6: |
Lugs | 2:93da96b41127 | 490 | PlayingFreq = 1047; |
Lugs | 2:93da96b41127 | 491 | break; |
Lugs | 3:fcf745cd4f6d | 492 | case Cs6: |
Lugs | 2:93da96b41127 | 493 | PlayingFreq = 1109; |
Lugs | 2:93da96b41127 | 494 | break; |
Lugs | 3:fcf745cd4f6d | 495 | case D6: |
Lugs | 2:93da96b41127 | 496 | PlayingFreq = 1175; |
Lugs | 2:93da96b41127 | 497 | break; |
Lugs | 3:fcf745cd4f6d | 498 | case Ds6: |
Lugs | 2:93da96b41127 | 499 | PlayingFreq = 1245; |
Lugs | 2:93da96b41127 | 500 | break; |
Lugs | 3:fcf745cd4f6d | 501 | case E6: |
Lugs | 2:93da96b41127 | 502 | PlayingFreq = 1319; |
Lugs | 2:93da96b41127 | 503 | break; |
Lugs | 3:fcf745cd4f6d | 504 | case F6: |
Lugs | 2:93da96b41127 | 505 | PlayingFreq = 1397; |
Lugs | 2:93da96b41127 | 506 | break; |
Lugs | 3:fcf745cd4f6d | 507 | case Fs6: |
Lugs | 2:93da96b41127 | 508 | PlayingFreq = 1480; |
Lugs | 2:93da96b41127 | 509 | break; |
Lugs | 3:fcf745cd4f6d | 510 | case G6: |
Lugs | 2:93da96b41127 | 511 | PlayingFreq = 1568; |
Lugs | 2:93da96b41127 | 512 | break; |
Lugs | 3:fcf745cd4f6d | 513 | case Gs6: |
Lugs | 2:93da96b41127 | 514 | PlayingFreq = 1661; |
Lugs | 2:93da96b41127 | 515 | break; |
Lugs | 3:fcf745cd4f6d | 516 | case END: |
Lugs | 3:fcf745cd4f6d | 517 | i = 0; |
Lugs | 4:24086b80928e | 518 | printf("SONG END. PRESS BUTTON TO RESTART."); |
Lugs | 4:24086b80928e | 519 | while(Button) |
Lugs | 4:24086b80928e | 520 | { |
Lugs | 5:8ba2f1e291b9 | 521 | wait_ms(2); |
Lugs | 4:24086b80928e | 522 | } |
Lugs | 3:fcf745cd4f6d | 523 | goto restart; |
Lugs | 2:93da96b41127 | 524 | } |
Lugs | 4:24086b80928e | 525 | |
Lugs | 3:fcf745cd4f6d | 526 | Track.sampleRate = PlayingFreq * 16; //TONE FREQ = SAMPLE RATE / SAMPLES PER CYCLE |
Lugs | 3:fcf745cd4f6d | 527 | PWM.period_us(1); //1MHz |
Lugs | 3:fcf745cd4f6d | 528 | float ticker_period = (float) 1/(Track.sampleRate); |
Lugs | 3:fcf745cd4f6d | 529 | 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 | 530 | |
Lugs | 3:fcf745cd4f6d | 531 | SampleTime.attach(&placeNewSample,ticker_period); |
Lugs | 4:24086b80928e | 532 | wait( (1/(float)song[i].length) *SPB); |
Lugs | 3:fcf745cd4f6d | 533 | SampleTime.detach(); |
Lugs | 1:78ca0566c062 | 534 | |
Lugs | 4:24086b80928e | 535 | |
Lugs | 4:24086b80928e | 536 | |
Lugs | 2:93da96b41127 | 537 | printf("\033[A\033[A\033[A\033[A"); |
Lugs | 0:ad5ce0aff429 | 538 | } |
Lugs | 3:fcf745cd4f6d | 539 | } |
Lugs | 0:ad5ce0aff429 | 540 |