For MAX323630FTHR: Plays a WAV file in the SD card. Interfaced through serial port using puTTY or powershell.
Dependencies: USBMSD_BD SDFileSystem max32630fthr USBDevice
Revision 3:2ddba0146fd8, committed 2019-11-09
- Comitter:
- Lugs
- Date:
- Sat Nov 09 01:33:17 2019 +0000
- Parent:
- 2:d4f9c8c25fa6
- Commit message:
- Removed demoboard library
Changed in this revision
--- a/DEMOBOARD.lib Fri Jul 26 03:00:19 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -DEMOBOARD#d17f6aa62917
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/generalinterface.cpp Sat Nov 09 01:33:17 2019 +0000 @@ -0,0 +1,161 @@ +#include "generalinterface.h" + +Serial daplink(P2_1,P2_0); +USBSerial microUSB; +SDBlockDevice bd(P0_5, P0_6, P0_4, P0_7); +FATFileSystem fs("fs"); +USBMSD_BD msd(&bd); + +void clearSerialStream() +{ + char c; + while(daplink.readable()) { + c = daplink.getc(); + wait_ms(1); + } +} + +bool getInput(int maxSize,char *inputArray) +{ + int i; + char c; + clearSerialStream(); + for(i=0; i<maxSize && c!='\r'; i++) { + c = daplink.getc(); + daplink.putc(c); + inputArray[i] = c; + } + if(i == maxSize) { + return 0; + } else { + return 1; + } +} + +void startFileSystem() +{ + printf("\f---STARTING FILESYSTEM...---\r\n"); + + Thread::wait(100); + + // Try to mount the filesystem + printf("Mounting the filesystem... "); + fflush(stdout); + int err = fs.mount(&bd); + printf("%s\r\n", (err ? "Fail :(" : "OK")); + if (err) { + // Reformat if we can't mount the filesystem + // this should only happen on the first boot + printf("No filesystem found, formatting... "); + fflush(stdout); + err = fs.reformat(&bd); + printf("%s\r\n", (err ? "Fail :(" : "OK")); + } + + // Open the numbers file + printf("Opening \"/fs/numbers.txt\"... "); + fflush(stdout); + FILE *f = fopen("/fs/numbers.txt", "r+"); + printf("%s\r\n", (!f ? "Fail :(" : "OK")); + if (!f) { + // Create the numbers file if it doesn't exist + printf("No file found, creating a new file... "); + fflush(stdout); + f = fopen("/fs/numbers.txt", "w+"); + printf("%s\r\n", (!f ? "Fail :(" : "OK")); + + for (int i = 0; i < 10; i++) { + printf("\rWriting numbers (%d/%d)... ", i, 10); + fflush(stdout); + err = fprintf(f, " %d\r\n", i); + if (err < 0) { + printf("Fail :(\r\n"); + } + } + printf("\rWriting numbers (%d/%d)... OK\r\n", 10, 10); + + printf("Seeking file... "); + fflush(stdout); + err = fseek(f, 0, SEEK_SET); + printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); + } + + // Go through and increment the numbers + for (int i = 0; i < 10; i++) { + printf("\rIncrementing numbers (%d/%d)... ", i, 10); + fflush(stdout); + + // Get current stream position + long pos = ftell(f); + + // Parse out the number and increment + int32_t number; + fscanf(f, "%d", &number); + number += 1; + + // Seek to beginning of number + fseek(f, pos, SEEK_SET); + + // Store number + fprintf(f, " %d\r\n", number); + } + printf("\rIncrementing numbers (%d/%d)... OK\r\n", 10, 10); + + // Close the file which also flushes any cached writes + printf("Closing \"/fs/numbers.txt\"... "); + fflush(stdout); + err = fclose(f); + printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); + + // Display the root directory + printf("Opening the root directory... "); + fflush(stdout); + DIR *d = opendir("/fs/"); + printf("%s\r\n", (!d ? "Fail :(" : "OK")); + + printf("root directory:\r\n"); + while (true) { + struct dirent *e = readdir(d); + if (!e) { + break; + } + printf(" %s\r\n", e->d_name); + } + + printf("Closing the root directory... "); + fflush(stdout); + err = closedir(d); + printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); + + // Display the numbers file + printf("Opening \"/fs/numbers.txt\"... "); + fflush(stdout); + f = fopen("/fs/numbers.txt", "r"); + printf("%s\r\n", (!f ? "Fail :(" : "OK")); + + printf("numbers:\r\n"); + while (!feof(f)) { + int c = fgetc(f); + printf("%c", c); + } + + printf("\rClosing \"/fs/numbers.txt\"... "); + fflush(stdout); + err = fclose(f); + printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); + + + + + // Switch to MSD +// printf("Unmounting... "); +// fflush(stdout); +// err = fs.unmount(); +// printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); + + + printf("Starting MSD... "); + msd.disk_initialize(); + err = msd.connect(); + printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/generalinterface.h Sat Nov 09 01:33:17 2019 +0000 @@ -0,0 +1,33 @@ +#ifndef _GENERALINTERFACE_H_ +#define _GENERALINTERFACE_H_ + +#include "USBSerial.h" +#include "SDFileSystem.h" +#include "USBMSD_BD.h" +#include "SDBlockDevice.h" +#include "HeapBlockDevice.h" +#include "FATFileSystem.h" + +extern Serial daplink; +extern USBSerial microUSB; +extern SDBlockDevice bd; +extern FATFileSystem fs; +extern USBMSD_BD msd; + +/* + Cleans up any left over characters sent via the daplink serial stream +*/ +void clearSerialStream(); + +/* + Initializes the USB file system visible to the computer (routes files to SD card) +*/ +void startFileSystem(); + +/* + Obtains input from the DAPLINK TERMINAL via getc() and echoes it back to the puTTY terminal via putc() + this blocks program flow until a CARRIAGE RETURN ( \r ) is recieved from the PC. +*/ +bool getInput(int maxSize,char *inputArray); + +#endif \ No newline at end of file
--- a/main.cpp Fri Jul 26 03:00:19 2019 +0000 +++ b/main.cpp Sat Nov 09 01:33:17 2019 +0000 @@ -77,11 +77,11 @@ printf("Please input filename: "); char *inputptr = title+4; if(!getInput(24,inputptr)) { - printf("Filenames cannot be more than 20 characters.\033[A\r\n"); + printf("Filenames cannot be more than 20 characters.\r\n"); } audio = fopen(title,"r"); if(audio == NULL) { - printf("File not found. Please append filetype at the end of filename.\033[A\r\n"); + printf("File not found. Please append filetype at the end of filename.\r\n"); continue; } break; @@ -115,14 +115,13 @@ } maxSampleVal/=2; - int flag; //find start of actual audio fseek(audio,44,SEEK_SET); printf("Size: %i bytes\r\n",Track.size); - PWM.period_us(10); + PWM.period_us(20); short size = sizeof(short); fread((void *)audioDataBuffer,size,HALF_BUFFER,audio);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noteplayer.cpp Sat Nov 09 01:33:17 2019 +0000 @@ -0,0 +1,148 @@ +#include "noteplayer.h" + +int pitch2freq(pitchname pitch) +{ + switch(pitch) { + case rest: + return 0; + case D2: + return 73; + case Ds2: + return 78; + case E2: + return 82; + case F2: + return 87; + case Fs2: + return 92; + case G2: + return 98; + case Gs2: + return 104; + case A2: + return 110; + case As2: + return 117; + case B2: + return 123; + case C3: + return 131; + case Cs3: + return 139; + case D3: + return 147; + case Ds3: + return 156; + case E3: + return 165; + case F3: + return 175; + case Fs3: + return 185; + case G3: + return 196; + case Gs3: + return 208; + case A3: + return 220; + case As3: + return 233; + case B3: + return 247; + case C4: + return 262; + case Cs4: + return 277; + case D4: + return 294; + case Ds4: + return 311; + case E4: + return 330; + case F4: + return 349; + case Fs4: + return 370; + + case G4: + return 392; + + case Gs4: + return 415; + + case A4: + return 440; + + case As4: + return 466; + + case B4: + return 494; + + case C5: + return 523; + + case Cs5: + return 554; + + case D5: + return 587; + + case Ds5: + return 622; + + case E5: + return 659; + + case F5: + return 698; + + case Fs5: + return 740; + + case G5: + return 784; + + case Gs5: + return 831; + + case A5: + return 880; + + case As5: + return 932; + + case B5: + return 988; + + case C6: + return 1047; + + case Cs6: + return 1109; + + case D6: + return 1175; + + case Ds6: + return 1245; + + case E6: + return 1319; + + case F6: + return 1397; + + case Fs6: + return 1480; + + case G6: + return 1568; + + case Gs6: + return 1661; + + case END: + return -1; + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noteplayer.h Sat Nov 09 01:33:17 2019 +0000 @@ -0,0 +1,21 @@ +#ifndef _NOTEPLAYER_H_ +#define _NOTEPLAYER_H_ + +typedef enum : unsigned char { + C2,Cs2,D2,Ds2,E2,F2,Fs2,G2,Gs2,A2,As2,B2, //C2:0 + C3,Cs3,D3,Ds3,E3,F3,Fs3,G3,Gs3,A3,As3,B3, //C3:12 + C4,Cs4,D4,Ds4,E4,F4,Fs4,G4,Gs4,A4,As4,B4, //C4:24 + C5,Cs5,D5,Ds5,E5,F5,Fs5,G5,Gs5,A5,As5,B5, //C5:36 + C6,Cs6,D6,Ds6,E6,F6,Fs6,G6,Gs6, //C6:48 + rest, + END +} pitchname; + +typedef struct { + unsigned char length; + pitchname pitch; +} note; + +int pitch2freq(pitchname pitch); + +#endif \ No newline at end of file