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.
Revision 0:a6a8578e95af, committed 2019-07-30
- Comitter:
- Lugs
- Date:
- Tue Jul 30 02:44:30 2019 +0000
- Commit message:
- actually working header-using version of mini piano player
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/generalinterface.cpp Tue Jul 30 02:44:30 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 Tue Jul 30 02:44:30 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/noteplayer.cpp Tue Jul 30 02:44:30 2019 +0000
@@ -0,0 +1,194 @@
+#include "noteplayer.h"
+
+float *audioDataBuffer = new float[128];
+volatile int bufferPOS = 0;
+Ticker SampleTime;
+
+void placeNewSample()
+{
+ speaker.write(audioDataBuffer[bufferPOS++]); //multiply by POT value for volume.
+ bufferPOS = (bufferPOS+1) & 0x07F;
+}
+
+void loadBuffer()
+{
+ printf("Generating sine...\r\n");
+ for(int i=0; i<128; i++) {
+ audioDataBuffer[i] =((1.0 + sin((double(i)/16.0*6.28318530717959)))/2.0); //formula copied from mbed example
+ }
+}
+
+note makeNote(unsigned char length, pitchname pitch2make)
+{
+ note madenote;
+ madenote.length = length;
+ madenote.pitch = pitch2make;
+ return madenote;
+}
+
+void playNote(note note2play)
+{
+ float SPB = 60.0*4.0/(120.0);
+ int PlayingFreq = pitch2freq(note2play.pitch);
+ if(PlayingFreq == 0) { //rest
+ wait((1/(float)note2play.length)*SPB);
+ } else {
+ int sampleRate = PlayingFreq * 16;
+ speaker.period_us(1); //1MHz
+ float ticker_period = (float) 1/(sampleRate);
+ printf("\r\nTicker Period: %f\tTicker Freq: %f\r\nTarget Freq: %i \r\n\r\n",ticker_period, 1/ticker_period, PlayingFreq);
+
+ SampleTime.attach(&placeNewSample,ticker_period);
+ wait((1/(float)note2play.length)*SPB);
+ SampleTime.detach();
+
+ printf("\033[A\033[A\033[A\033[A");
+ }
+}
+
+int pitch2freq(pitchname pitch2switch)
+{
+ switch(pitch2switch) {
+ 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 Tue Jul 30 02:44:30 2019 +0000
@@ -0,0 +1,32 @@
+#ifndef _NOTEPLAYER_H_
+#define _NOTEPLAYER_H_
+#include "mbed.h"
+
+extern float *audioDataBuffer;
+extern volatile int bufferPOS;
+extern PwmOut speaker;
+extern Ticker SampleTime;
+
+void loadBuffer();
+
+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 pitch2switch);
+void placeNewSample();
+note makeNote(int length, pitchname pitch2make);
+void playNote(note note2play);
+
+#endif
\ No newline at end of file