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

Dependencies:   MMA8451Q N3310LCD mbed

Fork of FRDM_MMA8451Q by mbed official

Committer:
SomeRandomBloke
Date:
Thu Mar 21 21:53:58 2013 +0000
Revision:
11:90d35ac294af
Parent:
10:f3e93cc092d7
Child:
12:dbd6c9c366ac
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris 2:41db78380a6e 1 #include "mbed.h"
chris 2:41db78380a6e 2 #include "MMA8451Q.h"
SomeRandomBloke 8:857444086b3f 3 #include "N3310SPIConfig.h"
SomeRandomBloke 8:857444086b3f 4 #include "N3310LCD.h"
SomeRandomBloke 8:857444086b3f 5 #include "Joystick.h"
SomeRandomBloke 8:857444086b3f 6 #include "splash.h"
chris 2:41db78380a6e 7
chris 2:41db78380a6e 8 #define MMA8451_I2C_ADDRESS (0x1d<<1)
SomeRandomBloke 8:857444086b3f 9 Serial pc(USBTX,USBRX);
chris 2:41db78380a6e 10
SomeRandomBloke 8:857444086b3f 11 #define MIN(a,b) (((a)<(b))?(a):(b))
SomeRandomBloke 8:857444086b3f 12 #define MAX(a,b) (((a)>(b))?(a):(b))
SomeRandomBloke 8:857444086b3f 13
SomeRandomBloke 8:857444086b3f 14 // menu starting points
SomeRandomBloke 8:857444086b3f 15 #define MENU_X 10 // 0-83
SomeRandomBloke 8:857444086b3f 16 #define MENU_Y 0 // 0-5
SomeRandomBloke 8:857444086b3f 17
SomeRandomBloke 9:1afbf9010118 18 #define MENU_ITEMS 4
SomeRandomBloke 8:857444086b3f 19
SomeRandomBloke 8:857444086b3f 20 int8_t blflag = 1; // Backlight initially ON
SomeRandomBloke 8:857444086b3f 21
SomeRandomBloke 8:857444086b3f 22 void about(N3310LCD* lcd, Joystick* jstick);
SomeRandomBloke 8:857444086b3f 23 void draw(N3310LCD* lcd, Joystick* jstick);
SomeRandomBloke 9:1afbf9010118 24 void snakeGame(N3310LCD* lcd, Joystick* jstick);
SomeRandomBloke 8:857444086b3f 25 void backlight(N3310LCD* lcd, Joystick* jstick);
SomeRandomBloke 8:857444086b3f 26 void waitforOKKey(N3310LCD* lcd, Joystick* jstick);
SomeRandomBloke 8:857444086b3f 27 uint8_t checkKeypressed( Joystick* jstick);
SomeRandomBloke 8:857444086b3f 28
SomeRandomBloke 8:857444086b3f 29 // menu definition
SomeRandomBloke 8:857444086b3f 30 char menu_items[MENU_ITEMS][12] = {
SomeRandomBloke 8:857444086b3f 31 "SKETCH",
SomeRandomBloke 9:1afbf9010118 32 "SNAKE",
SomeRandomBloke 8:857444086b3f 33 "BACKLIGHT",
SomeRandomBloke 8:857444086b3f 34 "ABOUT"
SomeRandomBloke 8:857444086b3f 35 };
SomeRandomBloke 8:857444086b3f 36
SomeRandomBloke 8:857444086b3f 37 void (*menu_funcs[MENU_ITEMS])(N3310LCD*,Joystick*) = {
SomeRandomBloke 8:857444086b3f 38 draw,
SomeRandomBloke 9:1afbf9010118 39 snakeGame,
SomeRandomBloke 8:857444086b3f 40 backlight,
SomeRandomBloke 8:857444086b3f 41 about
SomeRandomBloke 8:857444086b3f 42 };
SomeRandomBloke 8:857444086b3f 43
SomeRandomBloke 8:857444086b3f 44
SomeRandomBloke 8:857444086b3f 45 void about(N3310LCD* lcd, Joystick* jstick)
SomeRandomBloke 8:857444086b3f 46 {
SomeRandomBloke 8:857444086b3f 47 lcd->writeString(0, 0, "mbed-a-sketch", NORMAL);
SomeRandomBloke 8:857444086b3f 48 lcd->writeString(12, 1, "driven by", NORMAL);
SomeRandomBloke 8:857444086b3f 49 lcd->writeString(10, 2, "KL25Z mbed", NORMAL);
SomeRandomBloke 8:857444086b3f 50 lcd->writeString(0, 3, "By AD Lindsay", NORMAL);
SomeRandomBloke 8:857444086b3f 51 }
SomeRandomBloke 8:857444086b3f 52
SomeRandomBloke 8:857444086b3f 53 void backlight(N3310LCD* lcd, Joystick* jstick)
SomeRandomBloke 8:857444086b3f 54 {
SomeRandomBloke 8:857444086b3f 55 lcd->writeString( 0, 1, "Toggle", NORMAL);
SomeRandomBloke 8:857444086b3f 56 lcd->writeString( 0, 2, "Backlight", NORMAL);
SomeRandomBloke 8:857444086b3f 57 if( blflag != 0 ) {
SomeRandomBloke 8:857444086b3f 58 lcd->writeString( 60, 2, "Off", HIGHLIGHT);
SomeRandomBloke 8:857444086b3f 59 } else {
SomeRandomBloke 8:857444086b3f 60 lcd->writeString( 60, 2, "On", HIGHLIGHT);
SomeRandomBloke 8:857444086b3f 61 }
SomeRandomBloke 8:857444086b3f 62
SomeRandomBloke 8:857444086b3f 63 bool exitFlag = false;
SomeRandomBloke 8:857444086b3f 64
SomeRandomBloke 8:857444086b3f 65 while( !exitFlag ) {
SomeRandomBloke 8:857444086b3f 66 for (int i = 0; i < NUM_KEYS; i++) {
SomeRandomBloke 8:857444086b3f 67 if (jstick->getKeyState(i) != 0) {
SomeRandomBloke 8:857444086b3f 68 jstick->resetKeyState(i); // reset button flag
SomeRandomBloke 8:857444086b3f 69 switch(i) {
SomeRandomBloke 8:857444086b3f 70 case CENTER_KEY:
SomeRandomBloke 8:857444086b3f 71 exitFlag = true;
SomeRandomBloke 8:857444086b3f 72 break;
SomeRandomBloke 8:857444086b3f 73 case UP_KEY:
SomeRandomBloke 8:857444086b3f 74 case DOWN_KEY:
SomeRandomBloke 8:857444086b3f 75 if( blflag != 0 ) {
SomeRandomBloke 8:857444086b3f 76 blflag=0;
SomeRandomBloke 8:857444086b3f 77 lcd->backlight( OFF );
SomeRandomBloke 8:857444086b3f 78 } else {
SomeRandomBloke 8:857444086b3f 79 blflag = 1;
SomeRandomBloke 8:857444086b3f 80 lcd->backlight( ON );
SomeRandomBloke 8:857444086b3f 81 }
SomeRandomBloke 8:857444086b3f 82 lcd->writeString( 60, 2, (blflag != 0 ? (char*)"Off" : (char*)"On "), HIGHLIGHT);
SomeRandomBloke 8:857444086b3f 83 break;
SomeRandomBloke 8:857444086b3f 84 }
SomeRandomBloke 8:857444086b3f 85 }
SomeRandomBloke 8:857444086b3f 86 }
SomeRandomBloke 8:857444086b3f 87 }
SomeRandomBloke 8:857444086b3f 88 }
SomeRandomBloke 8:857444086b3f 89
SomeRandomBloke 11:90d35ac294af 90 void centreBoard( MMA8451Q *acc, float *cValues )
SomeRandomBloke 11:90d35ac294af 91 {
SomeRandomBloke 11:90d35ac294af 92 // Take 100 readings to get stable centre point
SomeRandomBloke 11:90d35ac294af 93 for( int i = 0; i < 100; i++ ) {
SomeRandomBloke 11:90d35ac294af 94 float axis[3] = { 0.0, 0.0, 0.0 };
SomeRandomBloke 11:90d35ac294af 95 acc->getAccAllAxis( axis );
SomeRandomBloke 11:90d35ac294af 96 cValues[0] += axis[0];
SomeRandomBloke 11:90d35ac294af 97 cValues[1] += axis[1];
SomeRandomBloke 11:90d35ac294af 98 cValues[2] += axis[2];
SomeRandomBloke 11:90d35ac294af 99 }
SomeRandomBloke 11:90d35ac294af 100 cValues[0] /= 100.0;
SomeRandomBloke 11:90d35ac294af 101 cValues[1] /= 100.0;
SomeRandomBloke 11:90d35ac294af 102 cValues[2] /= 100.0;
SomeRandomBloke 11:90d35ac294af 103 // pc.printf( "Steady State X: %f Y: %f Z: %f\n", cValues[0], cValues[1], cValues[2] );
SomeRandomBloke 11:90d35ac294af 104 }
SomeRandomBloke 11:90d35ac294af 105
SomeRandomBloke 8:857444086b3f 106
SomeRandomBloke 8:857444086b3f 107 void draw(N3310LCD* lcd, Joystick* jstick)
SomeRandomBloke 8:857444086b3f 108 {
SomeRandomBloke 8:857444086b3f 109 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, false, 4);
SomeRandomBloke 10:f3e93cc092d7 110 float x, y;
SomeRandomBloke 10:f3e93cc092d7 111 float cValues[3] = { 0.0, 0.0, 0.0 };
SomeRandomBloke 10:f3e93cc092d7 112 int16_t xI, yI;
SomeRandomBloke 10:f3e93cc092d7 113 int16_t xInc, yInc;
SomeRandomBloke 10:f3e93cc092d7 114
SomeRandomBloke 11:90d35ac294af 115 centreBoard( &acc, cValues );
SomeRandomBloke 8:857444086b3f 116 lcd->cls();
SomeRandomBloke 8:857444086b3f 117
SomeRandomBloke 10:f3e93cc092d7 118 // Take 100 readings to get stable centre point
SomeRandomBloke 11:90d35ac294af 119 /* for( int i = 0; i < 100; i++ ) {
SomeRandomBloke 11:90d35ac294af 120 float axis[3] = { 0.0, 0.0, 0.0 };
SomeRandomBloke 11:90d35ac294af 121 acc.getAccAllAxis( axis );
SomeRandomBloke 11:90d35ac294af 122 cValues[0] += axis[0];
SomeRandomBloke 11:90d35ac294af 123 cValues[1] += axis[1];
SomeRandomBloke 11:90d35ac294af 124 }
SomeRandomBloke 10:f3e93cc092d7 125
SomeRandomBloke 11:90d35ac294af 126 cValues[0] /= 100.0;
SomeRandomBloke 11:90d35ac294af 127 cValues[1] /= 100.0;
SomeRandomBloke 11:90d35ac294af 128 */
SomeRandomBloke 8:857444086b3f 129 // Draw a rectangle
SomeRandomBloke 8:857444086b3f 130 lcd->drawRectangle(0,0,83,47, PIXEL_ON);
SomeRandomBloke 8:857444086b3f 131
SomeRandomBloke 8:857444086b3f 132 int8_t px = 42;
SomeRandomBloke 8:857444086b3f 133 int8_t py = 24;
SomeRandomBloke 8:857444086b3f 134 int8_t nx = px;
SomeRandomBloke 8:857444086b3f 135 int8_t ny = py;
SomeRandomBloke 8:857444086b3f 136
SomeRandomBloke 8:857444086b3f 137 lcd->setPixel( px, py, PIXEL_ON );
SomeRandomBloke 8:857444086b3f 138
SomeRandomBloke 9:1afbf9010118 139
SomeRandomBloke 9:1afbf9010118 140
SomeRandomBloke 9:1afbf9010118 141 // Exit on joystick pressed
SomeRandomBloke 9:1afbf9010118 142 bool exitFlag = false;
SomeRandomBloke 9:1afbf9010118 143 while ( !exitFlag ) {
SomeRandomBloke 9:1afbf9010118 144 uint8_t keyPress = checkKeypressed( jstick );
SomeRandomBloke 9:1afbf9010118 145 if( keyPress == CENTER_KEY )
SomeRandomBloke 9:1afbf9010118 146 exitFlag = true;
SomeRandomBloke 9:1afbf9010118 147
SomeRandomBloke 9:1afbf9010118 148 x = (acc.getAccX() - cValues[0]) * 100.0;
SomeRandomBloke 9:1afbf9010118 149 y = (acc.getAccY() - cValues[1]) * 100.0;
SomeRandomBloke 9:1afbf9010118 150
SomeRandomBloke 9:1afbf9010118 151 xI = (int16_t)(x);
SomeRandomBloke 9:1afbf9010118 152 yI = (int16_t)(y);
SomeRandomBloke 9:1afbf9010118 153 xInc = xI/10;
SomeRandomBloke 9:1afbf9010118 154 yInc = ((yI/10) * -1);
SomeRandomBloke 9:1afbf9010118 155
SomeRandomBloke 9:1afbf9010118 156 nx += xInc;
SomeRandomBloke 9:1afbf9010118 157 ny += yInc;
SomeRandomBloke 9:1afbf9010118 158
SomeRandomBloke 9:1afbf9010118 159 nx = MAX( 1, MIN( nx, 82 ) );
SomeRandomBloke 9:1afbf9010118 160 ny = MAX( 1, MIN( ny, 46 ) );
SomeRandomBloke 11:90d35ac294af 161 //pc.printf( "X: %d Y: %d\n", nx, ny );
SomeRandomBloke 9:1afbf9010118 162
SomeRandomBloke 9:1afbf9010118 163 if( abs(xInc) > 1 || abs(yInc) > 1 ) {
SomeRandomBloke 9:1afbf9010118 164 // Draw line
SomeRandomBloke 9:1afbf9010118 165 lcd->drawLine((uint8_t)px, (uint8_t)py, (uint8_t)nx, (uint8_t)ny, PIXEL_ON);
SomeRandomBloke 9:1afbf9010118 166 } else {
SomeRandomBloke 9:1afbf9010118 167 // Plot pixel
SomeRandomBloke 9:1afbf9010118 168 lcd->setPixel( (uint8_t)nx, (uint8_t)ny, PIXEL_ON );
SomeRandomBloke 9:1afbf9010118 169 }
SomeRandomBloke 9:1afbf9010118 170
SomeRandomBloke 9:1afbf9010118 171 px = nx;
SomeRandomBloke 9:1afbf9010118 172 py = ny;
SomeRandomBloke 9:1afbf9010118 173
SomeRandomBloke 9:1afbf9010118 174 wait(0.1);
SomeRandomBloke 9:1afbf9010118 175 }
SomeRandomBloke 9:1afbf9010118 176 }
SomeRandomBloke 9:1afbf9010118 177
SomeRandomBloke 9:1afbf9010118 178 #define MAX_SNAKE_LEN 400
SomeRandomBloke 11:90d35ac294af 179 #define MAX_FOOD 10
SomeRandomBloke 9:1afbf9010118 180 #define SNAKE_X 0
SomeRandomBloke 9:1afbf9010118 181 #define SNAKE_Y 1
SomeRandomBloke 9:1afbf9010118 182 #define DIR_N 1
SomeRandomBloke 9:1afbf9010118 183 #define DIR_E 2
SomeRandomBloke 9:1afbf9010118 184 #define DIR_S 3
SomeRandomBloke 9:1afbf9010118 185 #define DIR_W 4
SomeRandomBloke 11:90d35ac294af 186 #define FOOD_X 0
SomeRandomBloke 11:90d35ac294af 187 #define FOOD_Y 1
SomeRandomBloke 9:1afbf9010118 188
SomeRandomBloke 9:1afbf9010118 189 int16_t snakeLen = 10;
SomeRandomBloke 11:90d35ac294af 190 int16_t foodCount = 0;
SomeRandomBloke 9:1afbf9010118 191
SomeRandomBloke 9:1afbf9010118 192 int8_t snake[MAX_SNAKE_LEN][2]; // Snake positions, use -1 for no point
SomeRandomBloke 11:90d35ac294af 193 int8_t food[MAX_FOOD][2];
SomeRandomBloke 9:1afbf9010118 194
SomeRandomBloke 9:1afbf9010118 195 void initSnake( void )
SomeRandomBloke 9:1afbf9010118 196 {
SomeRandomBloke 9:1afbf9010118 197 for( int i=0; i< MAX_SNAKE_LEN; i++ ) {
SomeRandomBloke 9:1afbf9010118 198 snake[i][SNAKE_X] = -1;
SomeRandomBloke 9:1afbf9010118 199 snake[i][SNAKE_Y] = -1;
SomeRandomBloke 9:1afbf9010118 200 }
SomeRandomBloke 9:1afbf9010118 201
SomeRandomBloke 9:1afbf9010118 202 // Add initial snake points
SomeRandomBloke 9:1afbf9010118 203 for(int i=0; i<snakeLen; i++ ) {
SomeRandomBloke 9:1afbf9010118 204 snake[i][SNAKE_X] = 42 + i;
SomeRandomBloke 9:1afbf9010118 205 snake[i][SNAKE_Y] = 24;
SomeRandomBloke 9:1afbf9010118 206 }
SomeRandomBloke 9:1afbf9010118 207 }
SomeRandomBloke 9:1afbf9010118 208
SomeRandomBloke 9:1afbf9010118 209 void drawSnake( N3310LCD* lcd )
SomeRandomBloke 9:1afbf9010118 210 {
SomeRandomBloke 9:1afbf9010118 211 for(int i=0; i<snakeLen; i++ ) {
SomeRandomBloke 9:1afbf9010118 212 lcd->setPixel( snake[i][SNAKE_X], snake[i][SNAKE_Y], PIXEL_ON );
SomeRandomBloke 9:1afbf9010118 213 }
SomeRandomBloke 9:1afbf9010118 214 }
SomeRandomBloke 9:1afbf9010118 215
SomeRandomBloke 9:1afbf9010118 216
SomeRandomBloke 10:f3e93cc092d7 217 void moveSnake( N3310LCD* lcd, uint8_t direction, uint8_t growLength )
SomeRandomBloke 9:1afbf9010118 218 {
SomeRandomBloke 9:1afbf9010118 219 int8_t oX = snake[0][SNAKE_X];
SomeRandomBloke 9:1afbf9010118 220 int8_t oY = snake[0][SNAKE_Y];
SomeRandomBloke 9:1afbf9010118 221 switch( direction ) {
SomeRandomBloke 9:1afbf9010118 222 case DIR_N:
SomeRandomBloke 9:1afbf9010118 223 snake[0][SNAKE_Y]--;
SomeRandomBloke 9:1afbf9010118 224 break;
SomeRandomBloke 9:1afbf9010118 225 case DIR_E:
SomeRandomBloke 9:1afbf9010118 226 snake[0][SNAKE_X]++;
SomeRandomBloke 9:1afbf9010118 227 break;
SomeRandomBloke 9:1afbf9010118 228 case DIR_S:
SomeRandomBloke 9:1afbf9010118 229 snake[0][SNAKE_Y]++;
SomeRandomBloke 9:1afbf9010118 230 break;
SomeRandomBloke 9:1afbf9010118 231 case DIR_W:
SomeRandomBloke 9:1afbf9010118 232 snake[0][SNAKE_X]--;
SomeRandomBloke 9:1afbf9010118 233 break;
SomeRandomBloke 9:1afbf9010118 234 }
SomeRandomBloke 9:1afbf9010118 235
SomeRandomBloke 9:1afbf9010118 236 int8_t nX = -1;
SomeRandomBloke 9:1afbf9010118 237 int8_t nY = -1;
SomeRandomBloke 9:1afbf9010118 238 for(int i=1; i<snakeLen; i++ ) {
SomeRandomBloke 9:1afbf9010118 239 nX = oX;
SomeRandomBloke 9:1afbf9010118 240 nY = oY;
SomeRandomBloke 9:1afbf9010118 241 oX = snake[i][SNAKE_X];
SomeRandomBloke 9:1afbf9010118 242 oY = snake[i][SNAKE_Y];
SomeRandomBloke 9:1afbf9010118 243 snake[i][SNAKE_X] = nX;
SomeRandomBloke 11:90d35ac294af 244 snake[i][SNAKE_Y] = nY;
SomeRandomBloke 9:1afbf9010118 245 }
SomeRandomBloke 9:1afbf9010118 246
SomeRandomBloke 9:1afbf9010118 247 drawSnake(lcd);
SomeRandomBloke 10:f3e93cc092d7 248 if( growLength > 0 && snakeLen < MAX_SNAKE_LEN ) {
SomeRandomBloke 10:f3e93cc092d7 249 // Dont clear tail, add point to tail
SomeRandomBloke 10:f3e93cc092d7 250 snake[snakeLen][SNAKE_X] = oX;
SomeRandomBloke 10:f3e93cc092d7 251 snake[snakeLen][SNAKE_Y] = oY;
SomeRandomBloke 10:f3e93cc092d7 252 snakeLen++;
SomeRandomBloke 10:f3e93cc092d7 253 } else {
SomeRandomBloke 10:f3e93cc092d7 254 // Clear tail
SomeRandomBloke 10:f3e93cc092d7 255 lcd->setPixel( oX, oY, PIXEL_OFF );
SomeRandomBloke 10:f3e93cc092d7 256 }
SomeRandomBloke 9:1afbf9010118 257 }
SomeRandomBloke 9:1afbf9010118 258
SomeRandomBloke 11:90d35ac294af 259 bool checkCollision( uint8_t dir)
SomeRandomBloke 11:90d35ac294af 260 {
SomeRandomBloke 11:90d35ac294af 261 bool collisionFlag = false;
SomeRandomBloke 10:f3e93cc092d7 262
SomeRandomBloke 10:f3e93cc092d7 263 return collisionFlag;
SomeRandomBloke 10:f3e93cc092d7 264 }
SomeRandomBloke 9:1afbf9010118 265
SomeRandomBloke 9:1afbf9010118 266
SomeRandomBloke 11:90d35ac294af 267 void initFood()
SomeRandomBloke 11:90d35ac294af 268 {
SomeRandomBloke 11:90d35ac294af 269 for( uint8_t i = 0; i< MAX_FOOD; i++ ) {
SomeRandomBloke 11:90d35ac294af 270 food[i][FOOD_X] = -1;
SomeRandomBloke 11:90d35ac294af 271 food[i][FOOD_Y] = -1;
SomeRandomBloke 11:90d35ac294af 272 }
SomeRandomBloke 11:90d35ac294af 273 }
SomeRandomBloke 11:90d35ac294af 274
SomeRandomBloke 11:90d35ac294af 275
SomeRandomBloke 11:90d35ac294af 276 // Get current snake head and work out if position is a food
SomeRandomBloke 11:90d35ac294af 277 bool checkFood(uint8_t dir)
SomeRandomBloke 11:90d35ac294af 278 {
SomeRandomBloke 11:90d35ac294af 279 bool foodFlag = false;
SomeRandomBloke 11:90d35ac294af 280 pc.printf("CHECK: X %d, Y %d\n",snake[0][SNAKE_X], snake[0][SNAKE_Y]);
SomeRandomBloke 11:90d35ac294af 281 for( int i = 0; i < MAX_FOOD; i++ ) {
SomeRandomBloke 11:90d35ac294af 282 pc.printf("FOOD: X %d, Y %d\n",food[i][FOOD_X], food[i][FOOD_Y]);
SomeRandomBloke 11:90d35ac294af 283
SomeRandomBloke 11:90d35ac294af 284 if( snake[0][SNAKE_X] == food[i][FOOD_X] && snake[0][SNAKE_Y] == food[i][FOOD_Y] ) {
SomeRandomBloke 11:90d35ac294af 285 foodFlag = true;
SomeRandomBloke 11:90d35ac294af 286 food[i][FOOD_X] = -1;
SomeRandomBloke 11:90d35ac294af 287 food[i][FOOD_Y] = -1;
SomeRandomBloke 11:90d35ac294af 288 foodCount--;
SomeRandomBloke 11:90d35ac294af 289 pc.printf("FOOD!\n");
SomeRandomBloke 11:90d35ac294af 290 break;
SomeRandomBloke 11:90d35ac294af 291 }
SomeRandomBloke 11:90d35ac294af 292 }
SomeRandomBloke 11:90d35ac294af 293 return foodFlag;
SomeRandomBloke 11:90d35ac294af 294 }
SomeRandomBloke 11:90d35ac294af 295
SomeRandomBloke 11:90d35ac294af 296 bool isOnSnake( int8_t *sX, int8_t *sY )
SomeRandomBloke 11:90d35ac294af 297 {
SomeRandomBloke 11:90d35ac294af 298 bool onSnake = false;
SomeRandomBloke 11:90d35ac294af 299 pc.printf("isOnSnake sX = %d, sY = %d \n", *sX, *sY );
SomeRandomBloke 11:90d35ac294af 300 for( int i = 0; i< snakeLen; i++ ) {
SomeRandomBloke 11:90d35ac294af 301 if( snake[i][SNAKE_X] == *sX && snake[i][SNAKE_Y] == *sY ) {
SomeRandomBloke 11:90d35ac294af 302 onSnake = true;
SomeRandomBloke 11:90d35ac294af 303 break;
SomeRandomBloke 11:90d35ac294af 304 }
SomeRandomBloke 11:90d35ac294af 305 }
SomeRandomBloke 11:90d35ac294af 306 return onSnake;
SomeRandomBloke 11:90d35ac294af 307 }
SomeRandomBloke 11:90d35ac294af 308
SomeRandomBloke 11:90d35ac294af 309
SomeRandomBloke 11:90d35ac294af 310 void getRandomPosition( int8_t *rX, int8_t *rY )
SomeRandomBloke 11:90d35ac294af 311 {
SomeRandomBloke 11:90d35ac294af 312 *rX = ((rand() % 100)*82)/100+1;
SomeRandomBloke 11:90d35ac294af 313 *rY = ((rand() % 100)*46)/100+1;
SomeRandomBloke 11:90d35ac294af 314
SomeRandomBloke 11:90d35ac294af 315 while( isOnSnake( rX, rY )) {
SomeRandomBloke 11:90d35ac294af 316 *rX = ((rand() % 100)*82)/100+1;
SomeRandomBloke 11:90d35ac294af 317 *rY = ((rand() % 100)*46)/100+1;
SomeRandomBloke 11:90d35ac294af 318 }
SomeRandomBloke 11:90d35ac294af 319
SomeRandomBloke 11:90d35ac294af 320 }
SomeRandomBloke 11:90d35ac294af 321
SomeRandomBloke 11:90d35ac294af 322 void storeFood( int8_t fX, int8_t fY )
SomeRandomBloke 11:90d35ac294af 323 {
SomeRandomBloke 11:90d35ac294af 324 pc.printf("Store fX = %d, fY = %d \n", fX, fY );
SomeRandomBloke 11:90d35ac294af 325 for( int i = 0; i< MAX_FOOD; i++ ) {
SomeRandomBloke 11:90d35ac294af 326 if( food[i][FOOD_X] < 0 ) {
SomeRandomBloke 11:90d35ac294af 327 pc.printf("Store in %d\n",i);
SomeRandomBloke 11:90d35ac294af 328 food[i][FOOD_X] = fX;
SomeRandomBloke 11:90d35ac294af 329 food[i][FOOD_Y] = fY;
SomeRandomBloke 11:90d35ac294af 330 break;
SomeRandomBloke 11:90d35ac294af 331 }
SomeRandomBloke 11:90d35ac294af 332 }
SomeRandomBloke 11:90d35ac294af 333 }
SomeRandomBloke 11:90d35ac294af 334
SomeRandomBloke 11:90d35ac294af 335
SomeRandomBloke 11:90d35ac294af 336 void dropFood(N3310LCD* lcd)
SomeRandomBloke 11:90d35ac294af 337 {
SomeRandomBloke 11:90d35ac294af 338 // If no food present then drop some
SomeRandomBloke 11:90d35ac294af 339 // Pick random x and y coords x >0 and x < 83, y > 0 and y <47
SomeRandomBloke 11:90d35ac294af 340 while( foodCount < MAX_FOOD ) {
SomeRandomBloke 11:90d35ac294af 341 int8_t fX = 0;
SomeRandomBloke 11:90d35ac294af 342 int8_t fY = 0;
SomeRandomBloke 11:90d35ac294af 343 getRandomPosition( &fX, &fY );
SomeRandomBloke 11:90d35ac294af 344 lcd->setPixel( fX, fY, PIXEL_ON );
SomeRandomBloke 11:90d35ac294af 345 storeFood( fX, fY );
SomeRandomBloke 11:90d35ac294af 346 foodCount++;
SomeRandomBloke 11:90d35ac294af 347
SomeRandomBloke 11:90d35ac294af 348 pc.printf("fX = %d, fY = %d \n", fX, fY );
SomeRandomBloke 11:90d35ac294af 349 }
SomeRandomBloke 11:90d35ac294af 350
SomeRandomBloke 11:90d35ac294af 351 }
SomeRandomBloke 11:90d35ac294af 352
SomeRandomBloke 9:1afbf9010118 353 void snakeGame(N3310LCD* lcd, Joystick* jstick)
SomeRandomBloke 9:1afbf9010118 354 {
SomeRandomBloke 9:1afbf9010118 355 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, false, 4);
SomeRandomBloke 8:857444086b3f 356 float x, y; //, z;
SomeRandomBloke 8:857444086b3f 357 float cValues[3] = { 0.0, 0.0, 0.0 };
SomeRandomBloke 8:857444086b3f 358 int16_t xI, yI; //, zI;
SomeRandomBloke 8:857444086b3f 359 int16_t xInc, yInc;
SomeRandomBloke 8:857444086b3f 360
SomeRandomBloke 11:90d35ac294af 361 centreBoard( &acc, cValues );
SomeRandomBloke 10:f3e93cc092d7 362
SomeRandomBloke 10:f3e93cc092d7 363 lcd->cls();
SomeRandomBloke 10:f3e93cc092d7 364
SomeRandomBloke 10:f3e93cc092d7 365 // Draw a rectangle
SomeRandomBloke 10:f3e93cc092d7 366 lcd->drawRectangle(0,0,83,47, PIXEL_ON);
SomeRandomBloke 10:f3e93cc092d7 367
SomeRandomBloke 11:90d35ac294af 368 initSnake();
SomeRandomBloke 11:90d35ac294af 369 initFood();
SomeRandomBloke 11:90d35ac294af 370 dropFood(lcd);
SomeRandomBloke 10:f3e93cc092d7 371 drawSnake( lcd );
SomeRandomBloke 11:90d35ac294af 372 /* Test to move snake
SomeRandomBloke 11:90d35ac294af 373 for( int i=0; i<10; i++ ) {
SomeRandomBloke 11:90d35ac294af 374 moveSnake(lcd, DIR_N, 1);
SomeRandomBloke 11:90d35ac294af 375 wait(0.2);
SomeRandomBloke 11:90d35ac294af 376 }
SomeRandomBloke 11:90d35ac294af 377 for( int i=0; i<20; i++ ) {
SomeRandomBloke 11:90d35ac294af 378 moveSnake(lcd, DIR_E, 0);
SomeRandomBloke 11:90d35ac294af 379 wait(0.2);
SomeRandomBloke 11:90d35ac294af 380 }
SomeRandomBloke 11:90d35ac294af 381 for( int i=0; i<20; i++ ) {
SomeRandomBloke 11:90d35ac294af 382 moveSnake(lcd, DIR_S, 1);
SomeRandomBloke 11:90d35ac294af 383 wait(0.2);
SomeRandomBloke 11:90d35ac294af 384 }
SomeRandomBloke 11:90d35ac294af 385 for( int i=0; i<20; i++ ) {
SomeRandomBloke 11:90d35ac294af 386 moveSnake(lcd, DIR_W, 1);
SomeRandomBloke 11:90d35ac294af 387 wait(0.2);
SomeRandomBloke 11:90d35ac294af 388 }
SomeRandomBloke 11:90d35ac294af 389 */
SomeRandomBloke 11:90d35ac294af 390 // int8_t px = 42;
SomeRandomBloke 11:90d35ac294af 391 // int8_t py = 25;
SomeRandomBloke 11:90d35ac294af 392 // int8_t nx = px;
SomeRandomBloke 11:90d35ac294af 393 // int8_t ny = py;
SomeRandomBloke 10:f3e93cc092d7 394
SomeRandomBloke 10:f3e93cc092d7 395 // lcd->setPixel( px, py, PIXEL_ON );
SomeRandomBloke 10:f3e93cc092d7 396
SomeRandomBloke 10:f3e93cc092d7 397
SomeRandomBloke 8:857444086b3f 398 // Exit on joystick pressed
SomeRandomBloke 8:857444086b3f 399 bool exitFlag = false;
SomeRandomBloke 9:1afbf9010118 400 uint8_t snakeDirection = DIR_W;
SomeRandomBloke 10:f3e93cc092d7 401 uint8_t snakeGrowth = 0;
SomeRandomBloke 11:90d35ac294af 402
SomeRandomBloke 8:857444086b3f 403 while ( !exitFlag ) {
SomeRandomBloke 8:857444086b3f 404 uint8_t keyPress = checkKeypressed( jstick );
SomeRandomBloke 8:857444086b3f 405 if( keyPress == CENTER_KEY )
SomeRandomBloke 8:857444086b3f 406 exitFlag = true;
SomeRandomBloke 8:857444086b3f 407
SomeRandomBloke 8:857444086b3f 408 x = (acc.getAccX() - cValues[0]) * 100.0;
SomeRandomBloke 8:857444086b3f 409 y = (acc.getAccY() - cValues[1]) * 100.0;
SomeRandomBloke 8:857444086b3f 410 // pc.printf( "X: %f Y: %f Z: %f\n", x, y, z);
SomeRandomBloke 8:857444086b3f 411
SomeRandomBloke 8:857444086b3f 412 xI = (int16_t)(x);
SomeRandomBloke 8:857444086b3f 413 yI = (int16_t)(y);
SomeRandomBloke 8:857444086b3f 414 xInc = xI/10;
SomeRandomBloke 8:857444086b3f 415 yInc = ((yI/10) * -1);
SomeRandomBloke 11:90d35ac294af 416
SomeRandomBloke 9:1afbf9010118 417 if( xInc > 2 )
SomeRandomBloke 9:1afbf9010118 418 snakeDirection = DIR_E;
SomeRandomBloke 9:1afbf9010118 419 else if( xInc < -2 )
SomeRandomBloke 9:1afbf9010118 420 snakeDirection = DIR_W;
SomeRandomBloke 9:1afbf9010118 421 else if( yInc > 2 )
SomeRandomBloke 9:1afbf9010118 422 snakeDirection = DIR_S;
SomeRandomBloke 9:1afbf9010118 423 else if( yInc < -2 )
SomeRandomBloke 9:1afbf9010118 424 snakeDirection = DIR_N;
SomeRandomBloke 11:90d35ac294af 425
SomeRandomBloke 11:90d35ac294af 426 if( snakeGrowth > 0 ) snakeGrowth--;
SomeRandomBloke 11:90d35ac294af 427
SomeRandomBloke 10:f3e93cc092d7 428 moveSnake(lcd, snakeDirection, snakeGrowth);
SomeRandomBloke 10:f3e93cc092d7 429 // TODO Check if we've collided with anything
SomeRandomBloke 11:90d35ac294af 430 if( checkCollision(snakeDirection) ) {
SomeRandomBloke 10:f3e93cc092d7 431 // Collision detected!!
SomeRandomBloke 10:f3e93cc092d7 432 }
SomeRandomBloke 11:90d35ac294af 433
SomeRandomBloke 11:90d35ac294af 434 if( checkFood(snakeDirection) ) {
SomeRandomBloke 11:90d35ac294af 435 // Gobbled some food!!
SomeRandomBloke 11:90d35ac294af 436 // Indicate that snake is to grow during next moves
SomeRandomBloke 11:90d35ac294af 437 snakeGrowth = 10;
SomeRandomBloke 11:90d35ac294af 438
SomeRandomBloke 11:90d35ac294af 439 // Handle new food bing added to area
SomeRandomBloke 11:90d35ac294af 440 dropFood(lcd);
SomeRandomBloke 11:90d35ac294af 441 }
SomeRandomBloke 11:90d35ac294af 442
SomeRandomBloke 11:90d35ac294af 443 // A little pause - vary this after snake length has reached 100, then 200, then 300
SomeRandomBloke 9:1afbf9010118 444 wait(0.2);
SomeRandomBloke 9:1afbf9010118 445
SomeRandomBloke 8:857444086b3f 446 }
SomeRandomBloke 8:857444086b3f 447
SomeRandomBloke 8:857444086b3f 448 }
SomeRandomBloke 8:857444086b3f 449
SomeRandomBloke 8:857444086b3f 450
SomeRandomBloke 8:857444086b3f 451
SomeRandomBloke 8:857444086b3f 452 void initMenu(N3310LCD* lcd)
SomeRandomBloke 8:857444086b3f 453 {
SomeRandomBloke 8:857444086b3f 454 lcd->writeString(MENU_X, MENU_Y, menu_items[0], HIGHLIGHT );
SomeRandomBloke 8:857444086b3f 455
SomeRandomBloke 8:857444086b3f 456 for (int i = 1; i < MENU_ITEMS; i++) {
SomeRandomBloke 8:857444086b3f 457 lcd->writeString(MENU_X, MENU_Y + i, menu_items[i], NORMAL);
SomeRandomBloke 8:857444086b3f 458 }
SomeRandomBloke 8:857444086b3f 459 }
SomeRandomBloke 8:857444086b3f 460
SomeRandomBloke 8:857444086b3f 461 void waitforOKKey(N3310LCD* lcd, Joystick* jstick)
SomeRandomBloke 8:857444086b3f 462 {
SomeRandomBloke 8:857444086b3f 463 lcd->writeString(38, 5, "OK", HIGHLIGHT );
SomeRandomBloke 8:857444086b3f 464
SomeRandomBloke 8:857444086b3f 465 int key = 0xFF;
SomeRandomBloke 8:857444086b3f 466 while (key != CENTER_KEY) {
SomeRandomBloke 8:857444086b3f 467 for (int i = 0; i < NUM_KEYS; i++) {
SomeRandomBloke 8:857444086b3f 468 if (jstick->getKeyState(i) !=0) {
SomeRandomBloke 8:857444086b3f 469 jstick->resetKeyState(i); // reset
SomeRandomBloke 8:857444086b3f 470 if (CENTER_KEY == i) key = CENTER_KEY;
SomeRandomBloke 8:857444086b3f 471 }
SomeRandomBloke 8:857444086b3f 472 }
SomeRandomBloke 8:857444086b3f 473 }
SomeRandomBloke 8:857444086b3f 474 }
SomeRandomBloke 8:857444086b3f 475
SomeRandomBloke 8:857444086b3f 476 // Check if joystick is moved or pressed
SomeRandomBloke 8:857444086b3f 477 uint8_t checkKeypressed( Joystick* jstick)
SomeRandomBloke 8:857444086b3f 478 {
SomeRandomBloke 8:857444086b3f 479 uint8_t key = 0xFF;
SomeRandomBloke 8:857444086b3f 480
SomeRandomBloke 8:857444086b3f 481 for(int i=0; i<NUM_KEYS; i++) {
SomeRandomBloke 8:857444086b3f 482 if (jstick->getKeyState(i) !=0) {
SomeRandomBloke 8:857444086b3f 483 jstick->resetKeyState(i); // reset
SomeRandomBloke 8:857444086b3f 484 if (CENTER_KEY == i) key = CENTER_KEY;
SomeRandomBloke 8:857444086b3f 485 }
SomeRandomBloke 8:857444086b3f 486 }
SomeRandomBloke 8:857444086b3f 487 return key;
SomeRandomBloke 8:857444086b3f 488 }
SomeRandomBloke 8:857444086b3f 489
SomeRandomBloke 8:857444086b3f 490
SomeRandomBloke 8:857444086b3f 491 int main(void)
SomeRandomBloke 8:857444086b3f 492 {
SomeRandomBloke 8:857444086b3f 493 // MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, false, 4);
SomeRandomBloke 8:857444086b3f 494 Joystick jstick(N3310SPIPort::AD0);
SomeRandomBloke 8:857444086b3f 495 N3310LCD lcd(N3310SPIPort::MOSI, N3310SPIPort::MISO, N3310SPIPort::SCK,
SomeRandomBloke 8:857444086b3f 496 N3310SPIPort::CE, N3310SPIPort::DAT_CMD, N3310SPIPort::LCD_RST,
SomeRandomBloke 8:857444086b3f 497 N3310SPIPort::BL_ON);
SomeRandomBloke 8:857444086b3f 498 lcd.init();
SomeRandomBloke 8:857444086b3f 499 lcd.cls();
SomeRandomBloke 8:857444086b3f 500 lcd.backlight(ON);
SomeRandomBloke 8:857444086b3f 501
SomeRandomBloke 8:857444086b3f 502 // demo stuff
SomeRandomBloke 8:857444086b3f 503 // autoDemo(&lcd);
SomeRandomBloke 8:857444086b3f 504
SomeRandomBloke 11:90d35ac294af 505 // lcd.drawBitmap(0, 0, splash, 84, 48);
SomeRandomBloke 11:90d35ac294af 506 // wait( 5 );
SomeRandomBloke 8:857444086b3f 507 lcd.cls();
SomeRandomBloke 8:857444086b3f 508
SomeRandomBloke 8:857444086b3f 509 initMenu(&lcd);
SomeRandomBloke 8:857444086b3f 510 int currentMenuItem = 0;
SomeRandomBloke 8:857444086b3f 511 Ticker jstickPoll;
SomeRandomBloke 8:857444086b3f 512 jstickPoll.attach(&jstick, &Joystick::updateADCKey, 0.01); // check ever 10ms
chris 4:367de1084ea9 513
emilmont 5:bf5becf7469c 514 while (true) {
SomeRandomBloke 8:857444086b3f 515 for (int i = 0; i < NUM_KEYS; i++) {
SomeRandomBloke 8:857444086b3f 516 if (jstick.getKeyState(i) != 0) {
SomeRandomBloke 8:857444086b3f 517 jstick.resetKeyState(i); // reset button flag
SomeRandomBloke 8:857444086b3f 518 switch(i) {
SomeRandomBloke 8:857444086b3f 519 case UP_KEY:
SomeRandomBloke 8:857444086b3f 520 // current item to normal display
SomeRandomBloke 8:857444086b3f 521 lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], NORMAL);
SomeRandomBloke 8:857444086b3f 522 currentMenuItem -=1;
SomeRandomBloke 8:857444086b3f 523 if (currentMenuItem <0) currentMenuItem = MENU_ITEMS -1;
SomeRandomBloke 8:857444086b3f 524 // next item to highlight display
SomeRandomBloke 8:857444086b3f 525 lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], HIGHLIGHT);
SomeRandomBloke 8:857444086b3f 526 break;
SomeRandomBloke 8:857444086b3f 527
SomeRandomBloke 8:857444086b3f 528 case DOWN_KEY:
SomeRandomBloke 8:857444086b3f 529 // current item to normal display
SomeRandomBloke 8:857444086b3f 530 lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], NORMAL);
SomeRandomBloke 8:857444086b3f 531 currentMenuItem +=1;
SomeRandomBloke 8:857444086b3f 532 if(currentMenuItem >(MENU_ITEMS - 1)) currentMenuItem = 0;
SomeRandomBloke 8:857444086b3f 533 // next item to highlight display
SomeRandomBloke 8:857444086b3f 534 lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], HIGHLIGHT);
SomeRandomBloke 8:857444086b3f 535 break;
SomeRandomBloke 8:857444086b3f 536
SomeRandomBloke 8:857444086b3f 537 case LEFT_KEY:
SomeRandomBloke 8:857444086b3f 538 initMenu(&lcd);
SomeRandomBloke 8:857444086b3f 539 currentMenuItem = 0;
SomeRandomBloke 8:857444086b3f 540 break;
SomeRandomBloke 8:857444086b3f 541
SomeRandomBloke 8:857444086b3f 542 case RIGHT_KEY:
SomeRandomBloke 8:857444086b3f 543 lcd.cls();
SomeRandomBloke 8:857444086b3f 544 (*menu_funcs[currentMenuItem])(&lcd, &jstick);
SomeRandomBloke 8:857444086b3f 545 waitforOKKey(&lcd, &jstick);
SomeRandomBloke 8:857444086b3f 546 lcd.cls();
SomeRandomBloke 8:857444086b3f 547 initMenu(&lcd);
SomeRandomBloke 8:857444086b3f 548 currentMenuItem = 0;
SomeRandomBloke 8:857444086b3f 549 break;
SomeRandomBloke 8:857444086b3f 550 }
SomeRandomBloke 8:857444086b3f 551 }
SomeRandomBloke 8:857444086b3f 552 }
chris 2:41db78380a6e 553 }
SomeRandomBloke 8:857444086b3f 554
SomeRandomBloke 8:857444086b3f 555 /*
SomeRandomBloke 8:857444086b3f 556 // PwmOut rled(LED_RED);
SomeRandomBloke 8:857444086b3f 557 // PwmOut gled(LED_GREEN);
SomeRandomBloke 8:857444086b3f 558 // PwmOut bled(LED_BLUE);
SomeRandomBloke 8:857444086b3f 559 float x, y, z;
SomeRandomBloke 8:857444086b3f 560 float cValues[3] = { 0.0, 0.0, 0.0 };
SomeRandomBloke 8:857444086b3f 561 int16_t xI, yI, zI;
SomeRandomBloke 8:857444086b3f 562
SomeRandomBloke 8:857444086b3f 563 // Take 100 readings to get stable centre point
SomeRandomBloke 8:857444086b3f 564
SomeRandomBloke 8:857444086b3f 565 for( int i = 0; i < 100; i++ ) {
SomeRandomBloke 8:857444086b3f 566 float axis[3] = { 0.0, 0.0, 0.0 };
SomeRandomBloke 8:857444086b3f 567 acc.getAccAllAxis( axis );
SomeRandomBloke 8:857444086b3f 568 cValues[0] += axis[0];
SomeRandomBloke 8:857444086b3f 569 cValues[1] += axis[1];
SomeRandomBloke 8:857444086b3f 570 cValues[2] += axis[2];
SomeRandomBloke 8:857444086b3f 571 }
SomeRandomBloke 8:857444086b3f 572
SomeRandomBloke 8:857444086b3f 573 cValues[0] /= 100.0;
SomeRandomBloke 8:857444086b3f 574 cValues[1] /= 100.0;
SomeRandomBloke 8:857444086b3f 575 cValues[2] /= 100.0;
SomeRandomBloke 8:857444086b3f 576 pc.printf( "Steady State X: %f Y: %f Z: %f\n", cValues[0], cValues[1], cValues[2] );
SomeRandomBloke 8:857444086b3f 577
SomeRandomBloke 8:857444086b3f 578 while (true) {
SomeRandomBloke 8:857444086b3f 579
SomeRandomBloke 8:857444086b3f 580 x = (acc.getAccX() - cValues[0]) * 100.0;
SomeRandomBloke 8:857444086b3f 581 y = (acc.getAccY() - cValues[1]) * 100.0;
SomeRandomBloke 8:857444086b3f 582 z = (acc.getAccZ() - cValues[2]) * 100.0;
SomeRandomBloke 8:857444086b3f 583 // pc.printf( "X: %f Y: %f Z: %f\n", x, y, z);
SomeRandomBloke 8:857444086b3f 584
SomeRandomBloke 8:857444086b3f 585 xI = (int16_t)(x);
SomeRandomBloke 8:857444086b3f 586 yI = (int16_t)(y);
SomeRandomBloke 8:857444086b3f 587 zI = (int16_t)(z);
SomeRandomBloke 8:857444086b3f 588 pc.printf( "X: %d Y: %d Z: %d\n", xI, yI, zI);
SomeRandomBloke 8:857444086b3f 589 // pc.printf( "X: %f Y: %f Z: %f\n", x*100.0, y*100.0, z*100.0);
SomeRandomBloke 8:857444086b3f 590 // rled = 1.0 - abs(acc.getAccX());
SomeRandomBloke 8:857444086b3f 591 // gled = 1.0 - abs(acc.getAccY());
SomeRandomBloke 8:857444086b3f 592 // bled = 1.0 - abs(acc.getAccZ());
SomeRandomBloke 8:857444086b3f 593 wait(0.5);
SomeRandomBloke 8:857444086b3f 594 }
SomeRandomBloke 8:857444086b3f 595 */
chris 2:41db78380a6e 596 }