Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. #include "IRremote.h"
  2. #include "LedControl.h"
  3. #include <LiquidCrystal.h>
  4.  
  5. /////////
  6. // TODO
  7. // -----
  8. // 1. IR sensor slowdown
  9. // 2. snake segment representation [CHECK]
  10. // 3. memory reservation [CHECK]
  11. // 4. movement
  12. // 5. random placement
  13. // 6. score display
  14. // 7. signal for collecting
  15.  
  16. /////////////////
  17. // Definitions //
  18. /////////////////
  19. // Connections
  20. #define PIN_IR      A0
  21. #define PIN_RS      7
  22. #define PIN_ENABLE  8
  23. #define PIN_D4      9
  24. #define PIN_D5      10
  25. #define PIN_D6      11
  26. #define PIN_D7      12
  27.  
  28. /////////////
  29. // Receiver
  30. #define ARROW_UP    0xFF18E7
  31. #define ARROW_DOWN  0xFF4AB5
  32. #define ARROW_LEFT  0xFF10EF
  33. #define ARROW_RIGHT 0xFF5AA5
  34. #define ARROW_RESET 0x000000 //DEFINE TO OUT
  35.  
  36. //////////
  37. // Field
  38. #define MATRIX_WIDTH 8
  39. #define ARENA_WIDTH 16
  40. #define ARENA_HEIGHT 8
  41. #define INIT_X 0
  42. #define INIT_Y 0
  43.  
  44. //////////
  45. // Snake
  46. #define MAX_LENGTH 128
  47. #define FRAME_TIME 500
  48.  
  49. //////////////////
  50. // Declarations //
  51. //////////////////
  52. // Constants
  53. const LiquidCrystal  SCOREBOARD( PIN_RS, PIN_ENABLE, PIN_D4, PIN_D5, PIN_D6, PIN_D7 );
  54. const LedControl     ARENA( 6, 4, 5, 2 );
  55. const IRrecv         RECEIVER( PIN_IR );
  56. const decode_results RESULTS;
  57.  
  58. ///////////////
  59. // Structures
  60. struct Segment {
  61.  
  62.   uint8_t x;
  63.   uint8_t y;
  64.  
  65. };
  66.  
  67. struct Direction {
  68.  
  69.   uint8_t x;
  70.   uint8_t y;
  71.  
  72. };
  73.  
  74. //////////
  75. // Snake
  76. uint8_t posX;
  77. uint8_t posY;
  78. uint8_t matrix;
  79. Direction direction;
  80.  
  81. Segment* head;
  82. uint8_t length;
  83.  
  84. unsigned long timestamp;
  85.  
  86. ////////////
  87. // Methods
  88. void reserve() {
  89.  
  90.   /////////
  91.   // Head
  92.   head = new Segment{ INIT_X, INIT_Y };
  93.   length = 1;
  94.  
  95.   /////////
  96.   // Body
  97.   for( uint8_t i = 0; i < MAX_LENGTH - 1; i++ ) {
  98.  
  99.     new Segment{ 0, 0 };
  100.    
  101.   }
  102.  
  103. }
  104.  
  105. void move() {
  106.  
  107.   uint8_t oldX;
  108.   uint8_t oldY;
  109.   bool head = true;
  110.  
  111.   for( uint8_t i = 0; i < length; i++ ) {
  112.  
  113.     /////////
  114.     // Fetch
  115.     Segment* segment = head + i * ( sizeof( struct Segment ) * 8 ); //TODO CHECK DOES SIZEOF RETURN 16BITS OR 2BYTES
  116.  
  117.     if( head ) {
  118.  
  119.       //////////
  120.       // Store
  121.       oldX = (*segment).x;
  122.       oldY = (*segment).y;
  123.  
  124.       ////////
  125.       // Set
  126.       (*segment).x += direction.x;
  127.       (*segment).y += direction.y;
  128.      
  129.     } else {
  130.  
  131.       //////////
  132.       // Current
  133.       uint8_t currentX = (*segment).x;
  134.       uint8_t currentY = (*segment).y;
  135.  
  136.       ////////
  137.       // Set
  138.       (*segment).x = oldX;
  139.       (*segment).y = oldY;
  140.  
  141.       ////////////
  142.       // Store/Replace
  143.       oldX = currentX;
  144.       oldY = currentY;
  145.      
  146.     }
  147.  
  148.     head = false;
  149.    
  150.   }
  151.  
  152. }
  153.  
  154. void setup() {
  155.  
  156.   ////////////////////
  157.   // Initialization //
  158.   ////////////////////
  159.   // Arena
  160.   ARENA.shutdown( 0, false );
  161.   ARENA.shutdown( 1, false );
  162.   ARENA.clearDisplay( 0 );
  163.   ARENA.clearDisplay( 1 );
  164.  
  165.   ///////////////
  166.   // Scoreboard  
  167.   SCOREBOARD.begin( 16, 2 );
  168.  
  169.   /////////////
  170.   // Receiver
  171.   RECEIVER.enableIRIn();
  172.   RECEIVER.blink13( true );
  173.  
  174.   //////////
  175.   // Debug
  176.   Serial.begin( 9600 );
  177.  
  178.   //////////
  179.   // Logic
  180.   //////////
  181.   // Head
  182.   posX = INIT_X;
  183.   posY = INIT_Y;
  184.   matrix = 0;
  185.   direction = { 0, 0 };
  186.  
  187.   /////////
  188.   // Body
  189.   reserve();
  190.  
  191.   ///////////
  192.   // Timing
  193.   timestamp = millis();
  194.  
  195. }
  196.  
  197. void loop() {
  198.  
  199.   //Decode
  200.   if ( RECEIVER.decode( &RESULTS ) ) {
  201.    
  202.     Serial.println( RESULTS.value, HEX );
  203.     RECEIVER.resume();
  204.        
  205.   }
  206.  
  207.   //Control
  208.   switch( RESULTS.value ) {
  209.  
  210.     case ARROW_UP: {
  211.  
  212.       //direction = ( direction != ARROW_DOWN ) ? ARROW_UP : ARROW_DOWN;
  213.      
  214.     } break;
  215.     case ARROW_DOWN: {
  216.  
  217.       //direction = ( direction != ARROW_UP ) ? ARROW_DOWN : ARROW_UP;
  218.      
  219.     } break;
  220.     case ARROW_RIGHT: {
  221.  
  222.       //direction = ( direction != ARROW_LEFT ) ? ARROW_RIGHT : ARROW_LEFT;
  223.      
  224.     } break;
  225.     case ARROW_LEFT: {
  226.  
  227.       //direction = ( direction != ARROW_RIGHT ) ? ARROW_LEFT : ARROW_RIGHT;
  228.      
  229.     } break;
  230.    
  231.   }
  232.  
  233.   //MOVEMENT
  234.   unsigned long eta = millis();
  235.  
  236.   if ( eta - timestamp >= FRAME_TIME ) {
  237.  
  238.     //switch( direction ) {
  239.  
  240.       //case ARROW_UP: moveY( -1 ); break;
  241.       //case ARROW_DOWN: moveY( 1 ); break;
  242.       //case ARROW_RIGHT: moveX( -1 ); break;
  243.       //case ARROW_LEFT: moveX( 1 ); break;
  244.    
  245.     //}
  246.  
  247.     timestamp = eta;
  248.      
  249.   }
  250.  
  251.   //Display
  252.   //TODO REMOVE
  253.   ARENA.clearDisplay( 0 );
  254.   ARENA.clearDisplay( 1 );
  255.  
  256.   ARENA.setLed( matrix, posX % MATRIX_WIDTH, posY, true );
  257.  
  258. }
  259.  
  260. void moveX( int8_t x ) {
  261.  
  262.   int8_t dx = x % ARENA_WIDTH;
  263.   int8_t nx = posX + dx;
  264.  
  265.   if ( nx < 0 ) {
  266.  
  267.     posX = ARENA_WIDTH + nx;
  268.    
  269.   } else if ( nx >= ARENA_WIDTH ) {
  270.  
  271.     posX = nx - ARENA_WIDTH;
  272.    
  273.   } else {
  274.  
  275.     posX = nx;
  276.    
  277.   }
  278.  
  279.   matrix = posX / MATRIX_WIDTH;
  280.  
  281. }
  282.  
  283. void moveY( int8_t y ) {
  284.  
  285.   int8_t dy = y % ARENA_HEIGHT;
  286.   int8_t ny = posY + dy;
  287.  
  288.   if ( ny < 0 ) {
  289.  
  290.     posY = ARENA_HEIGHT + ny;
  291.    
  292.   } else if ( ny >= ARENA_HEIGHT ) {
  293.  
  294.     posY = ny - ARENA_HEIGHT;
  295.    
  296.   } else {
  297.  
  298.     posY = ny;
  299.    
  300.   }
  301.  
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement