FRDM-KL25Z with Nokia 3110 type LCD display Using accelerometer to make movements

Dependencies:   MMA8451Q N3310LCD mbed

Fork of FRDM_MMA8451Q by mbed official

Revision:
9:1afbf9010118
Parent:
8:857444086b3f
Child:
10:f3e93cc092d7
--- a/main.cpp	Fri Mar 15 22:48:58 2013 +0000
+++ b/main.cpp	Mon Mar 18 22:41:09 2013 +0000
@@ -15,13 +15,13 @@
 #define MENU_X    10        // 0-83
 #define MENU_Y    0        // 0-5
 
-#define MENU_ITEMS 3
+#define MENU_ITEMS 4
 
 int8_t blflag = 1;  // Backlight initially ON
 
-
 void about(N3310LCD* lcd, Joystick* jstick);
 void draw(N3310LCD* lcd, Joystick* jstick);
+void snakeGame(N3310LCD* lcd, Joystick* jstick);
 void backlight(N3310LCD* lcd, Joystick* jstick);
 void waitforOKKey(N3310LCD* lcd, Joystick* jstick);
 uint8_t checkKeypressed( Joystick* jstick);
@@ -29,12 +29,14 @@
 // menu definition
 char menu_items[MENU_ITEMS][12] = {
     "SKETCH",
+    "SNAKE",
     "BACKLIGHT",
     "ABOUT"
 };
 
 void (*menu_funcs[MENU_ITEMS])(N3310LCD*,Joystick*) = {
     draw,
+    snakeGame,
     backlight,
     about
 };
@@ -58,7 +60,6 @@
         lcd->writeString( 60, 2, "On", HIGHLIGHT);
     }
 
