Initial commit
This commit is contained in:
24
include/animation_functions.h
Normal file
24
include/animation_functions.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef ANIMATIONFUNCTIONS_H
|
||||
#define ANIMATIONFUNCTIONS_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "wordclock_constants.h"
|
||||
|
||||
extern bool spiral_direction; // Direction of sprial animation
|
||||
|
||||
enum Direction
|
||||
{
|
||||
RIGHT,
|
||||
LEFT,
|
||||
UP,
|
||||
DOWN
|
||||
};
|
||||
|
||||
Direction next_direction(Direction dir, int d);
|
||||
int random_snake(bool init, const uint8_t len, const uint32_t color, int numSteps);
|
||||
int random_tetris(bool init);
|
||||
int draw_heart_animation(void);
|
||||
int draw_spiral(bool init, bool empty, uint8_t size);
|
||||
void show_digital_clock(uint8_t hours, uint8_t minutes, uint32_t color);
|
||||
|
||||
#endif /* ANIMATIONFUNCTIONS_H */
|
||||
97
include/compile_time.h
Normal file
97
include/compile_time.h
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
*
|
||||
* Created: 29.03.2018
|
||||
*
|
||||
* Authors:
|
||||
*
|
||||
* Assembled from the code released on Stackoverflow by:
|
||||
* Dennis (instructable.com/member/nqtronix) | https://stackoverflow.com/questions/23032002/c-c-how-to-get-integer-unix-timestamp-of-build-time-not-string
|
||||
* and
|
||||
* Alexis Wilke | https://stackoverflow.com/questions/10538444/do-you-know-of-a-c-macro-to-compute-unix-time-and-date
|
||||
*
|
||||
* Assembled by Jean Rabault
|
||||
*
|
||||
* UNIX_TIMESTAMP gives the UNIX timestamp (unsigned long integer of seconds since 1st Jan 1970) of compilation from macros using the compiler defined __TIME__ macro.
|
||||
* This should include Gregorian calendar leap days, in particular the 29ths of February, 100 and 400 years modulo leaps.
|
||||
*
|
||||
* Careful: __TIME__ is the local time of the computer, NOT the UTC time in general!
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef COMPILE_TIME_H_
|
||||
#define COMPILE_TIME_H_
|
||||
|
||||
// Some definitions for calculation
|
||||
#define SEC_PER_MIN 60UL
|
||||
#define SEC_PER_HOUR 3600UL
|
||||
#define SEC_PER_DAY 86400UL
|
||||
#define SEC_PER_YEAR (SEC_PER_DAY*365)
|
||||
|
||||
// extracts 1..4 characters from a string and interprets it as a decimal value
|
||||
#define CONV_STR2DEC_1(str, i) (str[i]>'0'?str[i]-'0':0)
|
||||
#define CONV_STR2DEC_2(str, i) (CONV_STR2DEC_1(str, i)*10 + str[i+1]-'0')
|
||||
#define CONV_STR2DEC_3(str, i) (CONV_STR2DEC_2(str, i)*10 + str[i+2]-'0')
|
||||
#define CONV_STR2DEC_4(str, i) (CONV_STR2DEC_3(str, i)*10 + str[i+3]-'0')
|
||||
|
||||
// Custom "glue logic" to convert the month name to a usable number
|
||||
#define GET_MONTH(str, i) (str[i]=='J' && str[i+1]=='a' && str[i+2]=='n' ? 1 : \
|
||||
str[i]=='F' && str[i+1]=='e' && str[i+2]=='b' ? 2 : \
|
||||
str[i]=='M' && str[i+1]=='a' && str[i+2]=='r' ? 3 : \
|
||||
str[i]=='A' && str[i+1]=='p' && str[i+2]=='r' ? 4 : \
|
||||
str[i]=='M' && str[i+1]=='a' && str[i+2]=='y' ? 5 : \
|
||||
str[i]=='J' && str[i+1]=='u' && str[i+2]=='n' ? 6 : \
|
||||
str[i]=='J' && str[i+1]=='u' && str[i+2]=='l' ? 7 : \
|
||||
str[i]=='A' && str[i+1]=='u' && str[i+2]=='g' ? 8 : \
|
||||
str[i]=='S' && str[i+1]=='e' && str[i+2]=='p' ? 9 : \
|
||||
str[i]=='O' && str[i+1]=='c' && str[i+2]=='t' ? 10 : \
|
||||
str[i]=='N' && str[i+1]=='o' && str[i+2]=='v' ? 11 : \
|
||||
str[i]=='D' && str[i+1]=='e' && str[i+2]=='c' ? 12 : 0)
|
||||
|
||||
// extract the information from the time string given by __TIME__ and __DATE__
|
||||
#define __TIME_SECONDS__ CONV_STR2DEC_2(__TIME__, 6)
|
||||
#define __TIME_MINUTES__ CONV_STR2DEC_2(__TIME__, 3)
|
||||
#define __TIME_HOURS__ CONV_STR2DEC_2(__TIME__, 0)
|
||||
#define __TIME_DAYS__ CONV_STR2DEC_2(__DATE__, 4)
|
||||
#define __TIME_MONTH__ GET_MONTH(__DATE__, 0)
|
||||
#define __TIME_YEARS__ CONV_STR2DEC_4(__DATE__, 7)
|
||||
|
||||
// Days in February
|
||||
#define _UNIX_TIMESTAMP_FDAY(year) \
|
||||
(((year) % 400) == 0UL ? 29UL : \
|
||||
(((year) % 100) == 0UL ? 28UL : \
|
||||
(((year) % 4) == 0UL ? 29UL : \
|
||||
28UL)))
|
||||
|
||||
// Days in the year
|
||||
#define _UNIX_TIMESTAMP_YDAY(year, month, day) \
|
||||
( \
|
||||
/* January */ day \
|
||||
/* February */ + (month >= 2 ? 31UL : 0UL) \
|
||||
/* March */ + (month >= 3 ? _UNIX_TIMESTAMP_FDAY(year) : 0UL) \
|
||||
/* April */ + (month >= 4 ? 31UL : 0UL) \
|
||||
/* May */ + (month >= 5 ? 30UL : 0UL) \
|
||||
/* June */ + (month >= 6 ? 31UL : 0UL) \
|
||||
/* July */ + (month >= 7 ? 30UL : 0UL) \
|
||||
/* August */ + (month >= 8 ? 31UL : 0UL) \
|
||||
/* September */+ (month >= 9 ? 31UL : 0UL) \
|
||||
/* October */ + (month >= 10 ? 30UL : 0UL) \
|
||||
/* November */ + (month >= 11 ? 31UL : 0UL) \
|
||||
/* December */ + (month >= 12 ? 30UL : 0UL) \
|
||||
)
|
||||
|
||||
// get the UNIX timestamp from a digits representation
|
||||
#define _UNIX_TIMESTAMP(year, month, day, hour, minute, second) \
|
||||
( /* time */ second \
|
||||
+ minute * SEC_PER_MIN \
|
||||
+ hour * SEC_PER_HOUR \
|
||||
+ /* year day (month + day) */ (_UNIX_TIMESTAMP_YDAY(year, month, day) - 1) * SEC_PER_DAY \
|
||||
+ /* year */ (year - 1970UL) * SEC_PER_YEAR \
|
||||
+ ((year - 1969UL) / 4UL) * SEC_PER_DAY \
|
||||
- ((year - 1901UL) / 100UL) * SEC_PER_DAY \
|
||||
+ ((year - 1601UL) / 400UL) * SEC_PER_DAY \
|
||||
)
|
||||
|
||||
// the UNIX timestamp
|
||||
#define UNIX_TIMESTAMP (_UNIX_TIMESTAMP(__TIME_YEARS__, __TIME_MONTH__, __TIME_DAYS__, __TIME_HOURS__, __TIME_MINUTES__, __TIME_SECONDS__))
|
||||
|
||||
#endif
|
||||
25
include/diagnosis.h
Normal file
25
include/diagnosis.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef DIAGNOSIS_H
|
||||
#define DIAGNOSIS_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "led_matrix.h"
|
||||
#include "udp_logger.h"
|
||||
|
||||
class Diagnosis
|
||||
{
|
||||
public:
|
||||
Diagnosis(UDPLogger *logger, LEDMatrix *matrix); // constructor
|
||||
|
||||
String handle_command(const String &command);
|
||||
String print_device_info();
|
||||
String print_sketch_info();
|
||||
String print_last_reset_details();
|
||||
String print_matrix_fps();
|
||||
|
||||
private:
|
||||
UDPLogger *_logger;
|
||||
LEDMatrix * _matrix;
|
||||
void print(const String &s);
|
||||
};
|
||||
|
||||
#endif // DIAGNOSIS_H
|
||||
60
include/led_matrix.h
Normal file
60
include/led_matrix.h
Normal file
@@ -0,0 +1,60 @@
|
||||
#ifndef LEDMATRIX_H
|
||||
#define LEDMATRIX_H
|
||||
|
||||
#ifndef FASTLED_INTERNAL
|
||||
#define FASTLED_INTERNAL
|
||||
#endif
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <FastLED_NeoMatrix.h>
|
||||
#include "wordclock_constants.h"
|
||||
#include "udp_logger.h"
|
||||
|
||||
#define DEFAULT_CURRENT_LIMIT 9999
|
||||
|
||||
extern const uint32_t colors_24bit[NUM_COLORS];
|
||||
class LEDMatrix
|
||||
{
|
||||
public:
|
||||
LEDMatrix(FastLED_NeoMatrix *matrix, uint8_t brightness, UDPLogger *logger);
|
||||
static uint16_t color_24_to_16bit(uint32_t color24bit);
|
||||
static uint32_t color_24bit(uint8_t r, uint8_t g, uint8_t b);
|
||||
static uint32_t interpolate_color_24bit(uint32_t color1, uint32_t color2, float factor);
|
||||
static uint32_t wheel(uint8_t WheelPos);
|
||||
uint16_t get_fps(void);
|
||||
void draw_on_matrix_instant();
|
||||
void draw_on_matrix_smooth(float factor);
|
||||
void flush(void);
|
||||
void grid_add_pixel(uint8_t x, uint8_t y, uint32_t color);
|
||||
void print_char(uint8_t xpos, uint8_t ypos, char character, uint32_t color);
|
||||
void print_number(uint8_t xpos, uint8_t ypos, uint8_t number, uint32_t color);
|
||||
void set_brightness(uint8_t mybrightness);
|
||||
void set_current_limit(uint16_t new_current_limit);
|
||||
void set_min_indicator(uint8_t pattern, uint32_t color);
|
||||
void setup_matrix();
|
||||
|
||||
private:
|
||||
FastLED_NeoMatrix *_neomatrix;
|
||||
UDPLogger *_logger;
|
||||
|
||||
uint8_t _brightness;
|
||||
uint16_t _current_limit;
|
||||
|
||||
// target representation of matrix as 2D array
|
||||
uint32_t _target_grid[MATRIX_HEIGHT][MATRIX_WIDTH];
|
||||
|
||||
// current representation of matrix as 2D array
|
||||
uint32_t _current_grid[MATRIX_HEIGHT][MATRIX_WIDTH];
|
||||
|
||||
// target representation of minutes indicator LEDs
|
||||
uint32_t _target_minute_indicators[4] = {0, 0, 0, 0};
|
||||
|
||||
// current representation of minutes indicator LEDs
|
||||
uint32_t _current_minute_indicators[4] = {0, 0, 0, 0};
|
||||
|
||||
void _draw_on_matrix(float factor);
|
||||
uint16_t _calc_estimated_led_current(uint32_t color);
|
||||
};
|
||||
|
||||
#endif /* LEDMATRIX_H */
|
||||
29
include/littlefs_wrapper.h
Normal file
29
include/littlefs_wrapper.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef LITTLEFS_WRAPPER_H
|
||||
#define LITTLEFS_WRAPPER_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#define USE_LittleFS
|
||||
|
||||
//#define DEBUGGING // Einkommentieren für die Serielle Ausgabe
|
||||
|
||||
#ifdef DEBUGGING
|
||||
#define DEBUG_B(...) Serial.begin(__VA_ARGS__)
|
||||
#define DEBUG_P(...) Serial.println(__VA_ARGS__)
|
||||
#define DEBUG_F(...) Serial.printf(__VA_ARGS__)
|
||||
#else
|
||||
#define DEBUG_B(...)
|
||||
#define DEBUG_P(...)
|
||||
#define DEBUG_F(...)
|
||||
#endif
|
||||
|
||||
bool handle_file(String &&path);
|
||||
bool handle_list();
|
||||
const String format_bytes(size_t const &bytes);
|
||||
void delete_recursive(const String &path);
|
||||
void format_filesystem();
|
||||
void handle_upload();
|
||||
void send_response();
|
||||
void setup_filesystem();
|
||||
|
||||
#endif /* LITTLEFS_WRAPPER_H */
|
||||
9
include/ota_wrapper.h
Normal file
9
include/ota_wrapper.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef OTA_FUNCTIONS_H
|
||||
#define OTA_FUNCTIONS_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
void handleOTA();
|
||||
void setupOTA(String hostname);
|
||||
|
||||
#endif /* OTA_FUNCTIONS_H */
|
||||
20
include/own_font.h
Normal file
20
include/own_font.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef OWN_FONT_H
|
||||
#define OWN_FONT_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
uint8_t numbers_font[10][5] = {{0b00000111, 0b00000101, 0b00000101, 0b00000101, 0b00000111},
|
||||
{0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001},
|
||||
{0b00000111, 0b00000001, 0b00000111, 0b00000100, 0b00000111},
|
||||
{0b00000111, 0b00000001, 0b00000111, 0b00000001, 0b00000111},
|
||||
{0b00000101, 0b00000101, 0b00000111, 0b00000001, 0b00000001},
|
||||
{0b00000111, 0b00000100, 0b00000111, 0b00000001, 0b00000111},
|
||||
{0b00000111, 0b00000100, 0b00000111, 0b00000101, 0b00000111},
|
||||
{0b00000111, 0b00000001, 0b00000001, 0b00000001, 0b00000001},
|
||||
{0b00000111, 0b00000101, 0b00000111, 0b00000101, 0b00000111},
|
||||
{0b00000111, 0b00000101, 0b00000111, 0b00000001, 0b00000111}};
|
||||
|
||||
uint8_t chars_font[2][5] = {{0b00000010, 0b00000010, 0b00000010, 0b00000010, 0b00000010},
|
||||
{0b00000111, 0b00000101, 0b00000111, 0b00000100, 0b00000100}};
|
||||
|
||||
#endif /* OWN_FONT_H */
|
||||
100
include/pong.h
Normal file
100
include/pong.h
Normal file
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* @file pong.h
|
||||
* @author techniccontroller (mail[at]techniccontroller.com)
|
||||
* @brief Class declaration for pong game
|
||||
* @version 0.1
|
||||
* @date 2022-03-05
|
||||
*
|
||||
* @copyright Copyright (c) 2022
|
||||
*
|
||||
* main code from https://elektro.turanis.de/html/prj041/index.html
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef PONG_H
|
||||
#define PONG_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "led_matrix.h"
|
||||
#include "udp_logger.h"
|
||||
|
||||
#ifdef DEBOUNCE_TIME
|
||||
#undef DEBOUNCE_TIME
|
||||
#endif
|
||||
#define DEBOUNCE_TIME 10 // in ms
|
||||
|
||||
#define X_MAX 11
|
||||
#define Y_MAX 11
|
||||
|
||||
#ifdef GAME_DELAY
|
||||
#undef GAME_DELAY
|
||||
#endif
|
||||
#define GAME_DELAY 80 // in ms
|
||||
|
||||
#define BALL_DELAY_MAX 350 // in ms
|
||||
#define BALL_DELAY_MIN 50 // in ms
|
||||
#define BALL_DELAY_STEP 5 // in ms
|
||||
|
||||
#define PLAYER_AMOUNT 2
|
||||
#define PLAYER_1 0
|
||||
#define PLAYER_2 1
|
||||
|
||||
#define PADDLE_WIDTH 3
|
||||
|
||||
#define PADDLE_MOVE_NONE 0
|
||||
#define PADDLE_MOVE_UP 1
|
||||
#define PADDLE_MOVE_DOWN 2
|
||||
|
||||
#ifdef LED_TYPE_OFF
|
||||
#undef LED_TYPE_OFF
|
||||
#endif
|
||||
#define LED_TYPE_OFF 1
|
||||
#define LED_TYPE_PADDLE 2
|
||||
#define LED_TYPE_BALL 3
|
||||
#define LED_TYPE_BALL_RED 4
|
||||
|
||||
#define GAME_STATE_RUNNING 1
|
||||
#define GAME_STATE_END 2
|
||||
#define GAME_STATE_INIT 3
|
||||
|
||||
class Pong
|
||||
{
|
||||
struct Coords
|
||||
{
|
||||
uint8_t x;
|
||||
uint8_t y;
|
||||
};
|
||||
|
||||
public:
|
||||
Pong();
|
||||
Pong(LEDMatrix * matrix, UDPLogger * logger);
|
||||
void loopCycle();
|
||||
void initGame(uint8_t numBots);
|
||||
void ctrlUp(uint8_t playerid);
|
||||
void ctrlDown(uint8_t playerid);
|
||||
void ctrlNone(uint8_t playerid);
|
||||
|
||||
private:
|
||||
LEDMatrix *_ledmatrix;
|
||||
UDPLogger *_logger;
|
||||
uint8_t _gameState = 0;
|
||||
uint8_t _numBots = 0;
|
||||
uint8_t _playerMovement[PLAYER_AMOUNT];
|
||||
Coords _paddles[PLAYER_AMOUNT][PADDLE_WIDTH];
|
||||
Coords _ball = {0, 0};
|
||||
Coords _ball_old = {0, 0};
|
||||
int _ballMovement[2] = {0, 0};
|
||||
unsigned int _ballDelay = 0;
|
||||
unsigned long _lastDrawUpdate = 0;
|
||||
unsigned long _lastBallUpdate = 0;
|
||||
unsigned long _lastButtonClick = 0;
|
||||
|
||||
void updateBall();
|
||||
void endGame();
|
||||
void updateGame();
|
||||
uint8_t getPlayerMovement(uint8_t playerId);
|
||||
void resetLEDs();
|
||||
void toggleLed(uint8_t x, uint8_t y, uint8_t type);
|
||||
};
|
||||
|
||||
#endif /* PONG_H */
|
||||
11
include/render_functions.h
Normal file
11
include/render_functions.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef RENDER_FUNCTIONS_H
|
||||
#define RENDER_FUNCTIONS_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
int show_string_on_clock(String message, uint32_t color);
|
||||
String split(String s, char parser, int index);
|
||||
String time_to_string(uint8_t hours, uint8_t minutes);
|
||||
void draw_minute_indicator(uint8_t minutes, uint32_t color);
|
||||
|
||||
#endif /* RENDER_FUNCTIONS_H */
|
||||
94
include/snake.h
Normal file
94
include/snake.h
Normal file
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* @file snake.h
|
||||
* @author techniccontroller (mail[at]techniccontroller.com)
|
||||
* @brief Class declaration of snake game
|
||||
* @version 0.1
|
||||
* @date 2022-03-05
|
||||
*
|
||||
* @copyright Copyright (c) 2022
|
||||
*
|
||||
* main code from https://elektro.turanis.de/html/prj099/index.html
|
||||
*
|
||||
*/
|
||||
#ifndef snake_h
|
||||
#define snake_h
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "led_matrix.h"
|
||||
#include "udp_logger.h"
|
||||
|
||||
#ifdef DEBOUNCE_TIME
|
||||
#undef DEBOUNCE_TIME
|
||||
#endif
|
||||
#define DEBOUNCE_TIME 250 // in ms
|
||||
|
||||
#define X_MAX 11
|
||||
#define Y_MAX 11
|
||||
|
||||
#ifdef GAME_DELAY
|
||||
#undef GAME_DELAY
|
||||
#endif
|
||||
#define GAME_DELAY 400 // in ms
|
||||
|
||||
#define LED_TYPE_SNAKE 1
|
||||
#ifdef LED_TYPE_OFF
|
||||
#undef LED_TYPE_OFF
|
||||
#endif
|
||||
#define LED_TYPE_OFF 2
|
||||
#define LED_TYPE_FOOD 3
|
||||
#define LED_TYPE_BLOOD 4
|
||||
|
||||
#define DIRECTION_NONE 0
|
||||
#define DIRECTION_UP 1
|
||||
#define DIRECTION_DOWN 2
|
||||
#define DIRECTION_LEFT 3
|
||||
#define DIRECTION_RIGHT 4
|
||||
|
||||
#define GAME_STATE_RUNNING 1
|
||||
#define GAME_STATE_END 2
|
||||
#define GAME_STATE_INIT 3
|
||||
|
||||
#define MAX_TAIL_LENGTH (X_MAX * Y_MAX)
|
||||
#define MIN_TAIL_LENGTH 3
|
||||
|
||||
class Snake
|
||||
{
|
||||
|
||||
struct Coords
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
public:
|
||||
Snake();
|
||||
Snake(LEDMatrix * matrix, UDPLogger * logger);
|
||||
void loopCycle();
|
||||
void initGame();
|
||||
void ctrlUp();
|
||||
void ctrlDown();
|
||||
void ctrlLeft();
|
||||
void ctrlRight();
|
||||
|
||||
private:
|
||||
LEDMatrix * _ledmatrix;
|
||||
UDPLogger * _logger;
|
||||
uint8_t _userDirection = 0;
|
||||
uint8_t _gameState = 0;
|
||||
Coords _head = {0, 0};
|
||||
Coords _tail[MAX_TAIL_LENGTH] = {0, 0};
|
||||
Coords _food = {0, 0};
|
||||
unsigned long _lastDrawUpdate = 0;
|
||||
unsigned long _lastButtonClick = 0;
|
||||
unsigned int _wormLength = 0;
|
||||
|
||||
void resetLEDs();
|
||||
void updateGame();
|
||||
void endGame();
|
||||
void updateTail();
|
||||
void updateFood();
|
||||
bool isCollision();
|
||||
void toggleLed(uint8_t x, uint8_t y, uint8_t type);
|
||||
};
|
||||
|
||||
#endif
|
||||
199
include/tetris.h
Normal file
199
include/tetris.h
Normal file
@@ -0,0 +1,199 @@
|
||||
/**
|
||||
* @file tetris.h
|
||||
* @author techniccontroller (mail[at]techniccontroller.com)
|
||||
* @brief Class definition for tetris game
|
||||
* @version 0.1
|
||||
* @date 2022-03-05
|
||||
*
|
||||
* @copyright Copyright (c) 2022
|
||||
*
|
||||
* main tetris code originally written by Klaas De Craemer, Ing. David Hrbaty
|
||||
*
|
||||
*/
|
||||
#ifndef TETRIS_H
|
||||
#define TETRIS_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "led_matrix.h"
|
||||
#include "udp_logger.h"
|
||||
|
||||
#ifdef DEBOUNCE_TIME
|
||||
#undef DEBOUNCE_TIME
|
||||
#endif
|
||||
#define DEBOUNCE_TIME 100
|
||||
|
||||
#define RED_END_TIME 1500
|
||||
#define GAME_STATE_RUNNING 1
|
||||
#define GAME_STATE_END 2
|
||||
#define GAME_STATE_INIT 3
|
||||
#define GAME_STATE_PAUSED 4
|
||||
#define GAME_STATE_READY 5
|
||||
|
||||
// common
|
||||
#define DIR_UP 1
|
||||
#define DIR_DOWN 2
|
||||
#define DIR_LEFT 3
|
||||
#define DIR_RIGHT 4
|
||||
// Maximum size of bricks. Individual bricks can still be smaller (eg 3x3)
|
||||
#define GREEN 0x008000
|
||||
#define RED 0xFF0000
|
||||
#define BLUE 0x0000FF
|
||||
#define YELLOW 0xFFFF00
|
||||
#define CHOCOLATE 0xD2691E
|
||||
#define PURPLE 0xFF00FF
|
||||
#define WHITE 0XFFFFFF
|
||||
#define AQUA 0x00FFFF
|
||||
#define HOTPINK 0xFF1493
|
||||
#define DARKORANGE 0xFF8C00
|
||||
|
||||
#define MAX_BRICK_SIZE 4
|
||||
#define BRICKOFFSET -1 // Y offset for new bricks
|
||||
|
||||
#define INIT_SPEED 800 // Initial delay in ms between brick drops
|
||||
#define SPEED_STEP 10 // Factor for speed increase between levels, default 10
|
||||
#define LEVELUP 4 // Number of rows before levelup, default 5
|
||||
|
||||
#define WIDTH 11
|
||||
#define HEIGHT 11
|
||||
|
||||
class Tetris
|
||||
{
|
||||
|
||||
// Playing field
|
||||
struct Field
|
||||
{
|
||||
uint8_t pix[MATRIX_WIDTH][MATRIX_HEIGHT + 1]; // Make field one larger so that collision detection with bottom of field can be done in a uniform way
|
||||
uint32_t color[MATRIX_WIDTH][MATRIX_HEIGHT];
|
||||
};
|
||||
|
||||
// Structure to represent active brick on screen
|
||||
struct Brick
|
||||
{
|
||||
boolean enabled; // Brick is disabled when it has landed
|
||||
int xpos, ypos;
|
||||
|
||||
int yOffset; // Y-offset to use when placing brick at top of field
|
||||
uint8_t siz;
|
||||
uint8_t pix[MAX_BRICK_SIZE][MAX_BRICK_SIZE];
|
||||
|
||||
uint32_t col;
|
||||
};
|
||||
|
||||
// Struct to contain the different choices of blocks
|
||||
struct AbstractBrick
|
||||
{
|
||||
int yOffset; // Y-offset to use when placing brick at top of field
|
||||
uint8_t siz;
|
||||
uint8_t pix[MAX_BRICK_SIZE][MAX_BRICK_SIZE];
|
||||
uint32_t col;
|
||||
};
|
||||
|
||||
public:
|
||||
Tetris();
|
||||
Tetris(LEDMatrix *myledmatrix, UDPLogger *mylogger);
|
||||
|
||||
void ctrlStart();
|
||||
void ctrlPlayPause();
|
||||
void ctrlRight();
|
||||
void ctrlLeft();
|
||||
void ctrlUp();
|
||||
void ctrlDown();
|
||||
void setSpeed(int32_t i);
|
||||
|
||||
void loopCycle();
|
||||
|
||||
private:
|
||||
void resetLEDs();
|
||||
void tetrisInit();
|
||||
void printField();
|
||||
|
||||
/* *** Game functions *** */
|
||||
void newActiveBrick();
|
||||
boolean checkFieldCollision(struct Brick *brick);
|
||||
boolean checkSidesCollision(struct Brick *brick);
|
||||
void rotateActiveBrick();
|
||||
void shiftActiveBrick(int dir);
|
||||
void addActiveBrickToField();
|
||||
void moveFieldDownOne(uint8_t startRow);
|
||||
void checkFullLines();
|
||||
|
||||
void clearField();
|
||||
void everythingRed();
|
||||
void showscore();
|
||||
|
||||
LEDMatrix *_ledmatrix;
|
||||
UDPLogger *_logger;
|
||||
Brick _activeBrick = {0};
|
||||
Field _field;
|
||||
|
||||
bool _allowdrop = false;
|
||||
bool _tetrisGameOver = false;
|
||||
int _gameState = GAME_STATE_INIT;
|
||||
int _score = 0;
|
||||
unsigned int _speedtetris = 80;
|
||||
unsigned long _brickSpeed = 0;
|
||||
unsigned long _droptime = 0;
|
||||
unsigned long _lastButtonClick = 0;
|
||||
unsigned long _lastButtonClickr = 0;
|
||||
unsigned long _nbRowsThisLevel = 0;
|
||||
unsigned long _nbRowsTotal = 0;
|
||||
unsigned long _prevUpdateTime = 0;
|
||||
unsigned long _tetrisshowscore = 0;
|
||||
|
||||
// color library
|
||||
uint32_t _colorLib[10] = {RED, GREEN, BLUE, YELLOW, CHOCOLATE, PURPLE, WHITE, AQUA, HOTPINK, DARKORANGE};
|
||||
|
||||
// Brick "library"
|
||||
AbstractBrick _brickLib[7] = {
|
||||
{1, // yoffset when adding brick to field
|
||||
4,
|
||||
{{0, 0, 0, 0},
|
||||
{0, 1, 1, 0},
|
||||
{0, 1, 1, 0},
|
||||
{0, 0, 0, 0}},
|
||||
WHITE},
|
||||
{0,
|
||||
4,
|
||||
{{0, 1, 0, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 1, 0, 0}},
|
||||
GREEN},
|
||||
{1,
|
||||
3,
|
||||
{{0, 0, 0, 0},
|
||||
{1, 1, 1, 0},
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 0, 0}},
|
||||
BLUE},
|
||||
{1,
|
||||
3,
|
||||
{{0, 0, 1, 0},
|
||||
{1, 1, 1, 0},
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0}},
|
||||
YELLOW},
|
||||
{1,
|
||||
3,
|
||||
{{0, 0, 0, 0},
|
||||
{1, 1, 1, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 0, 0, 0}},
|
||||
AQUA},
|
||||
{1,
|
||||
3,
|
||||
{{0, 1, 1, 0},
|
||||
{1, 1, 0, 0},
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0}},
|
||||
HOTPINK},
|
||||
{1,
|
||||
3,
|
||||
{{1, 1, 0, 0},
|
||||
{0, 1, 1, 0},
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0}},
|
||||
RED}};
|
||||
};
|
||||
|
||||
#endif /* TETRIS_H */
|
||||
37
include/udp_logger.h
Normal file
37
include/udp_logger.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* @file udp_logger.h
|
||||
* @author techniccontroller (mail[at]techniccontroller.com)
|
||||
* @brief Class for sending logging Strings as multicast messages
|
||||
* @version 0.1
|
||||
* @date 2022-03-21
|
||||
*
|
||||
* @copyright Copyright (c) 2022
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef UDP_LOGGER_H
|
||||
#define UDP_LOGGER_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <WiFiUdp.h>
|
||||
|
||||
class UDPLogger
|
||||
{
|
||||
public:
|
||||
UDPLogger();
|
||||
UDPLogger(IPAddress interface_addr, IPAddress multicast_addr, uint16_t port, String name);
|
||||
void set_name(String name);
|
||||
void log_string(String message);
|
||||
void log_color_24bit(uint32_t color);
|
||||
|
||||
private:
|
||||
char _packetBuffer[100] = {0};
|
||||
int _port;
|
||||
IPAddress _interfaceAddr;
|
||||
IPAddress _multicastAddr;
|
||||
String _name = "Logger";
|
||||
unsigned long _lastSend = 0;
|
||||
WiFiUDP _udp;
|
||||
};
|
||||
|
||||
#endif /* UDP_LOGGER_H */
|
||||
79
include/wordclock_constants.h
Normal file
79
include/wordclock_constants.h
Normal file
@@ -0,0 +1,79 @@
|
||||
#ifndef WORDCLOCK_CONSTANTS_H
|
||||
#define WORDCLOCK_CONSTANTS_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
// ----------------------------------------------------------------------------------
|
||||
// CONSTANTS
|
||||
// ----------------------------------------------------------------------------------
|
||||
#define AP_SSID "WordclockAP" // SSID name of Access Point
|
||||
#define NTP_SERVER_URL "de.pool.ntp.org" // NTP server address
|
||||
#define MY_TZ "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00" // Timezone
|
||||
|
||||
#define HOSTNAME (String("wordclock")) // Local hostname
|
||||
#define LOGGER_MULTICAST_IP (IPAddress(230, 120, 10, 2)) // IP for UDP server
|
||||
#define LOGGER_MULTICAST_PORT (8123) // Port for UDP server
|
||||
#define HTTP_PORT (80) // Standard HTTP port
|
||||
|
||||
// ESP8266 Pins
|
||||
#define FASTLED_PIN (0) // pin to which the LEDs are attached
|
||||
#define BUTTON_PIN (5) // pin to which the button is attached
|
||||
|
||||
// Time limits
|
||||
#define HOUR_MAX (23)
|
||||
#define MINUTE_MAX (59)
|
||||
|
||||
#define MINUTES_IN_HOUR (60)
|
||||
#define HOURS_IN_DAY (24)
|
||||
|
||||
// Night mode
|
||||
#define NIGHTMODE_START_HR (23)
|
||||
#define NIGHTMODE_START_MIN (0)
|
||||
#define NIGHTMODE_END_HR (7)
|
||||
#define NIGHTMODE_END_MIN (0)
|
||||
|
||||
// Timings in us
|
||||
#define PERIOD_ANIMATION_US (200 * 1000) // 200ms
|
||||
#define PERIOD_CLOCK_UPDATE_US (1 * 1000 * 1000) // Must be 1s! Do not change!
|
||||
#define PERIOD_HEARTBEAT_US (1 * 1000 * 1000) // 1s
|
||||
#define PERIOD_MATRIX_UPDATE_US (33 * 1000) // 33ms
|
||||
#define PERIOD_NIGHTMODE_CHECK_US (30 * 1000 * 1000) // 30s
|
||||
#define PERIOD_TIME_UPDATE_US (1 * 1000 * 1000) // 1000ms
|
||||
#define PERIOD_PONG_US (10 * 1000) // 10ms
|
||||
#define PERIOD_SNAKE_US (50 * 1000) // 50ms
|
||||
#define PERIOD_STATE_CHANGE_US (10 * 1000 * 1000) // 10s
|
||||
#define PERIOD_TETRIS_US (50 * 1000) // 50ms
|
||||
#define TIMEOUT_LEDDIRECT_US (5 * 1000 * 1000) // 5s
|
||||
#define PERIOD_BRIGHTNESS_UPDATE_US (5 * 60 * 1000 * 1000) // 300s
|
||||
|
||||
#define SHORT_PRESS_US (100 * 1000) // 100ms
|
||||
#define LONG_PRESS_US (2 * 1000 * 1000) // 2s
|
||||
#define VERY_LONG_PRESS_US (10 * 1000 * 1000) // 10s
|
||||
|
||||
// Current limit
|
||||
#define CURRENT_LIMIT_LED (2500) // limit the total current consumed by LEDs (mA)
|
||||
|
||||
// Brightness ranges range: 0 - 255
|
||||
#define DEFAULT_BRIGHTNESS (40)
|
||||
#define MIN_BRIGHTNESS (10)
|
||||
#define MAX_BRIGHTNESS UINT8_MAX
|
||||
|
||||
// LED smoothing
|
||||
#define DEFAULT_SMOOTHING_FACTOR (0.5f)
|
||||
|
||||
// Number of colors in colors array
|
||||
#define NUM_COLORS (7)
|
||||
#define COLOR_ORDER GRB // WS2812B color order
|
||||
|
||||
// LED matrix size
|
||||
#define MATRIX_WIDTH (11)
|
||||
#define MATRIX_HEIGHT (11)
|
||||
#define NUM_MATRIX (MATRIX_WIDTH * (MATRIX_HEIGHT + 1))
|
||||
|
||||
// NTP macros TODO
|
||||
#define BUILD_YEAR (__DATE__ + 7) // Will expand to current year at compile time as string.
|
||||
#define NTP_MINIMUM_RX_YEAR (atoi(BUILD_YEAR) - 1) // Will expand to current year minus one at compile time.
|
||||
#define NTP_START_YEAR (1900) // NTP minimum year is 1900
|
||||
#define NTP_MAX_OFFLINE_TIME_S (7 * 24 * 3600) // Watchdog value, maximum offline time before a restart is triggered
|
||||
|
||||
#endif /* WORDCLOCK_CONSTANTS_H */
|
||||
89
include/wordclock_esp32.h
Normal file
89
include/wordclock_esp32.h
Normal file
@@ -0,0 +1,89 @@
|
||||
#ifndef WORDCLOCK_ESP8266_H
|
||||
#define WORDCLOCK_ESP8266_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <stdlib.h>
|
||||
#include <WebServer.h>
|
||||
#include "led_matrix.h"
|
||||
#include "udp_logger.h"
|
||||
|
||||
#define RANGE_LIMIT(X, MIN, MAX) (((X) < (MIN)) ? (MIN) : (((X) > (MAX)) ? (MAX) : (X)))
|
||||
#define RANGE_LIMIT_SUB(X, MIN, MAX, SUB) (((X) < (MIN)) ? (SUB) : (((X) > (MAX)) ? (SUB) : (X)))
|
||||
|
||||
#define EEPROM_SIZE (sizeof(EepromLayout_st) / sizeof(uint8_t))
|
||||
|
||||
// ----------------------------------------------------------------------------------
|
||||
// TYPEDEFS
|
||||
// ----------------------------------------------------------------------------------
|
||||
typedef struct
|
||||
{
|
||||
int start_hour;
|
||||
int start_min;
|
||||
int end_hour;
|
||||
int end_min;
|
||||
} NightModeTimes_st;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t red;
|
||||
uint8_t green;
|
||||
uint8_t blue;
|
||||
uint8_t alpha; // note: unused
|
||||
} Color_st;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t static_brightness; // user-controlled static brightness of LEDs
|
||||
uint8_t dyn_brightness_min; // user-controlled min brightness of LEDs
|
||||
uint8_t dyn_brightness_max; // user-controlled max brightness of LEDs
|
||||
bool flg_dynamic_brightness; // flag if user wants to use daytime dynamic brightness
|
||||
} Brightness_st;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
NightModeTimes_st night_mode_times;
|
||||
Brightness_st brightness_values;
|
||||
Color_st color_values;
|
||||
} EepromLayout_st;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
ST_CLOCK,
|
||||
ST_DICLOCK,
|
||||
ST_SPIRAL,
|
||||
ST_TETRIS,
|
||||
ST_SNAKE,
|
||||
ST_PINGPONG,
|
||||
ST_HEARTS,
|
||||
NUM_STATES
|
||||
} ClockState_en;
|
||||
|
||||
// ----------------------------------------------------------------------------------
|
||||
// FUNCTIONS DECLARATIONS
|
||||
// ----------------------------------------------------------------------------------
|
||||
bool check_wifi_status(void);
|
||||
String leading_zero2digit(int value);
|
||||
uint8_t calculate_dynamic_brightness(uint8_t min_brightness, uint8_t max_brightness, int hours, int minutes, bool summertime);
|
||||
uint8_t update_brightness(void);
|
||||
void check_night_mode(void);
|
||||
void cold_start_setup(void);
|
||||
void draw_main_color(void);
|
||||
void handle_button(void);
|
||||
void handle_command(void);
|
||||
void handle_current_state(void);
|
||||
void handle_data_request(void);
|
||||
void handle_led_direct(void);
|
||||
void limit_value_ranges(void);
|
||||
void log_data(void);
|
||||
void on_state_entry(uint8_t state);
|
||||
void read_settings_from_EEPROM(void);
|
||||
void reset_wifi_credentials(void);
|
||||
void send_heartbeat(void);
|
||||
void set_dynamic_brightness(bool state);
|
||||
void set_main_color(uint8_t red, uint8_t green, uint8_t blue);
|
||||
void set_night_mode(bool on);
|
||||
void state_change(ClockState_en new_state);
|
||||
void update_matrix(void);
|
||||
void write_settings_to_EEPROM(void);
|
||||
|
||||
#endif /* WORDCLOCK_ESP8266_H */
|
||||
Reference in New Issue
Block a user