Various applications using the LCD. Cycle through the applications by pressing down on the joystick

Dependencies:   C12832 MMA7660 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "C12832.h"
00003 #include "MMA7660.h"
00004 
00005 BusIn joy(p15,p12,p13,p16);
00006 DigitalIn fire(p14);
00007 C12832 lcd(p5, p7, p6, p8, p11);
00008 AnalogIn pot1(p19);
00009 AnalogIn pot2(p20);
00010 DigitalIn joyUp(p15); 
00011 DigitalIn joyDown(p12);
00012 DigitalIn joyLeft(p13);
00013 DigitalIn joyRight(p16);
00014 MMA7660 MMA(p28, p27); //I2C Accelerometer
00015 Serial pc(USBTX, USBRX); // tx, rx
00016 
00017 BusOut leds(LED1,LED2,LED3,LED4);
00018 int appIndex = 0;
00019 const int NUM_APPS = 4;
00020 bool appSwitch = true;
00021 enum SnakeDirection{RIGHT,LEFT,UP,DOWN};
00022 const int SCREEN_WIDTH = 128;
00023 const int SCREEN_HEIGHT = 32;
00024 struct Point {int x; int y;};
00025 /*
00026 Index   App
00027 0       Etch a sketch
00028 1       Flat Level
00029 2       Snake
00030 3       ?
00031 */
00032 
00033 int main()
00034 {
00035     //potentiometer variables
00036     float etchX = 0;
00037     float etchY = 0;
00038     //accelerometer variables
00039     int x=0,y=0;
00040     //snake variables
00041     const int SNAKE_MAX = 256;
00042     Point snake[SNAKE_MAX];
00043     int snakeHead = 4;//the front filled square in the snake
00044     int newHead = 0;
00045     int snakeTail = 0;
00046     SnakeDirection snakeGo = RIGHT;
00047     bool gameOver = false;
00048     int points = 0;
00049     Point goalLoc;
00050     
00051     lcd.cls();
00052     lcd.printf("hello world");
00053     
00054     while(1) {
00055         if (fire) {
00056             appIndex = (appIndex+1)%NUM_APPS;
00057             leds=0xf;
00058             appSwitch = true;
00059         }
00060         else {
00061             leds=joy;
00062         }
00063         
00064         switch(appIndex) {
00065             case 0: {
00066                 if(appSwitch){
00067                     lcd.cls();
00068                     lcd.locate(0,0);
00069                     lcd.printf("Etch-a-Sketch");
00070                     wait(2);
00071                     lcd.cls();
00072                     lcd.locate(0,0);
00073                     appSwitch=false;
00074                 }
00075                 //read potentiometers in scaled volatge
00076                 etchY = 32-pot1.read()*32;
00077                 etchX = 128-pot2.read()*128;
00078                 lcd.pixel((int)etchX, (int)etchY, 1);
00079                 lcd.copy_to_lcd();
00080                 x = MMA.x();
00081                 y = MMA.y();
00082                 if(x > .9 || x < -.9 || y > .9 || y < -.9){
00083                     lcd.cls();
00084                 }
00085                 break;}
00086             case 1: {
00087                 if(appSwitch){
00088                     lcd.cls();
00089                     lcd.locate(0,0);
00090                     lcd.printf("Flat Level");
00091                     wait(2);
00092                     lcd.cls();
00093                     lcd.locate(0,0);
00094                     appSwitch=false;
00095                 }
00096                 //read X,Y +/-Gs and scale for #display pixels
00097                 x = (x + MMA.x() * 32.0)/2.0;
00098                 y = (y -(MMA.y() * 16.0))/2.0;
00099                 lcd.fillcircle(x+63, y+15, 3, 1); //draw bubble
00100                 lcd.line(0, 11, 127, 11, 1);
00101                 lcd.line(0, 20, 127, 20, 1);
00102                 lcd.line(59, 0, 59, 31, 1);
00103                 lcd.line(68, 0, 68, 31, 1);
00104                 //lcd.circle(63, 15, 8, 1);
00105                 wait(.2); //time delay
00106                 lcd.fillcircle(x+63, y+15, 3, 0); //erase bubble
00107                 break;}
00108             case 2: {//snake
00109                 if(appSwitch || gameOver){
00110                     lcd.cls();
00111                     lcd.locate(0,0);
00112                     lcd.printf("Snake");
00113                     
00114                     //setup new snake
00115                     snakeTail = 0;
00116                     snakeHead = 4;
00117                     snakeGo = RIGHT;
00118                     snake[0].x = 0;
00119                     snake[0].y = 14;
00120                     snake[1].x = 2;
00121                     snake[1].y = 14;
00122                     snake[2].x = 4;
00123                     snake[2].y = 14;
00124                     snake[3].x = 6;
00125                     snake[3].y = 14;
00126                     snake[4].x = 8;
00127                     snake[4].y = 14;
00128                     gameOver = false;
00129                     points = 0;
00130                     float seed = pot1.read()*pot2.read()*100000;
00131                     srand((int)seed);
00132                     goalLoc.x = (rand()%64)*2;
00133                     goalLoc.y = (rand()%16)*2;
00134                     
00135                     wait(2);
00136                     lcd.cls();
00137                     lcd.locate(0,0);
00138                     appSwitch=false;
00139                     
00140                     //draw initial snake
00141                     for(int i = 0; i<=snakeHead; i++){
00142                         lcd.fillrect(snake[i].x, snake[i].y, snake[i].x+1, snake[i].y+1, 1);
00143                     }
00144                     //draw initial goal location
00145                     lcd.fillrect(goalLoc.x, goalLoc.y, goalLoc.x+1, goalLoc.y+1, 1);
00146                     lcd.copy_to_lcd();
00147                     
00148                     //wait for user to see snake before moving
00149                     wait(1);
00150                 }
00151                 
00152                 //read joystick to get new direction
00153                 if(joyUp && DOWN != snakeGo)
00154                 {snakeGo = UP;}
00155                 if(joyDown && UP != snakeGo)
00156                 {snakeGo = DOWN;}
00157                 if(joyLeft && RIGHT != snakeGo)
00158                 {snakeGo = LEFT;}
00159                 if(joyRight && LEFT != snakeGo)
00160                 {snakeGo = RIGHT;}
00161                 
00162                 //move snake
00163                 switch(snakeGo){
00164                     case RIGHT:
00165                         //clear tail
00166                         lcd.fillrect(snake[snakeTail].x, snake[snakeTail].y, snake[snakeTail].x+1, snake[snakeTail].y+1, 0);
00167                         snakeTail = (snakeTail+1)%SNAKE_MAX;
00168                         //draw new front
00169                         newHead = (snakeHead+1)%SNAKE_MAX;
00170                         snake[newHead].x = snake[snakeHead].x + 2;
00171                         snake[newHead].y = snake[snakeHead].y;
00172                         snakeHead = newHead;
00173                         lcd.fillrect(snake[snakeHead].x, snake[snakeHead].y, snake[snakeHead].x+1, snake[snakeHead].y+1, 1);
00174                         //copy to lcd
00175                         lcd.copy_to_lcd();
00176                         //check if we went off the screen
00177                         if(snake[snakeHead].x >= SCREEN_WIDTH){
00178                             gameOver = true;
00179                             pc.printf("Off screen to right\r\n");
00180                             pc.printf("snakeHead:%d x=%d, y=%d",snakeHead,snake[snakeHead].x,snake[snakeHead].y);
00181                         }
00182                         break;
00183                     case LEFT:
00184                         //clear tail
00185                         lcd.fillrect(snake[snakeTail].x, snake[snakeTail].y, snake[snakeTail].x+1, snake[snakeTail].y+1, 0);
00186                         snakeTail = (snakeTail+1)%SNAKE_MAX;
00187                         //draw new front
00188                         newHead = (snakeHead+1)%SNAKE_MAX;
00189                         snake[newHead].x = snake[snakeHead].x - 2;
00190                         snake[newHead].y = snake[snakeHead].y;
00191                         snakeHead = newHead;
00192                         lcd.fillrect(snake[snakeHead].x, snake[snakeHead].y, snake[snakeHead].x+1, snake[snakeHead].y+1, 1);
00193                         //copy to lcd
00194                         lcd.copy_to_lcd();
00195                         //check if we went off the screen
00196                         if(snake[snakeHead].x < 0){
00197                             gameOver = true;
00198                             pc.printf("Off screen to left\r\n");
00199                             pc.printf("snakeHead:%d x=%d, y=%d",snakeHead,snake[snakeHead].x,snake[snakeHead].y);
00200                         }
00201                         break;
00202                     case UP:
00203                         //clear tail
00204                         lcd.fillrect(snake[snakeTail].x, snake[snakeTail].y, snake[snakeTail].x+1, snake[snakeTail].y+1, 0);
00205                         snakeTail = (snakeTail+1)%SNAKE_MAX;
00206                         //draw new front
00207                         newHead = (snakeHead+1)%SNAKE_MAX;
00208                         snake[newHead].x = snake[snakeHead].x;
00209                         snake[newHead].y = snake[snakeHead].y - 2;
00210                         snakeHead = newHead;
00211                         lcd.fillrect(snake[snakeHead].x, snake[snakeHead].y, snake[snakeHead].x+1, snake[snakeHead].y+1, 1);
00212                         //copy to lcd
00213                         lcd.copy_to_lcd();
00214                         //check if we went off the screen
00215                         if(snake[snakeHead].y < 0){
00216                             gameOver = true;
00217                             pc.printf("Off screen top\r\n");
00218                             pc.printf("snakeHead:%d x=%d, y=%d",snakeHead,snake[snakeHead].x,snake[snakeHead].y);
00219                         }
00220                         //wait?
00221                         break;
00222                     case DOWN:
00223                         //clear tail
00224                         lcd.fillrect(snake[snakeTail].x, snake[snakeTail].y, snake[snakeTail].x+1, snake[snakeTail].y+1, 0);
00225                         snakeTail = (snakeTail+1)%SNAKE_MAX;
00226                         //draw new front
00227                         newHead = (snakeHead+1)%SNAKE_MAX;
00228                         snake[newHead].x = snake[snakeHead].x;
00229                         snake[newHead].y = snake[snakeHead].y + 2;
00230                         snakeHead = newHead;
00231                         lcd.fillrect(snake[snakeHead].x, snake[snakeHead].y, snake[snakeHead].x+1, snake[snakeHead].y+1, 1);
00232                         //copy to lcd
00233                         lcd.copy_to_lcd();
00234                         //check if we went off the screen
00235                         if(snake[snakeHead].y >= SCREEN_HEIGHT){
00236                             gameOver = true;
00237                             pc.printf("Off screen bottom\r\n");
00238                             pc.printf("snakeHead:%d x=%d, y=%d",snakeHead,snake[snakeHead].x,snake[snakeHead].y);
00239                         }
00240                         //wait?
00241                         break;
00242                 }
00243                 //check if we scored a point
00244                 if(snake[snakeHead].x == goalLoc.x && snake[snakeHead].y == goalLoc.y){
00245                     points++;
00246                     //clear old goal and set new goal
00247                     lcd.fillrect(goalLoc.x, goalLoc.y, goalLoc.x+1, goalLoc.y+1, 0);
00248                     goalLoc.x = (rand()%64)*2;
00249                     goalLoc.y = (rand()%16)*2;
00250                     lcd.fillrect(goalLoc.x, goalLoc.y, goalLoc.x+1, goalLoc.y+1, 1);
00251                     
00252                     //add on to the tail
00253                     int newTail = (snakeTail-1+SNAKE_MAX)%SNAKE_MAX;
00254                     snake[newTail].x = snake[snakeTail].x;
00255                     snake[newTail].y = snake[snakeTail].y;
00256                     snakeTail = newTail;
00257                     
00258                     //copy everything to the lcd
00259                     lcd.copy_to_lcd();
00260                     if(points > SNAKE_MAX-5)
00261                     {
00262                         gameOver = true;
00263                         pc.printf("Game Winner!\r\n");
00264                     }
00265                 }
00266                 //check if we hit ourself
00267                 int testLimit = snakeHead;
00268                 if(snakeTail > snakeHead){
00269                     testLimit = snakeHead+SNAKE_MAX;
00270                 }
00271                 for(int i = snakeTail; i < testLimit; i++){
00272                     int iMod = i%SNAKE_MAX;
00273                     if(snake[snakeHead].x == snake[iMod].x && snake[snakeHead].y == snake[iMod].y){
00274                         gameOver = true;
00275                         pc.printf("Hit Self\r\n");
00276                         pc.printf("snakeHead:%d x=%d, y=%d",snakeHead,snake[snakeHead].x,snake[snakeHead].y);
00277                     }
00278                 }
00279                 
00280                 if(gameOver){
00281                     //lcd.cls();
00282                     lcd.locate(0,0);
00283                     lcd.printf("Game Over, Press Joystick to start over\nPoints Scored: %d",points);
00284                     pc.printf("Game Over\r\n");
00285                     while(!fire){
00286                         //infinite loop to wait for user to press joystick to restart
00287                     }
00288                     wait(.5);
00289                 }
00290                 
00291                 break;}
00292             case 3: {
00293                 if(appSwitch){
00294                     lcd.cls();
00295                     lcd.locate(0,0);
00296                     lcd.printf("Horizontal Level");
00297                     wait(2);
00298                     lcd.cls();
00299                     lcd.locate(0,0);
00300                     appSwitch=false;
00301                 }
00302                 break;}
00303             default: {
00304                 lcd.cls();
00305                 lcd.locate(0,0);
00306                 lcd.printf("Error: appIndex out of range - appIndes=%d",appIndex);
00307                 break;}
00308         }
00309         
00310         
00311         wait(0.1);
00312     }
00313 }