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: 4DGL-uLCD-SE SDFileSystem mbed wave_player
main.cpp
00001 #include "mbed.h" 00002 #include <string> 00003 #include "mbed.h" 00004 #include "uLCD_4DGL.h" 00005 #include <string> 00006 #include <ctype.h> 00007 #include "SDFileSystem.h" 00008 #include "wave_player.h" 00009 RawSerial dev(p28,p27);//bluetooth 00010 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board 00011 AnalogOut DACout(p18);//speaker 00012 wave_player waver(&DACout);//wave player 00013 int x1=0; 00014 int y1=11; 00015 //nav switch class for joystick 00016 class Nav_Switch 00017 { 00018 public: 00019 Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); 00020 int read(); 00021 //boolean functions to test each switch 00022 bool up(); 00023 bool down(); 00024 bool left(); 00025 bool right(); 00026 bool fire(); 00027 00028 //automatic read on RHS 00029 operator int (); 00030 //index to any switch array style 00031 bool operator[](int index) { 00032 return _pins[index]; 00033 }; 00034 private: 00035 BusIn _pins; 00036 00037 }; 00038 Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire): 00039 _pins(up, down, left, right, fire) 00040 { 00041 _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise 00042 wait(0.001); //delays just a bit for pullups to pull inputs high 00043 } 00044 //sets up navigation switch 00045 inline bool Nav_Switch::up() 00046 { 00047 00048 return !(_pins[0]); 00049 } 00050 inline bool Nav_Switch::down() 00051 { 00052 00053 return !(_pins[1]); 00054 } 00055 inline bool Nav_Switch::left() 00056 { 00057 00058 return !(_pins[2]); 00059 } 00060 inline bool Nav_Switch::right() 00061 { 00062 00063 return !(_pins[3]); 00064 } 00065 inline bool Nav_Switch::fire() 00066 { 00067 00068 return !(_pins[4]); 00069 } 00070 inline int Nav_Switch::read() 00071 { 00072 return _pins.read(); 00073 } 00074 inline Nav_Switch::operator int () 00075 { 00076 return _pins.read(); 00077 } 00078 00079 Nav_Switch myNav( p26, p23, p24, p22, p25); //pin order on Sparkfun breakout 00080 00081 00082 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; 00083 int alphabet[26]; 00084 int x=0; 00085 int category; 00086 int word; 00087 string word1;//string of word 00088 string category1;//category 00089 string words; 00090 string words1; 00091 int numletters=0;//num letters in the phrase/word 00092 char letter; 00093 string chosenLetters;//string of chosen letters 00094 int lives=6;//you get 6 lives 00095 00096 00097 //device receive. this is so the device receives instructions 00098 void dev_recv() 00099 { 00100 00101 if(x>0&&x<3){ 00102 00103 00104 if(x==1){//x ==1 refers to phase 1 where you get category 00105 00106 category=dev.getc();//used to get category 00107 if(category==10)//this is to show end line 00108 { 00109 x=2;//move on to next phase 00110 } 00111 else{ 00112 category1=category1+char(category);//appends letter for category 00113 00114 } 00115 00116 } 00117 00118 00119 else//refers to phase 2 where you get the word/phase 00120 { 00121 00122 00123 word=dev.getc();//gets word/phrase from device 00124 if(word==10)//end line 00125 { 00126 x=3; 00127 } 00128 else{ 00129 word1=word1+char(word);//append characters 00130 00131 } 00132 00133 00134 00135 00136 00137 } 00138 00139 } 00140 } 00141 //prints the sentence/word/phrase using _ 00142 void printSentence() 00143 { 00144 string space=" "; 00145 numletters=word1.length(); 00146 for(int i=0;i<word1.length();i++) 00147 { 00148 00149 if(int(word1[i])!=32) 00150 { 00151 words=words+"_ "; 00152 words1=words1+word1[i]+" "; 00153 00154 } 00155 else 00156 { 00157 words=words+"\n"; 00158 words1=words1+"\n"; 00159 numletters--;//if space, decrease num letters 00160 } 00161 00162 //words1=words1+word1[i]+" "; 00163 } 00164 uLCD.locate(0,3); 00165 uLCD.printf("%s",words); 00166 00167 } 00168 00169 //this is if you win 00170 void youWon() 00171 { 00172 uLCD.cls(); 00173 uLCD.printf("You Win! \n"); 00174 uLCD.printf("The word/phrase \nwas:\n%s",word1); 00175 mkdir("/sd/mydir", 0777); 00176 00177 FILE *fp = fopen("/sd/win.wav", "r"); 00178 if(fp == NULL) { 00179 error("Could not open file for write\n"); 00180 } 00181 //fprintf(fp, "Hello fun SD Card World!"); 00182 waver.play(fp); 00183 fclose(fp); 00184 while(1) 00185 { 00186 wait(.1); 00187 } 00188 } 00189 //this is if you lose 00190 void youLose() 00191 { 00192 uLCD.cls(); 00193 uLCD.printf("You Lose! \n"); 00194 uLCD.printf("The word/phrase \nwas:\n%s",word1); 00195 mkdir("/sd/mydir", 0777); 00196 00197 FILE *fp = fopen("/sd/lose.wav", "r"); 00198 if(fp == NULL) { 00199 error("Could not open file for write\n"); 00200 } 00201 //fprintf(fp, "Hello fun SD Card World!"); 00202 waver.play(fp); 00203 fclose(fp); 00204 while(1) 00205 { 00206 wait(.1); 00207 } 00208 00209 } 00210 00211 //draws hangman. draws more parts depending on number lives left 00212 void drawHangman() 00213 { 00214 00215 uLCD.line(93, 127, 127, 127, 0xFF0000); 00216 uLCD.line(103, 127, 103, 90, 0xFF0000); 00217 uLCD.line(103, 90, 115, 90, 0xFF0000); 00218 uLCD.line(115, 90, 115, 94, 0xFF0000); 00219 if(lives==5){ 00220 uLCD.circle(115, 99, 5, RED);uLCD.circle(115, 99, 5, RED);}//head 00221 else if(lives==4){ 00222 uLCD.line(115, 104, 115, 119, 0xFF0000);}//body 00223 else if(lives==3){ 00224 uLCD.line(107, 107, 115, 111, 0xFF0000);}//arm 00225 else if(lives==2){ 00226 uLCD.line(123, 107, 115, 111, 0xFF0000);}//arm2 00227 else if(lives==1){ 00228 uLCD.line(107, 125, 115, 119, 0xFF0000);}//leg 00229 else if(lives==0){ 00230 uLCD.line(123, 125, 115, 119, 0xFF0000);//leg2 00231 youLose();//calls youlose if you lost 00232 } 00233 } 00234 00235 //this is when the person chooses a letter. 00236 void chooseLetter(char y) 00237 { //puts the letter in the alphabet array equal to a space so we know it has been used 00238 if(y1==11){ 00239 alphabet[(y1-11)*x1+x1]=32; 00240 } 00241 else{ 00242 alphabet[(y1-12)*x1+x1+13]=32; 00243 } 00244 00245 00246 bool isThere=false;//checks if the letter is there 00247 for(int i=0;i<words1.length();i++) 00248 { 00249 00250 if(int(y)==tolower(int(words1[i])))//changes letters to lower case 00251 { 00252 isThere=true; 00253 numletters--; 00254 words[i]=words1[i]; 00255 00256 } 00257 00258 00259 } 00260 uLCD.locate(0,3); 00261 uLCD.printf("%s",words);//prints _ for each letter now with filled in letters 00262 if(numletters==0) 00263 { 00264 youWon();//if no more letters, you win 00265 } 00266 chosenLetters= chosenLetters+y;//this is the list of letters you already chose 00267 //this is for setting up the already chosen list 00268 uLCD.locate(0,14); 00269 int yc=14; 00270 for(int i=0;i<chosenLetters.length();i++) 00271 { 00272 00273 if(i>12) 00274 { 00275 yc=15; 00276 } 00277 00278 uLCD.locate((i%13),yc); 00279 uLCD.printf("%c",chosenLetters[i]);//prints out letters you already chose 00280 00281 } 00282 if(isThere==false)//if the letter isnt there, lose a life, redraw hangman 00283 {lives--; 00284 drawHangman(); 00285 00286 } 00287 //this puts the white space on a different letter 00288 if(x1==12){ 00289 x1=0; 00290 if(y1==11){ 00291 y1=12;} 00292 else 00293 y1=11; 00294 } 00295 else { 00296 x1++;} 00297 00298 00299 00300 } 00301 00302 00303 //this sets the joystick movement up 00304 void joystick() 00305 { 00306 int i; 00307 00308 i = ~(myNav); //update leds with nav switch direction inputs 00309 i=i+32; 00310 00311 //this makes the chosen letter up 00312 if(i==1) 00313 { 00314 if(y1==12) 00315 y1=11; 00316 else 00317 y1=12; 00318 00319 } 00320 00321 00322 //goes down 00323 else if(i==2) 00324 { 00325 if(y1==11) 00326 y1=12; 00327 else 00328 y1=11; 00329 00330 } 00331 //goes left 00332 else if(i==4) 00333 { 00334 if(x1==0){ 00335 x1=12; 00336 } 00337 else{ 00338 x1--; 00339 } 00340 00341 00342 } 00343 //goes right 00344 else if(i==8) 00345 { 00346 if(x1==12) 00347 x1=0; 00348 else 00349 x1++; 00350 00351 } 00352 //select letter 00353 else if(i==16) 00354 { 00355 //following code basically to choose if you will check if the letter is there or not(if you already chose the letter before, you cant choose again) 00356 uLCD.locate(0,10); 00357 00358 uLCD.color(WHITE); 00359 bool done=true; 00360 int choose; 00361 if(chosenLetters.length()>0){ 00362 for(int i=0;i<chosenLetters.length();i++){ 00363 if(y1==11){ 00364 choose=(y1-11)*x1+x1+97; 00365 if(char((y1-11)*x1+x1+97)==chosenLetters[i]) 00366 { 00367 done=false; 00368 } 00369 00370 } 00371 else 00372 { 00373 choose=(y1-12)*x1+x1+97+13; 00374 if(char((y1-12)*x1+x1+97+13)==chosenLetters[i]) 00375 { 00376 done=false;} 00377 } 00378 } 00379 if(done) 00380 { 00381 chooseLetter(char(choose)); 00382 } 00383 00384 } 00385 else 00386 { 00387 if(y1==11) 00388 chooseLetter(char((y1-11)*x1+x1+97)); 00389 else 00390 chooseLetter(char((y1-11)*x1+x1+97+13)); 00391 } 00392 } 00393 00394 uLCD.locate(0,0); 00395 00396 wait(0.05); 00397 00398 } 00399 //this draws the letters that you can choose from and letters chosen 00400 void drawLetters() 00401 { 00402 //sets up location 00403 uLCD.locate(0,10); 00404 uLCD.printf("Choose one:"); 00405 uLCD.locate(0,13); 00406 uLCD.printf("Chosen:"); 00407 uLCD.locate(0,11); 00408 00409 int ya=11; 00410 for(int i=0;i<26;i++) 00411 { 00412 00413 if(i>12) 00414 { 00415 ya=12; 00416 } 00417 //makes background black and letter white if not chosen 00418 if(ya==y1&&((i%13)==x1)){ 00419 uLCD.textbackground_color(WHITE); 00420 uLCD.color(BLACK); 00421 } 00422 //makes background white and letter black if chosen 00423 else 00424 { 00425 uLCD.textbackground_color(BLACK); 00426 uLCD.color(WHITE); 00427 } 00428 //prints the alphabet 00429 uLCD.locate((i%13),ya); 00430 uLCD.printf("%c",alphabet[i]); 00431 uLCD.textbackground_color(BLACK); 00432 uLCD.color(WHITE); 00433 } 00434 00435 } 00436 int main() 00437 { 00438 //clears screan 00439 uLCD.cls(); 00440 00441 00442 uLCD.color(WHITE); 00443 for(int i=0;i<26;i++) 00444 { 00445 alphabet[i]=i+97; 00446 } 00447 00448 00449 uLCD.baudrate(3000000); //jack up baud rate to max for fast display 00450 00451 dev.baud(9600); 00452 dev.attach(&dev_recv, Serial::RxIrq); 00453 //tells player with mbed to start game 00454 uLCD.printf("You are now starting a game of Hangman. Your friend is choosing a category and phrase"); 00455 x=1; 00456 //tells player with device to start game 00457 dev.printf("You are now starting a game of Hangman with your friend \n"); 00458 wait(.1); 00459 //tells player with device to choose category 00460 dev.printf("Please enter a category and press enter"); 00461 00462 //phase 1(choose category) waiting 00463 while((x==1)){ 00464 wait(.1); 00465 } 00466 00467 //clear lcd 00468 uLCD.cls(); 00469 //draw hangman 00470 drawHangman(); 00471 uLCD.printf("Category/Hint:\n%s",category1); 00472 //tells player with device to choose word/phrase 00473 dev.printf("Please enter a word/phrase and press enter"); 00474 00475 00476 //phase 2(choose word) 00477 while(x==2) { 00478 wait(.1); 00479 } 00480 //print sentence 00481 printSentence(); 00482 //loop 00483 while(1){ 00484 00485 //draws letters 00486 drawLetters(); 00487 //check joystick 00488 joystick(); 00489 } 00490 //chooseLetter('a'); 00491 //joystick(); 00492 00493 00494 }
Generated on Fri Aug 19 2022 09:19:58 by
1.7.2