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