-    int8_t i;
     bool exitFlag = false;
 
     while( !exitFlag ) {
@@ -91,7 +92,6 @@
 {
     MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, false, 4);
     lcd->cls();
-//    lcd->writeString(10, 2, "La", NORMAL);
 
     // Draw a rectangle
     lcd->drawRectangle(0,0,83,47, PIXEL_ON);
@@ -103,6 +103,171 @@
 
     lcd->setPixel( px, py, PIXEL_ON );
 
+    float x, y;
+    float cValues[3] = { 0.0, 0.0, 0.0 };
+    int16_t xI, yI;
+    int16_t xInc, yInc;
+
+    // Take 100 readings to get stable centre point
+
+    for( int i = 0; i < 100; i++ ) {
+        float axis[2] = { 0.0, 0.0 };
+        acc.getAccAllAxis( axis );
+        cValues[0] += axis[0];
+        cValues[1] += axis[1];
+    }
+
+    cValues[0] /= 100.0;
+    cValues[1] /= 100.0;
+
+    // Exit on joystick pressed
+    bool exitFlag = false;
+    while ( !exitFlag ) {
+        uint8_t keyPress = checkKeypressed( jstick );
+        if( keyPress == CENTER_KEY )
+            exitFlag = true;
+
+        x = (acc.getAccX() - cValues[0]) * 100.0;
+        y = (acc.getAccY() - cValues[1]) * 100.0;
+
+        xI = (int16_t)(x);
+        yI = (int16_t)(y);
+        xInc = xI/10;
+        yInc = ((yI/10) * -1);
+
+        nx += xInc;
+        ny += yInc;
+
+        nx = MAX( 1, MIN( nx, 82 ) );
+        ny = MAX( 1, MIN( ny, 46 ) );
+
+        if( abs(xInc) > 1 || abs(yInc) > 1 ) {
+            // Draw line
+            lcd->drawLine((uint8_t)px, (uint8_t)py, (uint8_t)nx, (uint8_t)ny, PIXEL_ON);
+        } else {
+            // Plot pixel
+            lcd->setPixel( (uint8_t)nx, (uint8_t)ny, PIXEL_ON );
+        }
+
+        px = nx;
+        py = ny;
+
+        wait(0.1);
+    }
+}
+
+#define MAX_SNAKE_LEN 400
+#define SNAKE_X 0
+#define SNAKE_Y 1
+#define DIR_N 1
+#define DIR_E 2
+#define DIR_S 3
+#define DIR_W 4
+
+int16_t snakeLen = 10;
+
+int8_t snake[MAX_SNAKE_LEN][2];       // Snake positions, use -1 for no point
+
+void initSnake( void )
+{
+    for( int i=0; i< MAX_SNAKE_LEN; i++ ) {
+        snake[i][SNAKE_X] = -1;
+        snake[i][SNAKE_Y] = -1;
+    }
+
+    // Add initial snake points
+    for(int i=0; i<snakeLen; i++ ) {
+        snake[i][SNAKE_X] = 42 + i;
+        snake[i][SNAKE_Y] = 24;
+    }
+}
+
+void drawSnake( N3310LCD* lcd )
+{
+    for(int i=0; i<snakeLen; i++ ) {
+        lcd->setPixel( snake[i][SNAKE_X], snake[i][SNAKE_Y], PIXEL_ON );
+    }
+}
+
+void growSnake( N3310LCD* lcd, int len )
+{
+
+}
+
+
+void moveSnake( N3310LCD* lcd, uint8_t direction )
+{
+    int8_t oX = snake[0][SNAKE_X];
+    int8_t oY = snake[0][SNAKE_Y];
+    switch( direction ) {
+        case DIR_N:
+            snake[0][SNAKE_Y]--;
+            break;
+        case DIR_E:
+            snake[0][SNAKE_X]++;
+            break;
+        case DIR_S:
+            snake[0][SNAKE_Y]++;
+            break;
+        case DIR_W:
+            snake[0][SNAKE_X]--;
+            break;
+    }
+
+    int8_t nX = -1;
+    int8_t nY = -1;
+    for(int i=1; i<snakeLen; i++ ) {
+        nX = oX;
+        nY = oY;
+        oX = snake[i][SNAKE_X];
+        oY = snake[i][SNAKE_Y];
+        snake[i][SNAKE_X] = nX;
+        snake[i][SNAKE_Y] = nY;        
+    }
+
+    drawSnake(lcd);
+    lcd->setPixel( oX, oY, PIXEL_OFF );
+}
+
+
+
+void snakeGame(N3310LCD* lcd, Joystick* jstick)
+{
+    MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, false, 4);
+
+    initSnake();
+
+    lcd->cls();
+
+    // Draw a rectangle
+    lcd->drawRectangle(0,0,83,47, PIXEL_ON);
+
+    drawSnake( lcd );
+/*
+    for( int i=0; i<10; i++ ) {
+        moveSnake(lcd, DIR_N);
+        wait(0.2);
+    }
+    for( int i=0; i<20; i++ ) {
+        moveSnake(lcd, DIR_E);
+        wait(0.2);
+    }
+    for( int i=0; i<20; i++ ) {
+        moveSnake(lcd, DIR_S);
+        wait(0.2);
+    }
+    for( int i=0; i<20; i++ ) {
+        moveSnake(lcd, DIR_W);
+        wait(0.2);
+    }
+
+    int8_t px = 42;
+    int8_t py = 25;
+    int8_t nx = px;
+    int8_t ny = py;
+*/
+//    lcd->setPixel( px, py, PIXEL_ON );
+
     float x, y; //, z;
     float cValues[3] = { 0.0, 0.0, 0.0 };
     int16_t xI, yI; //, zI;
@@ -125,6 +290,8 @@
 
     // Exit on joystick pressed
     bool exitFlag = false;
+    uint8_t snakeDirection = DIR_W;
+    
     while ( !exitFlag ) {
         uint8_t keyPress = checkKeypressed( jstick );
         if( keyPress == CENTER_KEY )
@@ -141,6 +308,19 @@
         xInc = xI/10;
         yInc = ((yI/10) * -1);
 
+        
+        if( xInc > 2 )
+            snakeDirection = DIR_E;
+        else if( xInc < -2 )
+            snakeDirection = DIR_W;
+        else if( yInc > 2 )
+            snakeDirection = DIR_S;
+        else if( yInc < -2 )
+            snakeDirection = DIR_N;
+        moveSnake(lcd, snakeDirection);
+        wait(0.2);
+
+/*
         nx += xInc;
         ny += yInc;
 
@@ -160,14 +340,13 @@
 
 //        pc.printf( "X: %d Y: %d Z: %d\n", xI, yI, zI);
         wait(0.1);
+        */
     }
 
-
 }
 
 
 
-
 void initMenu(N3310LCD* lcd)
 {
     lcd->writeString(MENU_X, MENU_Y, menu_items[0], HIGHLIGHT );