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: USBHost USBHostXpad mbed-rtos mbed
Diff: Audio.h
- Revision:
- 8:36b2ef26a0b1
- Parent:
- 2:5e870c215495
--- a/Audio.h Fri Dec 05 23:39:56 2014 +0000
+++ b/Audio.h Sat Dec 06 05:15:25 2014 +0000
@@ -8,51 +8,98 @@
private:
Mutex mutex;
Speaker& speaker;
- std::vector<Thread*> threadpool;
+ bool playing;
+ int code; //sound code
-
+ bool getplaying(){
+ bool b;
+ mutex.lock();
+ b = playing;
+ mutex.unlock();
+ return b;
+ }
+ int getcode(){
+ int c;
+ mutex.lock();
+ c = code;
+ mutex.unlock();
+ return c;
+ }
public:
Audio(Speaker& ao) : speaker(ao)
{
}
- void playSong(/**/){
+ void play(int code){
mutex.lock();
-
+ this->code = code;
+ this->playing = true;
mutex.unlock();
}
- void triggerShot(){
+ void stop(){
mutex.lock();
-
+ this->playing = false;
mutex.unlock();
}
void run(){
while(true){ //service audio requests...
-
- //put stuff here
- Thread::wait(10); //ms
+
+ //if not playing, wait fast
+ if(!getplaying()){
+ Thread::wait(1);
+ continue;
+ }
+
+
+ int code = getcode();
+
+ switch(code){
+ case 0: // up
+ speaker.PlayNote(500.0, 0.05, 1.0);
+ speaker.PlayNote(600.0, 0.05, 1.0);
+ speaker.PlayNote(700.0, 0.05, 1.0);
+ break;
+ case 1: // down
+ speaker.PlayNote(500.0, 0.05, 1.0);
+ speaker.PlayNote(550.0, 0.05, 1.0);
+ break;
+ case 2: //left
+ speaker.PlayNote(400.0, 0.1, 1.0);
+ speaker.PlayNote(500.0, 0.1, 1.0);
+ break;
+ case 3: // right
+ speaker.PlayNote(600.0, 0.05, 1.0);
+ speaker.PlayNote(500.0, 0.05, 1.0);
+ break;
+
+ // trigger sound
+ case 4:
+
+ speaker.PlayNote(500.0, 0.05, 1.0);
+ speaker.PlayNote(600.0, 0.05, 1.0);
+ speaker.PlayNote(700.0, 0.05, 1.0);
+ break;
+
+ // mario!
+ case 5:
+ playMario();
+ break;
+ default: break;
+ }
+
+
+ //Thread::wait(10);
}
}
+
+
void playNote(float a, float b, float c){
- float args[3] = {a,b,c};
- mutex.lock();
- //Thread t(_threadPlayNote, (void*)args);
- mutex.unlock();
+ speaker.PlayNote(a,b,c);
}
+public:
+
void playMario(){
-
- }
-
-private:
- void _threadPlayNote(void const * args){
- float const* fargs = (float const*)args;
- mutex.lock();
- speaker.PlayNote(fargs[0],fargs[1],fargs[2]);
- mutex.unlock();
- }
-
- void _playMario(){
playNote(660.0, 0.08, 1.0);
Thread::wait(113);
