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 wave_player mbed-rtos SDFileSystem
Diff: main.cpp
- Revision:
- 2:b2c4f3766090
- Parent:
- 0:bdbd3d6fc5d5
- Child:
- 3:f53cb35273f8
diff -r e4d7342be507 -r b2c4f3766090 main.cpp
--- a/main.cpp Tue May 16 05:18:55 2017 +0000
+++ b/main.cpp Thu Dec 03 16:09:24 2020 +0000
@@ -1,19 +1,139 @@
#include "mbed.h"
+#include "rtos.h"
#include "SDFileSystem.h"
-
-SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
-
+#include "wave_player.h"
+#include <string>
+
+SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
+Serial pc (USBTX,USBRX);
+DigitalOut led(LED1);
+AnalogOut DACout(p18); // speaker
+wave_player waver(&DACout);
+//#define MAXLINE 100
+//char myline [MAXLINE];
+//int i, inch ;
+string dir;
+string song;
+bool play;
+Mutex speaker_lock;
+
+
+
+void speaker_thread(void const* args)
+{
+ while(1) {
+ // check helper function for new song in GUI
+ // grab file here and put together string
+ // string song_title = "/sd/" + "" + ".wav";
+
+ FILE *wave_file;
+ dir = "/sd/" + song + ".wav";
+ wave_file=fopen(dir.c_str(),"r");
+
+ speaker_lock.lock();
+ if (play) {
+ waver.play(wave_file);
+ }
+ speaker_lock.unlock();
+ fclose(wave_file);
+ Thread::wait(100);
+ }
+}
+
+
+
+
+
+
+
+
+
+
+
+
+int main()
+{
+ Thread speaker(speaker_thread);
+ char c;
+ while(1) {
+ led = !led;
+ wait(0.2);
+ while(!pc.readable()) {
+ Thread::wait(1);
+ }
+ if (pc.getc() == '!') {
+ c = pc.getc();
+ switch(c) {
+ case '1':
+ song = "africa-toto";
+ break;
+ case '2':
+ song = "around_the_world-atc";
+ break;
+ case '3':
+ song = "beautiful_life-ace_of_base";
+ break;
+ case '4':
+ song = "dont_speak-no_doubt";
+ break;
+ case '5':
+ song = "my-love";
+ break;
+ case '6':
+ song = "Song1_test";
+ break;
+ case 'P':
+ // wave_player plays
+ play = true;
+ break;
+ case 'S':
+ // wave_player stops
+ play = false;
+ break;
+ default:
+ break;
+ }
+ }
+ Thread::wait(200);
+ }
+}
+
+
+
+
+
+
+
+//============================================================================//
+/*
int main() {
- printf("Hello World!\n");
-
- mkdir("/sd/mydir", 0777);
-
- FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
+ printf("\n\r= = = = = = = = = =");
+ printf("\n\rHello World!\n\r");
+ mkdir("/sd/mydir", 0777); // Test that previous run had created permanent folder
+ // write
+ FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); // Commenting out, in a later run, from here ..
if(fp == NULL) {
error("Could not open file for write\n");
}
- fprintf(fp, "Hello fun SD Card World!");
- fclose(fp);
-
- printf("Goodbye World!\n");
+ fprintf(fp, "Hello from SD Card World!");
+ fclose(fp);
+ printf("Created file in sdcard\n\r"); //.. to here shows file really is permanent .. .. since it still reads correctly.
+ // read
+ printf("Reading file\n");
+ string inputString;
+ FILE *fp1 = fopen("/sd/mydir/sdtest.txt", "r");
+ if (fp1 == NULL) {
+ printf("Error Open \n");
+ } else {
+ printf("\r");
+ while(fscanf(fp1, "%s", inputString)!=EOF) {
+ printf("%s ", inputString.c_str());
+ }
+ }
+ fclose(fp1); // close the file
+ printf("\n\rFinished reading the file");
+ printf("\n\r= = = = = = = = = =");
+
+ return 0;
}
+*/
\ No newline at end of file