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@4:24086b80928e, 2019-07-19 (annotated)
- Committer:
 - Lugs
 - Date:
 - Fri Jul 19 04:47:38 2019 +0000
 - Revision:
 - 4:24086b80928e
 - Parent:
 - 3:fcf745cd4f6d
 - Child:
 - 5:8ba2f1e291b9
 
wait_ms is 1ms to wait for the bytes to arrive. Manages to get all of fur elise in the song. LACKING: rest.
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 | 4:24086b80928e | 154 | |
| Lugs | 4:24086b80928e | 155 | if(current_inputtype == nokiacomposer) { | 
| Lugs | 4:24086b80928e | 156 | //for every [note] slot in [song] | 
| Lugs | 4:24086b80928e | 157 | for(i=0; i<2048; i++) { | 
| Lugs | 4:24086b80928e | 158 | //take the first base 10 integer you see. | 
| Lugs | 4:24086b80928e | 159 | //this initializes songpos ALWAYS. lol. | 
| Lugs | 4:24086b80928e | 160 | candidate_number = strtol(songpos-1,&songpos,10); | 
| Lugs | 4:24086b80928e | 161 | printf("Character:%i\r\n",candidate_number); | 
| Lugs | 4:24086b80928e | 162 | //if it's 1,2,4,8,16 or 32, VALID. | 
| Lugs | 4:24086b80928e | 163 | if((candidate_number==1)||candidate_number==2||candidate_number==4||candidate_number==8||candidate_number==16||candidate_number==32) { | 
| Lugs | 4:24086b80928e | 164 | song[i].length = candidate_number; | 
| Lugs | 4:24086b80928e | 165 | printf("Entered candidate [%i] into song[%i].length\r\n",candidate_number,i); | 
| Lugs | 4:24086b80928e | 166 | } else { | 
| Lugs | 4:24086b80928e | 167 | printf("Found invalid NOTE LENGTH value [%c] at position %li. Skipping until next whitespace...\r\n",*songpos,songpos-buffer); | 
| Lugs | 4:24086b80928e | 168 | //skip to after next whitespace | 
| Lugs | 4:24086b80928e | 169 | do { | 
| Lugs | 4:24086b80928e | 170 | songpos++; | 
| Lugs | 4:24086b80928e | 171 | } while(!isspace(*(songpos))); | 
| Lugs | 4:24086b80928e | 172 | do { | 
| Lugs | 4:24086b80928e | 173 | songpos++; | 
| Lugs | 4:24086b80928e | 174 | } while(!isalnum(*(songpos))); | 
| Lugs | 4:24086b80928e | 175 | //then do next iteration of LENGTH,PITCH,OCTAVE parse | 
| Lugs | 4:24086b80928e | 176 | i--; | 
| Lugs | 4:24086b80928e | 177 | continue; | 
| Lugs | 4:24086b80928e | 178 | } | 
| Lugs | 4:24086b80928e | 179 | //parse next character | 
| Lugs | 4:24086b80928e | 180 | printf("Character:%c\r\n",*songpos); | 
| Lugs | 4:24086b80928e | 181 | pn_det=0; | 
| Lugs | 4:24086b80928e | 182 | //watch out for sharps | 
| Lugs | 4:24086b80928e | 183 | if(!(*songpos=='#'||'a'<=*songpos<='z')) { | 
| Lugs | 4:24086b80928e | 184 | printf("Found invalid PITCH NAME value [%c] at position %li. Skipping word...\r\n",*songpos,songpos-buffer); | 
| Lugs | 4:24086b80928e | 185 | //skip to after next whitespace | 
| Lugs | 4:24086b80928e | 186 | do { | 
| Lugs | 4:24086b80928e | 187 | songpos++; | 
| Lugs | 4:24086b80928e | 188 | } while(!isspace(*(songpos))); | 
| Lugs | 4:24086b80928e | 189 | do { | 
| Lugs | 4:24086b80928e | 190 | songpos++; | 
| Lugs | 4:24086b80928e | 191 | } while(!isalnum(*(songpos))); | 
| Lugs | 4:24086b80928e | 192 | //then do next iteration of LENGTH,PITCH,OCTAVE parse | 
| Lugs | 4:24086b80928e | 193 | i--; | 
| Lugs | 4:24086b80928e | 194 | continue; | 
| Lugs | 4:24086b80928e | 195 | } | 
| Lugs | 4:24086b80928e | 196 | if(*songpos=='#') { | 
| Lugs | 4:24086b80928e | 197 | pn_det+=1; | 
| Lugs | 4:24086b80928e | 198 | songpos++; | 
| Lugs | 4:24086b80928e | 199 | printf("Sharp found.\r\n"); | 
| Lugs | 4:24086b80928e | 200 | } | 
| Lugs | 4:24086b80928e | 201 | switch(*songpos) { | 
| Lugs | 4:24086b80928e | 202 | case 'c': | 
| Lugs | 4:24086b80928e | 203 | pn_det+=0; | 
| Lugs | 4:24086b80928e | 204 | break; | 
| Lugs | 4:24086b80928e | 205 | case 'd': | 
| Lugs | 4:24086b80928e | 206 | pn_det+=2; | 
| Lugs | 4:24086b80928e | 207 | break; | 
| Lugs | 4:24086b80928e | 208 | case 'e': | 
| Lugs | 4:24086b80928e | 209 | pn_det+=4; | 
| Lugs | 4:24086b80928e | 210 | break; | 
| Lugs | 4:24086b80928e | 211 | case 'f': | 
| Lugs | 4:24086b80928e | 212 | pn_det+=5; | 
| Lugs | 4:24086b80928e | 213 | break; | 
| Lugs | 4:24086b80928e | 214 | case 'g': | 
| Lugs | 4:24086b80928e | 215 | pn_det+=7; | 
| Lugs | 4:24086b80928e | 216 | break; | 
| Lugs | 4:24086b80928e | 217 | case 'a': | 
| Lugs | 4:24086b80928e | 218 | pn_det+=9; | 
| Lugs | 4:24086b80928e | 219 | break; | 
| Lugs | 4:24086b80928e | 220 | case 'b': | 
| Lugs | 4:24086b80928e | 221 | pn_det+=11; | 
| Lugs | 4:24086b80928e | 222 | break; | 
| Lugs | 4:24086b80928e | 223 | } | 
| Lugs | 4:24086b80928e | 224 | //check for invalid character at current position | 
| Lugs | 4:24086b80928e | 225 | //means that character was valid. move on. | 
| Lugs | 4:24086b80928e | 226 | songpos++; | 
| Lugs | 4:24086b80928e | 227 | printf("%c\n",*songpos); | 
| Lugs | 4:24086b80928e | 228 | if('0'<=*songpos<='9') { | 
| Lugs | 4:24086b80928e | 229 | int num = strtol(songpos,&songpos,10); | 
| Lugs | 4:24086b80928e | 230 | num+=3;//shift up thrice. board can't handle things this low. | 
| Lugs | 4:24086b80928e | 231 | printf("octave parsed: %i \t-> adder:%i\r\n",num,((num-2)*12)); | 
| Lugs | 4:24086b80928e | 232 | pn_det=pn_det+((num-2)*12); | 
| Lugs | 4:24086b80928e | 233 | } else { | 
| Lugs | 4:24086b80928e | 234 | printf("Found invalid OCTAVE value [%c] at position %li. Skipping word...\r\n",*songpos,songpos-buffer); | 
| Lugs | 4:24086b80928e | 235 | //skip to after next whitespace | 
| Lugs | 4:24086b80928e | 236 | while(!isspace(*(songpos))) { | 
| Lugs | 4:24086b80928e | 237 | songpos++; | 
| Lugs | 4:24086b80928e | 238 | } | 
| Lugs | 4:24086b80928e | 239 | while(!isalnum(*(songpos))) { | 
| Lugs | 4:24086b80928e | 240 | songpos++; | 
| Lugs | 4:24086b80928e | 241 | } | 
| Lugs | 4:24086b80928e | 242 | //then do next iteration of LENGTH,PITCH,OCTAVE parse | 
| Lugs | 4:24086b80928e | 243 | i--; | 
| Lugs | 4:24086b80928e | 244 | continue; | 
| Lugs | 4:24086b80928e | 245 | } | 
| Lugs | 4:24086b80928e | 246 | song[i].pitch = (pitchname)pn_det; | 
| Lugs | 4:24086b80928e | 247 | printf("Entered pitch [%i] into song[%i].pitch\r\n",song[i].pitch,i); | 
| Lugs | 4:24086b80928e | 248 | printf("Final: {%i,%i} at i=%i\r\n",song[i].length,song[i].pitch,i); | 
| Lugs | 4:24086b80928e | 249 | printf("-------------------\r\n"); | 
| Lugs | 4:24086b80928e | 250 | //make sure you're after the next whitespace | 
| Lugs | 4:24086b80928e | 251 | while(!isspace(*(songpos))) { | 
| Lugs | 4:24086b80928e | 252 | songpos++; | 
| Lugs | 4:24086b80928e | 253 | } | 
| Lugs | 4:24086b80928e | 254 | while(isspace(*(songpos))) { | 
| Lugs | 4:24086b80928e | 255 | songpos++; | 
| Lugs | 4:24086b80928e | 256 | }; | 
| Lugs | 4:24086b80928e | 257 | if(*songpos == '\0'){ | 
| Lugs | 4:24086b80928e | 258 | printf("NULL found. Escaping...\r\n"); | 
| Lugs | 4:24086b80928e | 259 | goto esc; | 
| Lugs | 4:24086b80928e | 260 | } | 
| Lugs | 4:24086b80928e | 261 | } | 
| Lugs | 4:24086b80928e | 262 | } else if(current_inputtype == normal) { | 
| Lugs | 4:24086b80928e | 263 | //add normaltype parser here. | 
| Lugs | 4:24086b80928e | 264 | } | 
| Lugs | 4:24086b80928e | 265 | |
| Lugs | 4:24086b80928e | 266 | //add end term | 
| Lugs | 4:24086b80928e | 267 | |
| Lugs | 4:24086b80928e | 268 | //parsing is done | 
| Lugs | 4:24086b80928e | 269 | esc: | 
| Lugs | 4:24086b80928e | 270 | |
| Lugs | 4:24086b80928e | 271 | printf("at end: i = %i\r\n",i); | 
| Lugs | 4:24086b80928e | 272 | i++; | 
| Lugs | 4:24086b80928e | 273 | song[i].length = 1; | 
| Lugs | 4:24086b80928e | 274 | song[i].pitch = END; | 
| Lugs | 4:24086b80928e | 275 | |
| Lugs | 4:24086b80928e | 276 | printf("Parsing done.\r\n"); | 
| Lugs | 4:24086b80928e | 277 | |
| Lugs | 4:24086b80928e | 278 | //ENDOFPARSER | 
| Lugs | 4:24086b80928e | 279 | |
| Lugs | 4:24086b80928e | 280 | int k; | 
| Lugs | 4:24086b80928e | 281 | for(k=0; song[k].pitch != END; k++) { | 
| Lugs | 4:24086b80928e | 282 | printf("{%i,%i},",song[k].length,song[k].pitch); | 
| Lugs | 4:24086b80928e | 283 | } | 
| Lugs | 4:24086b80928e | 284 | printf("{%i,%i},",song[k].length,song[k].pitch); | 
| Lugs | 4:24086b80928e | 285 | printf("\r\nPrinting done.\r\n"); | 
| Lugs | 4:24086b80928e | 286 | |
| Lugs | 0:ad5ce0aff429 | 287 | printf("Generating sine...\r\n"); | 
| Lugs | 4:24086b80928e | 288 | for(i=0; i<128; i++) { | 
| Lugs | 0:ad5ce0aff429 | 289 | audioDataBuffer[i] =((1.0 + sin((double(i)/16.0*6.28318530717959)))/2.0); //formula copied from mbed example | 
| Lugs | 0:ad5ce0aff429 | 290 | } | 
| Lugs | 4:24086b80928e | 291 | |
| Lugs | 4:24086b80928e | 292 | /* HERE'S WHERE THE MUSIC STARTS */ | 
| Lugs | 4:24086b80928e | 293 | /* LITERALLY. */ | 
| Lugs | 4:24086b80928e | 294 | |
| Lugs | 4:24086b80928e | 295 | //FORMAT: { NUM_BEATS, PITCH } | 
| Lugs | 4:24086b80928e | 296 | char bpminp[5]; | 
| Lugs | 4:24086b80928e | 297 | int PlayingFreq; | 
| Lugs | 4:24086b80928e | 298 | |
| Lugs | 4:24086b80928e | 299 | |
| Lugs | 4:24086b80928e | 300 | printf("Please enter desired BPM:\r\n"); | 
| Lugs | 4:24086b80928e | 301 | |
| Lugs | 4:24086b80928e | 302 | for(i=0; i<5 && c!='\r'; i++) { | 
| Lugs | 4:24086b80928e | 303 | c = daplink.getc(); | 
| Lugs | 4:24086b80928e | 304 | daplink.putc(c); | 
| Lugs | 4:24086b80928e | 305 | bpminp[i] = c; | 
| Lugs | 4:24086b80928e | 306 | } | 
| Lugs | 0:ad5ce0aff429 | 307 | |
| Lugs | 4:24086b80928e | 308 | printf("BPM Recieved.\r\n"); | 
| Lugs | 0:ad5ce0aff429 | 309 | |
| Lugs | 4:24086b80928e | 310 | c=NULL; //reset C for next time. | 
| Lugs | 4:24086b80928e | 311 | int BPM = strtol(bpminp,NULL,0); | 
| Lugs | 4:24086b80928e | 312 | float SPB = 60*4/(float)BPM; | 
| Lugs | 3:fcf745cd4f6d | 313 | |
| Lugs | 4:24086b80928e | 314 | printf("BPM: %i",BPM); | 
| Lugs | 0:ad5ce0aff429 | 315 | |
| Lugs | 4:24086b80928e | 316 | restart: | 
| Lugs | 4:24086b80928e | 317 | |
| Lugs | 4:24086b80928e | 318 | /* | 
| Lugs | 4:24086b80928e | 319 | HARVEST MOON SONG IN NORMAL FORMAT | 
| Lugs | 4:24086b80928e | 320 | |
| Lugs | 3:fcf745cd4f6d | 321 | note song[] = { | 
| Lugs | 3:fcf745cd4f6d | 322 | //batch 1 | 
| Lugs | 3:fcf745cd4f6d | 323 | {4,(pitchname)38},{4,E5},{4,Fs5},{4,D5},{4,Fs5},{4,G5},{4,A5},{4,Fs5},{4,D5}, | 
| Lugs | 3:fcf745cd4f6d | 324 | {4,A5},{4,B5},{4,A5},{4,G5},{4,B5},{4,A5}, | 
| Lugs | 3:fcf745cd4f6d | 325 | {4,rest}, | 
| Lugs | 3:fcf745cd4f6d | 326 | //batch 2 | 
| Lugs | 3:fcf745cd4f6d | 327 | {4,D5},{4,E5},{4,Fs5},{4,D5},{4,Fs5},{4,G5},{4,A5},{4,Fs5},{4,D5}, | 
| Lugs | 3:fcf745cd4f6d | 328 | {4,A5},{4,G5},{4,D5},{4,Fs5},{4,D5},{4,E5}, | 
| Lugs | 3:fcf745cd4f6d | 329 | {4,rest}, | 
| Lugs | 3:fcf745cd4f6d | 330 | //batch 1 | 
| Lugs | 3:fcf745cd4f6d | 331 | {4,D5},{4,E5},{4,Fs5},{4,D5},{4,Fs5},{4,G5},{4,A5},{4,Fs5},{4,D5}, | 
| Lugs | 3:fcf745cd4f6d | 332 | {4,A5},{4,B5},{4,A5},{4,G5},{4,B5},{4,A5}, | 
| Lugs | 3:fcf745cd4f6d | 333 | {4,rest}, | 
| Lugs | 3:fcf745cd4f6d | 334 | //batch 3 | 
| Lugs | 3:fcf745cd4f6d | 335 | {2,G5},{4,G5},{4,E5},{4,Fs5},{4,E5},{4,D5},{4,Cs5},{4,D5}, | 
| Lugs | 3:fcf745cd4f6d | 336 | //END | 
| Lugs | 3:fcf745cd4f6d | 337 | {1,END} | 
| Lugs | 3:fcf745cd4f6d | 338 | }; | 
| Lugs | 4:24086b80928e | 339 | */ | 
| Lugs | 4:24086b80928e | 340 | |
| Lugs | 4:24086b80928e | 341 | for(i = 0; 1; i++) { | 
| Lugs | 4:24086b80928e | 342 | switch(song[i].pitch) { | 
| Lugs | 3:fcf745cd4f6d | 343 | case rest: | 
| Lugs | 3:fcf745cd4f6d | 344 | wait(song[i].length*SPB); | 
| Lugs | 3:fcf745cd4f6d | 345 | continue; | 
| Lugs | 3:fcf745cd4f6d | 346 | case D2: | 
| Lugs | 3:fcf745cd4f6d | 347 | PlayingFreq = 73; | 
| Lugs | 3:fcf745cd4f6d | 348 | break; | 
| Lugs | 3:fcf745cd4f6d | 349 | case Ds2: | 
| Lugs | 3:fcf745cd4f6d | 350 | PlayingFreq = 78; | 
| Lugs | 3:fcf745cd4f6d | 351 | break; | 
| Lugs | 3:fcf745cd4f6d | 352 | case E2: | 
| Lugs | 3:fcf745cd4f6d | 353 | PlayingFreq = 82; | 
| Lugs | 3:fcf745cd4f6d | 354 | break; | 
| Lugs | 3:fcf745cd4f6d | 355 | case F2: | 
| Lugs | 3:fcf745cd4f6d | 356 | PlayingFreq = 87; | 
| Lugs | 3:fcf745cd4f6d | 357 | break; | 
| Lugs | 3:fcf745cd4f6d | 358 | case Fs2: | 
| Lugs | 3:fcf745cd4f6d | 359 | PlayingFreq = 92; | 
| Lugs | 3:fcf745cd4f6d | 360 | break; | 
| Lugs | 3:fcf745cd4f6d | 361 | case G2: | 
| Lugs | 2:93da96b41127 | 362 | PlayingFreq = 98; | 
| Lugs | 2:93da96b41127 | 363 | break; | 
| Lugs | 3:fcf745cd4f6d | 364 | case Gs2: | 
| Lugs | 3:fcf745cd4f6d | 365 | PlayingFreq = 104; | 
| Lugs | 2:93da96b41127 | 366 | break; | 
| Lugs | 3:fcf745cd4f6d | 367 | case A2: | 
| Lugs | 2:93da96b41127 | 368 | PlayingFreq = 110; | 
| Lugs | 2:93da96b41127 | 369 | break; | 
| Lugs | 3:fcf745cd4f6d | 370 | case As2: | 
| Lugs | 2:93da96b41127 | 371 | PlayingFreq = 117; | 
| Lugs | 2:93da96b41127 | 372 | break; | 
| Lugs | 3:fcf745cd4f6d | 373 | case B2: | 
| Lugs | 2:93da96b41127 | 374 | PlayingFreq = 123; | 
| Lugs | 2:93da96b41127 | 375 | break; | 
| Lugs | 3:fcf745cd4f6d | 376 | case C3: | 
| Lugs | 2:93da96b41127 | 377 | PlayingFreq = 131; | 
| Lugs | 2:93da96b41127 | 378 | break; | 
| Lugs | 3:fcf745cd4f6d | 379 | case Cs3: | 
| Lugs | 2:93da96b41127 | 380 | PlayingFreq = 139; | 
| Lugs | 2:93da96b41127 | 381 | break; | 
| Lugs | 3:fcf745cd4f6d | 382 | case D3: | 
| Lugs | 2:93da96b41127 | 383 | PlayingFreq = 147; | 
| Lugs | 2:93da96b41127 | 384 | break; | 
| Lugs | 3:fcf745cd4f6d | 385 | case Ds3: | 
| Lugs | 2:93da96b41127 | 386 | PlayingFreq = 156; | 
| Lugs | 2:93da96b41127 | 387 | break; | 
| Lugs | 3:fcf745cd4f6d | 388 | case E3: | 
| Lugs | 2:93da96b41127 | 389 | PlayingFreq = 165; | 
| Lugs | 2:93da96b41127 | 390 | break; | 
| Lugs | 3:fcf745cd4f6d | 391 | case F3: | 
| Lugs | 2:93da96b41127 | 392 | PlayingFreq = 175; | 
| Lugs | 2:93da96b41127 | 393 | break; | 
| Lugs | 3:fcf745cd4f6d | 394 | case Fs3: | 
| Lugs | 2:93da96b41127 | 395 | PlayingFreq = 185; | 
| Lugs | 2:93da96b41127 | 396 | break; | 
| Lugs | 3:fcf745cd4f6d | 397 | case G3: | 
| Lugs | 2:93da96b41127 | 398 | PlayingFreq = 196; | 
| Lugs | 2:93da96b41127 | 399 | break; | 
| Lugs | 3:fcf745cd4f6d | 400 | case Gs3: | 
| Lugs | 2:93da96b41127 | 401 | PlayingFreq = 208; | 
| Lugs | 2:93da96b41127 | 402 | break; | 
| Lugs | 3:fcf745cd4f6d | 403 | case A3: | 
| Lugs | 2:93da96b41127 | 404 | PlayingFreq = 220; | 
| Lugs | 2:93da96b41127 | 405 | break; | 
| Lugs | 3:fcf745cd4f6d | 406 | case As3: | 
| Lugs | 2:93da96b41127 | 407 | PlayingFreq = 233; | 
| Lugs | 2:93da96b41127 | 408 | break; | 
| Lugs | 3:fcf745cd4f6d | 409 | case B3: | 
| Lugs | 2:93da96b41127 | 410 | PlayingFreq = 247; | 
| Lugs | 2:93da96b41127 | 411 | break; | 
| Lugs | 3:fcf745cd4f6d | 412 | case C4: | 
| Lugs | 2:93da96b41127 | 413 | PlayingFreq = 262; | 
| Lugs | 2:93da96b41127 | 414 | break; | 
| Lugs | 3:fcf745cd4f6d | 415 | case Cs4: | 
| Lugs | 2:93da96b41127 | 416 | PlayingFreq = 277; | 
| Lugs | 2:93da96b41127 | 417 | break; | 
| Lugs | 3:fcf745cd4f6d | 418 | case D4: | 
| Lugs | 2:93da96b41127 | 419 | PlayingFreq = 294; | 
| Lugs | 2:93da96b41127 | 420 | break; | 
| Lugs | 3:fcf745cd4f6d | 421 | case Ds4: | 
| Lugs | 2:93da96b41127 | 422 | PlayingFreq = 311; | 
| Lugs | 2:93da96b41127 | 423 | break; | 
| Lugs | 3:fcf745cd4f6d | 424 | case E4: | 
| Lugs | 2:93da96b41127 | 425 | PlayingFreq = 330; | 
| Lugs | 2:93da96b41127 | 426 | break; | 
| Lugs | 3:fcf745cd4f6d | 427 | case F4: | 
| Lugs | 2:93da96b41127 | 428 | PlayingFreq = 349; | 
| Lugs | 2:93da96b41127 | 429 | break; | 
| Lugs | 3:fcf745cd4f6d | 430 | case Fs4: | 
| Lugs | 2:93da96b41127 | 431 | PlayingFreq = 370; | 
| Lugs | 2:93da96b41127 | 432 | break; | 
| Lugs | 3:fcf745cd4f6d | 433 | case G4: | 
| Lugs | 2:93da96b41127 | 434 | PlayingFreq = 392; | 
| Lugs | 2:93da96b41127 | 435 | break; | 
| Lugs | 3:fcf745cd4f6d | 436 | case Gs4: | 
| Lugs | 2:93da96b41127 | 437 | PlayingFreq = 415; | 
| Lugs | 2:93da96b41127 | 438 | break; | 
| Lugs | 3:fcf745cd4f6d | 439 | case A4: | 
| Lugs | 2:93da96b41127 | 440 | PlayingFreq = 440; | 
| Lugs | 2:93da96b41127 | 441 | break; | 
| Lugs | 3:fcf745cd4f6d | 442 | case As4: | 
| Lugs | 2:93da96b41127 | 443 | PlayingFreq = 466; | 
| Lugs | 2:93da96b41127 | 444 | break; | 
| Lugs | 3:fcf745cd4f6d | 445 | case B4: | 
| Lugs | 2:93da96b41127 | 446 | PlayingFreq = 494; | 
| Lugs | 2:93da96b41127 | 447 | break; | 
| Lugs | 3:fcf745cd4f6d | 448 | case C5: | 
| Lugs | 2:93da96b41127 | 449 | PlayingFreq = 523; | 
| Lugs | 2:93da96b41127 | 450 | break; | 
| Lugs | 3:fcf745cd4f6d | 451 | case Cs5: | 
| Lugs | 2:93da96b41127 | 452 | PlayingFreq = 554; | 
| Lugs | 2:93da96b41127 | 453 | break; | 
| Lugs | 3:fcf745cd4f6d | 454 | case D5: | 
| Lugs | 2:93da96b41127 | 455 | PlayingFreq = 587; | 
| Lugs | 2:93da96b41127 | 456 | break; | 
| Lugs | 3:fcf745cd4f6d | 457 | case Ds5: | 
| Lugs | 2:93da96b41127 | 458 | PlayingFreq = 622; | 
| Lugs | 2:93da96b41127 | 459 | break; | 
| Lugs | 3:fcf745cd4f6d | 460 | case E5: | 
| Lugs | 2:93da96b41127 | 461 | PlayingFreq = 659; | 
| Lugs | 2:93da96b41127 | 462 | break; | 
| Lugs | 3:fcf745cd4f6d | 463 | case F5: | 
| Lugs | 2:93da96b41127 | 464 | PlayingFreq = 698; | 
| Lugs | 2:93da96b41127 | 465 | break; | 
| Lugs | 3:fcf745cd4f6d | 466 | case Fs5: | 
| Lugs | 2:93da96b41127 | 467 | PlayingFreq = 740; | 
| Lugs | 2:93da96b41127 | 468 | break; | 
| Lugs | 3:fcf745cd4f6d | 469 | case G5: | 
| Lugs | 2:93da96b41127 | 470 | PlayingFreq = 784; | 
| Lugs | 2:93da96b41127 | 471 | break; | 
| Lugs | 3:fcf745cd4f6d | 472 | case Gs5: | 
| Lugs | 2:93da96b41127 | 473 | PlayingFreq = 831; | 
| Lugs | 2:93da96b41127 | 474 | break; | 
| Lugs | 3:fcf745cd4f6d | 475 | case A5: | 
| Lugs | 2:93da96b41127 | 476 | PlayingFreq = 880; | 
| Lugs | 2:93da96b41127 | 477 | break; | 
| Lugs | 3:fcf745cd4f6d | 478 | case As5: | 
| Lugs | 2:93da96b41127 | 479 | PlayingFreq = 932; | 
| Lugs | 2:93da96b41127 | 480 | break; | 
| Lugs | 3:fcf745cd4f6d | 481 | case B5: | 
| Lugs | 2:93da96b41127 | 482 | PlayingFreq = 988; | 
| Lugs | 2:93da96b41127 | 483 | break; | 
| Lugs | 3:fcf745cd4f6d | 484 | case C6: | 
| Lugs | 2:93da96b41127 | 485 | PlayingFreq = 1047; | 
| Lugs | 2:93da96b41127 | 486 | break; | 
| Lugs | 3:fcf745cd4f6d | 487 | case Cs6: | 
| Lugs | 2:93da96b41127 | 488 | PlayingFreq = 1109; | 
| Lugs | 2:93da96b41127 | 489 | break; | 
| Lugs | 3:fcf745cd4f6d | 490 | case D6: | 
| Lugs | 2:93da96b41127 | 491 | PlayingFreq = 1175; | 
| Lugs | 2:93da96b41127 | 492 | break; | 
| Lugs | 3:fcf745cd4f6d | 493 | case Ds6: | 
| Lugs | 2:93da96b41127 | 494 | PlayingFreq = 1245; | 
| Lugs | 2:93da96b41127 | 495 | break; | 
| Lugs | 3:fcf745cd4f6d | 496 | case E6: | 
| Lugs | 2:93da96b41127 | 497 | PlayingFreq = 1319; | 
| Lugs | 2:93da96b41127 | 498 | break; | 
| Lugs | 3:fcf745cd4f6d | 499 | case F6: | 
| Lugs | 2:93da96b41127 | 500 | PlayingFreq = 1397; | 
| Lugs | 2:93da96b41127 | 501 | break; | 
| Lugs | 3:fcf745cd4f6d | 502 | case Fs6: | 
| Lugs | 2:93da96b41127 | 503 | PlayingFreq = 1480; | 
| Lugs | 2:93da96b41127 | 504 | break; | 
| Lugs | 3:fcf745cd4f6d | 505 | case G6: | 
| Lugs | 2:93da96b41127 | 506 | PlayingFreq = 1568; | 
| Lugs | 2:93da96b41127 | 507 | break; | 
| Lugs | 3:fcf745cd4f6d | 508 | case Gs6: | 
| Lugs | 2:93da96b41127 | 509 | PlayingFreq = 1661; | 
| Lugs | 2:93da96b41127 | 510 | break; | 
| Lugs | 3:fcf745cd4f6d | 511 | case END: | 
| Lugs | 3:fcf745cd4f6d | 512 | i = 0; | 
| Lugs | 4:24086b80928e | 513 | printf("SONG END. PRESS BUTTON TO RESTART."); | 
| Lugs | 4:24086b80928e | 514 | while(Button) | 
| Lugs | 4:24086b80928e | 515 | { | 
| Lugs | 4:24086b80928e | 516 | printf("\033[A\033[A\033[A\033[A"); | 
| Lugs | 4:24086b80928e | 517 | } | 
| Lugs | 3:fcf745cd4f6d | 518 | goto restart; | 
| Lugs | 2:93da96b41127 | 519 | } | 
| Lugs | 4:24086b80928e | 520 | |
| Lugs | 3:fcf745cd4f6d | 521 | Track.sampleRate = PlayingFreq * 16; //TONE FREQ = SAMPLE RATE / SAMPLES PER CYCLE | 
| Lugs | 3:fcf745cd4f6d | 522 | PWM.period_us(1); //1MHz | 
| Lugs | 3:fcf745cd4f6d | 523 | float ticker_period = (float) 1/(Track.sampleRate); | 
| Lugs | 3:fcf745cd4f6d | 524 | 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 | 525 | |
| Lugs | 3:fcf745cd4f6d | 526 | SampleTime.attach(&placeNewSample,ticker_period); | 
| Lugs | 4:24086b80928e | 527 | wait( (1/(float)song[i].length) *SPB); | 
| Lugs | 3:fcf745cd4f6d | 528 | SampleTime.detach(); | 
| Lugs | 1:78ca0566c062 | 529 | |
| Lugs | 4:24086b80928e | 530 | |
| Lugs | 4:24086b80928e | 531 | |
| Lugs | 2:93da96b41127 | 532 | printf("\033[A\033[A\033[A\033[A"); | 
| Lugs | 0:ad5ce0aff429 | 533 | } | 
| Lugs | 3:fcf745cd4f6d | 534 | } | 
| Lugs | 0:ad5ce0aff429 | 535 |