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:
Mon Mar 18 22:41:09 2013 +0000
Revision:
9:1afbf9010118
Parent:
8:857444086b3f
Child:
10:f3e93cc092d7
daily 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 8:857444086b3f 90
SomeRandomBloke 8:857444086b3f 91 void draw(N3310LCD* lcd, Joystick* jstick)
SomeRandomBloke 8:857444086b3f 92 {
SomeRandomBloke 8:857444086b3f 93 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, false, 4);
SomeRandomBloke 8:857444086b3f 94 lcd->cls();
SomeRandomBloke 8:857444086b3f 95
SomeRandomBloke 8:857444086b3f 96 // Draw a rectangle
SomeRandomBloke 8:857444086b3f 97 lcd->drawRectangle(0,0,83,47, PIXEL_ON);
SomeRandomBloke 8:857444086b3f 98
SomeRandomBloke 8:857444086b3f 99 int8_t px = 42;
SomeRandomBloke 8:857444086b3f 100 int8_t py = 24;
SomeRandomBloke 8:857444086b3f 101 int8_t nx = px;
SomeRandomBloke 8:857444086b3f 102 int8_t ny = py;
SomeRandomBloke 8:857444086b3f 103
SomeRandomBloke 8:857444086b3f 104 lcd->setPixel( px, py, PIXEL_ON );
SomeRandomBloke 8:857444086b3f 105
SomeRandomBloke 9:1afbf9010118 106 float x, y;
SomeRandomBloke 9:1afbf9010118 107 float cValues[3] = { 0.0, 0.0, 0.0 };
SomeRandomBloke 9:1afbf9010118 108 int16_t xI, yI;
SomeRandomBloke 9:1afbf9010118 109 int16_t xInc, yInc;
SomeRandomBloke 9:1afbf9010118 110
SomeRandomBloke 9:1afbf9010118 111 // Take 100 readings to get stable centre point
SomeRandomBloke 9:1afbf9010118 112
SomeRandomBloke 9:1afbf9010118 113 for( int i = 0; i < 100; i++ ) {
SomeRandomBloke 9:1afbf9010118 114 float axis[2] = { 0.0, 0.0 };
SomeRandomBloke 9:1afbf9010118 115 acc.getAccAllAxis( axis );
SomeRandomBloke 9:1afbf9010118 116 cValues[0] += axis[0];
SomeRandomBloke 9:1afbf9010118 117 cValues[1] += axis[1];
SomeRandomBloke 9:1afbf9010118 118 }
SomeRandomBloke 9:1afbf9010118 119
SomeRandomBloke 9:1afbf9010118 120 cValues[0] /= 100.0;
SomeRandomBloke 9:1afbf9010118 121 cValues[1] /= 100.0;
SomeRandomBloke 9:1afbf9010118 122
SomeRandomBloke 9:1afbf9010118 123 // Exit on joystick pressed
SomeRandomBloke 9:1afbf9010118 124 bool exitFlag = false;
SomeRandomBloke 9:1afbf9010118 125 while ( !exitFlag ) {
SomeRandomBloke 9:1afbf9010118 126 uint8_t keyPress = checkKeypressed( jstick );
SomeRandomBloke 9:1afbf9010118 127 if( keyPress == CENTER_KEY )
SomeRandomBloke 9:1afbf9010118 128 exitFlag = true;
SomeRandomBloke 9:1afbf9010118 129
SomeRandomBloke 9:1afbf9010118 130 x = (acc.getAccX() - cValues[0]) * 100.0;
SomeRandomBloke 9:1afbf9010118 131 y = (acc.getAccY() - cValues[1]) * 100.0;
SomeRandomBloke 9:1afbf9010118 132
SomeRandomBloke 9:1afbf9010118 133 xI = (int16_t)(x);
SomeRandomBloke 9:1afbf9010118 134 yI = (int16_t)(y);
SomeRandomBloke 9:1afbf9010118 135 xInc = xI/10;
SomeRandomBloke 9:1afbf9010118 136 yInc = ((yI/10) * -1);
SomeRandomBloke 9:1afbf9010118 137
SomeRandomBloke 9:1afbf9010118 138 nx += xInc;
SomeRandomBloke 9:1afbf9010118 139 ny += yInc;
SomeRandomBloke 9:1afbf9010118 140
SomeRandomBloke 9:1afbf9010118 141 nx = MAX( 1, MIN( nx, 82 ) );
SomeRandomBloke 9:1afbf9010118 142 ny = MAX( 1, MIN( ny, 46 ) );
SomeRandomBloke 9:1afbf9010118 143
SomeRandomBloke 9:1afbf9010118 144 if( abs(xInc) > 1 || abs(yInc) > 1 ) {
SomeRandomBloke 9:1afbf9010118 145 // Draw line
SomeRandomBloke 9:1afbf9010118 146 lcd->drawLine((uint8_t)px, (uint8_t)py, (uint8_t)nx, (uint8_t)ny, PIXEL_ON);
SomeRandomBloke 9:1afbf9010118 147 } else {
SomeRandomBloke 9:1afbf9010118 148 // Plot pixel
SomeRandomBloke 9:1afbf9010118 149 lcd->setPixel( (uint8_t)nx, (uint8_t)ny, PIXEL_ON );
SomeRandomBloke 9:1afbf9010118 150 }
SomeRandomBloke 9:1afbf9010118 151
SomeRandomBloke 9:1afbf9010118 152 px = nx;
SomeRandomBloke 9:1afbf9010118 153 py = ny;
SomeRandomBloke 9:1afbf9010118 154
SomeRandomBloke 9:1afbf9010118 155 wait(0.1);
SomeRandomBloke 9:1afbf9010118 156 }
SomeRandomBloke 9:1afbf9010118 157 }
SomeRandomBloke 9:1afbf9010118 158
SomeRandomBloke 9:1afbf9010118 159 #define MAX_SNAKE_LEN 400
SomeRandomBloke 9:1afbf9010118 160 #define SNAKE_X 0
SomeRandomBloke 9:1afbf9010118 161 #define SNAKE_Y 1
SomeRandomBloke 9:1afbf9010118 162 #define DIR_N 1
SomeRandomBloke 9:1afbf9010118 163 #define DIR_E 2
SomeRandomBloke 9:1afbf9010118 164 #define DIR_S 3
SomeRandomBloke 9:1afbf9010118 165 #define DIR_W 4
SomeRandomBloke 9:1afbf9010118 166
SomeRandomBloke 9:1afbf9010118 167 int16_t snakeLen = 10;
SomeRandomBloke 9:1afbf9010118 168
SomeRandomBloke 9:1afbf9010118 169 int8_t snake[MAX_SNAKE_LEN][2]; // Snake positions, use -1 for no point
SomeRandomBloke 9:1afbf9010118 170
SomeRandomBloke 9:1afbf9010118 171 void initSnake( void )
SomeRandomBloke 9:1afbf9010118 172 {
SomeRandomBloke 9:1afbf9010118 173 for( int i=0; i< MAX_SNAKE_LEN; i++ ) {
SomeRandomBloke 9:1afbf9010118 174 snake[i][SNAKE_X] = -1;
SomeRandomBloke 9:1afbf9010118 175 snake[i][SNAKE_Y] = -1;
SomeRandomBloke 9:1afbf9010118 176 }
SomeRandomBloke 9:1afbf9010118 177
SomeRandomBloke 9:1afbf9010118 178 // Add initial snake points
SomeRandomBloke 9:1afbf9010118 179 for(int i=0; i<snakeLen; i++ ) {
SomeRandomBloke 9:1afbf9010118 180 snake[i][SNAKE_X] = 42 + i;
SomeRandomBloke 9:1afbf9010118 181 snake[i][SNAKE_Y] = 24;
SomeRandomBloke 9:1afbf9010118 182 }
SomeRandomBloke 9:1afbf9010118 183 }
SomeRandomBloke 9:1afbf9010118 184
SomeRandomBloke 9:1afbf9010118 185 void drawSnake( N3310LCD* lcd )
SomeRandomBloke 9:1afbf9010118 186 {
SomeRandomBloke 9:1afbf9010118 187 for(int i=0; i<snakeLen; i++ ) {
SomeRandomBloke 9:1afbf9010118 188 lcd->setPixel( snake[i][SNAKE_X], snake[i][SNAKE_Y], PIXEL_ON );
SomeRandomBloke 9:1afbf9010118 189 }
SomeRandomBloke 9:1afbf9010118 190 }
SomeRandomBloke 9:1afbf9010118 191
SomeRandomBloke 9:1afbf9010118 192 void growSnake( N3310LCD* lcd, int len )
SomeRandomBloke 9:1afbf9010118 193 {
SomeRandomBloke 9:1afbf9010118 194
SomeRandomBloke 9:1afbf9010118 195 }
SomeRandomBloke 9:1afbf9010118 196
SomeRandomBloke 9:1afbf9010118 197
SomeRandomBloke 9:1afbf9010118 198 void moveSnake( N3310LCD* lcd, uint8_t direction )
SomeRandomBloke 9:1afbf9010118 199 {
SomeRandomBloke 9:1afbf9010118 200 int8_t oX = snake[0][SNAKE_X];
SomeRandomBloke 9:1afbf9010118 201 int8_t oY = snake[0][SNAKE_Y];
SomeRandomBloke 9:1afbf9010118 202 switch( direction ) {
SomeRandomBloke 9:1afbf9010118 203 case DIR_N:
SomeRandomBloke 9:1afbf9010118 204 snake[0][SNAKE_Y]--;
SomeRandomBloke 9:1afbf9010118 205 break;
SomeRandomBloke 9:1afbf9010118 206 case DIR_E:
SomeRandomBloke 9:1afbf9010118 207 snake[0][SNAKE_X]++;
SomeRandomBloke 9:1afbf9010118 208 break;
SomeRandomBloke 9:1afbf9010118 209 case DIR_S:
SomeRandomBloke 9:1afbf9010118 210 snake[0][SNAKE_Y]++;
SomeRandomBloke 9:1afbf9010118 211 break;
SomeRandomBloke 9:1afbf9010118 212 case DIR_W:
SomeRandomBloke 9:1afbf9010118 213 snake[0][SNAKE_X]--;
SomeRandomBloke 9:1afbf9010118 214 break;
SomeRandomBloke 9:1afbf9010118 215 }
SomeRandomBloke 9:1afbf9010118 216
SomeRandomBloke 9:1afbf9010118 217 int8_t nX = -1;
SomeRandomBloke 9:1afbf9010118 218 int8_t nY = -1;
SomeRandomBloke 9:1afbf9010118 219 for(int i=1; i<snakeLen; i++ ) {
SomeRandomBloke 9:1afbf9010118 220 nX = oX;
SomeRandomBloke 9:1afbf9010118 221 nY = oY;
SomeRandomBloke 9:1afbf9010118 222 oX = snake[i][SNAKE_X];
SomeRandomBloke 9:1afbf9010118 223 oY = snake[i][SNAKE_Y];
SomeRandomBloke 9:1afbf9010118 224 snake[i][SNAKE_X] = nX;
SomeRandomBloke 9:1afbf9010118 225 snake[i][SNAKE_Y] = nY;
SomeRandomBloke 9:1afbf9010118 226 }
SomeRandomBloke 9:1afbf9010118 227
SomeRandomBloke 9:1afbf9010118 228 drawSnake(lcd);
SomeRandomBloke 9:1afbf9010118 229 lcd->setPixel( oX, oY, PIXEL_OFF );
SomeRandomBloke 9:1afbf9010118 230 }
SomeRandomBloke 9:1afbf9010118 231
SomeRandomBloke 9:1afbf9010118 232
SomeRandomBloke 9:1afbf9010118 233
SomeRandomBloke 9:1afbf9010118 234 void snakeGame(N3310LCD* lcd, Joystick* jstick)
SomeRandomBloke 9:1afbf9010118 235 {
SomeRandomBloke 9:1afbf9010118 236 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, false, 4);
SomeRandomBloke 9:1afbf9010118 237
SomeRandomBloke 9:1afbf9010118 238 initSnake();
SomeRandomBloke 9:1afbf9010118 239
SomeRandomBloke 9:1afbf9010118 240 lcd->cls();
SomeRandomBloke 9:1afbf9010118 241
SomeRandomBloke 9:1afbf9010118 242 // Draw a rectangle
SomeRandomBloke 9:1afbf9010118 243 lcd->drawRectangle(0,0,83,47, PIXEL_ON);
SomeRandomBloke 9:1afbf9010118 244
SomeRandomBloke 9:1afbf9010118 245 drawSnake( lcd );
SomeRandomBloke 9:1afbf9010118 246 /*
SomeRandomBloke 9:1afbf9010118 247 for( int i=0; i<10; i++ ) {
SomeRandomBloke 9:1afbf9010118 248 moveSnake(lcd, DIR_N);
SomeRandomBloke 9:1afbf9010118 249 wait(0.2);
SomeRandomBloke 9:1afbf9010118 250 }
SomeRandomBloke 9:1afbf9010118 251 for( int i=0; i<20; i++ ) {
SomeRandomBloke 9:1afbf9010118 252 moveSnake(lcd, DIR_E);
SomeRandomBloke 9:1afbf9010118 253 wait(0.2);
SomeRandomBloke 9:1afbf9010118 254 }
SomeRandomBloke 9:1afbf9010118 255 for( int i=0; i<20; i++ ) {
SomeRandomBloke 9:1afbf9010118 256 moveSnake(lcd, DIR_S);
SomeRandomBloke 9:1afbf9010118 257 wait(0.2);
SomeRandomBloke 9:1afbf9010118 258 }
SomeRandomBloke 9:1afbf9010118 259 for( int i=0; i<20; i++ ) {
SomeRandomBloke 9:1afbf9010118 260 moveSnake(lcd, DIR_W);
SomeRandomBloke 9:1afbf9010118 261 wait(0.2);
SomeRandomBloke 9:1afbf9010118 262 }
SomeRandomBloke 9:1afbf9010118 263
SomeRandomBloke 9:1afbf9010118 264 int8_t px = 42;
SomeRandomBloke 9:1afbf9010118 265 int8_t py = 25;
SomeRandomBloke 9:1afbf9010118 266 int8_t nx = px;
SomeRandomBloke 9:1afbf9010118 267 int8_t ny = py;
SomeRandomBloke 9:1afbf9010118 268 */
SomeRandomBloke 9:1afbf9010118 269 // lcd->setPixel( px, py, PIXEL_ON );
SomeRandomBloke 9:1afbf9010118 270
SomeRandomBloke 8:857444086b3f 271 float x, y; //, z;
SomeRandomBloke 8:857444086b3f 272 float cValues[3] = { 0.0, 0.0, 0.0 };
SomeRandomBloke 8:857444086b3f 273 int16_t xI, yI; //, zI;
SomeRandomBloke 8:857444086b3f 274 int16_t xInc, yInc;
SomeRandomBloke 8:857444086b3f 275
SomeRandomBloke 8:857444086b3f 276 // Take 100 readings to get stable centre point
SomeRandomBloke 8:857444086b3f 277
SomeRandomBloke 8:857444086b3f 278 for( int i = 0; i < 100; i++ ) {
SomeRandomBloke 8:857444086b3f 279 float axis[3] = { 0.0, 0.0, 0.0 };
SomeRandomBloke 8:857444086b3f 280 acc.getAccAllAxis( axis );
SomeRandomBloke 8:857444086b3f 281 cValues[0] += axis[0];
SomeRandomBloke 8:857444086b3f 282 cValues[1] += axis[1];
SomeRandomBloke 8:857444086b3f 283 // cValues[2] += axis[2];
SomeRandomBloke 8:857444086b3f 284 }
SomeRandomBloke 8:857444086b3f 285
SomeRandomBloke 8:857444086b3f 286 cValues[0] /= 100.0;
SomeRandomBloke 8:857444086b3f 287 cValues[1] /= 100.0;
SomeRandomBloke 8:857444086b3f 288 // cValues[2] /= 100.0;
SomeRandomBloke 8:857444086b3f 289 // pc.printf( "Steady State X: %f Y: %f Z: %f\n", cValues[0], cValues[1], cValues[2] );
SomeRandomBloke 8:857444086b3f 290
SomeRandomBloke 8:857444086b3f 291 // Exit on joystick pressed
SomeRandomBloke 8:857444086b3f 292 bool exitFlag = false;
SomeRandomBloke 9:1afbf9010118 293 uint8_t snakeDirection = DIR_W;
SomeRandomBloke 9:1afbf9010118 294
SomeRandomBloke 8:857444086b3f 295 while ( !exitFlag ) {
SomeRandomBloke 8:857444086b3f 296 uint8_t keyPress = checkKeypressed( jstick );
SomeRandomBloke 8:857444086b3f 297 if( keyPress == CENTER_KEY )
SomeRandomBloke 8:857444086b3f 298 exitFlag = true;
SomeRandomBloke 8:857444086b3f 299
SomeRandomBloke 8:857444086b3f 300 x = (acc.getAccX() - cValues[0]) * 100.0;
SomeRandomBloke 8:857444086b3f 301 y = (acc.getAccY() - cValues[1]) * 100.0;
SomeRandomBloke 8:857444086b3f 302 // z = (acc.getAccZ() - cValues[2]) * 100.0;
SomeRandomBloke 8:857444086b3f 303 // pc.printf( "X: %f Y: %f Z: %f\n", x, y, z);
SomeRandomBloke 8:857444086b3f 304
SomeRandomBloke 8:857444086b3f 305 xI = (int16_t)(x);
SomeRandomBloke 8:857444086b3f 306 yI = (int16_t)(y);
SomeRandomBloke 8:857444086b3f 307 // zI = (int16_t)(z);
SomeRandomBloke 8:857444086b3f 308 xInc = xI/10;
SomeRandomBloke 8:857444086b3f 309 yInc = ((yI/10) * -1);
SomeRandomBloke 8:857444086b3f 310
SomeRandomBloke 9:1afbf9010118 311
SomeRandomBloke 9:1afbf9010118 312 if( xInc > 2 )
SomeRandomBloke 9:1afbf9010118 313 snakeDirection = DIR_E;
SomeRandomBloke 9:1afbf9010118 314 else if( xInc < -2 )
SomeRandomBloke 9:1afbf9010118 315 snakeDirection = DIR_W;
SomeRandomBloke 9:1afbf9010118 316 else if( yInc > 2 )
SomeRandomBloke 9:1afbf9010118 317 snakeDirection = DIR_S;
SomeRandomBloke 9:1afbf9010118 318 else if( yInc < -2 )
SomeRandomBloke 9:1afbf9010118 319 snakeDirection = DIR_N;
SomeRandomBloke 9:1afbf9010118 320 moveSnake(lcd, snakeDirection);
SomeRandomBloke 9:1afbf9010118 321 wait(0.2);
SomeRandomBloke 9:1afbf9010118 322
SomeRandomBloke 9:1afbf9010118 323 /*
SomeRandomBloke 8:857444086b3f 324 nx += xInc;
SomeRandomBloke 8:857444086b3f 325 ny += yInc;
SomeRandomBloke 8:857444086b3f 326
SomeRandomBloke 8:857444086b3f 327 nx = MAX( 1, MIN( nx, 82 ) );
SomeRandomBloke 8:857444086b3f 328 ny = MAX( 1, MIN( ny, 46 ) );
SomeRandomBloke 8:857444086b3f 329
SomeRandomBloke 8:857444086b3f 330 if( abs(xInc) > 1 || abs(yInc) > 1 ) {
SomeRandomBloke 8:857444086b3f 331 // Draw line
SomeRandomBloke 8:857444086b3f 332 lcd->drawLine((uint8_t)px, (uint8_t)py, (uint8_t)nx, (uint8_t)ny, PIXEL_ON);
SomeRandomBloke 8:857444086b3f 333 } else {
SomeRandomBloke 8:857444086b3f 334 // Plot pixel
SomeRandomBloke 8:857444086b3f 335 lcd->setPixel( (uint8_t)nx, (uint8_t)ny, PIXEL_ON );
SomeRandomBloke 8:857444086b3f 336 }
SomeRandomBloke 8:857444086b3f 337
SomeRandomBloke 8:857444086b3f 338 px = nx;
SomeRandomBloke 8:857444086b3f 339 py = ny;
SomeRandomBloke 8:857444086b3f 340
SomeRandomBloke 8:857444086b3f 341 // pc.printf( "X: %d Y: %d Z: %d\n", xI, yI, zI);
SomeRandomBloke 8:857444086b3f 342 wait(0.1);
SomeRandomBloke 9:1afbf9010118 343 */
SomeRandomBloke 8:857444086b3f 344 }
SomeRandomBloke 8:857444086b3f 345
SomeRandomBloke 8:857444086b3f 346 }
SomeRandomBloke 8:857444086b3f 347
SomeRandomBloke 8:857444086b3f 348
SomeRandomBloke 8:857444086b3f 349
SomeRandomBloke 8:857444086b3f 350 void initMenu(N3310LCD* lcd)
SomeRandomBloke 8:857444086b3f 351 {
SomeRandomBloke 8:857444086b3f 352 lcd->writeString(MENU_X, MENU_Y, menu_items[0], HIGHLIGHT );
SomeRandomBloke 8:857444086b3f 353
SomeRandomBloke 8:857444086b3f 354 for (int i = 1; i < MENU_ITEMS; i++) {
SomeRandomBloke 8:857444086b3f 355 lcd->writeString(MENU_X, MENU_Y + i, menu_items[i], NORMAL);
SomeRandomBloke 8:857444086b3f 356 }
SomeRandomBloke 8:857444086b3f 357 }
SomeRandomBloke 8:857444086b3f 358
SomeRandomBloke 8:857444086b3f 359 void waitforOKKey(N3310LCD* lcd, Joystick* jstick)
SomeRandomBloke 8:857444086b3f 360 {
SomeRandomBloke 8:857444086b3f 361 lcd->writeString(38, 5, "OK", HIGHLIGHT );
SomeRandomBloke 8:857444086b3f 362
SomeRandomBloke 8:857444086b3f 363 int key = 0xFF;
SomeRandomBloke 8:857444086b3f 364 while (key != CENTER_KEY) {
SomeRandomBloke 8:857444086b3f 365 for (int i = 0; i < NUM_KEYS; i++) {
SomeRandomBloke 8:857444086b3f 366 if (jstick->getKeyState(i) !=0) {
SomeRandomBloke 8:857444086b3f 367 jstick->resetKeyState(i); // reset
SomeRandomBloke 8:857444086b3f 368 if (CENTER_KEY == i) key = CENTER_KEY;
SomeRandomBloke 8:857444086b3f 369 }
SomeRandomBloke 8:857444086b3f 370 }
SomeRandomBloke 8:857444086b3f 371 }
SomeRandomBloke 8:857444086b3f 372 }
SomeRandomBloke 8:857444086b3f 373
SomeRandomBloke 8:857444086b3f 374 // Check if joystick is moved or pressed
SomeRandomBloke 8:857444086b3f 375 uint8_t checkKeypressed( Joystick* jstick)
SomeRandomBloke 8:857444086b3f 376 {
SomeRandomBloke 8:857444086b3f 377 uint8_t key = 0xFF;
SomeRandomBloke 8:857444086b3f 378
SomeRandomBloke 8:857444086b3f 379 for(int i=0; i<NUM_KEYS; i++) {
SomeRandomBloke 8:857444086b3f 380 if (jstick->getKeyState(i) !=0) {
SomeRandomBloke 8:857444086b3f 381 jstick->resetKeyState(i); // reset
SomeRandomBloke 8:857444086b3f 382 if (CENTER_KEY == i) key = CENTER_KEY;
SomeRandomBloke 8:857444086b3f 383 }
SomeRandomBloke 8:857444086b3f 384 }
SomeRandomBloke 8:857444086b3f 385 return key;
SomeRandomBloke 8:857444086b3f 386 }
SomeRandomBloke 8:857444086b3f 387
SomeRandomBloke 8:857444086b3f 388
SomeRandomBloke 8:857444086b3f 389 int main(void)
SomeRandomBloke 8:857444086b3f 390 {
SomeRandomBloke 8:857444086b3f 391 // MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, false, 4);
SomeRandomBloke 8:857444086b3f 392 Joystick jstick(N3310SPIPort::AD0);
SomeRandomBloke 8:857444086b3f 393 N3310LCD lcd(N3310SPIPort::MOSI, N3310SPIPort::MISO, N3310SPIPort::SCK,
SomeRandomBloke 8:857444086b3f 394 N3310SPIPort::CE, N3310SPIPort::DAT_CMD, N3310SPIPort::LCD_RST,
SomeRandomBloke 8:857444086b3f 395 N3310SPIPort::BL_ON);
SomeRandomBloke 8:857444086b3f 396 lcd.init();
SomeRandomBloke 8:857444086b3f 397 lcd.cls();
SomeRandomBloke 8:857444086b3f 398 lcd.backlight(ON);
SomeRandomBloke 8:857444086b3f 399
SomeRandomBloke 8:857444086b3f 400 // demo stuff
SomeRandomBloke 8:857444086b3f 401 // autoDemo(&lcd);
SomeRandomBloke 8:857444086b3f 402
SomeRandomBloke 8:857444086b3f 403 lcd.drawBitmap(0, 0, splash, 84, 48);
SomeRandomBloke 8:857444086b3f 404 wait( 5 );
SomeRandomBloke 8:857444086b3f 405 lcd.cls();
SomeRandomBloke 8:857444086b3f 406
SomeRandomBloke 8:857444086b3f 407 initMenu(&lcd);
SomeRandomBloke 8:857444086b3f 408 int currentMenuItem = 0;
SomeRandomBloke 8:857444086b3f 409 Ticker jstickPoll;
SomeRandomBloke 8:857444086b3f 410 jstickPoll.attach(&jstick, &Joystick::updateADCKey, 0.01); // check ever 10ms
chris 4:367de1084ea9 411
emilmont 5:bf5becf7469c 412 while (true) {
SomeRandomBloke 8:857444086b3f 413 for (int i = 0; i < NUM_KEYS; i++) {
SomeRandomBloke 8:857444086b3f 414 if (jstick.getKeyState(i) != 0) {
SomeRandomBloke 8:857444086b3f 415 jstick.resetKeyState(i); // reset button flag
SomeRandomBloke 8:857444086b3f 416 switch(i) {
SomeRandomBloke 8:857444086b3f 417 case UP_KEY:
SomeRandomBloke 8:857444086b3f 418 // current item to normal display
SomeRandomBloke 8:857444086b3f 419 lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], NORMAL);
SomeRandomBloke 8:857444086b3f 420 currentMenuItem -=1;
SomeRandomBloke 8:857444086b3f 421 if (currentMenuItem <0) currentMenuItem = MENU_ITEMS -1;
SomeRandomBloke 8:857444086b3f 422 // next item to highlight display
SomeRandomBloke 8:857444086b3f 423 lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], HIGHLIGHT);
SomeRandomBloke 8:857444086b3f 424 break;
SomeRandomBloke 8:857444086b3f 425
SomeRandomBloke 8:857444086b3f 426 case DOWN_KEY:
SomeRandomBloke 8:857444086b3f 427 // current item to normal display
SomeRandomBloke 8:857444086b3f 428 lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], NORMAL);
SomeRandomBloke 8:857444086b3f 429 currentMenuItem +=1;
SomeRandomBloke 8:857444086b3f 430 if(currentMenuItem >(MENU_ITEMS - 1)) currentMenuItem = 0;
SomeRandomBloke 8:857444086b3f 431 // next item to highlight display
SomeRandomBloke 8:857444086b3f 432 lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], HIGHLIGHT);
SomeRandomBloke 8:857444086b3f 433 break;
SomeRandomBloke 8:857444086b3f 434
SomeRandomBloke 8:857444086b3f 435 case LEFT_KEY:
SomeRandomBloke 8:857444086b3f 436 initMenu(&lcd);
SomeRandomBloke 8:857444086b3f 437 currentMenuItem = 0;
SomeRandomBloke 8:857444086b3f 438 break;
SomeRandomBloke 8:857444086b3f 439
SomeRandomBloke 8:857444086b3f 440 case RIGHT_KEY:
SomeRandomBloke 8:857444086b3f 441 lcd.cls();
SomeRandomBloke 8:857444086b3f 442 (*menu_funcs[currentMenuItem])(&lcd, &jstick);
SomeRandomBloke 8:857444086b3f 443 waitforOKKey(&lcd, &jstick);
SomeRandomBloke 8:857444086b3f 444 lcd.cls();
SomeRandomBloke 8:857444086b3f 445 initMenu(&lcd);
SomeRandomBloke 8:857444086b3f 446 currentMenuItem = 0;
SomeRandomBloke 8:857444086b3f 447 break;
SomeRandomBloke 8:857444086b3f 448 }
SomeRandomBloke 8:857444086b3f 449 }
SomeRandomBloke 8:857444086b3f 450 }
chris 2:41db78380a6e 451 }
SomeRandomBloke 8:857444086b3f 452
SomeRandomBloke 8:857444086b3f 453 /*
SomeRandomBloke 8:857444086b3f 454 // PwmOut rled(LED_RED);
SomeRandomBloke 8:857444086b3f 455 // PwmOut gled(LED_GREEN);
SomeRandomBloke 8:857444086b3f 456 // PwmOut bled(LED_BLUE);
SomeRandomBloke 8:857444086b3f 457 float x, y, z;
SomeRandomBloke 8:857444086b3f 458 float cValues[3] = { 0.0, 0.0, 0.0 };
SomeRandomBloke 8:857444086b3f 459 int16_t xI, yI, zI;
SomeRandomBloke 8:857444086b3f 460
SomeRandomBloke 8:857444086b3f 461 // Take 100 readings to get stable centre point
SomeRandomBloke 8:857444086b3f 462
SomeRandomBloke 8:857444086b3f 463 for( int i = 0; i < 100; i++ ) {
SomeRandomBloke 8:857444086b3f 464 float axis[3] = { 0.0, 0.0, 0.0 };
SomeRandomBloke 8:857444086b3f 465 acc.getAccAllAxis( axis );
SomeRandomBloke 8:857444086b3f 466 cValues[0] += axis[0];
SomeRandomBloke 8:857444086b3f 467 cValues[1] += axis[1];
SomeRandomBloke 8:857444086b3f 468 cValues[2] += axis[2];
SomeRandomBloke 8:857444086b3f 469 }
SomeRandomBloke 8:857444086b3f 470
SomeRandomBloke 8:857444086b3f 471 cValues[0] /= 100.0;
SomeRandomBloke 8:857444086b3f 472 cValues[1] /= 100.0;
SomeRandomBloke 8:857444086b3f 473 cValues[2] /= 100.0;
SomeRandomBloke 8:857444086b3f 474 pc.printf( "Steady State X: %f Y: %f Z: %f\n", cValues[0], cValues[1], cValues[2] );
SomeRandomBloke 8:857444086b3f 475
SomeRandomBloke 8:857444086b3f 476 while (true) {
SomeRandomBloke 8:857444086b3f 477
SomeRandomBloke 8:857444086b3f 478 x = (acc.getAccX() - cValues[0]) * 100.0;
SomeRandomBloke 8:857444086b3f 479 y = (acc.getAccY() - cValues[1]) * 100.0;
SomeRandomBloke 8:857444086b3f 480 z = (acc.getAccZ() - cValues[2]) * 100.0;
SomeRandomBloke 8:857444086b3f 481 // pc.printf( "X: %f Y: %f Z: %f\n", x, y, z);
SomeRandomBloke 8:857444086b3f 482
SomeRandomBloke 8:857444086b3f 483 xI = (int16_t)(x);
SomeRandomBloke 8:857444086b3f 484 yI = (int16_t)(y);
SomeRandomBloke 8:857444086b3f 485 zI = (int16_t)(z);
SomeRandomBloke 8:857444086b3f 486 pc.printf( "X: %d Y: %d Z: %d\n", xI, yI, zI);
SomeRandomBloke 8:857444086b3f 487 // pc.printf( "X: %f Y: %f Z: %f\n", x*100.0, y*100.0, z*100.0);
SomeRandomBloke 8:857444086b3f 488 // rled = 1.0 - abs(acc.getAccX());
SomeRandomBloke 8:857444086b3f 489 // gled = 1.0 - abs(acc.getAccY());
SomeRandomBloke 8:857444086b3f 490 // bled = 1.0 - abs(acc.getAccZ());
SomeRandomBloke 8:857444086b3f 491 wait(0.5);
SomeRandomBloke 8:857444086b3f 492 }
SomeRandomBloke 8:857444086b3f 493 */
chris 2:41db78380a6e 494 }