Lab4 4180 photocell controlled cursor game
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
Revision 0:a17e0e0a506a, committed 2015-10-20
- Comitter:
- pwolfe8
- Date:
- Tue Oct 20 20:22:41 2015 +0000
- Commit message:
- 4180Lab4Fall15PhilipWolfe;
Changed in this revision
diff -r 000000000000 -r a17e0e0a506a 4DGL-uLCD-SE.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Tue Oct 20 20:22:41 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/4180_1/code/4DGL-uLCD-SE/#e39a44de229a
diff -r 000000000000 -r a17e0e0a506a PwmSpeaker.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PwmSpeaker.cpp Tue Oct 20 20:22:41 2015 +0000 @@ -0,0 +1,50 @@ +#include "PwmSpeaker.h" + +PwmSpeaker::PwmSpeaker(PinName pin) : _pin(pin) { + // _pin(pin) means pass pin to the Speaker Constructor +} + +// class method to play a note based on PwmOut class +void PwmSpeaker::playNote(float frequency, float duration, float volume) { + _pin.period(1.0/frequency); + _pin = volume/2.0; //50% duty cycle - max volume + wait(duration); + _pin = 0.0; //turn off note +} + +// generate a short 150Hz tone using PWM hardware output +// something like this can be used for a button click effect for feedback + //for (i=0; i<10; i++) { +// speaker.period(1.0/150.0); // 500hz period +// speaker =0.25; //25% duty cycle - mid range volume +// wait(.02); +// speaker=0.0; // turn off audio +// wait(0.5); +// } +// sweep up in frequency by changing the PWM period + //for (i=0; i<8000; i=i+100) { +// speaker.period(1.0/float(i)); +// speaker=0.25; +// wait(.1); +// } +// wait(2); +// two tone police siren effect - two periods or two frequencies +// increase volume - by changing the PWM duty cycle +// for (i=0; i<26; i=i+2) { +// speaker.period(1.0/969.0); +// speaker = float(i)/50.0; +// wait(.5); +// speaker.period(1.0/800.0); +// wait(.5); +// } +//// decrease volume +// for (i=25; i>=0; i=i-2) { +// speaker.period(1.0/969.0); +// speaker = float(i)/50.0; +// wait(.5); +// speaker.period(1.0/800.0); +// wait(.5); +// } +// speaker =0.0; +// wait(2); +//} \ No newline at end of file
diff -r 000000000000 -r a17e0e0a506a PwmSpeaker.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PwmSpeaker.h Tue Oct 20 20:22:41 2015 +0000 @@ -0,0 +1,10 @@ +#include "mbed.h" + +class PwmSpeaker +{ + public: + PwmSpeaker(PinName pin); + void playNote(float frequency, float duration, float volume); + private: + PwmOut _pin; +}; \ No newline at end of file
diff -r 000000000000 -r a17e0e0a506a SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Tue Oct 20 20:22:41 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mbed_official/code/SDFileSystem/#7b35d1709458
diff -r 000000000000 -r a17e0e0a506a main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Oct 20 20:22:41 2015 +0000 @@ -0,0 +1,141 @@ +#include "mbed.h" +#include "rtos.h" +#include "uLCD_4DGL.h" +#include "SDFileSystem.h" +#include "wave_player.h" + +// Mutex initialization +Mutex lcdLock; +Mutex speakerLock; +Mutex hitLock; + +// AnalogIn initialization +AnalogIn photocell1(p15); +AnalogIn photocell2(p16); + +// Speaker initialization +//PwmSpeaker speaker(p21); + SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card + FILE *wave_file; + AnalogOut DACout(p18); + wave_player waver(&DACout); + +// uLCD initialization +uLCD_4DGL uLCD(p28,p27,p26); // tx, rx, rst; + +//Variable initializations +int c1=0, c2=0, s1=0; //photocell variables +int x=64, y=64, cursorRadius = 5; // cursor variables +int xr=0,yr=0, hitRadius = 7; // beatmap variables +int pnts = 0; int distAwaySqr = 100, distReqSqr = 101; int clearX=0, clearY=0; //handleHit variables +static void handleHit() { + Thread::wait(50); + + hitLock.lock(); + clearX = xr; + clearY = yr; + hitLock.unlock(); + + distAwaySqr = (x-clearX)*(x-clearX) + (y-clearY)*(y-clearY); + distReqSqr = (cursorRadius+hitRadius)*(cursorRadius+hitRadius); + if ( distAwaySqr <= distReqSqr ) { + pnts++; + Thread::wait(50); + + lcdLock.lock(); + uLCD.circle(clearX,clearY,hitRadius+3,RED); + uLCD.circle(clearX,clearY,hitRadius+5,RED); + lcdLock.unlock(); + Thread::wait(100); + lcdLock.lock(); + uLCD.filled_circle(clearX,clearY,hitRadius+5,BLACK); + lcdLock.unlock(); + + Thread::wait(50); + + lcdLock.lock(); + uLCD.printf("pnts: %d\r", pnts); + lcdLock.unlock(); + } + Thread::wait(50); +} + +// Photocell thread +void phc_thread(void const *args) { + while(1){ + c1 = (photocell1.read()- 0.06)*150; + c1 = (c1>64) ? 64 : c1; //clamp below 64 + c1 = c1 - 32; // move between -32 and 31 + + c2 = (photocell2.read() - 0.06)*150; + c2 = (c2>64) ? 64 : c2; //clamp below 64 + c2 = c2 - 32; // move between -32 and 31 + Thread::wait(50); + } +} + +// player cursor thread +void cursor_thread(void const *args) { + while(1){ + lcdLock.lock(); + uLCD.filled_circle(x,y,cursorRadius,BLACK); + lcdLock.unlock(); + Thread::wait(50); + + x = 64-c1; + y = 64-c2; + handleHit(); + + lcdLock.lock(); + uLCD.filled_circle(x,y,cursorRadius,GREEN); + lcdLock.unlock(); + Thread::wait(50); + } +} + +// beatmap thread +void bm_thread(void const *args) { + while(1){ + hitLock.lock(); + xr = rand()%64 + 32; + yr = rand()%64 + 32; + hitLock.unlock(); + + lcdLock.lock(); + uLCD.filled_circle(xr,yr,hitRadius,0xFF00FF); + lcdLock.unlock(); + + Thread::wait(4000); + + lcdLock.lock(); + uLCD.filled_circle(xr,yr,hitRadius,BLACK); + lcdLock.unlock(); + Thread::wait(50); + } +} + +// Sound thread +void sound_thread(void const *args) { + while(1){ + wave_file=fopen("/sd/wavfiles/CaveStory.wav","r"); + speakerLock.lock(); + waver.play(wave_file); + speakerLock.unlock(); + fclose(wave_file); + Thread::wait(30000); // wait 30s before playing again + } + +} + +int main() { + uLCD.printf("pnts: %d\n\n\n\n\n\n\n\n\n\n\n\n\n\n", pnts); + uLCD.printf("Chase pink circle!"); + uLCD.locate(0,0); + Thread cursor(cursor_thread); + Thread beatmap(bm_thread); + Thread sound(sound_thread); + Thread pcell(phc_thread); + + while (true) { + } +}
diff -r 000000000000 -r a17e0e0a506a mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Tue Oct 20 20:22:41 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#12552ef4e980
diff -r 000000000000 -r a17e0e0a506a mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Oct 20 20:22:41 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/34e6b704fe68 \ No newline at end of file
diff -r 000000000000 -r a17e0e0a506a wave_player.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wave_player.lib Tue Oct 20 20:22:41 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/sravet/code/wave_player/#acc3e18e77ad