Commit 83b4fd63 authored by Lucas Grunenberg's avatar Lucas Grunenberg
Browse files

inital commit

parents
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include "7seg.h"
#include "adc.h"
#include "button.h"
#include "com.h"
#include "led.h"
#include "display.h"
#include "assets.h"
uint8_t numbers[4][4];
void setupDisplay();
void keyPressed();
uint8_t moveCells(int8_t deltaX, int8_t deltaY);
void randomFill();
void showNumbers();
void drawNumber(uint8_t row, uint8_t col, uint16_t value);
void drawGrid();
void drawArrowBorders();
static uint8_t getPressedButtons() {
static uint8_t previousState[2] = { UNKNOWN, UNKNOWN };
uint8_t currentState[2] = { sb_button_getState(BUTTON0), sb_button_getState(
BUTTON1) };
uint8_t r = 0;
for (uint8_t i = 0; i < 2; i++) {
r |= currentState[i] == PRESSED && previousState[i] == RELEASED ?
(1 << i) : 0;
previousState[i] = currentState[i];
}
return r;
}
int8_t deltasX[4] = { -1, 0, 1, 0 };
int8_t deltasY[4] = { 0, -1, 0, 1 };
void main() {
setupDisplay();
while (1) {
uint8_t direction = 3 - sb_adc_read(POTI) * 4 / 1024;
sb_display_drawBitmap(4, 90, 2, 16, arrows[direction]);
if (getPressedButtons() & 0x01) {
uint8_t newNumber = moveCells(deltasX[direction],
deltasY[direction]);
showNumbers();
if (newNumber) {
randomFill();
showNumbers();
}
}
}
}
void setupDisplay() {
sb_display_enable();
sb_display_fillScreen(NULL);
for (uint8_t i = 0; i < 4; i++) {
for (uint8_t j = 0; j < 4; j++) {
numbers[i][j] = 0;
}
}
randomFill();
randomFill();
drawGrid();
showNumbers();
drawArrowBorders();
}
void drawArrowBorders() {
sb_display_draw(4, 87, 0xFF);
sb_display_draw(4, 86, 0xFF);
sb_display_draw(5, 87, 0xFF);
sb_display_draw(5, 86, 0xFF);
sb_display_draw(4, 109, 0xFF);
sb_display_draw(4, 110, 0xFF);
sb_display_draw(5, 109, 0xFF);
sb_display_draw(5, 110, 0xFF);
sb_display_draw(3, 86, 0xF0);
sb_display_draw(3, 87, 0xF0);
sb_display_draw(3, 109, 0xF0);
sb_display_draw(3, 110, 0xF0);
sb_display_draw(6, 86, 0xF);
sb_display_draw(6, 87, 0xF);
sb_display_draw(6, 109, 0xF);
sb_display_draw(6, 110, 0xF);
for (uint8_t i = 0; i < 21; i++) {
sb_display_draw(3, 88 + i, 0x30);
}
for (uint8_t i = 0; i < 21; i++) {
sb_display_draw(6, 88 + i, 0xC);
}
}
void keyPressed() {
}
uint8_t moveCells(int8_t deltaX, int8_t deltaY) {
uint8_t change = 1;
uint8_t changeHappened = 0;
while (change != 0) {
change = 0;
for (uint8_t i = 0; i < 4; i++) {
for (uint8_t j = 0; j < 4; j++) {
if (j + deltaX < 0 || j + deltaX >= 4 || i + deltaY < 0
|| i + deltaY >= 4) {
continue;
}
if (numbers[i + deltaY][j + deltaX] == 0
&& numbers[i][j] != 0) {
numbers[i + deltaY][j + deltaX] = numbers[i][j];
numbers[i][j] = 0;
change = 1;
changeHappened = 1;
}
}
}
}
for (uint8_t i = 0; i < 4; i++) {
for (uint8_t j = 1; j < 4; j++) {
if (j + deltaX < 0 || j + deltaX >= 4 || i + deltaY < 0
|| i + deltaY >= 4) {
continue;
}
if (numbers[i + deltaY][j + deltaX] == numbers[i][j]) {
numbers[i + deltaY][j + deltaX] += numbers[i][j];
numbers[i][j] = 0;
change = 1;
changeHappened = 1;
}
}
}
return changeHappened;
}
void randomFill() {
uint8_t filled = 0;
while (filled == 0) {
uint8_t r = (uint8_t) rand() & 0x0F;
uint8_t r2 = (uint8_t) rand() & 0x0F;
if (numbers[r / 4][r % 4] == 0) {
if (r2 == 15) {
numbers[r / 4][r % 4] = 4;
} else {
numbers[r / 4][r % 4] = 2;
}
filled = 1;
}
}
}
void showNumbers() {
static uint16_t values[4][4];
for (uint8_t i = 0; i < 4; i++) {
for (uint8_t j = 0; j < 4; j++) {
if (values[i][j] != numbers[i][j])
drawNumber(i, j, numbers[i][j]);
values[i][j] = numbers[i][j];
}
}
}
void drawNumber(uint8_t row, uint8_t col, uint16_t value) {
uint8_t number[16];
for (uint8_t i = 0; i < 16; i++) {
number[i] = 0;
}
switch (value) {
case 2048:
for (uint8_t i = 0; i < 4; i++) {
number[i] = font0[2][i];
}
for (uint8_t i = 0; i < 4; i++) {
number[i + 4] = font0[0][i];
}
for (uint8_t i = 0; i < 4; i++) {
number[i + 8] = font0[4][i];
}
for (uint8_t i = 0; i < 4; i++) {
number[i + 12] = font0[8][i];
}
break;
case 1024:
for (uint8_t i = 0; i < 4; i++) {
number[i] = font0[1][i];
}
for (uint8_t i = 0; i < 4; i++) {
number[i + 4] = font0[0][i];
}
for (uint8_t i = 0; i < 4; i++) {
number[i + 8] = font0[2][i];
}
for (uint8_t i = 0; i < 4; i++) {
number[i + 12] = font0[4][i];
}
break;
case 512:
for (uint8_t i = 0; i < 5; i++) {
number[i + 1] = font1[5][i] << 1;
}
for (uint8_t i = 0; i < 5; i++) {
number[i + 5 + 1] = font1[1][i] << 1;
}
for (uint8_t i = 0; i < 5; i++) {
number[i + 10 + 1] = font1[2][i] << 1;
}
break;
case 256:
for (uint8_t i = 0; i < 5; i++) {
number[i + 1] = font1[2][i] << 1;
}
for (uint8_t i = 0; i < 5; i++) {
number[i + 5 + 1] = font1[5][i] << 1;
}
for (uint8_t i = 0; i < 5; i++) {
number[i + 10 + 1] = font1[6][i] << 1;
}
break;
case 128:
for (uint8_t i = 0; i < 5; i++) {
number[i + 1] = font1[1][i] << 1;
}
for (uint8_t i = 0; i < 5; i++) {
number[i + 5 + 1] = font1[2][i] << 1;
}
for (uint8_t i = 0; i < 5; i++) {
number[i + 10 + 1] = font1[8][i] << 1;
}
break;
case 64:
for (uint8_t i = 0; i < 6; i++) {
number[i + 2] = font2[6][i];
}
for (uint8_t i = 0; i < 6; i++) {
number[i + 6 + 2] = font2[4][i];
}
break;
case 32:
for (uint8_t i = 0; i < 6; i++) {
number[i + 2] = font2[3][i];
}
for (uint8_t i = 0; i < 6; i++) {
number[i + 6 + 2] = font2[2][i];
}
break;
case 16:
for (uint8_t i = 0; i < 6; i++) {
number[i + 2] = font2[1][i];
}
for (uint8_t i = 0; i < 6; i++) {
number[i + 6 + 2] = font2[6][i];
}
break;
case 8:
for (uint8_t i = 0; i < 6; i++) {
number[i + 5] = font2[8][i];
}
break;
case 4:
for (uint8_t i = 0; i < 6; i++) {
number[i + 5] = font2[4][i];
}
break;
case 2:
for (uint8_t i = 0; i < 6; i++) {
number[i + 5] = font2[2][i];
}
break;
case 1:
for (uint8_t i = 0; i < 6; i++) {
number[i + 5] = font2[1][i];
}
break;
}
for (uint8_t i = 0; i < 16; i++) {
sb_display_draw(row * 2, col * 19 + 1 + i, (number[i] << 4) | 0x01);
sb_display_draw(row * 2 + 1, col * 19 + 1 + i, number[i] >> 4 | 0x80);
}
}
void drawGrid() {
for (uint8_t row = 0; row < 4; row++) {
for (uint8_t col = 0; col < 4; col++) {
sb_display_draw(row * 2, col * 19 + 0, 0xFF);
sb_display_draw(row * 2 + 1, col * 19 + 0, 0xFF);
sb_display_draw(row * 2, col * 19 + 18, 0xFF);
sb_display_draw(row * 2 + 1, col * 19 + 18, 0xFF);
for (uint8_t i = 1; i < 18; i++) {
sb_display_draw(row * 2, col * 19 + i, 0x01);
sb_display_draw(row * 2 + 1, col * 19 + i, 0x80);
}
}
}
}
LIBSPICBOARDDIR ?= ../libspicboard
include $(LIBSPICBOARDDIR)/debug.mk
2048
====
Umsetzung des Spiels "2048" für das SPiCboard.
TODO
----
* Leider ist der Algorithmus zum Kombinieren von Zahlendfeldern fehlerhaft und muss überarbeitet werden
* Implementierung eines Punkte-Zählsystems mit Scoreboard
\ No newline at end of file
#ifndef __ASSETS_H_
#define __ASSETS_H_
uint8_t arrows[4][32] = {{0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFE, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 },
{0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFF, 0xFF, 0xFE, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00 },
{0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xFE, 0xFE, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01, 0x00, 0x00 },
{0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF, 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x00}
};
uint8_t font0[10][4] = {
{
0x00, 0x78, 0x44, 0x3C
}
, // 0x30
{
0x00, 0x48, 0x7C, 0x40
}
, // 0x31
{
0x00, 0x64, 0x54, 0x48
}
, // 0x32
{
0x00, 0x44, 0x54, 0x28
}
, // 0x33
{
0x00, 0x18, 0x10, 0x7C
}
, // 0x34
{
0x00, 0x5C, 0x54, 0x24
}
, // 0x35
{
0x00, 0x38, 0x54, 0x20
}
, // 0x36
{
0x00, 0x04, 0x74, 0x1C
}
, // 0x37
{
0x00, 0x28, 0x54, 0x28
}
, // 0x38
{
0x00, 0x08, 0x54, 0x38
}
, // 0x39
};
uint8_t font1[10][5] = {
{
0x00, 0x1E, 0x21, 0x21, 0x1E
}
, // 0x30
{
0x00, 0x22, 0x3F, 0x20, 0x00
}
, // 0x31
{
0x00, 0x31, 0x29, 0x26, 0x00
}
, // 0x32
{
0x00, 0x25, 0x25, 0x1A, 0x00
}
, // 0x33
{
0x00, 0x0C, 0x0A, 0x3F, 0x08
}
, // 0x34
{
0x00, 0x27, 0x25, 0x19, 0x00
}
, // 0x35
{
0x00, 0x1E, 0x25, 0x25, 0x18
}
, // 0x36
{
0x00, 0x01, 0x39, 0x05, 0x03
}
, // 0x37
{
0x00, 0x1A, 0x25, 0x25, 0x1A
}
, // 0x38
{
0x00, 0x06, 0x29, 0x29, 0x1E
}
, // 0x39
};
uint8_t font2[10][6] = {
{
0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E
}
, // 0x30
{
0x00, 0x00, 0x42, 0x7F, 0x40, 0x00
}
, // 0x31
{
0x00, 0x62, 0x51, 0x49, 0x49, 0x46
}
, // 0x32
{
0x00, 0x22, 0x49, 0x49, 0x49, 0x36
}
, // 0x33
{
0x00, 0x18, 0x14, 0x12, 0x7F, 0x10
}
, // 0x34
{
0x00, 0x2F, 0x49, 0x49, 0x49, 0x31
}
, // 0x35
{
0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30
}
, // 0x36
{
0x00, 0x01, 0x71, 0x09, 0x05, 0x03
}
, // 0x37
{
0x00, 0x36, 0x49, 0x49, 0x49, 0x36
}
, // 0x38
{
0x00, 0x06, 0x49, 0x49, 0x29, 0x1E
}
, // 0x39
};
#endif
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment