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