diff --git a/include/constants.hpp b/include/constants.hpp new file mode 100644 index 0000000..d752261 --- /dev/null +++ b/include/constants.hpp @@ -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 diff --git a/include/display.hpp b/include/display.hpp index f6d9896..32ed387 100644 --- a/include/display.hpp +++ b/include/display.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#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; }; diff --git a/include/game.hpp b/include/game.hpp index c353fdd..938c319 100644 --- a/include/game.hpp +++ b/include/game.hpp @@ -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; diff --git a/include/menu.hpp b/include/menu.hpp index 4fcfe32..58c5d63 100644 --- a/include/menu.hpp +++ b/include/menu.hpp @@ -1,6 +1,6 @@ #pragma once -#define MENU_ITEM_COUNT 4 #include +#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; }; diff --git a/src/display.cpp b/src/display.cpp index fafcc81..65a0d4e 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -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 } } } diff --git a/src/game.cpp b/src/game.cpp index aa00fb5..c8543c8 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -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", diff --git a/src/menu.cpp b/src/menu.cpp index 93ac132..73b7fe9 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -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;