Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions animationfunctions.ino
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,13 @@ int randomtetris(bool init){
}
}
else{
uint8_t tempscreen[HEIGHT+3][WIDTH] = {0};
uint8_t moveX = WIDTH-1;
uint8_t moveY = HEIGHT+2;
// moving blocks exists -> move them one pixel down
// loop over pixels and move every pixel down, which belongs to a moving block
for(int c = WIDTH-1; c >= 0; c--){
for(int r = HEIGHT+1; r >= 0; r--){
if((screen[r][c] != 0) && tomove[screen[r][c]]){
tempscreen[r+1][c] = screen[r][c];
screen[r+1][c] = screen[r][c];
screen[r][c] = 0;
// save top left corner of block
Expand Down
2 changes: 1 addition & 1 deletion ledmatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void LEDMatrix::setMinIndicator(uint8_t pattern, uint32_t color)
void LEDMatrix::gridAddPixel(uint8_t x, uint8_t y, uint32_t color)
{
// limit ranges of x and y
if(x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT){
if(x < WIDTH && y < HEIGHT){
targetgrid[y][x] = color;
}
else{
Expand Down
8 changes: 4 additions & 4 deletions ntp_client_plus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ void NTPClientPlus::calcDate()

// calc how many leap days since 1.Jan 1900
int leapDays = 0;
for (int i = 1900; i < this->_dateYear; i++)
for (unsigned int i = 1900; i < this->_dateYear; i++)
{
// check if leap year
if (this->isLeapYear(i))
Expand Down Expand Up @@ -278,7 +278,7 @@ void NTPClientPlus::calcDate()
this->_dateDay = 0;

// calc day of month
for (int i = 0; i < this->_dateMonth; i++)
for (unsigned int i = 0; i < this->_dateMonth; i++)
{
this->_dateDay = this->_dateDay + daysInMonth[i];
}
Expand All @@ -289,7 +289,7 @@ void NTPClientPlus::calcDate()
// 1. Januar 1900 was a monday
this->_dayOfWeek = 1;

for (int i = 0; i < days1900; i++)
for (unsigned long i = 0; i < days1900; i++)
{

if (this->_dayOfWeek < 7)
Expand Down Expand Up @@ -335,7 +335,7 @@ unsigned int NTPClientPlus::getYear()
unsigned int days = 0;
unsigned int days1900 = 0;

int for_i = 0;
unsigned long for_i = 0;
bool leapYear = LOW;

days1900 = sec1900 / this->secondperday;
Expand Down
10 changes: 5 additions & 5 deletions pong.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void Pong::loopCycle(){
* @param playerid id of player {0, 1}
*/
void Pong::ctrlUp(uint8_t playerid){
if (millis() > _lastButtonClick + DEBOUNCE_TIME) {
if (millis() > _lastButtonClick + DEBOUNCE_TIME_PONG) {
_playerMovement[playerid] = PADDLE_MOVE_DOWN; // need to swap direction as field is rotated 180deg
_lastButtonClick = millis();
}
Expand All @@ -68,7 +68,7 @@ void Pong::ctrlUp(uint8_t playerid){
* @param playerid id of player {0, 1}
*/
void Pong::ctrlDown(uint8_t playerid){
if (millis() > _lastButtonClick + DEBOUNCE_TIME) {
if (millis() > _lastButtonClick + DEBOUNCE_TIME_PONG) {
_playerMovement[playerid] = PADDLE_MOVE_UP; // need to swap direction as field is rotated 180deg
_lastButtonClick = millis();
}
Expand All @@ -80,7 +80,7 @@ void Pong::ctrlDown(uint8_t playerid){
* @param playerid id of player {0, 1}
*/
void Pong::ctrlNone(uint8_t playerid){
if (millis() > _lastButtonClick + DEBOUNCE_TIME) {
if (millis() > _lastButtonClick + DEBOUNCE_TIME_PONG) {
_playerMovement[playerid] = PADDLE_MOVE_NONE;
_lastButtonClick = millis();
}
Expand Down Expand Up @@ -189,7 +189,7 @@ void Pong::endGame()
*/
void Pong::updateGame()
{
if ((millis() - _lastDrawUpdate) < GAME_DELAY) {
if ((millis() - _lastDrawUpdate) < GAME_DELAY_PONG) {
return;
}
_lastDrawUpdate = millis();
Expand Down Expand Up @@ -273,7 +273,7 @@ void Pong::resetLEDs()
*/
void Pong::toggleLed(uint8_t x, uint8_t y, uint8_t type)
{
uint32_t color;
uint32_t color = LEDMatrix::Color24bit(0, 0, 0);

switch(type) {
case LED_TYPE_PADDLE:
Expand Down
4 changes: 2 additions & 2 deletions pong.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
#include "ledmatrix.h"
#include "udplogger.h"

#define DEBOUNCE_TIME 10 // in ms
#define DEBOUNCE_TIME_PONG 10 // in ms

#define X_MAX 11
#define Y_MAX 11

#define GAME_DELAY 80 // in ms
#define GAME_DELAY_PONG 80 // in ms
#define BALL_DELAY_MAX 350 // in ms
#define BALL_DELAY_MIN 50 // in ms
#define BALL_DELAY_STEP 5 // in ms
Expand Down
24 changes: 12 additions & 12 deletions snake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void Snake::loopCycle()
*
*/
void Snake::ctrlUp(){
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING) {
if (millis() > _lastButtonClick + DEBOUNCE_TIME_SNAKE && _gameState == GAME_STATE_RUNNING) {
(*_logger).logString("Snake: UP");
_userDirection = DIRECTION_DOWN; // need to swap direction as field is rotated 180deg
_lastButtonClick = millis();
Expand All @@ -68,7 +68,7 @@ void Snake::ctrlUp(){
*
*/
void Snake::ctrlDown(){
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING) {
if (millis() > _lastButtonClick + DEBOUNCE_TIME_SNAKE && _gameState == GAME_STATE_RUNNING) {
(*_logger).logString("Snake: DOWN");
_userDirection = DIRECTION_UP; // need to swap direction as field is rotated 180deg
_lastButtonClick = millis();
Expand All @@ -80,7 +80,7 @@ void Snake::ctrlDown(){
*
*/
void Snake::ctrlRight(){
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING) {
if (millis() > _lastButtonClick + DEBOUNCE_TIME_SNAKE && _gameState == GAME_STATE_RUNNING) {
(*_logger).logString("Snake: RIGHT");
_userDirection = DIRECTION_LEFT; // need to swap direction as field is rotated 180deg
_lastButtonClick = millis();
Expand All @@ -92,7 +92,7 @@ void Snake::ctrlRight(){
*
*/
void Snake::ctrlLeft(){
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameState == GAME_STATE_RUNNING) {
if (millis() > _lastButtonClick + DEBOUNCE_TIME_SNAKE && _gameState == GAME_STATE_RUNNING) {
(*_logger).logString("Snake: LEFT");
_userDirection = DIRECTION_RIGHT; // need to swap direction as field is rotated 180deg
_lastButtonClick = millis();
Expand Down Expand Up @@ -138,9 +138,9 @@ void Snake::initGame()
*/
void Snake::updateGame()
{
if ((millis() - _lastDrawUpdate) > GAME_DELAY) {
if ((millis() - _lastDrawUpdate) > GAME_DELAY_SNAKE) {
(*_logger).logString("Snake: update game");
toggleLed(_tail[_wormLength-1].x, _tail[_wormLength-1].y, LED_TYPE_OFF);
toggleLed(_tail[_wormLength-1].x, _tail[_wormLength-1].y, LED_TYPE_EMPTY);
switch(_userDirection) {
case DIRECTION_RIGHT:
if (_head.x > 0) {
Expand Down Expand Up @@ -198,14 +198,14 @@ void Snake::endGame()
*/
void Snake::updateTail()
{
for(int i=_wormLength-1; i>0; i--) {
for(unsigned int i=_wormLength-1; i>0; i--) {
_tail[i].x = _tail[i-1].x;
_tail[i].y = _tail[i-1].y;
}
_tail[0].x = _head.x;
_tail[0].y = _head.y;

for(int i=0; i<_wormLength; i++) {
for(unsigned int i=0; i<_wormLength; i++) {
if (_tail[i].x > -1) {
toggleLed(_tail[i].x, _tail[i].y, LED_TYPE_SNAKE);
}
Expand All @@ -223,7 +223,7 @@ void Snake::updateFood()
found = true;
_food.x = random(0, X_MAX);
_food.y = random(0, Y_MAX);
for(int i=0; i<_wormLength; i++) {
for(unsigned int i=0; i<_wormLength; i++) {
if (_tail[i].x == _food.x && _tail[i].y == _food.y) {
found = false;
}
Expand All @@ -246,7 +246,7 @@ bool Snake::isCollision()
if (_head.y < 0 || _head.y >= Y_MAX) {
return true;
}
for(int i=1; i<_wormLength; i++) {
for(unsigned int i=1; i<_wormLength; i++) {
if (_tail[i].x == _head.x && _tail[i].y == _head.y) {
return true;
}
Expand All @@ -263,13 +263,13 @@ bool Snake::isCollision()
*/
void Snake::toggleLed(uint8_t x, uint8_t y, uint8_t type)
{
uint32_t color;
uint32_t color = LEDMatrix::Color24bit(0, 0, 0);

switch(type) {
case LED_TYPE_SNAKE:
color = LEDMatrix::Color24bit(0, 100, 100);
break;
case LED_TYPE_OFF:
case LED_TYPE_EMPTY:
color = LEDMatrix::Color24bit(0, 0, 0);
break;
case LED_TYPE_FOOD:
Expand Down
6 changes: 3 additions & 3 deletions snake.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
#include "ledmatrix.h"
#include "udplogger.h"

#define DEBOUNCE_TIME 300 // in ms
#define DEBOUNCE_TIME_SNAKE 300 // in ms

#define X_MAX 11
#define Y_MAX 11

#define GAME_DELAY 400 // in ms
#define GAME_DELAY_SNAKE 400 // in ms

#define LED_TYPE_SNAKE 1
#define LED_TYPE_OFF 2
#define LED_TYPE_EMPTY 2
#define LED_TYPE_FOOD 3
#define LED_TYPE_BLOOD 4

Expand Down
30 changes: 15 additions & 15 deletions tetris.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ void Tetris::loopCycle(){
if (_activeBrick.enabled) {
// move faster down when allow drop
if (_allowdrop) {
if (millis() > _droptime + 50) {
_droptime = millis();
if (millis() > _dropTime + 50) {
_dropTime = millis();
shiftActiveBrick(DIR_DOWN);
printField();
}
Expand Down Expand Up @@ -77,10 +77,10 @@ void Tetris::loopCycle(){
_tetrisGameOver = false;
(*_logger).logString("Tetris: end");
everythingRed();
_tetrisshowscore = millis();
_tetrisshowscoreTime = millis();
}

if (millis() > (_tetrisshowscore + RED_END_TIME)) {
if (millis() > (_tetrisshowscoreTime + RED_END_TIME)) {
resetLEDs();
_score = _nbRowsTotal;
showscore();
Expand All @@ -95,7 +95,7 @@ void Tetris::loopCycle(){
*
*/
void Tetris::ctrlStart() {
if (millis() > _lastButtonClick + DEBOUNCE_TIME)
if (millis() > _lastButtonClick + DEBOUNCE_TIME_TETRIS)
{
_lastButtonClick = millis();
_gameStatet = GAME_STATE_INITt;
Expand All @@ -107,7 +107,7 @@ void Tetris::ctrlStart() {
*
*/
void Tetris::ctrlPlayPause() {
if (millis() > _lastButtonClick + DEBOUNCE_TIME)
if (millis() > _lastButtonClick + DEBOUNCE_TIME_TETRIS)
{
_lastButtonClick = millis();
if (_gameStatet == GAME_STATE_PAUSEDt) {
Expand All @@ -128,7 +128,7 @@ void Tetris::ctrlPlayPause() {
*
*/
void Tetris::ctrlRight() {
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameStatet == GAME_STATE_RUNNINGt)
if (millis() > _lastButtonClick + DEBOUNCE_TIME_TETRIS && _gameStatet == GAME_STATE_RUNNINGt)
{
_lastButtonClick = millis();
shiftActiveBrick(DIR_RIGHT);
Expand All @@ -141,7 +141,7 @@ void Tetris::ctrlRight() {
*
*/
void Tetris::ctrlLeft() {
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameStatet == GAME_STATE_RUNNINGt)
if (millis() > _lastButtonClick + DEBOUNCE_TIME_TETRIS && _gameStatet == GAME_STATE_RUNNINGt)
{
_lastButtonClick = millis();
shiftActiveBrick(DIR_LEFT);
Expand All @@ -154,7 +154,7 @@ void Tetris::ctrlLeft() {
*
*/
void Tetris::ctrlUp() {
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameStatet == GAME_STATE_RUNNINGt)
if (millis() > _lastButtonClick + DEBOUNCE_TIME_TETRIS && _gameStatet == GAME_STATE_RUNNINGt)
{
_lastButtonClick = millis();
rotateActiveBrick();
Expand All @@ -168,7 +168,7 @@ void Tetris::ctrlUp() {
*/
void Tetris::ctrlDown() {
// longer debounce time, to prevent immediate drop
if (millis() > _lastButtonClickr + DEBOUNCE_TIME*5 && _gameStatet == GAME_STATE_RUNNINGt)
if (millis() > _lastButtonClickr + DEBOUNCE_TIME_TETRIS*5 && _gameStatet == GAME_STATE_RUNNINGt)
{
_allowdrop = true;
_lastButtonClickr = millis();
Expand All @@ -178,9 +178,10 @@ void Tetris::ctrlDown() {
/**
* @brief Set game speed
*
* @param i new speed value
* @param i new speed value (0 - 15)
*/
void Tetris::setSpeed(int32_t i) {
void Tetris::setSpeed(uint8_t i) {
if(i > 15) i = 15;
(*_logger).logString("setSpeed: " + String(i));
_speedtetris = -10 * i + 150;
}
Expand Down Expand Up @@ -321,12 +322,11 @@ boolean Tetris::checkFieldCollision(struct Brick * brick) {
boolean Tetris::checkSidesCollision(struct Brick * brick) {
//Check vertical collision with sides of field
uint8_t bx, by;
uint8_t fx, fy;
int8_t fx;
for (by = 0; by < MAX_BRICK_SIZE; by++) {
for (bx = 0; bx < MAX_BRICK_SIZE; bx++) {
if ( (*brick).pix[bx][by] == 1) {
fx = (*brick).xpos + bx;//Determine actual position in the field of the current pix of the brick
fy = (*brick).ypos + by;
if (fx < 0 || fx >= WIDTH) {
return true;
}
Expand Down Expand Up @@ -450,7 +450,7 @@ void Tetris::shiftActiveBrick(int dir) {
*/
void Tetris::addActiveBrickToField() {
uint8_t bx, by;
uint8_t fx, fy;
int8_t fx, fy;
for (by = 0; by < MAX_BRICK_SIZE; by++) {
for (bx = 0; bx < MAX_BRICK_SIZE; bx++) {
fx = _activeBrick.xpos + bx;
Expand Down
17 changes: 8 additions & 9 deletions tetris.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "ledmatrix.h"
#include "udplogger.h"

#define DEBOUNCE_TIME 100
#define DEBOUNCE_TIME_TETRIS 100
#define RED_END_TIME 1500
#define GAME_STATE_RUNNINGt 1
#define GAME_STATE_ENDt 2
Expand Down Expand Up @@ -90,7 +90,7 @@ class Tetris{
void ctrlLeft();
void ctrlUp();
void ctrlDown();
void setSpeed(int32_t i);
void setSpeed(uint8_t i);

void loopCycle();

Expand Down Expand Up @@ -119,21 +119,20 @@ class Tetris{
Brick _activeBrick;
Field _field;

long _lastButtonClick = 0;
long _lastButtonClickr = 0;
unsigned long _lastButtonClick = 0;
unsigned long _lastButtonClickr = 0;
int _score = 0;
int _gameStatet = GAME_STATE_INITt;
uint16_t _brickSpeed;
unsigned int _brickSpeed;
unsigned long _nbRowsThisLevel;
unsigned long _nbRowsTotal;

bool _tetrisGameOver;

unsigned long _prevUpdateTime = 0;

long _tetrisshowscore;
long _droptime = 0;
int _speedtetris = 80;
unsigned long _tetrisshowscoreTime = 0;
unsigned long _dropTime = 0;
unsigned int _speedtetris = 80;
bool _allowdrop;

// color library
Expand Down
Loading