chore(constants): extract constants into separate header

This commit is contained in:
2026-04-29 00:46:06 +02:00
parent ea3632326b
commit 0a7bb641dd
7 changed files with 21 additions and 17 deletions
+7
View File
@@ -0,0 +1,7 @@
#pragma once
#define LCD_I2C_ADDRESS 0x27
#define LCD_COLS 20
#define LCD_ROWS 4
#define ACTION_INTERVAL 60000 // 1 minute
#define MAXIMUM_STAT 100
+2 -3
View File
@@ -1,6 +1,7 @@
#pragma once
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
#include "constants.hpp"
#include "menu.hpp"
/*
@@ -8,7 +9,7 @@
*/
class Display {
public:
Display(uint8_t addr = 0x27, uint8_t cols = 20, uint8_t rows = 4);
Display();
void begin();
void clear();
@@ -17,7 +18,5 @@ class Display {
LiquidCrystal_I2C& getLCD();
private:
uint8_t rows;
uint8_t cols;
LiquidCrystal_I2C lcd;
};
-2
View File
@@ -4,8 +4,6 @@
#include "display.hpp"
#include "menu.hpp"
#include "pet.hpp"
#define ACTION_INTERVAL 60000 // 1 minute
#define MAXIMUM_STAT 100
typedef struct {
Pet pet;
+2 -2
View File
@@ -1,6 +1,6 @@
#pragma once
#define MENU_ITEM_COUNT 4
#include <Arduino.h>
#include "constants.hpp"
#include "joystick.hpp"
class Menu {
@@ -12,6 +12,6 @@ class Menu {
String& getItemAt(size_t index);
size_t getCurrentItemIndex() const;
private:
String items[MENU_ITEM_COUNT];
String items[LCD_ROWS];
int currentItem;
};
+6 -6
View File
@@ -1,7 +1,7 @@
#include "display.hpp"
// Initialize the LCD display with the specified number of columns and rows, using the I2C address.
Display::Display(uint8_t addr, uint8_t cols, uint8_t rows) : rows(rows), cols(cols), lcd(addr, cols, rows) {}
Display::Display() : lcd(LCD_I2C_ADDRESS, LCD_COLS, LCD_ROWS) {}
void Display::begin() {
lcd.init();
@@ -14,23 +14,23 @@ void Display::clear() {
void Display::drawBuffer(String buffer[]) {
clear();
for (size_t i = 0; i < rows; i++) {
for (size_t i = 0; i < LCD_ROWS; i++) {
lcd.setCursor(0, i);
lcd.print(buffer[i].substring(0, cols)); // Ensure we only print up to the number of columns
lcd.print(buffer[i].substring(0, LCD_COLS)); // Ensure we only print up to the number of columns
}
}
void Display::drawMenu(Menu &menu) {
clear();
size_t currentItemIndex = menu.getCurrentItemIndex();
for (size_t i = 0; i < MENU_ITEM_COUNT; i++) {
for (size_t i = 0; i < LCD_ROWS; i++) {
lcd.setCursor(0, i);
String item = menu.getItemAt(i);
if (i == currentItemIndex) {
lcd.print("> " + item);
lcd.print(("> " + item).substring(0, LCD_COLS)); // Add a ">" to indicate the current item
} else {
lcd.print(" " + item);
lcd.print((" " + item).substring(0, LCD_COLS)); // Indent non-selected items
}
}
}
+1 -1
View File
@@ -105,7 +105,7 @@ void Game::render() {
// If the pet is dead, display a message and return
if (!state.pet.isAlive) {
String buffer[4] = {
String buffer[LCD_ROWS] = {
"Your pet has died",
state.pet.getReasonForDeath(),
"Reset the device",
+3 -3
View File
@@ -4,7 +4,7 @@ Menu::Menu() : currentItem(0) {}
void Menu::setItems(String* items) {
this->currentItem = 0;
for (int i = 0; i < MENU_ITEM_COUNT; i++) {
for (int i = 0; i < LCD_ROWS; i++) {
this->items[i] = items[i];
}
}
@@ -12,10 +12,10 @@ void Menu::setItems(String* items) {
bool Menu::updateCurrentItem(JoystickDirection &direction) {
switch (direction) {
case JoystickDirection::UP:
currentItem = (currentItem - 1 + MENU_ITEM_COUNT) % MENU_ITEM_COUNT;
currentItem = (currentItem - 1 + LCD_ROWS) % LCD_ROWS;
return true;
case JoystickDirection::DOWN:
currentItem = (currentItem + 1) % MENU_ITEM_COUNT;
currentItem = (currentItem + 1) % LCD_ROWS;
return true;
default:
return false;