Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "IRremote.h"
- #include "LedControl.h"
- #include <LiquidCrystal.h>
- /////////
- // TODO
- // -----
- // 1. IR sensor slowdown
- // 2. snake segment representation [CHECK]
- // 3. memory reservation [CHECK]
- // 4. movement
- // 5. random placement
- // 6. score display
- // 7. signal for collecting
- /////////////////
- // Definitions //
- /////////////////
- // Connections
- #define PIN_IR A0
- #define PIN_RS 7
- #define PIN_ENABLE 8
- #define PIN_D4 9
- #define PIN_D5 10
- #define PIN_D6 11
- #define PIN_D7 12
- /////////////
- // Receiver
- #define ARROW_UP 0xFF18E7
- #define ARROW_DOWN 0xFF4AB5
- #define ARROW_LEFT 0xFF10EF
- #define ARROW_RIGHT 0xFF5AA5
- #define ARROW_RESET 0x000000 //DEFINE TO OUT
- //////////
- // Field
- #define MATRIX_WIDTH 8
- #define ARENA_WIDTH 16
- #define ARENA_HEIGHT 8
- #define INIT_X 0
- #define INIT_Y 0
- //////////
- // Snake
- #define MAX_LENGTH 128
- #define FRAME_TIME 500
- //////////////////
- // Declarations //
- //////////////////
- // Constants
- const LiquidCrystal SCOREBOARD( PIN_RS, PIN_ENABLE, PIN_D4, PIN_D5, PIN_D6, PIN_D7 );
- const LedControl ARENA( 6, 4, 5, 2 );
- const IRrecv RECEIVER( PIN_IR );
- const decode_results RESULTS;
- ///////////////
- // Structures
- struct Segment {
- uint8_t x;
- uint8_t y;
- };
- struct Direction {
- uint8_t x;
- uint8_t y;
- };
- //////////
- // Snake
- uint8_t posX;
- uint8_t posY;
- uint8_t matrix;
- Direction direction;
- Segment* head;
- uint8_t length;
- unsigned long timestamp;
- ////////////
- // Methods
- void reserve() {
- /////////
- // Head
- head = new Segment{ INIT_X, INIT_Y };
- length = 1;
- /////////
- // Body
- for( uint8_t i = 0; i < MAX_LENGTH - 1; i++ ) {
- new Segment{ 0, 0 };
- }
- }
- void move() {
- uint8_t oldX;
- uint8_t oldY;
- bool head = true;
- for( uint8_t i = 0; i < length; i++ ) {
- /////////
- // Fetch
- Segment* segment = head + i * ( sizeof( struct Segment ) * 8 ); //TODO CHECK DOES SIZEOF RETURN 16BITS OR 2BYTES
- if( head ) {
- //////////
- // Store
- oldX = (*segment).x;
- oldY = (*segment).y;
- ////////
- // Set
- (*segment).x += direction.x;
- (*segment).y += direction.y;
- } else {
- //////////
- // Current
- uint8_t currentX = (*segment).x;
- uint8_t currentY = (*segment).y;
- ////////
- // Set
- (*segment).x = oldX;
- (*segment).y = oldY;
- ////////////
- // Store/Replace
- oldX = currentX;
- oldY = currentY;
- }
- head = false;
- }
- }
- void setup() {
- ////////////////////
- // Initialization //
- ////////////////////
- // Arena
- ARENA.shutdown( 0, false );
- ARENA.shutdown( 1, false );
- ARENA.clearDisplay( 0 );
- ARENA.clearDisplay( 1 );
- ///////////////
- // Scoreboard
- SCOREBOARD.begin( 16, 2 );
- /////////////
- // Receiver
- RECEIVER.enableIRIn();
- RECEIVER.blink13( true );
- //////////
- // Debug
- Serial.begin( 9600 );
- //////////
- // Logic
- //////////
- // Head
- posX = INIT_X;
- posY = INIT_Y;
- matrix = 0;
- direction = { 0, 0 };
- /////////
- // Body
- reserve();
- ///////////
- // Timing
- timestamp = millis();
- }
- void loop() {
- //Decode
- if ( RECEIVER.decode( &RESULTS ) ) {
- Serial.println( RESULTS.value, HEX );
- RECEIVER.resume();
- }
- //Control
- switch( RESULTS.value ) {
- case ARROW_UP: {
- //direction = ( direction != ARROW_DOWN ) ? ARROW_UP : ARROW_DOWN;
- } break;
- case ARROW_DOWN: {
- //direction = ( direction != ARROW_UP ) ? ARROW_DOWN : ARROW_UP;
- } break;
- case ARROW_RIGHT: {
- //direction = ( direction != ARROW_LEFT ) ? ARROW_RIGHT : ARROW_LEFT;
- } break;
- case ARROW_LEFT: {
- //direction = ( direction != ARROW_RIGHT ) ? ARROW_LEFT : ARROW_RIGHT;
- } break;
- }
- //MOVEMENT
- unsigned long eta = millis();
- if ( eta - timestamp >= FRAME_TIME ) {
- //switch( direction ) {
- //case ARROW_UP: moveY( -1 ); break;
- //case ARROW_DOWN: moveY( 1 ); break;
- //case ARROW_RIGHT: moveX( -1 ); break;
- //case ARROW_LEFT: moveX( 1 ); break;
- //}
- timestamp = eta;
- }
- //Display
- //TODO REMOVE
- ARENA.clearDisplay( 0 );
- ARENA.clearDisplay( 1 );
- ARENA.setLed( matrix, posX % MATRIX_WIDTH, posY, true );
- }
- void moveX( int8_t x ) {
- int8_t dx = x % ARENA_WIDTH;
- int8_t nx = posX + dx;
- if ( nx < 0 ) {
- posX = ARENA_WIDTH + nx;
- } else if ( nx >= ARENA_WIDTH ) {
- posX = nx - ARENA_WIDTH;
- } else {
- posX = nx;
- }
- matrix = posX / MATRIX_WIDTH;
- }
- void moveY( int8_t y ) {
- int8_t dy = y % ARENA_HEIGHT;
- int8_t ny = posY + dy;
- if ( ny < 0 ) {
- posY = ARENA_HEIGHT + ny;
- } else if ( ny >= ARENA_HEIGHT ) {
- posY = ny - ARENA_HEIGHT;
- } else {
- posY = ny;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement