Lab4 4180 photocell controlled cursor game

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player

Committer:
pwolfe8
Date:
Tue Oct 20 20:22:41 2015 +0000
Revision:
0:a17e0e0a506a
4180Lab4Fall15PhilipWolfe;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pwolfe8 0:a17e0e0a506a 1 #include "mbed.h"
pwolfe8 0:a17e0e0a506a 2 #include "rtos.h"
pwolfe8 0:a17e0e0a506a 3 #include "uLCD_4DGL.h"
pwolfe8 0:a17e0e0a506a 4 #include "SDFileSystem.h"
pwolfe8 0:a17e0e0a506a 5 #include "wave_player.h"
pwolfe8 0:a17e0e0a506a 6
pwolfe8 0:a17e0e0a506a 7 // Mutex initialization
pwolfe8 0:a17e0e0a506a 8 Mutex lcdLock;
pwolfe8 0:a17e0e0a506a 9 Mutex speakerLock;
pwolfe8 0:a17e0e0a506a 10 Mutex hitLock;
pwolfe8 0:a17e0e0a506a 11
pwolfe8 0:a17e0e0a506a 12 // AnalogIn initialization
pwolfe8 0:a17e0e0a506a 13 AnalogIn photocell1(p15);
pwolfe8 0:a17e0e0a506a 14 AnalogIn photocell2(p16);
pwolfe8 0:a17e0e0a506a 15
pwolfe8 0:a17e0e0a506a 16 // Speaker initialization
pwolfe8 0:a17e0e0a506a 17 //PwmSpeaker speaker(p21);
pwolfe8 0:a17e0e0a506a 18 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
pwolfe8 0:a17e0e0a506a 19 FILE *wave_file;
pwolfe8 0:a17e0e0a506a 20 AnalogOut DACout(p18);
pwolfe8 0:a17e0e0a506a 21 wave_player waver(&DACout);
pwolfe8 0:a17e0e0a506a 22
pwolfe8 0:a17e0e0a506a 23 // uLCD initialization
pwolfe8 0:a17e0e0a506a 24 uLCD_4DGL uLCD(p28,p27,p26); // tx, rx, rst;
pwolfe8 0:a17e0e0a506a 25
pwolfe8 0:a17e0e0a506a 26 //Variable initializations
pwolfe8 0:a17e0e0a506a 27 int c1=0, c2=0, s1=0; //photocell variables
pwolfe8 0:a17e0e0a506a 28 int x=64, y=64, cursorRadius = 5; // cursor variables
pwolfe8 0:a17e0e0a506a 29 int xr=0,yr=0, hitRadius = 7; // beatmap variables
pwolfe8 0:a17e0e0a506a 30 int pnts = 0; int distAwaySqr = 100, distReqSqr = 101; int clearX=0, clearY=0; //handleHit variables
pwolfe8 0:a17e0e0a506a 31 static void handleHit() {
pwolfe8 0:a17e0e0a506a 32 Thread::wait(50);
pwolfe8 0:a17e0e0a506a 33
pwolfe8 0:a17e0e0a506a 34 hitLock.lock();
pwolfe8 0:a17e0e0a506a 35 clearX = xr;
pwolfe8 0:a17e0e0a506a 36 clearY = yr;
pwolfe8 0:a17e0e0a506a 37 hitLock.unlock();
pwolfe8 0:a17e0e0a506a 38
pwolfe8 0:a17e0e0a506a 39 distAwaySqr = (x-clearX)*(x-clearX) + (y-clearY)*(y-clearY);
pwolfe8 0:a17e0e0a506a 40 distReqSqr = (cursorRadius+hitRadius)*(cursorRadius+hitRadius);
pwolfe8 0:a17e0e0a506a 41 if ( distAwaySqr <= distReqSqr ) {
pwolfe8 0:a17e0e0a506a 42 pnts++;
pwolfe8 0:a17e0e0a506a 43 Thread::wait(50);
pwolfe8 0:a17e0e0a506a 44
pwolfe8 0:a17e0e0a506a 45 lcdLock.lock();
pwolfe8 0:a17e0e0a506a 46 uLCD.circle(clearX,clearY,hitRadius+3,RED);
pwolfe8 0:a17e0e0a506a 47 uLCD.circle(clearX,clearY,hitRadius+5,RED);
pwolfe8 0:a17e0e0a506a 48 lcdLock.unlock();
pwolfe8 0:a17e0e0a506a 49 Thread::wait(100);
pwolfe8 0:a17e0e0a506a 50 lcdLock.lock();
pwolfe8 0:a17e0e0a506a 51 uLCD.filled_circle(clearX,clearY,hitRadius+5,BLACK);
pwolfe8 0:a17e0e0a506a 52 lcdLock.unlock();
pwolfe8 0:a17e0e0a506a 53
pwolfe8 0:a17e0e0a506a 54 Thread::wait(50);
pwolfe8 0:a17e0e0a506a 55
pwolfe8 0:a17e0e0a506a 56 lcdLock.lock();
pwolfe8 0:a17e0e0a506a 57 uLCD.printf("pnts: %d\r", pnts);
pwolfe8 0:a17e0e0a506a 58 lcdLock.unlock();
pwolfe8 0:a17e0e0a506a 59 }
pwolfe8 0:a17e0e0a506a 60 Thread::wait(50);
pwolfe8 0:a17e0e0a506a 61 }
pwolfe8 0:a17e0e0a506a 62
pwolfe8 0:a17e0e0a506a 63 // Photocell thread
pwolfe8 0:a17e0e0a506a 64 void phc_thread(void const *args) {
pwolfe8 0:a17e0e0a506a 65 while(1){
pwolfe8 0:a17e0e0a506a 66 c1 = (photocell1.read()- 0.06)*150;
pwolfe8 0:a17e0e0a506a 67 c1 = (c1>64) ? 64 : c1; //clamp below 64
pwolfe8 0:a17e0e0a506a 68 c1 = c1 - 32; // move between -32 and 31
pwolfe8 0:a17e0e0a506a 69
pwolfe8 0:a17e0e0a506a 70 c2 = (photocell2.read() - 0.06)*150;
pwolfe8 0:a17e0e0a506a 71 c2 = (c2>64) ? 64 : c2; //clamp below 64
pwolfe8 0:a17e0e0a506a 72 c2 = c2 - 32; // move between -32 and 31
pwolfe8 0:a17e0e0a506a 73 Thread::wait(50);
pwolfe8 0:a17e0e0a506a 74 }
pwolfe8 0:a17e0e0a506a 75 }
pwolfe8 0:a17e0e0a506a 76
pwolfe8 0:a17e0e0a506a 77 // player cursor thread
pwolfe8 0:a17e0e0a506a 78 void cursor_thread(void const *args) {
pwolfe8 0:a17e0e0a506a 79 while(1){
pwolfe8 0:a17e0e0a506a 80 lcdLock.lock();
pwolfe8 0:a17e0e0a506a 81 uLCD.filled_circle(x,y,cursorRadius,BLACK);
pwolfe8 0:a17e0e0a506a 82 lcdLock.unlock();
pwolfe8 0:a17e0e0a506a 83 Thread::wait(50);
pwolfe8 0:a17e0e0a506a 84
pwolfe8 0:a17e0e0a506a 85 x = 64-c1;
pwolfe8 0:a17e0e0a506a 86 y = 64-c2;
pwolfe8 0:a17e0e0a506a 87 handleHit();
pwolfe8 0:a17e0e0a506a 88
pwolfe8 0:a17e0e0a506a 89 lcdLock.lock();
pwolfe8 0:a17e0e0a506a 90 uLCD.filled_circle(x,y,cursorRadius,GREEN);
pwolfe8 0:a17e0e0a506a 91 lcdLock.unlock();
pwolfe8 0:a17e0e0a506a 92 Thread::wait(50);
pwolfe8 0:a17e0e0a506a 93 }
pwolfe8 0:a17e0e0a506a 94 }
pwolfe8 0:a17e0e0a506a 95
pwolfe8 0:a17e0e0a506a 96 // beatmap thread
pwolfe8 0:a17e0e0a506a 97 void bm_thread(void const *args) {
pwolfe8 0:a17e0e0a506a 98 while(1){
pwolfe8 0:a17e0e0a506a 99 hitLock.lock();
pwolfe8 0:a17e0e0a506a 100 xr = rand()%64 + 32;
pwolfe8 0:a17e0e0a506a 101 yr = rand()%64 + 32;
pwolfe8 0:a17e0e0a506a 102 hitLock.unlock();
pwolfe8 0:a17e0e0a506a 103
pwolfe8 0:a17e0e0a506a 104 lcdLock.lock();
pwolfe8 0:a17e0e0a506a 105 uLCD.filled_circle(xr,yr,hitRadius,0xFF00FF);
pwolfe8 0:a17e0e0a506a 106 lcdLock.unlock();
pwolfe8 0:a17e0e0a506a 107
pwolfe8 0:a17e0e0a506a 108 Thread::wait(4000);
pwolfe8 0:a17e0e0a506a 109
pwolfe8 0:a17e0e0a506a 110 lcdLock.lock();
pwolfe8 0:a17e0e0a506a 111 uLCD.filled_circle(xr,yr,hitRadius,BLACK);
pwolfe8 0:a17e0e0a506a 112 lcdLock.unlock();
pwolfe8 0:a17e0e0a506a 113 Thread::wait(50);
pwolfe8 0:a17e0e0a506a 114 }
pwolfe8 0:a17e0e0a506a 115 }
pwolfe8 0:a17e0e0a506a 116
pwolfe8 0:a17e0e0a506a 117 // Sound thread
pwolfe8 0:a17e0e0a506a 118 void sound_thread(void const *args) {
pwolfe8 0:a17e0e0a506a 119 while(1){
pwolfe8 0:a17e0e0a506a 120 wave_file=fopen("/sd/wavfiles/CaveStory.wav","r");
pwolfe8 0:a17e0e0a506a 121 speakerLock.lock();
pwolfe8 0:a17e0e0a506a 122 waver.play(wave_file);
pwolfe8 0:a17e0e0a506a 123 speakerLock.unlock();
pwolfe8 0:a17e0e0a506a 124 fclose(wave_file);
pwolfe8 0:a17e0e0a506a 125 Thread::wait(30000); // wait 30s before playing again
pwolfe8 0:a17e0e0a506a 126 }
pwolfe8 0:a17e0e0a506a 127
pwolfe8 0:a17e0e0a506a 128 }
pwolfe8 0:a17e0e0a506a 129
pwolfe8 0:a17e0e0a506a 130 int main() {
pwolfe8 0:a17e0e0a506a 131 uLCD.printf("pnts: %d\n\n\n\n\n\n\n\n\n\n\n\n\n\n", pnts);
pwolfe8 0:a17e0e0a506a 132 uLCD.printf("Chase pink circle!");
pwolfe8 0:a17e0e0a506a 133 uLCD.locate(0,0);
pwolfe8 0:a17e0e0a506a 134 Thread cursor(cursor_thread);
pwolfe8 0:a17e0e0a506a 135 Thread beatmap(bm_thread);
pwolfe8 0:a17e0e0a506a 136 Thread sound(sound_thread);
pwolfe8 0:a17e0e0a506a 137 Thread pcell(phc_thread);
pwolfe8 0:a17e0e0a506a 138
pwolfe8 0:a17e0e0a506a 139 while (true) {
pwolfe8 0:a17e0e0a506a 140 }
pwolfe8 0:a17e0e0a506a 141 }