Advertisement
baldengineer

mrvoltz real code

Dec 20th, 2014
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.36 KB | None | 0 0
  1. #line 1 "sketch_dec20b.ino"
  2.  
  3. #define PIN_RST 8
  4. #define PIN_CLK 9
  5. #define PIN_X0 12
  6. #define PIN_X1 11
  7. #define PIN_X2 10
  8. #define PIN_LEFT 7
  9. #define PIN_RIGHT 6
  10.  
  11. #include "Arduino.h"
  12. void clearBuffer();
  13. inline void writeN(uint8_t n);
  14. void printBuffer();
  15. void setup();
  16. void renderSnake();
  17. void getNewCoords(coords *coords);
  18. void moveSnake();
  19. void loop();
  20. #line 10
  21. uint8_t framebuffer[3] = { 0, 0, 0 };
  22.  
  23. void clearBuffer() {
  24.   for(uint8_t x = 0; x < 3; x++) {
  25.     framebuffer[x] = 0;
  26.   }
  27. }
  28. inline void writeN(uint8_t n) {
  29.   digitalWrite(PIN_RST, 1);
  30.   digitalWrite(PIN_RST, 0);
  31.   for(; n > 0; n--) {
  32.       digitalWrite(PIN_CLK, 0);
  33.       digitalWrite(PIN_CLK, 1);
  34.   }
  35. }
  36. void printBuffer() {
  37.   digitalWrite(PIN_X1, 0);
  38.   digitalWrite(PIN_X2, 0);
  39.   digitalWrite(PIN_X0, 0);
  40.   writeN(framebuffer[0]);
  41.   digitalWrite(PIN_X0, 1);
  42.   delay(7);
  43.   digitalWrite(PIN_X0, 0);
  44.   writeN(framebuffer[1]);
  45.   digitalWrite(PIN_X1, 1);
  46.   delay(7);
  47.   digitalWrite(PIN_X1, 0);
  48.   writeN(framebuffer[2]);
  49.   digitalWrite(PIN_X2, 1);
  50.   delay(7);
  51. }
  52. void setup() {
  53.   pinMode(PIN_RST, OUTPUT);
  54.   pinMode(PIN_CLK, OUTPUT);
  55.   pinMode(PIN_X0, OUTPUT);
  56.   pinMode(PIN_X1, OUTPUT);
  57.   pinMode(PIN_X2, OUTPUT);
  58.  
  59.   digitalWrite(PIN_RST, 0);
  60.   digitalWrite(PIN_CLK, 0);
  61. }
  62.  
  63. #ifdef SNAKE
  64.   #define SNAKE_LENGTH 2
  65.  
  66.   typedef struct {
  67.     uint8_t x;
  68.     uint8_t y;
  69.   } coords;
  70.   typedef enum {
  71.     up, right, down, left
  72.   } direction;
  73.  
  74.   direction snake_direction = down;
  75.   coords snake[SNAKE_LENGTH] = {
  76.     { 0, 0 },
  77.     { 0, 1 }
  78.   };
  79.  
  80.   void renderSnake() {
  81.     for(uint8_t i = 0; i < SNAKE_LENGTH; i++) {
  82.       framebuffer[snake[i].x] |= 1 << snake[i].y;
  83.     }
  84.   }
  85.   void getNewCoords(coords *coords) {
  86.     if(snake_direction == up) {
  87.       coords->y --;
  88.     } else if(snake_direction == down) {
  89.       coords->y ++;
  90.     } else if(snake_direction == right) {
  91.       coords->x ++;
  92.     } else if(snake_direction == left) {
  93.       coords->x --;
  94.     }
  95.  
  96.     if(coords->y == 5)
  97.       coords->y = 0;
  98.     else if(coords->y == 255)
  99.       coords->y = 4;
  100.    
  101.     if(coords->x == 3)
  102.       coords->x = 0;
  103.     else if(coords->x == 255)
  104.       coords->x = 2;
  105.   }
  106.   void moveSnake() {
  107.     coords tmp;
  108.    
  109.     for(uint8_t i = 0; i < SNAKE_LENGTH - 1; i++) {
  110.       snake[i] = snake[i + 1];
  111.     }
  112.  
  113.     tmp = snake[SNAKE_LENGTH - 2];
  114.     getNewCoords(&tmp);
  115.     snake[SNAKE_LENGTH - 1] = tmp;
  116.   }
  117.  
  118.   long next_check_tick = millis();
  119.   long next_move_tick = millis();
  120.   void loop() {
  121.     if(millis() > next_check_tick && digitalRead(PIN_LEFT) == LOW) {
  122.       if(snake_direction == up) snake_direction = left;
  123.       else if(snake_direction == left) snake_direction = down;
  124.       else if(snake_direction == down) snake_direction = right;
  125.       else snake_direction = up;
  126.    
  127.       next_check_tick = millis() + 250;
  128.     } else if(millis() > next_check_tick && digitalRead(PIN_RIGHT) == LOW) {
  129.       if(snake_direction == up) snake_direction = right;
  130.       else if(snake_direction == left) snake_direction = up;
  131.       else if(snake_direction == down) snake_direction = left;
  132.       else snake_direction = down;
  133.    
  134.       next_check_tick = millis() + 250;
  135.     }
  136.  
  137.     if(millis() > next_move_tick) {
  138.       moveSnake();
  139.       clearBuffer();
  140.       renderSnake();
  141.    
  142.       next_move_tick = millis() + 250;
  143.     }
  144.     printBuffer();
  145.   }
  146. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement