Compare commits
2 Commits
1959a3f578
...
game_ctrl_
Author | SHA1 | Date | |
---|---|---|---|
f4b614bf0f | |||
a7ea698f9b |
@ -11,7 +11,7 @@ CFLAGS=-Wall -mmcu=$(MCU) -std=gnu99 -I $(TOOLCHAIN_PREFIX)/include -O1 -g0
|
|||||||
|
|
||||||
LDFLAGS=-mmcu=$(MCU) -L $(TOOLCHAIN_PREFIX)/include
|
LDFLAGS=-mmcu=$(MCU) -L $(TOOLCHAIN_PREFIX)/include
|
||||||
|
|
||||||
$(ARTIFACT).elf: main.o led.o time.o PontCoopScheduler.o displayDriver.o canvas.o shapes.o shape_i.o shape_j.o shape_l.o shape_o.o shape_s.o shape_t.o shape_z.o game.o
|
$(ARTIFACT).elf: main.o led.o time.o PontCoopScheduler.o displayDriver.o canvas.o shapes.o shape_i.o shape_j.o shape_l.o shape_o.o shape_s.o shape_t.o shape_z.o game.o buttons.o
|
||||||
$(CC) -o $@ $(LDFLAGS) $^
|
$(CC) -o $@ $(LDFLAGS) $^
|
||||||
$(OBJDUMP) -D $(ARTIFACT).elf > $(ARTIFACT).txt
|
$(OBJDUMP) -D $(ARTIFACT).elf > $(ARTIFACT).txt
|
||||||
|
|
||||||
|
84
game-ctrl/buttons.c
Normal file
84
game-ctrl/buttons.c
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
#include "stddef.h"
|
||||||
|
#include "stdint.h"
|
||||||
|
|
||||||
|
#include "buttons.h"
|
||||||
|
#include "PontCoopScheduler.h"
|
||||||
|
#include "shapes.h"
|
||||||
|
#include "canvas.h"
|
||||||
|
#include "led.h"
|
||||||
|
|
||||||
|
|
||||||
|
// TEST CODE
|
||||||
|
uint16_t counter;
|
||||||
|
|
||||||
|
|
||||||
|
static uint8_t buttonsMoveLeftPressed() {
|
||||||
|
// -----------------------
|
||||||
|
// TEST CODE
|
||||||
|
//if (counter == 25) {
|
||||||
|
// ledGreenToggle();
|
||||||
|
// return 1;
|
||||||
|
//}
|
||||||
|
// -----------------------
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t buttonsMoveRightPressed() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t buttonsRotateLeftPressed() {
|
||||||
|
// -----------------------
|
||||||
|
// TEST CODE
|
||||||
|
if (counter == 35) {
|
||||||
|
ledGreenToggle();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// -----------------------
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t buttonsRotateRightPressed() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void buttonsExec(void *handle) {
|
||||||
|
if (stone.shape == e_ShapeInvalid) {
|
||||||
|
// don't do anything, the stone has not been initialized
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TEST CODE
|
||||||
|
counter++;
|
||||||
|
|
||||||
|
uint8_t buttonPressed = 0;
|
||||||
|
|
||||||
|
if (buttonsMoveLeftPressed()) {
|
||||||
|
stoneMoveLeft();
|
||||||
|
buttonPressed = 1;
|
||||||
|
}
|
||||||
|
if (buttonsMoveRightPressed()) {
|
||||||
|
stoneMoveRight();
|
||||||
|
buttonPressed = 1;
|
||||||
|
}
|
||||||
|
if (buttonsRotateLeftPressed()) {
|
||||||
|
stoneRotateLeft();
|
||||||
|
buttonPressed = 1;
|
||||||
|
}
|
||||||
|
if (buttonsRotateRightPressed()) {
|
||||||
|
stoneRotateRight();
|
||||||
|
buttonPressed = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buttonPressed == 1) {
|
||||||
|
canvasShow();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void buttonsInit() {
|
||||||
|
// TEST CODE
|
||||||
|
counter = 0;
|
||||||
|
|
||||||
|
schAdd(buttonsExec, NULL, 0, 100);
|
||||||
|
}
|
||||||
|
|
7
game-ctrl/buttons.h
Normal file
7
game-ctrl/buttons.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#ifndef _BUTTONS_H_
|
||||||
|
#define _BUTTONS_H_
|
||||||
|
|
||||||
|
void buttonsInit();
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _BUTTONS_H_
|
@ -53,3 +53,9 @@ uint8_t canvasIsRowFilled(uint8_t row) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t canvasIsPixelFree(uint8_t column, uint8_t row) {
|
||||||
|
return (*((canvas.canvas) + (row * canvas.width + column)) == 0) &&
|
||||||
|
(column < canvas.width) &&
|
||||||
|
(row < canvas.height);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ void canvasClear();
|
|||||||
void canvasSetAll(uint8_t color);
|
void canvasSetAll(uint8_t color);
|
||||||
void canvasShow();
|
void canvasShow();
|
||||||
void canvasSetPixel(uint8_t column, uint8_t row, uint8_t color);
|
void canvasSetPixel(uint8_t column, uint8_t row, uint8_t color);
|
||||||
|
uint8_t canvasIsPixelFree(uint8_t column, uint8_t row);
|
||||||
void canvasWipeRow(uint8_t row);
|
void canvasWipeRow(uint8_t row);
|
||||||
uint8_t canvasIsRowFilled(uint8_t row);
|
uint8_t canvasIsRowFilled(uint8_t row);
|
||||||
|
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
#ifndef _COLORS_H_
|
|
||||||
#define _COLORS_H_
|
|
||||||
|
|
||||||
|
|
||||||
#define _off 0x00
|
|
||||||
#define _blue 0x01
|
|
||||||
#define _green 0x02
|
|
||||||
#define _orange 0x03
|
|
||||||
#define _rose 0x04
|
|
||||||
#define _magenta 0x05
|
|
||||||
#define _violet 0x06
|
|
||||||
#define _azure 0x07
|
|
||||||
#define _cyan 0x08
|
|
||||||
#define _springgreen 0x09
|
|
||||||
#define _chartreuse 0x0a
|
|
||||||
#define _yellow 0x0b
|
|
||||||
#define _white 0x0c
|
|
||||||
#define _red 0x0d
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
@ -9,6 +9,8 @@
|
|||||||
#include "displayDriver.h"
|
#include "displayDriver.h"
|
||||||
#include "canvas.h"
|
#include "canvas.h"
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
#include "buttons.h"
|
||||||
|
#include "shapes.h"
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
@ -29,7 +31,9 @@ int main() {
|
|||||||
displayDriverInit();
|
displayDriverInit();
|
||||||
canvasInit();
|
canvasInit();
|
||||||
|
|
||||||
|
shapesInit();
|
||||||
gameInit();
|
gameInit();
|
||||||
|
buttonsInit();
|
||||||
|
|
||||||
__enable_interrupt();
|
__enable_interrupt();
|
||||||
|
|
||||||
|
@ -1,31 +1,137 @@
|
|||||||
#include "shapes.h"
|
#include "shapes.h"
|
||||||
#include "shape_i.h"
|
#include "shape_i.h"
|
||||||
#include "canvas.h"
|
#include "canvas.h"
|
||||||
|
#include "../rgb-driver/colors.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define COLOR _cyan
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t x;
|
||||||
|
uint8_t y;
|
||||||
|
} pixel_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int8_t x;
|
||||||
|
int8_t y;
|
||||||
|
} offset_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
offset_t set[4];
|
||||||
|
offset_t reset[4];
|
||||||
|
offset_t offset;
|
||||||
|
} motion_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t color;
|
||||||
|
pixel_t draw[4];
|
||||||
|
motion_t motion[5][4];
|
||||||
|
} motionTable_t;
|
||||||
|
|
||||||
|
const motionTable_t motions[1] = {
|
||||||
|
{ // i
|
||||||
|
.color = _cyan,
|
||||||
|
.draw = { { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3} },
|
||||||
|
.motion = {
|
||||||
|
{
|
||||||
|
// move down
|
||||||
|
{ .set = { { 0, 4}, { 0, 4}, { 0, 4}, { 0, 4} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 0, 1} }, // 0
|
||||||
|
{ .set = { { 0, 1}, { 1, 1}, { 2, 1}, { 3, 1} }, .reset = { { 0, 0}, { 1, 0}, { 2, 0}, { 3, 0} }, .offset = { 0, 1} }, // 90
|
||||||
|
{ .set = { { 0, 4}, { 0, 4}, { 0, 4}, { 0, 4} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 0, 1} }, // 180
|
||||||
|
{ .set = { { 0, 1}, { 1, 1}, { 2, 1}, { 3, 1} }, .reset = { { 0, 0}, { 1, 0}, { 2, 0}, { 3, 0} }, .offset = { 0, 1} } // 270
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// move left
|
||||||
|
{ .set = { {-1, 0}, {-1, 1}, {-1, 2}, {-1, 3} }, .reset = { { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3} }, .offset = {-1, 0} }, // 0
|
||||||
|
{ .set = { {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0} }, .reset = { { 3, 0}, { 3, 0}, { 3, 0}, { 3, 0} }, .offset = { 0, 1} }, // 90
|
||||||
|
{ .set = { {-1, 0}, {-1, 1}, {-1, 2}, {-1, 3} }, .reset = { { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3} }, .offset = {-1, 0} }, // 180
|
||||||
|
{ .set = { {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0} }, .reset = { { 3, 0}, { 3, 0}, { 3, 0}, { 3, 0} }, .offset = { 0, 1} }, // 270
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// move right
|
||||||
|
{ .set = { { 1, 0}, { 1, 1}, { 1, 2}, { 1, 3} }, .reset = { { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3} }, .offset = { 1, 0} }, // 0
|
||||||
|
{ .set = { { 4, 0}, { 4, 0}, { 4, 0}, { 4, 0} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 1, 0} }, // 90
|
||||||
|
{ .set = { { 1, 0}, { 1, 1}, { 1, 2}, { 1, 3} }, .reset = { { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3} }, .offset = { 1, 0} }, // 180
|
||||||
|
{ .set = { { 4, 0}, { 4, 0}, { 4, 0}, { 4, 0} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 1, 0} }, // 270
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// rotate left
|
||||||
|
{ .set = { {-1, 1}, { 1, 1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 0, 2}, { 0, 3}, { 0, 3} }, .offset = {-1, 1} }, // 0
|
||||||
|
{ .set = { { 1,-2}, { 1,-1}, { 1, 1}, { 1, 1} }, .reset = { { 0, 0}, { 2, 0}, { 3, 0}, { 3, 0} }, .offset = { 1,-2} }, // 90
|
||||||
|
{ .set = { {-1, 1}, { 1, 1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 0, 2}, { 0, 3}, { 0, 3} }, .offset = {-1, 1} }, // 180
|
||||||
|
{ .set = { { 1,-2}, { 1,-1}, { 1, 1}, { 1, 1} }, .reset = { { 0, 0}, { 2, 0}, { 3, 0}, { 3, 0} }, .offset = { 1,-2} }, // 270
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// rotate right
|
||||||
|
{ .set = { {-2, 1}, {-1, 1}, { 1, 1}, { 1, 1} }, .reset = { { 0, 0}, { 0, 2}, { 0, 3}, { 0, 3} }, .offset = {-2, 1} }, // 0
|
||||||
|
{ .set = { { 2,-2}, { 2,-1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 1, 0}, { 3, 0}, { 3, 0} }, .offset = { 2,-2} }, // 90
|
||||||
|
{ .set = { {-2, 1}, {-1, 1}, { 1, 1}, { 1, 1} }, .reset = { { 0, 0}, { 0, 2}, { 0, 3}, { 0, 3} }, .offset = {-2, 1} }, // 180
|
||||||
|
{ .set = { { 2,-2}, { 2,-1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 1, 0}, { 3, 0}, { 3, 0} }, .offset = { 2,-2} }, // 270
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
uint8_t draw_i() {
|
uint8_t draw_i() {
|
||||||
canvasSetPixel(stone.x, stone.y, 0x01);
|
uint8_t res = 0;
|
||||||
canvasSetPixel(stone.x, stone.y+1, 0x01);
|
if (canvasIsPixelFree(stone.x + motions[stone.shape].draw[0].x, stone.y + motions[stone.shape].draw[0].y) &&
|
||||||
canvasSetPixel(stone.x, stone.y+2, 0x01);
|
canvasIsPixelFree(stone.x + motions[stone.shape].draw[1].x, stone.y + motions[stone.shape].draw[1].y) &&
|
||||||
canvasSetPixel(stone.x, stone.y+3, 0x01);
|
canvasIsPixelFree(stone.x + motions[stone.shape].draw[2].x, stone.y + motions[stone.shape].draw[2].y) &&
|
||||||
return 1;
|
canvasIsPixelFree(stone.x + motions[stone.shape].draw[3].x, stone.y + motions[stone.shape].draw[3].y)) {
|
||||||
|
canvasSetPixel(stone.x + motions[stone.shape].draw[0].x, stone.y + motions[stone.shape].draw[0].y, motions[stone.shape].color);
|
||||||
|
canvasSetPixel(stone.x + motions[stone.shape].draw[1].x, stone.y + motions[stone.shape].draw[1].y, motions[stone.shape].color);
|
||||||
|
canvasSetPixel(stone.x + motions[stone.shape].draw[2].x, stone.y + motions[stone.shape].draw[2].y, motions[stone.shape].color);
|
||||||
|
canvasSetPixel(stone.x + motions[stone.shape].draw[3].x, stone.y + motions[stone.shape].draw[3].y, motions[stone.shape].color);
|
||||||
|
res = 1;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t move(direction_t direction) {
|
||||||
|
uint8_t res = 0;
|
||||||
|
if (canvasIsPixelFree(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[0].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[0].y) &&
|
||||||
|
canvasIsPixelFree(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[1].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[1].y) &&
|
||||||
|
canvasIsPixelFree(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[2].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[2].y) &&
|
||||||
|
canvasIsPixelFree(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[3].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[3].y)) {
|
||||||
|
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].reset[0].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].reset[0].y, _off);
|
||||||
|
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].reset[1].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].reset[1].y, _off);
|
||||||
|
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].reset[2].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].reset[2].y, _off);
|
||||||
|
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].reset[3].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].reset[3].y, _off);
|
||||||
|
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[0].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[0].y, motions[stone.shape].color);
|
||||||
|
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[1].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[1].y, motions[stone.shape].color);
|
||||||
|
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[2].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[2].y, motions[stone.shape].color);
|
||||||
|
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[3].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[3].y, motions[stone.shape].color);
|
||||||
|
stone.x += motions[stone.shape].motion[direction][stone.orientation].offset.x;
|
||||||
|
stone.y += motions[stone.shape].motion[direction][stone.orientation].offset.y;
|
||||||
|
res = 1;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t moveDown_i() {
|
uint8_t moveDown_i() {
|
||||||
return 1;
|
return move(e_MoveDown);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t moveLeft_i() {
|
uint8_t moveLeft_i() {
|
||||||
return 1;
|
return move(e_MoveLeft);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t moveRight_i() {
|
uint8_t moveRight_i() {
|
||||||
return 1;
|
return move(e_MoveRight);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t rotateLeft_i() {
|
uint8_t rotateLeft_i() {
|
||||||
return 1;
|
uint8_t res = move(e_RotateLeft);
|
||||||
|
if (res) {
|
||||||
|
stone.orientation = ((orientation_t[]){ e_270, e_0, e_90, e_180 })[stone.orientation];
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t rotateRight_i() {
|
uint8_t rotateRight_i() {
|
||||||
return 1;
|
uint8_t res = move(e_RotateRight);
|
||||||
|
if (res) {
|
||||||
|
stone.orientation = ((orientation_t[]){ e_90, e_180, e_270, e_0 })[stone.orientation];
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,10 @@ const stoneOperations_t stoneOperations[] = {
|
|||||||
|
|
||||||
stone_t stone;
|
stone_t stone;
|
||||||
|
|
||||||
|
void shapesInit() {
|
||||||
|
stone.shape = e_ShapeInvalid;
|
||||||
|
}
|
||||||
|
|
||||||
void stoneCreate() {
|
void stoneCreate() {
|
||||||
stone.shape = e_I;
|
stone.shape = e_I;
|
||||||
stone.orientation = e_0;
|
stone.orientation = e_0;
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
void shapesInit();
|
||||||
void stoneCreate();
|
void stoneCreate();
|
||||||
uint8_t stoneDraw();
|
uint8_t stoneDraw();
|
||||||
uint8_t stoneMoveDown();
|
uint8_t stoneMoveDown();
|
||||||
@ -12,7 +13,8 @@ uint8_t stoneRotateLeft();
|
|||||||
uint8_t stoneRotateRight();
|
uint8_t stoneRotateRight();
|
||||||
|
|
||||||
|
|
||||||
typedef enum { e_I=0, e_O, e_T, e_Z, e_S, e_L, e_J, e_ShapeEnd } shape_t;
|
typedef enum { e_I=0, e_O, e_T, e_Z, e_S, e_L, e_J, e_ShapeInvalid } shape_t;
|
||||||
|
typedef enum { e_MoveDown, e_MoveLeft, e_MoveRight, e_RotateLeft, e_RotateRight } direction_t;
|
||||||
typedef enum { e_0, e_90, e_180, e_270 } orientation_t;
|
typedef enum { e_0, e_90, e_180, e_270 } orientation_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
Reference in New Issue
Block a user