Thomas Pettet / Mbed 2 deprecated Whack-a-Mole-ECE4180-Lab4

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed

Fork of MPR121_Demo by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021 */
00022 
00023 #include <mbed.h>
00024 #include <string>
00025 #include <list>
00026 #include "uLCD_4DGL.h"
00027 #include <mpr121.h>
00028 #include "SDFileSystem.h"
00029 #include "SongPlayer.h"
00030 
00031 //initialize lcd
00032 uLCD_4DGL lcd(p9, p10, p30); // tx, rx, reset
00033 
00034 DigitalOut led1(LED1);
00035 DigitalOut led2(LED2);
00036 DigitalOut led3(LED3);
00037 DigitalOut led4(LED4);
00038 
00039 //setup analog in pin to read noise for random number generation
00040 AnalogIn   Ain(p15);
00041 
00042 //Setup class D audio output on pwm out pin
00043 SongPlayer mySpeaker(p21);
00044 
00045 //Create the interrupt receiver object on pin 26
00046 InterruptIn interrupt(p26);
00047 
00048 //Setup the Serial to the PC for debugging
00049 Serial pc(USBTX, USBRX);
00050 
00051 //setup SD card filesystem
00052 SDFileSystem sd(p5, p6, p7, p8, "sd");
00053 
00054 // Setup the i2c bus on pins 28 and 27
00055 I2C i2c(p28, p27);
00056 
00057 //setup the note to be played upon hitting a mole
00058 float note[1]= {1568.0};
00059 float duration[1]= {0.1};
00060 
00061 //initialize the possition integers
00062 int pos1;
00063 int pos2;
00064 int pos3;
00065 int pos4;
00066 int pos5;
00067 int pos6;
00068 
00069 //initialize global variable to keep track of the score
00070 int score = 0;
00071 
00072 //create variables for the keypad outputs that correspond to mole possitions
00073 int hole1 = 0x100;
00074 int hole2 = 0x10;
00075 int hole3 = 0x1;
00076 int hole4 = 0x200;
00077 int hole5 = 0x20;
00078 int hole6 = 0x2;
00079 
00080 //create a character buffer to read from the SD card
00081 char str [80];
00082 
00083 // Setup the Mpr121:
00084 // constructor(i2c object, i2c address of the mpr121)
00085 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
00086 
00087 void fallInterrupt() {
00088     int key_code=0;
00089     int i=0;
00090     
00091     //read values from keypad after interupt and bitshift left 8 times
00092     int value=mpr121.read(0x00);
00093     value +=mpr121.read(0x01)<<8;
00094     //Check to see if the a mole is hit after an interupt
00095     if (value==256&&pos1==1)
00096     {
00097         mySpeaker.PlaySong(note,duration);//play note to indicate successful hit
00098         score++;//increment score by one
00099         pos1 = 0;//set the position to 0 after succesful hit to prevent multiple hits
00100     }
00101         if (value==16&&pos2==1)
00102     {
00103         mySpeaker.PlaySong(note,duration);
00104         score++;
00105         pos2 = 0;
00106     }
00107         if (value==1&&pos3==1)
00108             {
00109         mySpeaker.PlaySong(note,duration);
00110         score++;
00111         pos3 = 0;
00112     }
00113         if (value==512&&pos4==1)
00114     {
00115         mySpeaker.PlaySong(note,duration);
00116         score++;
00117         pos4 = 0;
00118     }
00119         if (value==32&&pos5==1)
00120     {
00121         mySpeaker.PlaySong(note,duration);
00122         score++;
00123         pos5 = 0;
00124     }
00125         if (value==2&&pos6==1)
00126     {
00127         mySpeaker.PlaySong(note,duration);
00128         score++;
00129         pos6 = 0;
00130     }
00131 }
00132 float arand;// variable to store analog noise
00133 unsigned int state; // global state variable
00134 
00135 int main() {
00136 lcd.baudrate(300000);// set lcd baudrate
00137     
00138 int out = 0xFF0000;// outline color
00139 int fill = 0x0000;// fill color. aka no mole
00140 int col1 = 21;
00141 int col2 = 64;
00142 int col3=107;
00143 int row3=107;
00144 int row2=64;
00145 int row1=21; 
00146 int r = 20;
00147 int rmole=18;//radius mole
00148 int mole= 0xD3D3D3;//mole color like gray
00149 
00150 
00151 //mole face
00152 int lx114=10;//14x
00153 int lx214=17;
00154 int rx114=25;
00155 int rx214=32;
00156 int lx125=53;//25x
00157 int lx225=60;
00158 int rx125=68;
00159 int rx225=75;
00160 int lx136=97;//36x
00161 int lx236=103;
00162 int rx136=111;
00163 int rx236=118;
00164 
00165 int eyey123=10;//123y
00166 int eyey456=56;//456y
00167 int eyecolor=0x0000;
00168 int nosecolor=0x0000;
00169 int whisker=0x0000;
00170 int rnose=2;
00171 
00172 int wb123=27;//pos123
00173 int wm123=21;
00174 int wt123=15;
00175 int wb456=70;//pos456
00176 int wm456=64;
00177 int wt456=58;
00178 
00179 
00180 
00181 
00182 lcd.circle(col1, row1 , r, out);// draws 6 circles to the lcd screen to show where the moles will appear
00183 lcd.circle(col1, row2 , r, out);
00184 lcd.circle(col2, row1 , r, out);
00185 lcd.circle(col2, row2 , r, out);
00186 lcd.circle(col3, row1 , r, out);
00187 lcd.circle(col3, row2 , r, out);
00188 
00189 // Begin test code for mpr121
00190 pc.printf("\nHello from the mbed & mpr121\n\r");
00191 
00192 unsigned char dataArray[2];
00193 int key;
00194 int count = 0;
00195 
00196 pc.printf("Test 1: read a value: \r\n");
00197 dataArray[0] = mpr121.read(AFE_CFG);
00198     pc.printf("Read value=%x\r\n\n",dataArray[0]);
00199 
00200     pc.printf("Test 2: read a value: \r\n");
00201     dataArray[0] = mpr121.read(0x5d);
00202     pc.printf("Read value=%x\r\n\n",dataArray[0]);
00203 
00204     pc.printf("Test 3: write & read a value: \r\n");
00205     mpr121.read(ELE0_T);
00206     mpr121.write(ELE0_T,0x22);
00207     dataArray[0] = mpr121.read(ELE0_T);
00208     pc.printf("Read value=%x\r\n\n",dataArray[0]);
00209 
00210     pc.printf("Test 4: Write many values: \r\n");
00211     unsigned char data[] = {0x1,0x3,0x5,0x9,0x15,0x25,0x41};
00212     mpr121.writeMany(0x42,data,7);
00213 
00214     // Now read them back ..
00215     key = 0x42;
00216     count = 0;
00217     while (count < 7) {
00218         char result = mpr121.read(key);
00219         key++;
00220         count++;
00221         pc.printf("Read value: '%x'=%x\n\r",key,result);
00222     }
00223 
00224     pc.printf("Test 5: Read Electrodes:\r\n");
00225     key = ELE0_T;
00226     count = 0;
00227     while (count < 24) {
00228         char result = mpr121.read(key);
00229         pc.printf("Read key:%x value:%x\n\r",key,result);
00230         key++;
00231         count++;
00232     }
00233     pc.printf("--------- \r\n\n");
00234 
00235     // mpr121.setProximityMode(true);
00236 
00237     pc.printf("ELE_CFG=%x", mpr121.read(ELE_CFG));
00238 // end test code of mpr121
00239 
00240 
00241     interrupt.fall(&fallInterrupt);//set up the interrupt mode
00242     interrupt.mode(PullUp);
00243     
00244     int counter = 0;// initialize the variable to count the number of transitions per game
00245     
00246     //read in a psuedo random noise reading and multiply so that it can later be cast to int by truncation
00247     arand = Ain.read()*1000000;
00248     
00249     //seed rand with noise reading
00250     srand(arand);
00251     
00252     //game runs for transitions
00253     while (counter < 30) {
00254 
00255         //determine state using rand()
00256         state = rand()%15;
00257 
00258         //switch the value of state to determine the output to the lcd
00259         switch(state)
00260         {
00261             case 0:
00262             //mutex functionality can be incorperated my uncommenting //POS_MUTEX.un/lock()
00263             //POS_MUTEX.lock();
00264             pos1=1;
00265             pos2=0;
00266             pos3=0;
00267             pos4=0;
00268             pos5=0;
00269             pos6=0;
00270             //POS_MUTEX.unlock();
00271             pc.printf("%i should be 0 \n\r",state);
00272             lcd.filled_circle(col1, row1 , rmole, fill);// 1
00273             lcd.filled_circle(col1, row2 , rmole, fill);
00274             lcd.filled_circle(col2, row1 , rmole, fill);
00275             lcd.filled_circle(col2, row2 , rmole, fill);
00276             lcd.filled_circle(col3, row1 , rmole, fill);
00277             lcd.filled_circle(col3, row2 , rmole, fill);
00278             lcd.filled_circle(col1, row1 , rmole, mole);
00279             //face1
00280             lcd.line(lx114, eyey123 , lx214, eyey123, eyecolor);//left eye
00281             lcd.line(rx114, eyey123 , rx214, eyey123, eyecolor);//right eye
00282             lcd.line(lx114, wb123 , rx214, wt123, whisker);//bottom
00283             lcd.line(lx114, wm123 , rx214, wm123, whisker);//middle
00284             lcd.line(lx114, wt123 , rx214, wb123, whisker);//top
00285             lcd.filled_circle(col1, row1 , rnose, nosecolor);//nose
00286 
00287             
00288             break;
00289             case 1:
00290             //POS_MUTEX.lock();
00291             pos1=0;
00292             pos2=1;
00293             pos3=0;
00294             pos4=0;
00295             pos5=0;
00296             pos6=0;
00297             //POS_MUTEX.unlock();
00298             pc.printf("%i should be 1 \n\r",state);
00299             lcd.filled_circle(col1, row1 , rmole, fill);// 2
00300             lcd.filled_circle(col1, row2 , rmole, fill);
00301             lcd.filled_circle(col2, row1 , rmole, fill);
00302             lcd.filled_circle(col2, row2 , rmole, fill);
00303             lcd.filled_circle(col3, row1 , rmole, fill);
00304             lcd.filled_circle(col3, row2 , rmole, fill);
00305             lcd.filled_circle(col2, row1 , rmole, mole);
00306             //face2
00307             lcd.line(lx125, eyey123 , lx225, eyey123, eyecolor);//left eye
00308             lcd.line(rx125, eyey123 , rx225, eyey123, eyecolor);//right eye
00309             lcd.line(lx125, wb123 , rx225, wt123, whisker);//bottom
00310             lcd.line(lx125, wm123 , rx225, wm123, whisker);//middle
00311             lcd.line(lx125, wt123 , rx225, wb123, whisker);//top
00312             lcd.filled_circle(col2, row1 , rnose, nosecolor);//nose
00313             break;
00314             case 2:
00315             //POS_MUTEX.lock();
00316             pos1=0;
00317             pos2=0;
00318             pos3=1;
00319             pos4=0;
00320             pos5=0;
00321             pos6=0;
00322             //POS_MUTEX.unlock();
00323             pc.printf("%i should be 2 \n\r",state);
00324             lcd.filled_circle(col1, row1 , rmole, fill);// 3
00325             lcd.filled_circle(col1, row2 , rmole, fill);
00326             lcd.filled_circle(col2, row1 , rmole, fill);
00327             lcd.filled_circle(col2, row2 , rmole, fill);
00328             lcd.filled_circle(col3, row1 , rmole, fill);
00329             lcd.filled_circle(col3, row2 , rmole, fill);
00330             lcd.filled_circle(col3, row1 , rmole, mole);
00331             
00332             //face3
00333             lcd.line(lx136, eyey123 , lx236, eyey123, eyecolor);//left eye
00334             lcd.line(rx136, eyey123 , rx236, eyey123, eyecolor);//right eye
00335             lcd.line(lx136, wb123 , rx236, wt123, whisker);//bottom
00336             lcd.line(lx136, wm123 , rx236, wm123, whisker);//middle
00337             lcd.line(lx136, wt123 , rx236, wb123, whisker);//top
00338             lcd.filled_circle(col3, row1 , rnose, nosecolor);//nose
00339             break;
00340             case 3:
00341             //POS_MUTEX.lock();
00342             pos1=0;
00343             pos2=0;
00344             pos3=0;
00345             pos4=1;
00346             pos5=0;
00347             pos6=0;
00348             //POS_MUTEX.unlock();
00349             pc.printf("%i should be 3 \n\r",state);
00350             lcd.filled_circle(col1, row1 , rmole, fill);// 4
00351             lcd.filled_circle(col1, row2 , rmole, fill);
00352             lcd.filled_circle(col2, row1 , rmole, fill);
00353             lcd.filled_circle(col2, row2 , rmole, fill);
00354             lcd.filled_circle(col3, row1 , rmole, fill);
00355             lcd.filled_circle(col3, row2 , rmole, fill);
00356             lcd.filled_circle(col1, row2 , rmole, mole);
00357             
00358             //face4
00359             lcd.line(lx114, eyey456 , lx214, eyey456, eyecolor);//left eye
00360             lcd.line(rx114, eyey456 , rx214, eyey456, eyecolor);//right eye
00361             lcd.line(lx114, wb456 , rx214, wt456, whisker);//bottom
00362             lcd.line(lx114, wm456 , rx214, wm456, whisker);//middle
00363             lcd.line(lx114, wt456 , rx214, wb456, whisker);//top
00364             lcd.filled_circle(col1, row2 , rnose, nosecolor);//nose
00365             
00366             break;
00367             case 4:
00368             //POS_MUTEX.lock();
00369             pos1=0;
00370             pos2=0;
00371             pos3=0;
00372             pos4=0;
00373             pos5=1;
00374             pos6=0;
00375             //POS_MUTEX.unlock();
00376             pc.printf("%i should be 4 \n\r",state);
00377             lcd.filled_circle(col1, row1 , rmole, fill);// 5
00378             lcd.filled_circle(col1, row2 , rmole, fill);
00379             lcd.filled_circle(col2, row1 , rmole, fill);
00380             lcd.filled_circle(col2, row2 , rmole, fill);
00381             lcd.filled_circle(col3, row1 , rmole, fill);
00382             lcd.filled_circle(col3, row2 , rmole, fill);
00383             lcd.filled_circle(col2, row2 , rmole, mole);
00384             //face5
00385              lcd.line(lx125, eyey456 , lx225, eyey456, eyecolor);//left eye
00386             lcd.line(rx125, eyey456 , rx225, eyey456, eyecolor);//right eye
00387             lcd.line(lx125, wb456 , rx225, wt456, whisker);//bottom
00388             lcd.line(lx125, wm456 , rx225, wm456, whisker);//middle
00389             lcd.line(lx125, wt456 , rx225, wb456, whisker);//top
00390             lcd.filled_circle(col2, row2 , rnose, nosecolor);//nose
00391             
00392             break;
00393             case 5:
00394             //POS_MUTEX.lock();
00395             pos1=0;
00396             pos2=0;
00397             pos3=0;
00398             pos4=0;
00399             pos5=0;
00400             pos6=1;
00401             //POS_MUTEX.unlock();
00402             pc.printf("%i should be 5 \n\r",state);
00403             
00404             lcd.filled_circle(col1, row1 , rmole, fill);// 6
00405             lcd.filled_circle(col1, row2 , rmole, fill);
00406             lcd.filled_circle(col2, row1 , rmole, fill);
00407             lcd.filled_circle(col2, row2 , rmole, fill);
00408             lcd.filled_circle(col3, row1 , rmole, fill);
00409             lcd.filled_circle(col3, row2 , rmole, fill);
00410             lcd.filled_circle(col3, row2 , rmole, mole);
00411             //face6
00412              lcd.line(lx136, eyey456 , lx236, eyey456, eyecolor);//left eye
00413             lcd.line(rx136, eyey456 , rx236, eyey456, eyecolor);//right eye
00414             lcd.line(lx136, wb456 , rx236, wt456, whisker);//bottom
00415             lcd.line(lx136, wm456 , rx236, wm456, whisker);//middle
00416             lcd.line(lx136, wt456 , rx236, wb456, whisker);//top
00417             lcd.filled_circle(col3, row2 , rnose, nosecolor);//nose
00418             
00419             break;
00420             case 6:
00421             //POS_MUTEX.lock();
00422             pos1=0;
00423             pos2=1;
00424             pos3=0;
00425             pos4=0;
00426             pos5=0;
00427             pos6=1;
00428             //POS_MUTEX.unlock();
00429             pc.printf("%i should be 6 \n\r",state);
00430             lcd.filled_circle(col1, row1 , rmole, fill);// 2,6
00431             lcd.filled_circle(col1, row2 , rmole, fill);
00432             lcd.filled_circle(col2, row1 , rmole, fill);
00433             lcd.filled_circle(col2, row2 , rmole, fill);
00434             lcd.filled_circle(col3, row1 , rmole, fill);
00435             lcd.filled_circle(col3, row2 , rmole, fill);
00436             lcd.filled_circle(col2, row1 , rmole, mole);
00437             lcd.filled_circle(col3, row2 , rmole, mole);
00438             
00439             //face2
00440             lcd.line(lx125, eyey123 , lx225, eyey123, eyecolor);//left eye
00441             lcd.line(rx125, eyey123 , rx225, eyey123, eyecolor);//right eye
00442             lcd.line(lx125, wb123 , rx225, wt123, whisker);//bottom
00443             lcd.line(lx125, wm123 , rx225, wm123, whisker);//middle
00444             lcd.line(lx125, wt123 , rx225, wb123, whisker);//top
00445             lcd.filled_circle(col2, row1 , rnose, nosecolor);//nose
00446             //face6
00447             lcd.line(lx136, eyey456 , lx236, eyey456, eyecolor);//left eye
00448             lcd.line(rx136, eyey456 , rx236, eyey456, eyecolor);//right eye
00449             lcd.line(lx136, wb456 , rx236, wt456, whisker);//bottom
00450             lcd.line(lx136, wm456 , rx236, wm456, whisker);//middle
00451             lcd.line(lx136, wt456 , rx236, wb456, whisker);//top
00452             lcd.filled_circle(col3, row2 , rnose, nosecolor);//nose
00453             break;
00454             case 7:
00455             //POS_MUTEX.lock();
00456             pos1=1;
00457             pos2=0;
00458             pos3=1;
00459             pos4=0;
00460             pos5=0;
00461             pos6=0;
00462             //POS_MUTEX.unlock();
00463             pc.printf("%i should be 7 \n\r",state);
00464             lcd.filled_circle(col1, row1 , rmole, fill);// 1,3
00465             lcd.filled_circle(col1, row2 , rmole, fill);
00466             lcd.filled_circle(col2, row1 , rmole, fill);
00467             lcd.filled_circle(col2, row2 , rmole, fill);
00468             lcd.filled_circle(col3, row1 , rmole, fill);
00469             lcd.filled_circle(col3, row2 , rmole, fill);
00470             lcd.filled_circle(col1, row1 , rmole, mole);
00471             lcd.filled_circle(col3, row1 , rmole, mole);
00472             
00473             //face1
00474             lcd.line(lx114, eyey123 , lx214, eyey123, eyecolor);//left eye
00475             lcd.line(rx114, eyey123 , rx214, eyey123, eyecolor);//right eye
00476             lcd.line(lx114, wb123 , rx214, wt123, whisker);//bottom
00477             lcd.line(lx114, wm123 , rx214, wm123, whisker);//middle
00478             lcd.line(lx114, wt123 , rx214, wb123, whisker);//top
00479             lcd.filled_circle(col1, row1 , rnose, nosecolor);//nose
00480             //face3
00481             lcd.line(lx136, eyey123 , lx236, eyey123, eyecolor);//left eye
00482             lcd.line(rx136, eyey123 , rx236, eyey123, eyecolor);//right eye
00483             lcd.line(lx136, wb123 , rx236, wt123, whisker);//bottom
00484             lcd.line(lx136, wm123 , rx236, wm123, whisker);//middle
00485             lcd.line(lx136, wt123 , rx236, wb123, whisker);//top
00486             lcd.filled_circle(col3, row1 , rnose, nosecolor);//nose
00487             break;
00488             case 8:
00489             //POS_MUTEX.lock();
00490             pos1=0;
00491             pos2=1;
00492             pos3=0;
00493             pos4=1;
00494             pos5=0;
00495             pos6=1;
00496             //POS_MUTEX.unlock();
00497             pc.printf("%i should be 8 \n\r",state);
00498             
00499             lcd.filled_circle(col1, row1 , rmole, fill);// 2,4,6
00500             lcd.filled_circle(col1, row2 , rmole, fill);
00501             lcd.filled_circle(col2, row1 , rmole, fill);
00502             lcd.filled_circle(col2, row2 , rmole, fill);
00503             lcd.filled_circle(col3, row1 , rmole, fill);
00504             lcd.filled_circle(col3, row2 , rmole, fill);
00505             lcd.filled_circle(col2, row1 , rmole, mole);
00506             lcd.filled_circle(col1, row2 , rmole, mole);
00507             //face2
00508             lcd.line(lx125, eyey123 , lx225, eyey123, eyecolor);//left eye
00509             lcd.line(rx125, eyey123 , rx225, eyey123, eyecolor);//right eye
00510             lcd.line(lx125, wb123 , rx225, wt123, whisker);//bottom
00511             lcd.line(lx125, wm123 , rx225, wm123, whisker);//middle
00512             lcd.line(lx125, wt123 , rx225, wb123, whisker);//top
00513             lcd.filled_circle(col2, row1 , rnose, nosecolor);//nose
00514             //face4
00515             lcd.line(lx114, eyey456 , lx214, eyey456, eyecolor);//left eye
00516             lcd.line(rx114, eyey456 , rx214, eyey456, eyecolor);//right eye
00517             lcd.line(lx114, wb456 , rx214, wt456, whisker);//bottom
00518             lcd.line(lx114, wm456 , rx214, wm456, whisker);//middle
00519             lcd.line(lx114, wt456 , rx214, wb456, whisker);//top
00520             lcd.filled_circle(col1, row2 , rnose, nosecolor);//nose
00521             
00522             break;
00523             case 9:
00524             //POS_MUTEX.lock();
00525             pos1=0;
00526             pos2=0;
00527             pos3=1;
00528             pos4=1;
00529             pos5=0;
00530             pos6=1;
00531             //POS_MUTEX.unlock();
00532             pc.printf("%i should be 9 \n\r",state);
00533             lcd.filled_circle(col1, row1 , rmole, fill);// 3,4,6
00534             lcd.filled_circle(col1, row2 , rmole, fill);
00535             lcd.filled_circle(col2, row1 , rmole, fill);
00536             lcd.filled_circle(col2, row2 , rmole, fill);
00537             lcd.filled_circle(col3, row1 , rmole, fill);
00538             lcd.filled_circle(col3, row2 , rmole, fill);
00539             lcd.filled_circle(col3, row1 , rmole, mole);
00540             lcd.filled_circle(col1, row2 , rmole, mole);
00541             lcd.filled_circle(col3, row2 , rmole, mole);
00542             //face3
00543             lcd.line(lx136, eyey123 , lx236, eyey123, eyecolor);//left eye
00544             lcd.line(rx136, eyey123 , rx236, eyey123, eyecolor);//right eye
00545             lcd.line(lx136, wb123 , rx236, wt123, whisker);//bottom
00546             lcd.line(lx136, wm123 , rx236, wm123, whisker);//middle
00547             lcd.line(lx136, wt123 , rx236, wb123, whisker);//top
00548             lcd.filled_circle(col3, row1 , rnose, nosecolor);//nose
00549             //face4
00550             lcd.line(lx114, eyey456 , lx214, eyey456, eyecolor);//left eye
00551             lcd.line(rx114, eyey456 , rx214, eyey456, eyecolor);//right eye
00552             lcd.line(lx114, wb456 , rx214, wt456, whisker);//bottom
00553             lcd.line(lx114, wm456 , rx214, wm456, whisker);//middle
00554             lcd.line(lx114, wt456 , rx214, wb456, whisker);//top
00555             lcd.filled_circle(col1, row2 , rnose, nosecolor);//nose
00556             //face6
00557             lcd.line(lx136, eyey456 , lx236, eyey456, eyecolor);//left eye
00558             lcd.line(rx136, eyey456 , rx236, eyey456, eyecolor);//right eye
00559             lcd.line(lx136, wb456 , rx236, wt456, whisker);//bottom
00560             lcd.line(lx136, wm456 , rx236, wm456, whisker);//middle
00561             lcd.line(lx136, wt456 , rx236, wb456, whisker);//top
00562             lcd.filled_circle(col3, row2 , rnose, nosecolor);//nose
00563             break;
00564             case 10:
00565             //POS_MUTEX.lock();
00566             pos1=1;
00567             pos2=0;
00568             pos3=0;
00569             pos4=0;
00570             pos5=0;
00571             pos6=1;
00572             //POS_MUTEX.unlock();
00573             pc.printf("%i should be 10 \n\r",state);
00574             lcd.filled_circle(col1, row1 , rmole, fill);// 1,6
00575             lcd.filled_circle(col1, row2 , rmole, fill);
00576             lcd.filled_circle(col2, row1 , rmole, fill);
00577             lcd.filled_circle(col2, row2 , rmole, fill);
00578             lcd.filled_circle(col3, row1 , rmole, fill);
00579             lcd.filled_circle(col3, row2 , rmole, fill);
00580             lcd.filled_circle(col1, row1 , rmole, mole);
00581             lcd.filled_circle(col3, row2 , rmole, mole);
00582             //face1
00583             lcd.line(lx114, eyey123 , lx214, eyey123, eyecolor);//left eye
00584             lcd.line(rx114, eyey123 , rx214, eyey123, eyecolor);//right eye
00585             lcd.line(lx114, wb123 , rx214, wt123, whisker);//bottom
00586             lcd.line(lx114, wm123 , rx214, wm123, whisker);//middle
00587             lcd.line(lx114, wt123 , rx214, wb123, whisker);//top
00588             lcd.filled_circle(col1, row1 , rnose, nosecolor);//nose
00589             //face6
00590             lcd.line(lx136, eyey456 , lx236, eyey456, eyecolor);//left eye
00591             lcd.line(rx136, eyey456 , rx236, eyey456, eyecolor);//right eye
00592             lcd.line(lx136, wb456 , rx236, wt456, whisker);//bottom
00593             lcd.line(lx136, wm456 , rx236, wm456, whisker);//middle
00594             lcd.line(lx136, wt456 , rx236, wb456, whisker);//top
00595             lcd.filled_circle(col3, row2 , rnose, nosecolor);//nose
00596             break;
00597             case 11:
00598             //POS_MUTEX.lock();
00599             pos1=0;
00600             pos2=1;
00601             pos3=1;
00602             pos4=0;
00603             pos5=1;
00604             pos6=1;
00605             //POS_MUTEX.unlock();
00606             pc.printf("%i should be 11 \n\r",state);
00607             lcd.filled_circle(col1, row1 , rmole, fill);// 2,3,5,6
00608             lcd.filled_circle(col1, row2 , rmole, fill);
00609             lcd.filled_circle(col2, row1 , rmole, fill);
00610             lcd.filled_circle(col2, row2 , rmole, fill);
00611             lcd.filled_circle(col3, row1 , rmole, fill);
00612             lcd.filled_circle(col3, row2 , rmole, fill);
00613             lcd.filled_circle(col2, row1 , rmole, mole);
00614             lcd.filled_circle(col3, row1 , rmole, mole);
00615             lcd.filled_circle(col2, row2 , rmole, mole);
00616             lcd.filled_circle(col3, row2 , rmole, mole);
00617             //face2
00618             lcd.line(lx125, eyey123 , lx225, eyey123, eyecolor);//left eye
00619             lcd.line(rx125, eyey123 , rx225, eyey123, eyecolor);//right eye
00620             lcd.line(lx125, wb123 , rx225, wt123, whisker);//bottom
00621             lcd.line(lx125, wm123 , rx225, wm123, whisker);//middle
00622             lcd.line(lx125, wt123 , rx225, wb123, whisker);//top
00623             lcd.filled_circle(col2, row1 , rnose, nosecolor);//nose
00624             //face3
00625             lcd.line(lx136, eyey123 , lx236, eyey123, eyecolor);//left eye
00626             lcd.line(rx136, eyey123 , rx236, eyey123, eyecolor);//right eye
00627             lcd.line(lx136, wb123 , rx236, wt123, whisker);//bottom
00628             lcd.line(lx136, wm123 , rx236, wm123, whisker);//middle
00629             lcd.line(lx136, wt123 , rx236, wb123, whisker);//top
00630             lcd.filled_circle(col3, row1 , rnose, nosecolor);//nose
00631             //face5
00632             lcd.line(lx125, eyey456 , lx225, eyey456, eyecolor);//left eye
00633             lcd.line(rx125, eyey456 , rx225, eyey456, eyecolor);//right eye
00634             lcd.line(lx125, wb456 , rx225, wt456, whisker);//bottom
00635             lcd.line(lx125, wm456 , rx225, wm456, whisker);//middle
00636             lcd.line(lx125, wt456 , rx225, wb456, whisker);//top
00637             lcd.filled_circle(col2, row2 , rnose, nosecolor);//nose
00638             //face6
00639              lcd.line(lx136, eyey456 , lx236, eyey456, eyecolor);//left eye
00640             lcd.line(rx136, eyey456 , rx236, eyey456, eyecolor);//right eye
00641             lcd.line(lx136, wb456 , rx236, wt456, whisker);//bottom
00642             lcd.line(lx136, wm456 , rx236, wm456, whisker);//middle
00643             lcd.line(lx136, wt456 , rx236, wb456, whisker);//top
00644             lcd.filled_circle(col3, row2 , rnose, nosecolor);//nose
00645             
00646             break;
00647             case 12:
00648             //POS_MUTEX.lock();
00649             pos1=0;
00650             pos2=0;
00651             pos3=1;
00652             pos4=0;
00653             pos5=1;
00654             pos6=0;
00655             //POS_MUTEX.unlock();
00656             pc.printf("%i should be 12 \n\r",state);
00657             lcd.filled_circle(col1, row1 , rmole, fill);// 3,5
00658             lcd.filled_circle(col1, row2 , rmole, fill);
00659             lcd.filled_circle(col2, row1 , rmole, fill);
00660             lcd.filled_circle(col2, row2 , rmole, fill);
00661             lcd.filled_circle(col3, row1 , rmole, fill);
00662             lcd.filled_circle(col3, row2 , rmole, fill);
00663             lcd.filled_circle(col3, row1 , rmole, mole);
00664             lcd.filled_circle(col2, row2 , rmole, mole);
00665             
00666             //face3
00667             lcd.line(lx136, eyey123 , lx236, eyey123, eyecolor);//left eye
00668             lcd.line(rx136, eyey123 , rx236, eyey123, eyecolor);//right eye
00669             lcd.line(lx136, wb123 , rx236, wt123, whisker);//bottom
00670             lcd.line(lx136, wm123 , rx236, wm123, whisker);//middle
00671             lcd.line(lx136, wt123 , rx236, wb123, whisker);//top
00672             lcd.filled_circle(col3, row1 , rnose, nosecolor);//nose
00673             //face5
00674             lcd.line(lx125, eyey456 , lx225, eyey456, eyecolor);//left eye
00675             lcd.line(rx125, eyey456 , rx225, eyey456, eyecolor);//right eye
00676             lcd.line(lx125, wb456 , rx225, wt456, whisker);//bottom
00677             lcd.line(lx125, wm456 , rx225, wm456, whisker);//middle
00678             lcd.line(lx125, wt456 , rx225, wb456, whisker);//top
00679             lcd.filled_circle(col2, row2 , rnose, nosecolor);//nose
00680             break;
00681             case 13:
00682             //POS_MUTEX.lock();
00683             pos1=1;
00684             pos2=0;
00685             pos3=0;
00686             pos4=1;
00687             pos5=1;
00688             pos6=0;
00689             //POS_MUTEX.unlock();
00690             pc.printf("%i should be 13 \n\r",state);
00691             lcd.filled_circle(col1, row1 , rmole, fill);// 1,4,5
00692             lcd.filled_circle(col1, row2 , rmole, fill);
00693             lcd.filled_circle(col2, row1 , rmole, fill);
00694             lcd.filled_circle(col2, row2 , rmole, fill);
00695             lcd.filled_circle(col3, row1 , rmole, fill);
00696             lcd.filled_circle(col3, row2 , rmole, fill);
00697             lcd.filled_circle(col1, row1 , rmole, mole);
00698             lcd.filled_circle(col1, row2 , rmole, mole);
00699             lcd.filled_circle(col2, row2 , rmole, mole);
00700             //face1
00701             lcd.line(lx114, eyey123 , lx214, eyey123, eyecolor);//left eye
00702             lcd.line(rx114, eyey123 , rx214, eyey123, eyecolor);//right eye
00703             lcd.line(lx114, wb123 , rx214, wt123, whisker);//bottom
00704             lcd.line(lx114, wm123 , rx214, wm123, whisker);//middle
00705             lcd.line(lx114, wt123 , rx214, wb123, whisker);//top
00706             lcd.filled_circle(col1, row1 , rnose, nosecolor);//nose
00707 
00708             //face4
00709              lcd.line(lx114, eyey456 , lx214, eyey456, eyecolor);//left eye
00710             lcd.line(rx114, eyey456 , rx214, eyey456, eyecolor);//right eye
00711             lcd.line(lx114, wb456 , rx214, wt456, whisker);//bottom
00712             lcd.line(lx114, wm456 , rx214, wm456, whisker);//middle
00713             lcd.line(lx114, wt456 , rx214, wb456, whisker);//top
00714             lcd.filled_circle(col1, row2 , rnose, nosecolor);//nose
00715             //face5
00716             lcd.line(lx125, eyey456 , lx225, eyey456, eyecolor);//left eye
00717             lcd.line(rx125, eyey456 , rx225, eyey456, eyecolor);//right eye
00718             lcd.line(lx125, wb456 , rx225, wt456, whisker);//bottom
00719             lcd.line(lx125, wm456 , rx225, wm456, whisker);//middle
00720             lcd.line(lx125, wt456 , rx225, wb456, whisker);//top
00721             lcd.filled_circle(col2, row2 , rnose, nosecolor);//nose
00722             break;
00723             case 14:
00724             //POS_MUTEX.lock();
00725             pos1=0;
00726             pos2=1;
00727             pos3=0;
00728             pos4=0;
00729             pos5=1;
00730             pos6=0;
00731             //POS_MUTEX.unlock();
00732             pc.printf("%i should be 14 \n\r",state);
00733             
00734             lcd.filled_circle(col1, row1 , rmole, fill);// 2,5
00735             lcd.filled_circle(col1, row2 , rmole, fill);
00736             lcd.filled_circle(col2, row1 , rmole, fill);
00737             lcd.filled_circle(col2, row2 , rmole, fill);
00738             lcd.filled_circle(col3, row1 , rmole, fill);
00739             lcd.filled_circle(col3, row2 , rmole, fill);
00740             lcd.filled_circle(col2, row1 , rmole, mole);
00741             lcd.filled_circle(col2, row2 , rmole, mole);
00742             
00743             //face2
00744             lcd.line(lx125, eyey123 , lx225, eyey123, eyecolor);//left eye
00745             lcd.line(rx125, eyey123 , rx225, eyey123, eyecolor);//right eye
00746             lcd.line(lx125, wb123 , rx225, wt123, whisker);//bottom
00747             lcd.line(lx125, wm123 , rx225, wm123, whisker);//middle
00748             lcd.line(lx125, wt123 , rx225, wb123, whisker);//top
00749             lcd.filled_circle(col2, row1 , rnose, nosecolor);//nose
00750             //face5
00751             lcd.line(lx125, eyey456 , lx225, eyey456, eyecolor);//left eye
00752             lcd.line(rx125, eyey456 , rx225, eyey456, eyecolor);//right eye
00753             lcd.line(lx125, wb456 , rx225, wt456, whisker);//bottom
00754             lcd.line(lx125, wm456 , rx225, wm456, whisker);//middle
00755             lcd.line(lx125, wt456 , rx225, wb456, whisker);//top
00756             lcd.filled_circle(col2, row2 , rnose, nosecolor);//nose
00757             break;
00758             }
00759         wait(0.5);
00760         counter++; // increment counter and run the "state machine again"
00761         lcd.text_string("Score: ", 1, 15, FONT_7X8, GREEN);
00762         lcd.printf("%d",score);
00763         }
00764         
00765     // clear the lcd screen
00766     lcd.cls();
00767     wait(0.5);
00768     lcd.cls();
00769     // int variable to read current high score
00770     int please;
00771     mkdir("/sd/mydir", 0777);
00772     FILE *fp1 = fopen("/sd/mydir/sdtest.txt", "r"); 
00773     if(fp1 == NULL) 
00774     { 
00775     pc.printf("Could not open file for read\n"); 
00776     }
00777     pc.printf("Opened file for read\n\r");
00778     fscanf (fp1, "%s", str);
00779     sscanf(str, "%d", &please);
00780     fclose(fp1);
00781     pc.printf (":   %d \n",please);
00782     if (score > please)
00783     {
00784         FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
00785         if(fp == NULL) {
00786         error("Could not open file for write\n");
00787     }
00788     char buf[10];
00789     sprintf(buf, "%d", score);
00790     fprintf(fp, "%s",buf);
00791     fclose(fp); 
00792         lcd.printf("CONGRATULATIONS!\n\n");
00793         lcd.printf("You set the new\n\n");
00794         lcd.printf("High score!!!!!!\n\n");
00795         lcd.printf("New high score: %d\n\n",score);
00796         lcd.printf("Press reset to\n\n");
00797         lcd.printf("Play Again!");
00798     }
00799     else
00800     {
00801         lcd.printf("High score: %d\n\n",please);
00802         lcd.printf("Your score: %d\n\n",score);
00803         lcd.printf("You did not set\n\n");
00804         lcd.printf("A new high score\n\n");
00805         lcd.printf("Press reset to\n\n");
00806         lcd.printf("Try again");
00807     }
00808 
00809 pc.printf("Goodbye World!\n");
00810 }//end
00811 
00812 
00813 
00814