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: mbed SDFileSystem
Revision 7:af45a10fdfb6, committed 2019-04-27
- Comitter:
- kchen7
- Date:
- Sat Apr 27 21:47:02 2019 +0000
- Parent:
- 6:7f0a090b12e2
- Commit message:
- baseline demo version
Changed in this revision
--- a/main.cpp Fri Apr 26 23:56:19 2019 +0000
+++ b/main.cpp Sat Apr 27 21:47:02 2019 +0000
@@ -1,18 +1,14 @@
// ESE350 Final Project: Drue
#include "mbed.h"
-#include "wave_player.h"
+#include "sd_card_player.h"
#include "SDFileSystem.h"
-//#include "SDBlockDevice.h"
-
-//#include "sdCard.cpp"
-
// Pin setup ////////////////////////////////////////////
SDFileSystem sd(p5, p6, p7, p8, "sd");
Timer timer;
Serial pc(USBTX, USBRX);
AnalogOut DACout(p18); //set up speaker
-wave_player waver(&DACout); //set up wave player library
+sd_card_player waveOut(&DACout);
// Button Inputs
DigitalIn butC(p14);
@@ -32,7 +28,10 @@
I2C i2cLED(p9, p10);
// LED for tests
-DigitalOut testLED(LED1);
+DigitalOut indicator1(LED1);
+DigitalOut indicator2(LED2);
+DigitalOut indicator3(LED3);
+DigitalOut indicator4(LED4);
/////////////////////////////////////////////////////////
@@ -55,7 +54,7 @@
void variableInit() {
// modes
mode = 0;
- numModes = 3;
+ numModes = 4;
switchPressed = false;
// keys
notePressed = false;
@@ -80,8 +79,17 @@
butMode.mode(PullUp);
}
-void speakerSetup(){
+void testSpeaker(){
+ FILE *wave_file;
+ wave_file = fopen("/sd/notes/C4.wav","r"); //opens the music file
+ if (wave_file == NULL){
+ error("Could not open file for read");
+ }
+ pc.printf("begin play sound \n");
+ waveOut.play(wave_file); //plays the music file)
+ pc.printf("after play sound\n");
+ fclose(wave_file);
}
void pressKey() {
@@ -122,6 +130,7 @@
if (!notePressed) {
if (butC == 0) {
speaker.period(1.0/523.25);
+ //testSpeaker();
prevNote = 0;
} else if (butD == 0) {
speaker.period(1.0/587.33);
@@ -169,6 +178,28 @@
}
}
+void lightIndicatorLED(int led) {
+ indicator1 = 0;
+ indicator2 = 0;
+ indicator3 = 0;
+ indicator4 = 0;
+
+ switch(led) {
+ case 0:
+ indicator1 = 1;
+ break;
+ case 1:
+ indicator2 = 1;
+ break;
+ case 2:
+ indicator3 = 1;
+ break;
+ case 3:
+ indicator4 = 1;
+ break;
+ }
+}
+
void switchModeCheck() {
if (butMode == 0) {
if (!switchPressed) {
@@ -186,6 +217,7 @@
mode = 0;
pianoMode = true;
}
+ lightIndicatorLED(mode);
}
} else {
switchPressed = false;
@@ -253,6 +285,8 @@
void playWhackAMole() {
if (needNote) {
+ lightLED(-1);
+ wait(0.05);
targetNote = rand() % 8;
needNote = false;
lightLED(targetNote);
@@ -260,38 +294,44 @@
pressKey(); // rest of checks are in this function
}
-void testSd(){
- pc.printf("Kevin sucks\n");
- mkdir("/sd/mydir", 0777);
- FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
- if(fp == NULL) {
- error("Could not open file for write\n");
- }
- fprintf(fp, "If you see this you owe Mirko a cookie hah");
- fclose(fp);
-
- pc.printf("Mirko wins\n");
-}
+//void testSd(){
+// pc.printf("Kevin sucks\n");
+// mkdir("/sd/mydir", 0777);
+// FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
+// if(fp == NULL) {
+// error("Could not open file for write\n");
+// }
+// fprintf(fp, "If you see this you owe Mirko a cookie hah!!");
+// fclose(fp);
+//
+// pc.printf("Mirko wins\n");
+//}
-void testSpeaker(){
-
- FILE *wave_file;
- wave_file = fopen("/sd/notes/C4.wav","r"); //opens the music file
- waver.play(wave_file); //plays the music file
- fclose(wave_file);
+void playTeachPiano() {
+ static int notes[] = {2,2,2,2,2,2,2,4,0,1,2,3,3,3,3,3,2,2,2,2,4,4,3,1,0};
+ static int i = 0;
+ if (needNote) {
+ lightLED(-1);
+ wait(0.05);
+ targetNote = notes[i];
+ i++;
+ if (i == 25) {
+ i = 0;
+ }
+ needNote = false;
+ lightLED(targetNote);
+ }
+ pressKey(); // rest of checks are in this function
}
int main() {
pc.baud (115200);
variableInit();
buttonSetup();
- speakerSetup();
wait(0.5);
+
+ lightIndicatorLED(0);
-// testSd();
-// testSpeaker();
-// testSd();
-
while(1) {
switchModeCheck();
@@ -300,7 +340,9 @@
lightLED(prevNote);
} else if (mode == 1) { // WHACK A MOLE MODE
playWhackAMole();
- } else if (mode == 2) { // CYCLE SOUNDS MODE
+ } else if (mode == 2) {
+ playTeachPiano();
+ } else if (mode == 3) { // CYCLE SOUNDS MODE
cycleSound();
lightLED(prevNote);
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sd_card_player.cpp Sat Apr 27 21:47:02 2019 +0000
@@ -0,0 +1,141 @@
+#include <mbed.h>
+#include <sd_card_player.h>
+
+// must pass p18 on mbed (only one w/ AnalogOut)
+sd_card_player::sd_card_player(AnalogOut *_dac) {
+ wave_DAC=_dac;
+ wave_DAC->write_u16(32768); //DAC is 0-3.3V, so idles at ~1.6V
+}
+
+//-----------------------------------------------------------------------------
+// player function. Takes a pointer to an opened wave file. The file needs
+// to be stored in a filesystem with enough bandwidth to feed the wave data.
+// LocalFileSystem isn't, but the SDcard is, at least for 22kHz files. The
+// SDcard filesystem can be hotrodded by increasing the SPI frequency it uses
+// internally.
+//-----------------------------------------------------------------------------
+void sd_card_player::play(FILE *wavefile)
+{
+ unsigned chunk_id,chunk_size,channel;
+ unsigned data,samp_int,i;
+ short unsigned dac_data;
+ long long slice_value;
+ char *slice_buf;
+ short *data_sptr;
+ unsigned char *data_bptr;
+ int *data_wptr;
+ FMT_STRUCT wav_format;
+ long slice,num_slices;
+ DAC_wptr=0;
+ DAC_rptr=0;
+ for (i=0;i<256;i+=2) {
+ DAC_fifo[i]=0;
+ DAC_fifo[i+1]=3000;
+ }
+ DAC_wptr=4;
+ DAC_on=0;
+
+ fread(&chunk_id,4,1,wavefile);
+ fread(&chunk_size,4,1,wavefile);
+ while (!feof(wavefile)) {
+ switch (chunk_id) {
+ case 0x46464952:
+ fread(&data,4,1,wavefile);
+ break;
+ case 0x20746d66:
+ fread(&wav_format,sizeof(wav_format),1,wavefile);
+ if (chunk_size > sizeof(wav_format))
+ fseek(wavefile,chunk_size-sizeof(wav_format),SEEK_CUR);
+ break;
+ case 0x61746164:
+// allocate a buffer big enough to hold a slice
+ slice_buf=(char *)malloc(wav_format.block_align);
+ if (!slice_buf) {
+ printf("Unable to malloc slice buffer");
+ exit(1);
+ }
+ num_slices=chunk_size/wav_format.block_align;
+ samp_int=1000000/(wav_format.sample_rate);
+
+// starting up ticker to write samples out -- no printfs until tick.detach is called
+ tick.attach_us(this,&sd_card_player::dac_out, samp_int);
+ DAC_on=1;
+
+// start reading slices, which contain one sample each for however many channels
+// are in the wave file. one channel=mono, two channels=stereo, etc. Since
+// mbed only has a single AnalogOut, all of the channels present are averaged
+// to produce a single sample value. This summing and averaging happens in
+// a variable of type signed long long, to make sure that the data doesn't
+// overflow regardless of sample size (8 bits, 16 bits, 32 bits).
+//
+// note that from what I can find that 8 bit wave files use unsigned data,
+// while 16 and 32 bit wave files use signed data
+//
+ for (slice=0;slice<num_slices;slice+=1) {
+ fread(slice_buf,wav_format.block_align,1,wavefile);
+ if (feof(wavefile)) {
+ printf("Oops -- not enough slices in the wave file\n");
+ exit(1);
+ }
+ data_sptr=(short *)slice_buf; // 16 bit samples
+ data_bptr=(unsigned char *)slice_buf; // 8 bit samples
+ data_wptr=(int *)slice_buf; // 32 bit samples
+ slice_value=0;
+ for (channel=0;channel<wav_format.num_channels;channel++) {
+ switch (wav_format.sig_bps) {
+ case 16:
+ slice_value+=data_sptr[channel];
+ break;
+ case 32:
+ slice_value+=data_wptr[channel];
+ break;
+ case 8:
+ slice_value+=data_bptr[channel];
+ break;
+ }
+ }
+ slice_value/=wav_format.num_channels;
+
+// slice_value is now averaged. Next it needs to be scaled to an unsigned 16 bit value
+// with DC offset so it can be written to the DAC.
+ switch (wav_format.sig_bps) {
+ case 8: slice_value<<=8;
+ break;
+ case 16: slice_value+=32768;
+ break;
+ case 32: slice_value>>=16;
+ slice_value+=32768;
+ break;
+ }
+ dac_data=(short unsigned)slice_value;
+ DAC_fifo[DAC_wptr]=dac_data;
+ DAC_wptr=(DAC_wptr+1) & 0xff;
+ while (DAC_wptr==DAC_rptr) {
+ }
+ }
+ DAC_on=0;
+ tick.detach();
+ free(slice_buf);
+ break;
+ case 0x5453494c:
+ fseek(wavefile,chunk_size,SEEK_CUR);
+ break;
+ default:
+ printf("unknown chunk type 0x%x, size %d\n",chunk_id,chunk_size);
+ data=fseek(wavefile,chunk_size,SEEK_CUR);
+ break;
+ }
+ fread(&chunk_id,4,1,wavefile);
+ fread(&chunk_size,4,1,wavefile);
+ }
+}
+
+
+void sd_card_player::dac_out()
+{
+ if (DAC_on) {
+ wave_DAC->write_u16(DAC_fifo[DAC_rptr]);
+ DAC_rptr=(DAC_rptr+1) & 0xff;
+ }
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sd_card_player.h Sat Apr 27 21:47:02 2019 +0000
@@ -0,0 +1,35 @@
+#include <mbed.h>
+
+typedef struct uFMT_STRUCT {
+ short comp_code;
+ short num_channels;
+ unsigned sample_rate;
+ unsigned avg_Bps;
+ short block_align;
+ short sig_bps;
+} FMT_STRUCT;
+
+
+class sd_card_player {
+
+public:
+
+sd_card_player(AnalogOut *_dac);
+
+/** the player function.
+ *
+ * @param wavefile A pointer to an opened wave file
+ */
+void play(FILE *wavefile);
+
+private:
+ void dac_out(void);
+ AnalogOut *wave_DAC;
+ Ticker tick;
+ unsigned short DAC_fifo[256];
+ short DAC_wptr;
+ volatile short DAC_rptr;
+ short DAC_on;
+};
+
+
--- a/wave_player.lib Fri Apr 26 23:56:19 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://os.mbed.com/users/sravet/code/wave_player/#acc3e18e77ad
