137 lines
2.5 KiB
Arduino
137 lines
2.5 KiB
Arduino
|
#include <Arduino.h>
|
||
|
#include <TFT_eSPI.h>
|
||
|
#include <SPI.h>
|
||
|
|
||
|
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
|
||
|
// pins are defined un User_Setup.h
|
||
|
|
||
|
const int BTN1_PIN = 25;
|
||
|
volatile bool btn1_pressed = false;
|
||
|
const int BTN2_PIN = 33;
|
||
|
volatile bool btn2_pressed = false;
|
||
|
|
||
|
// forward declarations
|
||
|
void IRAM_ATTR isr1();
|
||
|
void IRAM_ATTR isr2();
|
||
|
|
||
|
void setup() {
|
||
|
tft.init(); // initialize a ST7735S chip
|
||
|
|
||
|
// set up the buttons and the interrupts
|
||
|
pinMode(BTN1_PIN, INPUT_PULLUP);
|
||
|
attachInterrupt(BTN1_PIN, isr1, FALLING);
|
||
|
pinMode(BTN2_PIN, INPUT_PULLUP);
|
||
|
attachInterrupt(BTN2_PIN, isr2, FALLING);
|
||
|
|
||
|
init_board();
|
||
|
|
||
|
delay(100);
|
||
|
}
|
||
|
|
||
|
#define GRID_WIDTH 12
|
||
|
#define GRID_HEIGHT 18
|
||
|
#define BLOCK_SIZE 10
|
||
|
#define BLOCK_MARGIN 2
|
||
|
|
||
|
#define BLOCK_WIDTH 20
|
||
|
#define BLOCK_HEIGHT 30
|
||
|
|
||
|
uint32_t main_board[GRID_WIDTH][GRID_HEIGHT], offboard[GRID_WIDTH][GRID_HEIGHT];
|
||
|
|
||
|
void
|
||
|
init_board()
|
||
|
{
|
||
|
tft.fillScreen(TFT_BLACK);
|
||
|
|
||
|
for(int i = 0; i < GRID_WIDTH; i++)
|
||
|
for(int j = 0; j < GRID_HEIGHT; j++)
|
||
|
{
|
||
|
main_board[i][j] = 0;
|
||
|
offboard[i][j] = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void
|
||
|
draw_board()
|
||
|
{
|
||
|
for(int i = 0; i < GRID_WIDTH; i++)
|
||
|
for(int j = 0; j < GRID_HEIGHT; j++)
|
||
|
{
|
||
|
if (main_board[i][j] == offboard[i][j]) continue; // no need to redraw
|
||
|
offboard[i][j] = main_board[i][j];
|
||
|
tft.fillRect(i*BLOCK_SIZE+BLOCK_MARGIN,j*BLOCK_SIZE+BLOCK_MARGIN,
|
||
|
BLOCK_SIZE-BLOCK_MARGIN,BLOCK_SIZE-BLOCK_MARGIN,
|
||
|
main_board[i][j]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
uint8_t blocks[2][4] =
|
||
|
{
|
||
|
{0,1,2,3},{0,1,4,5}
|
||
|
};
|
||
|
|
||
|
|
||
|
// Draw a block of type TYPE with color COLOR, starting at coordinates X, Y.
|
||
|
// Updates the main_board directly
|
||
|
void
|
||
|
draw_block(int x, int y, int type, int color)
|
||
|
{
|
||
|
for (int i = 0; i < 4; i++)
|
||
|
{
|
||
|
int v = blocks[type][i];
|
||
|
int dx = v%4;
|
||
|
int dy = v/4;
|
||
|
main_board[x+dx][y+dy] = color;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int px = 0, cx = 0;
|
||
|
int py = 0, cy = 0;
|
||
|
int cblock_ty = 0;
|
||
|
void loop() {
|
||
|
// if (dy == 0) {
|
||
|
// dx = map(esp_random(), 0, 0xFFFFFFFF, 0, 128/5);
|
||
|
// }
|
||
|
cy++;
|
||
|
if (cy >= GRID_HEIGHT) {
|
||
|
cy = 0;
|
||
|
}
|
||
|
|
||
|
if (btn1_pressed)
|
||
|
{
|
||
|
btn1_pressed = false;
|
||
|
cx--;
|
||
|
}
|
||
|
if (btn2_pressed)
|
||
|
{
|
||
|
btn2_pressed = false;
|
||
|
cx++;
|
||
|
}
|
||
|
|
||
|
// clear the previous block
|
||
|
if (cy != py || cx != px)
|
||
|
{
|
||
|
draw_block(px, py, cblock_ty, TFT_BLACK);
|
||
|
}
|
||
|
draw_block(cx, cy, cblock_ty, TFT_RED);
|
||
|
|
||
|
draw_board();
|
||
|
|
||
|
delay(350);
|
||
|
|
||
|
px = cx;
|
||
|
py = cy;
|
||
|
}
|
||
|
|
||
|
void IRAM_ATTR
|
||
|
isr1()
|
||
|
{
|
||
|
btn1_pressed = true;
|
||
|
}
|
||
|
|
||
|
void IRAM_ATTR
|
||
|
isr2()
|
||
|
{
|
||
|
btn2_pressed = true;
|
||
|
}
|