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@11:df2fffa042b8, 2019-07-24 (annotated)
- Committer:
- Lugs
- Date:
- Wed Jul 24 08:03:12 2019 +0000
- Revision:
- 11:df2fffa042b8
- Parent:
- 10:97f600e6eae2
- Child:
- 12:f9ce63d44ba7
extracted some functions to headers
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 | 9:17de551d2208 | 7 | #include "USBMSD_BD.h" |
Lugs | 9:17de551d2208 | 8 | #include "SDBlockDevice.h" |
Lugs | 9:17de551d2208 | 9 | #include "HeapBlockDevice.h" |
Lugs | 9:17de551d2208 | 10 | #include "FATFileSystem.h" |
Lugs | 11:df2fffa042b8 | 11 | #include "noteplayer.h" |
Lugs | 3:fcf745cd4f6d | 12 | |
Lugs | 0:ad5ce0aff429 | 13 | #define BUFFER_SIZE 128 |
Lugs | 0:ad5ce0aff429 | 14 | #define HALF_BUFFER 64 |
Lugs | 8:ce16aa4cdb6a | 15 | #define OS_MAINSTKSIZE 1024 |
Lugs | 8:ce16aa4cdb6a | 16 | |
Lugs | 8:ce16aa4cdb6a | 17 | |
Lugs | 8:ce16aa4cdb6a | 18 | bool debugState = 1; |
Lugs | 0:ad5ce0aff429 | 19 | |
Lugs | 0:ad5ce0aff429 | 20 | DigitalOut rLED(LED1); |
Lugs | 0:ad5ce0aff429 | 21 | DigitalOut gLED(LED2); |
Lugs | 0:ad5ce0aff429 | 22 | DigitalOut bLED(LED3); |
Lugs | 0:ad5ce0aff429 | 23 | |
Lugs | 0:ad5ce0aff429 | 24 | DigitalIn Button(P2_3); |
Lugs | 0:ad5ce0aff429 | 25 | |
Lugs | 0:ad5ce0aff429 | 26 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
Lugs | 0:ad5ce0aff429 | 27 | PwmOut PWM(P5_6); |
Lugs | 0:ad5ce0aff429 | 28 | AnalogIn POT(AIN_0); |
Lugs | 0:ad5ce0aff429 | 29 | volatile int bufferPOS = 0; |
Lugs | 8:ce16aa4cdb6a | 30 | int i; |
Lugs | 0:ad5ce0aff429 | 31 | |
Lugs | 0:ad5ce0aff429 | 32 | Serial daplink(P2_1,P2_0); |
Lugs | 0:ad5ce0aff429 | 33 | USBSerial microUSB; |
Lugs | 0:ad5ce0aff429 | 34 | |
Lugs | 0:ad5ce0aff429 | 35 | float audioDataBuffer[BUFFER_SIZE]; |
Lugs | 0:ad5ce0aff429 | 36 | |
Lugs | 9:17de551d2208 | 37 | // Physical block device, can be any device that supports the BlockDevice API |
Lugs | 9:17de551d2208 | 38 | // HeapBlockDevice bd(512*BLOCK_SIZE, BLOCK_SIZE); |
Lugs | 9:17de551d2208 | 39 | SDBlockDevice bd(P0_5, P0_6, P0_4, P0_7); |
Lugs | 9:17de551d2208 | 40 | |
Lugs | 9:17de551d2208 | 41 | // File system declaration |
Lugs | 9:17de551d2208 | 42 | FATFileSystem fs("fs"); |
Lugs | 9:17de551d2208 | 43 | |
Lugs | 10:97f600e6eae2 | 44 | // USB MSD |
Lugs | 10:97f600e6eae2 | 45 | USBMSD_BD msd(&bd); |
Lugs | 10:97f600e6eae2 | 46 | |
Lugs | 10:97f600e6eae2 | 47 | void clearSerialStream() |
Lugs | 10:97f600e6eae2 | 48 | { |
Lugs | 10:97f600e6eae2 | 49 | char c; |
Lugs | 11:df2fffa042b8 | 50 | while(daplink.readable()) { |
Lugs | 10:97f600e6eae2 | 51 | c = daplink.getc(); |
Lugs | 11:df2fffa042b8 | 52 | wait_ms(1); |
Lugs | 11:df2fffa042b8 | 53 | } |
Lugs | 10:97f600e6eae2 | 54 | } |
Lugs | 9:17de551d2208 | 55 | |
Lugs | 10:97f600e6eae2 | 56 | bool getInput(int maxSize,char *inputArray) |
Lugs | 10:97f600e6eae2 | 57 | { |
Lugs | 10:97f600e6eae2 | 58 | int i; |
Lugs | 10:97f600e6eae2 | 59 | char c; |
Lugs | 10:97f600e6eae2 | 60 | clearSerialStream(); |
Lugs | 10:97f600e6eae2 | 61 | for(i=0; i<maxSize && c!='\r'; i++) { |
Lugs | 10:97f600e6eae2 | 62 | c = daplink.getc(); |
Lugs | 10:97f600e6eae2 | 63 | daplink.putc(c); |
Lugs | 10:97f600e6eae2 | 64 | inputArray[i] = c; |
Lugs | 10:97f600e6eae2 | 65 | } |
Lugs | 11:df2fffa042b8 | 66 | if(i == maxSize) { |
Lugs | 10:97f600e6eae2 | 67 | return 0; |
Lugs | 10:97f600e6eae2 | 68 | } else { |
Lugs | 10:97f600e6eae2 | 69 | return 1; |
Lugs | 10:97f600e6eae2 | 70 | } |
Lugs | 10:97f600e6eae2 | 71 | } |
Lugs | 9:17de551d2208 | 72 | |
Lugs | 9:17de551d2208 | 73 | // main() runs in its own thread in the OS |
Lugs | 9:17de551d2208 | 74 | // (note the calls to Thread::wait below for delays) |
Lugs | 9:17de551d2208 | 75 | void startFileSystem() |
Lugs | 9:17de551d2208 | 76 | { |
Lugs | 9:17de551d2208 | 77 | printf("\f---STARTING FILESYSTEM...---\r\n"); |
Lugs | 9:17de551d2208 | 78 | rLED = LED_ON; |
Lugs | 9:17de551d2208 | 79 | gLED = LED_ON; |
Lugs | 9:17de551d2208 | 80 | bLED = LED_OFF; |
Lugs | 9:17de551d2208 | 81 | |
Lugs | 9:17de551d2208 | 82 | Thread::wait(100); |
Lugs | 9:17de551d2208 | 83 | |
Lugs | 9:17de551d2208 | 84 | // Try to mount the filesystem |
Lugs | 9:17de551d2208 | 85 | printf("Mounting the filesystem... "); |
Lugs | 9:17de551d2208 | 86 | fflush(stdout); |
Lugs | 9:17de551d2208 | 87 | int err = fs.mount(&bd); |
Lugs | 9:17de551d2208 | 88 | printf("%s\r\n", (err ? "Fail :(" : "OK")); |
Lugs | 9:17de551d2208 | 89 | if (err) { |
Lugs | 9:17de551d2208 | 90 | // Reformat if we can't mount the filesystem |
Lugs | 9:17de551d2208 | 91 | // this should only happen on the first boot |
Lugs | 9:17de551d2208 | 92 | printf("No filesystem found, formatting... "); |
Lugs | 9:17de551d2208 | 93 | fflush(stdout); |
Lugs | 9:17de551d2208 | 94 | err = fs.reformat(&bd); |
Lugs | 9:17de551d2208 | 95 | printf("%s\r\n", (err ? "Fail :(" : "OK")); |
Lugs | 9:17de551d2208 | 96 | } |
Lugs | 9:17de551d2208 | 97 | |
Lugs | 9:17de551d2208 | 98 | rLED = LED_OFF; |
Lugs | 9:17de551d2208 | 99 | |
Lugs | 9:17de551d2208 | 100 | // Open the numbers file |
Lugs | 9:17de551d2208 | 101 | printf("Opening \"/fs/numbers.txt\"... "); |
Lugs | 9:17de551d2208 | 102 | fflush(stdout); |
Lugs | 9:17de551d2208 | 103 | FILE *f = fopen("/fs/numbers.txt", "r+"); |
Lugs | 9:17de551d2208 | 104 | printf("%s\r\n", (!f ? "Fail :(" : "OK")); |
Lugs | 9:17de551d2208 | 105 | if (!f) { |
Lugs | 9:17de551d2208 | 106 | // Create the numbers file if it doesn't exist |
Lugs | 9:17de551d2208 | 107 | printf("No file found, creating a new file... "); |
Lugs | 9:17de551d2208 | 108 | fflush(stdout); |
Lugs | 9:17de551d2208 | 109 | f = fopen("/fs/numbers.txt", "w+"); |
Lugs | 9:17de551d2208 | 110 | printf("%s\r\n", (!f ? "Fail :(" : "OK")); |
Lugs | 9:17de551d2208 | 111 | |
Lugs | 9:17de551d2208 | 112 | for (int i = 0; i < 10; i++) { |
Lugs | 9:17de551d2208 | 113 | printf("\rWriting numbers (%d/%d)... ", i, 10); |
Lugs | 9:17de551d2208 | 114 | fflush(stdout); |
Lugs | 9:17de551d2208 | 115 | err = fprintf(f, " %d\r\n", i); |
Lugs | 9:17de551d2208 | 116 | if (err < 0) { |
Lugs | 9:17de551d2208 | 117 | printf("Fail :(\r\n"); |
Lugs | 9:17de551d2208 | 118 | } |
Lugs | 9:17de551d2208 | 119 | } |
Lugs | 9:17de551d2208 | 120 | printf("\rWriting numbers (%d/%d)... OK\r\n", 10, 10); |
Lugs | 9:17de551d2208 | 121 | |
Lugs | 9:17de551d2208 | 122 | printf("Seeking file... "); |
Lugs | 9:17de551d2208 | 123 | fflush(stdout); |
Lugs | 9:17de551d2208 | 124 | err = fseek(f, 0, SEEK_SET); |
Lugs | 9:17de551d2208 | 125 | printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); |
Lugs | 9:17de551d2208 | 126 | } |
Lugs | 9:17de551d2208 | 127 | |
Lugs | 9:17de551d2208 | 128 | // Go through and increment the numbers |
Lugs | 9:17de551d2208 | 129 | for (int i = 0; i < 10; i++) { |
Lugs | 9:17de551d2208 | 130 | printf("\rIncrementing numbers (%d/%d)... ", i, 10); |
Lugs | 9:17de551d2208 | 131 | fflush(stdout); |
Lugs | 9:17de551d2208 | 132 | |
Lugs | 9:17de551d2208 | 133 | // Get current stream position |
Lugs | 9:17de551d2208 | 134 | long pos = ftell(f); |
Lugs | 9:17de551d2208 | 135 | |
Lugs | 9:17de551d2208 | 136 | // Parse out the number and increment |
Lugs | 9:17de551d2208 | 137 | int32_t number; |
Lugs | 9:17de551d2208 | 138 | fscanf(f, "%d", &number); |
Lugs | 9:17de551d2208 | 139 | number += 1; |
Lugs | 9:17de551d2208 | 140 | |
Lugs | 9:17de551d2208 | 141 | // Seek to beginning of number |
Lugs | 9:17de551d2208 | 142 | fseek(f, pos, SEEK_SET); |
Lugs | 10:97f600e6eae2 | 143 | |
Lugs | 9:17de551d2208 | 144 | // Store number |
Lugs | 9:17de551d2208 | 145 | fprintf(f, " %d\r\n", number); |
Lugs | 9:17de551d2208 | 146 | } |
Lugs | 9:17de551d2208 | 147 | printf("\rIncrementing numbers (%d/%d)... OK\r\n", 10, 10); |
Lugs | 9:17de551d2208 | 148 | |
Lugs | 9:17de551d2208 | 149 | // Close the file which also flushes any cached writes |
Lugs | 9:17de551d2208 | 150 | printf("Closing \"/fs/numbers.txt\"... "); |
Lugs | 9:17de551d2208 | 151 | fflush(stdout); |
Lugs | 9:17de551d2208 | 152 | err = fclose(f); |
Lugs | 9:17de551d2208 | 153 | printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); |
Lugs | 9:17de551d2208 | 154 | |
Lugs | 9:17de551d2208 | 155 | // Display the root directory |
Lugs | 9:17de551d2208 | 156 | printf("Opening the root directory... "); |
Lugs | 9:17de551d2208 | 157 | fflush(stdout); |
Lugs | 9:17de551d2208 | 158 | DIR *d = opendir("/fs/"); |
Lugs | 9:17de551d2208 | 159 | printf("%s\r\n", (!d ? "Fail :(" : "OK")); |
Lugs | 9:17de551d2208 | 160 | |
Lugs | 9:17de551d2208 | 161 | printf("root directory:\r\n"); |
Lugs | 9:17de551d2208 | 162 | while (true) { |
Lugs | 9:17de551d2208 | 163 | struct dirent *e = readdir(d); |
Lugs | 9:17de551d2208 | 164 | if (!e) { |
Lugs | 9:17de551d2208 | 165 | break; |
Lugs | 9:17de551d2208 | 166 | } |
Lugs | 9:17de551d2208 | 167 | printf(" %s\r\n", e->d_name); |
Lugs | 9:17de551d2208 | 168 | } |
Lugs | 9:17de551d2208 | 169 | |
Lugs | 9:17de551d2208 | 170 | printf("Closing the root directory... "); |
Lugs | 9:17de551d2208 | 171 | fflush(stdout); |
Lugs | 9:17de551d2208 | 172 | err = closedir(d); |
Lugs | 9:17de551d2208 | 173 | printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); |
Lugs | 9:17de551d2208 | 174 | |
Lugs | 9:17de551d2208 | 175 | // Display the numbers file |
Lugs | 9:17de551d2208 | 176 | printf("Opening \"/fs/numbers.txt\"... "); |
Lugs | 9:17de551d2208 | 177 | fflush(stdout); |
Lugs | 9:17de551d2208 | 178 | f = fopen("/fs/numbers.txt", "r"); |
Lugs | 9:17de551d2208 | 179 | printf("%s\r\n", (!f ? "Fail :(" : "OK")); |
Lugs | 9:17de551d2208 | 180 | |
Lugs | 9:17de551d2208 | 181 | printf("numbers:\r\n"); |
Lugs | 9:17de551d2208 | 182 | while (!feof(f)) { |
Lugs | 9:17de551d2208 | 183 | int c = fgetc(f); |
Lugs | 9:17de551d2208 | 184 | printf("%c", c); |
Lugs | 9:17de551d2208 | 185 | } |
Lugs | 9:17de551d2208 | 186 | |
Lugs | 9:17de551d2208 | 187 | printf("\rClosing \"/fs/numbers.txt\"... "); |
Lugs | 9:17de551d2208 | 188 | fflush(stdout); |
Lugs | 9:17de551d2208 | 189 | err = fclose(f); |
Lugs | 9:17de551d2208 | 190 | printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); |
Lugs | 9:17de551d2208 | 191 | |
Lugs | 9:17de551d2208 | 192 | |
Lugs | 9:17de551d2208 | 193 | |
Lugs | 9:17de551d2208 | 194 | |
Lugs | 9:17de551d2208 | 195 | // Switch to MSD |
Lugs | 9:17de551d2208 | 196 | // printf("Unmounting... "); |
Lugs | 9:17de551d2208 | 197 | // fflush(stdout); |
Lugs | 9:17de551d2208 | 198 | // err = fs.unmount(); |
Lugs | 9:17de551d2208 | 199 | // printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); |
Lugs | 10:97f600e6eae2 | 200 | |
Lugs | 9:17de551d2208 | 201 | |
Lugs | 9:17de551d2208 | 202 | printf("Starting MSD... "); |
Lugs | 9:17de551d2208 | 203 | msd.disk_initialize(); |
Lugs | 9:17de551d2208 | 204 | err = msd.connect(); |
Lugs | 9:17de551d2208 | 205 | bLED = LED_ON; |
Lugs | 9:17de551d2208 | 206 | printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); |
Lugs | 9:17de551d2208 | 207 | } |
Lugs | 9:17de551d2208 | 208 | |
Lugs | 0:ad5ce0aff429 | 209 | float potval,reading; |
Lugs | 0:ad5ce0aff429 | 210 | |
Lugs | 0:ad5ce0aff429 | 211 | void placeNewSample(void) |
Lugs | 0:ad5ce0aff429 | 212 | { |
Lugs | 2:93da96b41127 | 213 | PWM.write(audioDataBuffer[bufferPOS++]); //multiply by POT value for volume. |
Lugs | 0:ad5ce0aff429 | 214 | bufferPOS = (bufferPOS+1) & 0x07F; |
Lugs | 0:ad5ce0aff429 | 215 | } |
Lugs | 0:ad5ce0aff429 | 216 | |
Lugs | 8:ce16aa4cdb6a | 217 | void presence() |
Lugs | 4:24086b80928e | 218 | { |
Lugs | 0:ad5ce0aff429 | 219 | rLED = LED_ON; |
Lugs | 0:ad5ce0aff429 | 220 | wait_ms(500); |
Lugs | 0:ad5ce0aff429 | 221 | rLED = LED_OFF; |
Lugs | 0:ad5ce0aff429 | 222 | gLED = LED_ON; |
Lugs | 0:ad5ce0aff429 | 223 | wait_ms(500); |
Lugs | 0:ad5ce0aff429 | 224 | bLED = LED_ON; |
Lugs | 0:ad5ce0aff429 | 225 | gLED = LED_OFF; |
Lugs | 8:ce16aa4cdb6a | 226 | } |
Lugs | 4:24086b80928e | 227 | |
Lugs | 8:ce16aa4cdb6a | 228 | char *skipToNextEntry(char *songpos, bool skiptype) |
Lugs | 8:ce16aa4cdb6a | 229 | { |
Lugs | 9:17de551d2208 | 230 | if(skiptype == 0) { |
Lugs | 8:ce16aa4cdb6a | 231 | do { |
Lugs | 8:ce16aa4cdb6a | 232 | songpos++; |
Lugs | 8:ce16aa4cdb6a | 233 | } while(!isspace(*(songpos))); |
Lugs | 8:ce16aa4cdb6a | 234 | do { |
Lugs | 8:ce16aa4cdb6a | 235 | songpos++; |
Lugs | 8:ce16aa4cdb6a | 236 | } while(!isalnum(*(songpos))); |
Lugs | 8:ce16aa4cdb6a | 237 | return songpos; |
Lugs | 9:17de551d2208 | 238 | } else if(skiptype == 1) { |
Lugs | 8:ce16aa4cdb6a | 239 | while(!isspace(*(songpos))) { |
Lugs | 8:ce16aa4cdb6a | 240 | songpos++; |
Lugs | 8:ce16aa4cdb6a | 241 | } |
Lugs | 8:ce16aa4cdb6a | 242 | while(isspace(*(songpos))) { |
Lugs | 8:ce16aa4cdb6a | 243 | songpos++; |
Lugs | 8:ce16aa4cdb6a | 244 | }; |
Lugs | 8:ce16aa4cdb6a | 245 | } |
Lugs | 8:ce16aa4cdb6a | 246 | } |
Lugs | 8:ce16aa4cdb6a | 247 | |
Lugs | 8:ce16aa4cdb6a | 248 | bool songParse(note *song,char *buffer) |
Lugs | 8:ce16aa4cdb6a | 249 | { |
Lugs | 8:ce16aa4cdb6a | 250 | int candidate; |
Lugs | 8:ce16aa4cdb6a | 251 | char *songpos=buffer+1; //song position |
Lugs | 8:ce16aa4cdb6a | 252 | int pn_det; |
Lugs | 8:ce16aa4cdb6a | 253 | int i; |
Lugs | 8:ce16aa4cdb6a | 254 | for(i=0; i<256; i++) { |
Lugs | 8:ce16aa4cdb6a | 255 | //take the first base 10 integer you see. |
Lugs | 8:ce16aa4cdb6a | 256 | //this initializes songpos ALWAYS. lol. |
Lugs | 8:ce16aa4cdb6a | 257 | candidate = strtol(songpos-1,&songpos,10); |
Lugs | 8:ce16aa4cdb6a | 258 | |
Lugs | 8:ce16aa4cdb6a | 259 | if(debugState) { |
Lugs | 8:ce16aa4cdb6a | 260 | printf("Character:%i\r\n",candidate); |
Lugs | 8:ce16aa4cdb6a | 261 | } |
Lugs | 8:ce16aa4cdb6a | 262 | |
Lugs | 8:ce16aa4cdb6a | 263 | if(candidate == 0) { |
Lugs | 8:ce16aa4cdb6a | 264 | printf("Found invalid NOTE LENGTH value [%c] at position %li. Skipping until next whitespace...\r\n",*songpos,songpos-buffer); |
Lugs | 8:ce16aa4cdb6a | 265 | songpos = skipToNextEntry(songpos,0); |
Lugs | 8:ce16aa4cdb6a | 266 | i--; |
Lugs | 8:ce16aa4cdb6a | 267 | continue; |
Lugs | 8:ce16aa4cdb6a | 268 | } else { |
Lugs | 8:ce16aa4cdb6a | 269 | song[i].length = candidate; |
Lugs | 8:ce16aa4cdb6a | 270 | if(debugState) { |
Lugs | 8:ce16aa4cdb6a | 271 | printf("Entered candidate [%i] into song[%i].length\r\n",candidate,i); |
Lugs | 8:ce16aa4cdb6a | 272 | } |
Lugs | 8:ce16aa4cdb6a | 273 | } |
Lugs | 8:ce16aa4cdb6a | 274 | |
Lugs | 8:ce16aa4cdb6a | 275 | //parse next character |
Lugs | 8:ce16aa4cdb6a | 276 | if(debugState) { |
Lugs | 8:ce16aa4cdb6a | 277 | printf("Character:%c\r\n",*songpos); |
Lugs | 8:ce16aa4cdb6a | 278 | } |
Lugs | 8:ce16aa4cdb6a | 279 | |
Lugs | 8:ce16aa4cdb6a | 280 | pn_det=0; |
Lugs | 4:24086b80928e | 281 | |
Lugs | 8:ce16aa4cdb6a | 282 | if(!(*songpos=='#'||'a'<=*songpos<='z'||*songpos=='-')) { |
Lugs | 8:ce16aa4cdb6a | 283 | printf("Found invalid PITCH NAME value [%c] at position %li. Skipping word...\r\n",*songpos,songpos-buffer); |
Lugs | 8:ce16aa4cdb6a | 284 | songpos = skipToNextEntry(songpos,0); |
Lugs | 8:ce16aa4cdb6a | 285 | i--; |
Lugs | 8:ce16aa4cdb6a | 286 | continue; |
Lugs | 8:ce16aa4cdb6a | 287 | } |
Lugs | 8:ce16aa4cdb6a | 288 | if(*songpos=='#') { //watch out for sharps |
Lugs | 8:ce16aa4cdb6a | 289 | pn_det+=1; |
Lugs | 8:ce16aa4cdb6a | 290 | songpos++; |
Lugs | 8:ce16aa4cdb6a | 291 | printf("Sharp found.\r\n"); |
Lugs | 8:ce16aa4cdb6a | 292 | } |
Lugs | 8:ce16aa4cdb6a | 293 | switch(*songpos) { |
Lugs | 8:ce16aa4cdb6a | 294 | case 'c': |
Lugs | 8:ce16aa4cdb6a | 295 | pn_det+=0; |
Lugs | 8:ce16aa4cdb6a | 296 | break; |
Lugs | 8:ce16aa4cdb6a | 297 | case 'd': |
Lugs | 8:ce16aa4cdb6a | 298 | pn_det+=2; |
Lugs | 8:ce16aa4cdb6a | 299 | break; |
Lugs | 8:ce16aa4cdb6a | 300 | case 'e': |
Lugs | 8:ce16aa4cdb6a | 301 | pn_det+=4; |
Lugs | 8:ce16aa4cdb6a | 302 | break; |
Lugs | 8:ce16aa4cdb6a | 303 | case 'f': |
Lugs | 8:ce16aa4cdb6a | 304 | pn_det+=5; |
Lugs | 8:ce16aa4cdb6a | 305 | break; |
Lugs | 8:ce16aa4cdb6a | 306 | case 'g': |
Lugs | 8:ce16aa4cdb6a | 307 | pn_det+=7; |
Lugs | 8:ce16aa4cdb6a | 308 | break; |
Lugs | 8:ce16aa4cdb6a | 309 | case 'a': |
Lugs | 8:ce16aa4cdb6a | 310 | pn_det+=9; |
Lugs | 8:ce16aa4cdb6a | 311 | break; |
Lugs | 8:ce16aa4cdb6a | 312 | case 'b': |
Lugs | 8:ce16aa4cdb6a | 313 | pn_det+=11; |
Lugs | 8:ce16aa4cdb6a | 314 | break; |
Lugs | 8:ce16aa4cdb6a | 315 | case '-': |
Lugs | 8:ce16aa4cdb6a | 316 | pn_det=57; |
Lugs | 11:df2fffa042b8 | 317 | break; |
Lugs | 8:ce16aa4cdb6a | 318 | } |
Lugs | 8:ce16aa4cdb6a | 319 | |
Lugs | 8:ce16aa4cdb6a | 320 | songpos++; |
Lugs | 8:ce16aa4cdb6a | 321 | if('0'<=*songpos<='9') { |
Lugs | 8:ce16aa4cdb6a | 322 | int num = strtol(songpos,&songpos,10); |
Lugs | 11:df2fffa042b8 | 323 | num+=3; //nokia composer octaves are usually much too low. |
Lugs | 8:ce16aa4cdb6a | 324 | if(debugState) { |
Lugs | 8:ce16aa4cdb6a | 325 | printf("octave parsed: %i \t-> adder:%i\r\n",num,((num-2)*12)); |
Lugs | 8:ce16aa4cdb6a | 326 | } |
Lugs | 8:ce16aa4cdb6a | 327 | pn_det=pn_det+((num-2)*12); |
Lugs | 11:df2fffa042b8 | 328 | } else if (*songpos != '-') { |
Lugs | 8:ce16aa4cdb6a | 329 | printf("Found invalid OCTAVE value [%c] at position %li. Skipping word...\r\n",*songpos,songpos-buffer); |
Lugs | 8:ce16aa4cdb6a | 330 | songpos = skipToNextEntry(songpos,0); |
Lugs | 8:ce16aa4cdb6a | 331 | i--; //rewrite current note |
Lugs | 8:ce16aa4cdb6a | 332 | continue; |
Lugs | 8:ce16aa4cdb6a | 333 | } |
Lugs | 11:df2fffa042b8 | 334 | song[i].pitch = (pitchname)pn_det; |
Lugs | 10:97f600e6eae2 | 335 | |
Lugs | 8:ce16aa4cdb6a | 336 | if(debugState) { |
Lugs | 8:ce16aa4cdb6a | 337 | printf("Entered pitch [%i] into song[%i].pitch\r\n",song[i].pitch,i); |
Lugs | 8:ce16aa4cdb6a | 338 | printf("Final: {%i,%i} at i=%i\r\n",song[i].length,song[i].pitch,i); |
Lugs | 8:ce16aa4cdb6a | 339 | printf("-------------------\r\n"); |
Lugs | 8:ce16aa4cdb6a | 340 | } |
Lugs | 8:ce16aa4cdb6a | 341 | |
Lugs | 8:ce16aa4cdb6a | 342 | songpos = skipToNextEntry(songpos,1); |
Lugs | 8:ce16aa4cdb6a | 343 | |
Lugs | 8:ce16aa4cdb6a | 344 | //check for buffer end |
Lugs | 8:ce16aa4cdb6a | 345 | if(*(songpos) == '\0') { |
Lugs | 8:ce16aa4cdb6a | 346 | i++; |
Lugs | 8:ce16aa4cdb6a | 347 | song[i].length = 1; |
Lugs | 8:ce16aa4cdb6a | 348 | song[i].pitch = END; |
Lugs | 8:ce16aa4cdb6a | 349 | printf("Parsing done. i = %i\r\n",i); |
Lugs | 8:ce16aa4cdb6a | 350 | return 1; |
Lugs | 8:ce16aa4cdb6a | 351 | } |
Lugs | 8:ce16aa4cdb6a | 352 | if(songpos-buffer > 2048) { |
Lugs | 8:ce16aa4cdb6a | 353 | printf("songpos exceeded buffer size.\r\n"); |
Lugs | 8:ce16aa4cdb6a | 354 | return 0; |
Lugs | 8:ce16aa4cdb6a | 355 | } |
Lugs | 8:ce16aa4cdb6a | 356 | } |
Lugs | 8:ce16aa4cdb6a | 357 | printf("Song exceeded maximum number of notes.\r\n"); |
Lugs | 8:ce16aa4cdb6a | 358 | return 0; |
Lugs | 8:ce16aa4cdb6a | 359 | } |
Lugs | 8:ce16aa4cdb6a | 360 | |
Lugs | 8:ce16aa4cdb6a | 361 | void printSong(note *song) |
Lugs | 8:ce16aa4cdb6a | 362 | { |
Lugs | 8:ce16aa4cdb6a | 363 | for(i=0; song[i].pitch != END; i++) { |
Lugs | 8:ce16aa4cdb6a | 364 | printf("{%i,%i},",song[i].length,song[i].pitch); |
Lugs | 8:ce16aa4cdb6a | 365 | } |
Lugs | 8:ce16aa4cdb6a | 366 | printf("{%i,%i},",song[i].length,song[i].pitch); |
Lugs | 8:ce16aa4cdb6a | 367 | printf("\r\nPrinting done.\r\n"); |
Lugs | 8:ce16aa4cdb6a | 368 | } |
Lugs | 8:ce16aa4cdb6a | 369 | |
Lugs | 8:ce16aa4cdb6a | 370 | int main() |
Lugs | 8:ce16aa4cdb6a | 371 | { |
Lugs | 8:ce16aa4cdb6a | 372 | Ticker SampleTime; |
Lugs | 8:ce16aa4cdb6a | 373 | |
Lugs | 9:17de551d2208 | 374 | startFileSystem(); |
Lugs | 9:17de551d2208 | 375 | |
Lugs | 8:ce16aa4cdb6a | 376 | daplink.printf("\f---DAPLINK SERIAL PORT---\r\n\r\nMINI PIANO PLAYER ver 2 \r\n\r\n\r\n"); |
Lugs | 8:ce16aa4cdb6a | 377 | microUSB.printf("micro USB serial port\r\n"); |
Lugs | 8:ce16aa4cdb6a | 378 | presence(); |
Lugs | 4:24086b80928e | 379 | |
Lugs | 10:97f600e6eae2 | 380 | char *title = new char[24]; |
Lugs | 10:97f600e6eae2 | 381 | title[0] = '/'; |
Lugs | 10:97f600e6eae2 | 382 | title[1] = 'f'; |
Lugs | 10:97f600e6eae2 | 383 | title[2] = 's'; |
Lugs | 10:97f600e6eae2 | 384 | title[3] = '/'; |
Lugs | 10:97f600e6eae2 | 385 | FILE *txtfile; |
Lugs | 11:df2fffa042b8 | 386 | char *buffer = new char[1024]; |
Lugs | 10:97f600e6eae2 | 387 | while(1) { |
Lugs | 10:97f600e6eae2 | 388 | printf("Please input filename: "); |
Lugs | 10:97f600e6eae2 | 389 | char *inputptr = title+4; |
Lugs | 11:df2fffa042b8 | 390 | if(!getInput(24,inputptr)) { |
Lugs | 10:97f600e6eae2 | 391 | printf("Filenames cannot be more than 20 characters.\033[A\r\n"); |
Lugs | 10:97f600e6eae2 | 392 | } |
Lugs | 10:97f600e6eae2 | 393 | txtfile = fopen(title,"r"); |
Lugs | 10:97f600e6eae2 | 394 | if(txtfile == NULL) { |
Lugs | 10:97f600e6eae2 | 395 | printf("File not found. Please append filetype at the end of filename.\033[A\r\n"); |
Lugs | 10:97f600e6eae2 | 396 | continue; |
Lugs | 10:97f600e6eae2 | 397 | } |
Lugs | 10:97f600e6eae2 | 398 | break; |
Lugs | 10:97f600e6eae2 | 399 | } |
Lugs | 10:97f600e6eae2 | 400 | |
Lugs | 9:17de551d2208 | 401 | fseek(txtfile,0L,SEEK_END); |
Lugs | 9:17de551d2208 | 402 | int size = ftell(txtfile); |
Lugs | 9:17de551d2208 | 403 | fseek(txtfile,0,SEEK_SET); |
Lugs | 9:17de551d2208 | 404 | fread(buffer,1,size,txtfile); |
Lugs | 9:17de551d2208 | 405 | printf("Printing file...\r\n"); |
Lugs | 11:df2fffa042b8 | 406 | for(int i=0; i<size; i++) { |
Lugs | 9:17de551d2208 | 407 | printf("%c",buffer[i]); |
Lugs | 9:17de551d2208 | 408 | } |
Lugs | 11:df2fffa042b8 | 409 | buffer[i] = ' '; |
Lugs | 8:ce16aa4cdb6a | 410 | buffer[i+1] = '\0'; |
Lugs | 11:df2fffa042b8 | 411 | buffer[i+2] = 'a'; |
Lugs | 4:24086b80928e | 412 | |
Lugs | 4:24086b80928e | 413 | printf("\r\nFile recieved. Parsing...\r\n"); |
Lugs | 8:ce16aa4cdb6a | 414 | |
Lugs | 8:ce16aa4cdb6a | 415 | note song[1024]; |
Lugs | 8:ce16aa4cdb6a | 416 | if(!songParse(song,buffer)) { |
Lugs | 8:ce16aa4cdb6a | 417 | printf("Song parse unsuccessful.\r\n"); |
Lugs | 9:17de551d2208 | 418 | return 0; |
Lugs | 4:24086b80928e | 419 | } |
Lugs | 8:ce16aa4cdb6a | 420 | printSong(song); |
Lugs | 4:24086b80928e | 421 | |
Lugs | 0:ad5ce0aff429 | 422 | printf("Generating sine...\r\n"); |
Lugs | 11:df2fffa042b8 | 423 | for(int i=0; i<128; i++) { |
Lugs | 0:ad5ce0aff429 | 424 | audioDataBuffer[i] =((1.0 + sin((double(i)/16.0*6.28318530717959)))/2.0); //formula copied from mbed example |
Lugs | 0:ad5ce0aff429 | 425 | } |
Lugs | 4:24086b80928e | 426 | |
Lugs | 4:24086b80928e | 427 | char bpminp[5]; |
Lugs | 4:24086b80928e | 428 | printf("Please enter desired BPM:\r\n"); |
Lugs | 10:97f600e6eae2 | 429 | getInput(5,bpminp); |
Lugs | 4:24086b80928e | 430 | int BPM = strtol(bpminp,NULL,0); |
Lugs | 4:24086b80928e | 431 | float SPB = 60*4/(float)BPM; |
Lugs | 8:ce16aa4cdb6a | 432 | |
Lugs | 11:df2fffa042b8 | 433 | int sampleRate; |
Lugs | 11:df2fffa042b8 | 434 | int PlayingFreq; |
Lugs | 11:df2fffa042b8 | 435 | for(int i = 0; 1; i++) { |
Lugs | 11:df2fffa042b8 | 436 | PlayingFreq = pitch2freq(song[i].pitch); |
Lugs | 11:df2fffa042b8 | 437 | if(PlayingFreq == -1) { |
Lugs | 11:df2fffa042b8 | 438 | i = 0; |
Lugs | 11:df2fffa042b8 | 439 | printf("SONG END. PRESS BUTTON TO RESTART."); |
Lugs | 11:df2fffa042b8 | 440 | while(Button) { |
Lugs | 11:df2fffa042b8 | 441 | wait_ms(2); |
Lugs | 11:df2fffa042b8 | 442 | } |
Lugs | 11:df2fffa042b8 | 443 | } else if(PlayingFreq == 0) { //rest |
Lugs | 11:df2fffa042b8 | 444 | wait((1/(float)song[i].length)*SPB); |
Lugs | 11:df2fffa042b8 | 445 | } else { |
Lugs | 11:df2fffa042b8 | 446 | sampleRate = PlayingFreq * 16; |
Lugs | 11:df2fffa042b8 | 447 | PWM.period_us(1); //1MHz |
Lugs | 11:df2fffa042b8 | 448 | float ticker_period = (float) 1/(sampleRate); |
Lugs | 11:df2fffa042b8 | 449 | printf("\r\nTicker Period: %f\tTicker Freq: %f\r\nTarget Freq: %i \r\n\r\n",ticker_period, 1/ticker_period, PlayingFreq); |
Lugs | 11:df2fffa042b8 | 450 | |
Lugs | 11:df2fffa042b8 | 451 | SampleTime.attach(&placeNewSample,ticker_period); |
Lugs | 11:df2fffa042b8 | 452 | wait((1/(float)song[i].length) *SPB); |
Lugs | 11:df2fffa042b8 | 453 | SampleTime.detach(); |
Lugs | 11:df2fffa042b8 | 454 | |
Lugs | 11:df2fffa042b8 | 455 | printf("\033[A\033[A\033[A\033[A"); |
Lugs | 2:93da96b41127 | 456 | } |
Lugs | 0:ad5ce0aff429 | 457 | } |
Lugs | 3:fcf745cd4f6d | 458 | } |
Lugs | 0:ad5ce0aff429 | 459 |