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

Dependencies:   MMA8451Q N3310LCD mbed

Fork of FRDM_MMA8451Q by mbed official

Files at this revision

API Documentation at this revision

Comitter:
SomeRandomBloke
Date:
Thu Mar 21 00:09:07 2013 +0000
Parent:
9:1afbf9010118
Child:
11:90d35ac294af
Commit message:
update

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Mar 18 22:41:09 2013 +0000
+++ b/main.cpp	Thu Mar 21 00:09:07 2013 +0000
@@ -91,8 +91,24 @@
 void draw(N3310LCD* lcd, Joystick* jstick)
 {
     MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, false, 4);
+    float x, y;
+    float cValues[3] = { 0.0, 0.0, 0.0 };
+    int16_t xI, yI;
+    int16_t xInc, yInc;
+
     lcd->cls();
 
+    // Take 100 readings to get stable centre point
+    for( int i = 0; i < 100; i++ ) {
+        float axis[3] = { 0.0, 0.0, 0.0 };
+        acc.getAccAllAxis( axis );
+        cValues[0] += axis[0];
+        cValues[1] += axis[1];
+    }
+
+    cValues[0] /= 100.0;
+    cValues[1] /= 100.0;
+
     // Draw a rectangle
     lcd->drawRectangle(0,0,83,47, PIXEL_ON);
 
@@ -103,22 +119,7 @@
 
     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;
@@ -140,7 +141,10 @@
 
         nx = MAX( 1, MIN( nx, 82 ) );
         ny = MAX( 1, MIN( ny, 46 ) );
+pc.printf( "X: %d Y: %d\n", nx, ny );
 
+
+// Seem to have broken this
         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);
@@ -189,13 +193,8 @@
     }
 }
 
-void growSnake( N3310LCD* lcd, int len )
-{
 
-}
-
-
-void moveSnake( N3310LCD* lcd, uint8_t direction )
+void moveSnake( N3310LCD* lcd, uint8_t direction, uint8_t growLength )
 {
     int8_t oX = snake[0][SNAKE_X];
     int8_t oY = snake[0][SNAKE_Y];
@@ -226,48 +225,28 @@
     }
 
     drawSnake(lcd);
-    lcd->setPixel( oX, oY, PIXEL_OFF );
+    if( growLength > 0 && snakeLen < MAX_SNAKE_LEN ) {
+        // Dont clear tail, add point to tail
+        snake[snakeLen][SNAKE_X] = oX;
+        snake[snakeLen][SNAKE_Y] = oY;
+        snakeLen++;
+    } else {
+        // Clear tail
+        lcd->setPixel( oX, oY, PIXEL_OFF );
+    }
+    
 }
 
+bool checkCollision() {
+bool collisionFlag = false;
+
+    return collisionFlag;
+}
 
 
 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;
@@ -288,9 +267,44 @@
 //    cValues[2] /= 100.0;
 //    pc.printf( "Steady State X: %f Y: %f Z: %f\n", cValues[0], cValues[1], cValues[2] );
 
+    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, 1);
+        wait(0.2);
+    }
+    for( int i=0; i<20; i++ ) {
+        moveSnake(lcd, DIR_E, 0);
+        wait(0.2);
+    }
+    for( int i=0; i<20; i++ ) {
+        moveSnake(lcd, DIR_S, 1);
+        wait(0.2);
+    }
+    for( int i=0; i<20; i++ ) {
+        moveSnake(lcd, DIR_W, 1);
+        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 );
+
+
     // Exit on joystick pressed
     bool exitFlag = false;
     uint8_t snakeDirection = DIR_W;
+    uint8_t snakeGrowth = 0;
     
     while ( !exitFlag ) {
         uint8_t keyPress = checkKeypressed( jstick );
@@ -307,7 +321,6 @@
 //        zI = (int16_t)(z);
         xInc = xI/10;
         yInc = ((yI/10) * -1);
-
         
         if( xInc > 2 )
             snakeDirection = DIR_E;
@@ -317,7 +330,13 @@
             snakeDirection = DIR_S;
         else if( yInc < -2 )
             snakeDirection = DIR_N;
-        moveSnake(lcd, snakeDirection);
+        moveSnake(lcd, snakeDirection, snakeGrowth);
+        // TODO Check if we've collided with anything
+        if( checkCollision() ) {
+            // Collision detected!!
+        }
+        
+        if( snakeGrowth > 0 ) snakeGrowth--;
         wait(0.2);
 
 /*