Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #line 1 "sketch_dec20b.ino"
- #define PIN_RST 8
- #define PIN_CLK 9
- #define PIN_X0 12
- #define PIN_X1 11
- #define PIN_X2 10
- #define PIN_LEFT 7
- #define PIN_RIGHT 6
- #include "Arduino.h"
- void clearBuffer();
- inline void writeN(uint8_t n);
- void printBuffer();
- void setup();
- void renderSnake();
- void getNewCoords(coords *coords);
- void moveSnake();
- void loop();
- #line 10
- uint8_t framebuffer[3] = { 0, 0, 0 };
- void clearBuffer() {
- for(uint8_t x = 0; x < 3; x++) {
- framebuffer[x] = 0;
- }
- }
- inline void writeN(uint8_t n) {
- digitalWrite(PIN_RST, 1);
- digitalWrite(PIN_RST, 0);
- for(; n > 0; n--) {
- digitalWrite(PIN_CLK, 0);
- digitalWrite(PIN_CLK, 1);
- }
- }
- void printBuffer() {
- digitalWrite(PIN_X1, 0);
- digitalWrite(PIN_X2, 0);
- digitalWrite(PIN_X0, 0);
- writeN(framebuffer[0]);
- digitalWrite(PIN_X0, 1);
- delay(7);
- digitalWrite(PIN_X0, 0);
- writeN(framebuffer[1]);
- digitalWrite(PIN_X1, 1);
- delay(7);
- digitalWrite(PIN_X1, 0);
- writeN(framebuffer[2]);
- digitalWrite(PIN_X2, 1);
- delay(7);
- }
- void setup() {
- pinMode(PIN_RST, OUTPUT);
- pinMode(PIN_CLK, OUTPUT);
- pinMode(PIN_X0, OUTPUT);
- pinMode(PIN_X1, OUTPUT);
- pinMode(PIN_X2, OUTPUT);
- digitalWrite(PIN_RST, 0);
- digitalWrite(PIN_CLK, 0);
- }
- #ifdef SNAKE
- #define SNAKE_LENGTH 2
- typedef struct {
- uint8_t x;
- uint8_t y;
- } coords;
- typedef enum {
- up, right, down, left
- } direction;
- direction snake_direction = down;
- coords snake[SNAKE_LENGTH] = {
- { 0, 0 },
- { 0, 1 }
- };
- void renderSnake() {
- for(uint8_t i = 0; i < SNAKE_LENGTH; i++) {
- framebuffer[snake[i].x] |= 1 << snake[i].y;
- }
- }
- void getNewCoords(coords *coords) {
- if(snake_direction == up) {
- coords->y --;
- } else if(snake_direction == down) {
- coords->y ++;
- } else if(snake_direction == right) {
- coords->x ++;
- } else if(snake_direction == left) {
- coords->x --;
- }
- if(coords->y == 5)
- coords->y = 0;
- else if(coords->y == 255)
- coords->y = 4;
- if(coords->x == 3)
- coords->x = 0;
- else if(coords->x == 255)
- coords->x = 2;
- }
- void moveSnake() {
- coords tmp;
- for(uint8_t i = 0; i < SNAKE_LENGTH - 1; i++) {
- snake[i] = snake[i + 1];
- }
- tmp = snake[SNAKE_LENGTH - 2];
- getNewCoords(&tmp);
- snake[SNAKE_LENGTH - 1] = tmp;
- }
- long next_check_tick = millis();
- long next_move_tick = millis();
- void loop() {
- if(millis() > next_check_tick && digitalRead(PIN_LEFT) == LOW) {
- if(snake_direction == up) snake_direction = left;
- else if(snake_direction == left) snake_direction = down;
- else if(snake_direction == down) snake_direction = right;
- else snake_direction = up;
- next_check_tick = millis() + 250;
- } else if(millis() > next_check_tick && digitalRead(PIN_RIGHT) == LOW) {
- if(snake_direction == up) snake_direction = right;
- else if(snake_direction == left) snake_direction = up;
- else if(snake_direction == down) snake_direction = left;
- else snake_direction = down;
- next_check_tick = millis() + 250;
- }
- if(millis() > next_move_tick) {
- moveSnake();
- clearBuffer();
- renderSnake();
- next_move_tick = millis() + 250;
- }
- printBuffer();
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement