From afeabd81e7d9e2da129ad2b7f663dff1ef65b510 Mon Sep 17 00:00:00 2001 From: Dan Frumin Date: Sun, 1 Sep 2019 16:36:59 +0200 Subject: [PATCH] initial import --- Makefile | 61 ++++++++++++++++++++++++ tetris.ino | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 Makefile create mode 100644 tetris.ino diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d0c2c36 --- /dev/null +++ b/Makefile @@ -0,0 +1,61 @@ +ARDUINO_DIR = /home/dan/projects/arduino-1.8.9 +ARDUINO_PLATFORM_LIB_PATH = /home/dan/.arduino15/packages/esp32/hardware/esp32/1.0.2/libraries +USER_LIB_PATH = /home/dan/Arduino/libraries +ARDUINO = $(ARDUINO_DIR)/arduino +CTAGS_PATH = $(ARDUINO_DIR)/tools-builder/ctags/5.8-arduino11/ctags + +# ctags command: append, flags unsort (as will be sorted after) and specify filename +# use ETAGS format +CTAGS_CMD = $(CTAGS_PATH) -auef +TAGS_FILE = TAGS + +all: + $(ARDUINO) --upload tetris.ino + + +# Local sources + +LOCAL_C_SRCS ?= $(wildcard *.c) +LOCAL_CPP_SRCS ?= $(wildcard *.cpp) +LOCAL_CC_SRCS ?= $(wildcard *.cc) +LOCAL_PDE_SRCS ?= $(wildcard *.pde) +LOCAL_INO_SRCS ?= $(wildcard *.ino) +LOCAL_AS_SRCS ?= $(wildcard *.S) +LOCAL_SRCS = $(LOCAL_C_SRCS) $(LOCAL_CPP_SRCS) \ + $(LOCAL_CC_SRCS) $(LOCAL_PDE_SRCS) \ + $(LOCAL_INO_SRCS) $(LOCAL_AS_SRCS) + + +# Determine Arduino libs that are used +# first do platform-specific libraries +# then "core" libraries +# then sketchbook libraries and user-installed libraries +ifndef ARDUINO_LIBS + ARDUINO_LIBS_NAMES1 = $(filter $(notdir $(wildcard $(ARDUINO_PLATFORM_LIB_PATH)/*)), \ + $(shell sed -ne 's/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p' $(LOCAL_SRCS))) + ARDUINO_LIBS += $(foreach lib,$(ARDUINO_LIBS_NAMES1),$(ARDUINO_PLATFORM_LIB_PATH)/$(lib)) + + ARDUINO_LIBS_NAMES2 = $(filter $(notdir $(wildcard $(ARDUINO_DIR)/libraries/*)), \ + $(shell sed -ne 's/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p' $(LOCAL_SRCS))) + ARDUINO_LIBS += $(foreach lib,$(ARDUINO_LIBS_NAMES2),$(ARDUINO_DIR)/libraries/$(lib)) + +# ARDUINO_LIBS_NAMES = $(filter $(notdir $(wildcard $(ARDUINO_SKETCHBOOK)/libraries/*)), \ +# $(shell sed -ne 's/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p' $(LOCAL_SRCS))) +# ARDUINO_LIBS += $(foreach lib,$(ARDUINO_LIBS_NAMES),$(ARDUINO_SKETCHBOOK)/libraries/$(lib)) + + ARDUINO_LIBS_NAMES3 = $(filter $(notdir $(wildcard $(USER_LIB_PATH)/*)), \ + $(shell sed -ne 's/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p' $(LOCAL_SRCS))) + ARDUINO_LIBS += $(foreach lib,$(ARDUINO_LIBS_NAMES3),$(USER_LIB_PATH)/$(lib)) + +endif + +.PHONY: tags +tags: + rm -f $(TAGS_FILE) + @echo "Generating tags for local sources: " + $(CTAGS_CMD) $(TAGS_FILE) --langmap=c++:+.ino.pde $(LOCAL_SRCS) +ifneq ($(words $(ARDUINO_LIBS)), 0) + @echo "Generating tags for project libraries: " + $(CTAGS_CMD) $(TAGS_FILE) -R $(ARDUINO_LIBS) +endif +#sort $(TAGS_FILE) -o $(TAGS_FILE) diff --git a/tetris.ino b/tetris.ino new file mode 100644 index 0000000..568edd4 --- /dev/null +++ b/tetris.ino @@ -0,0 +1,136 @@ +#include +#include +#include + +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; +}