chore(constants): extract constants into separate header
This commit is contained in:
@@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
/*
|
||||||
|
* A header file to store all the constants used in the project.
|
||||||
|
*/
|
||||||
|
#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
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <LiquidCrystal_I2C.h>
|
#include <LiquidCrystal_I2C.h>
|
||||||
|
#include "constants.hpp"
|
||||||
#include "menu.hpp"
|
#include "menu.hpp"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -8,7 +9,7 @@
|
|||||||
*/
|
*/
|
||||||
class Display {
|
class Display {
|
||||||
public:
|
public:
|
||||||
Display(uint8_t addr = 0x27, uint8_t cols = 20, uint8_t rows = 4);
|
Display();
|
||||||
|
|
||||||
void begin();
|
void begin();
|
||||||
void clear();
|
void clear();
|
||||||
@@ -17,7 +18,5 @@ class Display {
|
|||||||
|
|
||||||
LiquidCrystal_I2C& getLCD();
|
LiquidCrystal_I2C& getLCD();
|
||||||
private:
|
private:
|
||||||
uint8_t rows;
|
|
||||||
uint8_t cols;
|
|
||||||
LiquidCrystal_I2C lcd;
|
LiquidCrystal_I2C lcd;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,8 +4,6 @@
|
|||||||
#include "display.hpp"
|
#include "display.hpp"
|
||||||
#include "menu.hpp"
|
#include "menu.hpp"
|
||||||
#include "pet.hpp"
|
#include "pet.hpp"
|
||||||
#define ACTION_INTERVAL 60000 // 1 minute
|
|
||||||
#define MAXIMUM_STAT 100
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Pet pet;
|
Pet pet;
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#define MENU_ITEM_COUNT 4
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include "constants.hpp"
|
||||||
#include "joystick.hpp"
|
#include "joystick.hpp"
|
||||||
|
|
||||||
class Menu {
|
class Menu {
|
||||||
@@ -12,6 +12,6 @@ class Menu {
|
|||||||
String& getItemAt(size_t index);
|
String& getItemAt(size_t index);
|
||||||
size_t getCurrentItemIndex() const;
|
size_t getCurrentItemIndex() const;
|
||||||
private:
|
private:
|
||||||
String items[MENU_ITEM_COUNT];
|
String items[LCD_ROWS];
|
||||||
int currentItem;
|
int currentItem;
|
||||||
};
|
};
|
||||||
|
|||||||
+6
-6
@@ -1,7 +1,7 @@
|
|||||||
#include "display.hpp"
|
#include "display.hpp"
|
||||||
|
|
||||||
// Initialize the LCD display with the specified number of columns and rows, using the I2C address.
|
// 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() {
|
void Display::begin() {
|
||||||
lcd.init();
|
lcd.init();
|
||||||
@@ -14,23 +14,23 @@ void Display::clear() {
|
|||||||
|
|
||||||
void Display::drawBuffer(String buffer[]) {
|
void Display::drawBuffer(String buffer[]) {
|
||||||
clear();
|
clear();
|
||||||
for (size_t i = 0; i < rows; i++) {
|
for (size_t i = 0; i < LCD_ROWS; i++) {
|
||||||
lcd.setCursor(0, 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) {
|
void Display::drawMenu(Menu &menu) {
|
||||||
clear();
|
clear();
|
||||||
size_t currentItemIndex = menu.getCurrentItemIndex();
|
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);
|
lcd.setCursor(0, i);
|
||||||
String item = menu.getItemAt(i);
|
String item = menu.getItemAt(i);
|
||||||
|
|
||||||
if (i == currentItemIndex) {
|
if (i == currentItemIndex) {
|
||||||
lcd.print("> " + item);
|
lcd.print(("> " + item).substring(0, LCD_COLS)); // Add a ">" to indicate the current item
|
||||||
} else {
|
} else {
|
||||||
lcd.print(" " + item);
|
lcd.print((" " + item).substring(0, LCD_COLS)); // Indent non-selected items
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -105,7 +105,7 @@ void Game::render() {
|
|||||||
|
|
||||||
// If the pet is dead, display a message and return
|
// If the pet is dead, display a message and return
|
||||||
if (!state.pet.isAlive) {
|
if (!state.pet.isAlive) {
|
||||||
String buffer[4] = {
|
String buffer[LCD_ROWS] = {
|
||||||
"Your pet has died",
|
"Your pet has died",
|
||||||
state.pet.getReasonForDeath(),
|
state.pet.getReasonForDeath(),
|
||||||
"Reset the device",
|
"Reset the device",
|
||||||
|
|||||||
+3
-3
@@ -4,7 +4,7 @@ Menu::Menu() : currentItem(0) {}
|
|||||||
|
|
||||||
void Menu::setItems(String* items) {
|
void Menu::setItems(String* items) {
|
||||||
this->currentItem = 0;
|
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];
|
this->items[i] = items[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,10 +12,10 @@ void Menu::setItems(String* items) {
|
|||||||
bool Menu::updateCurrentItem(JoystickDirection &direction) {
|
bool Menu::updateCurrentItem(JoystickDirection &direction) {
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case JoystickDirection::UP:
|
case JoystickDirection::UP:
|
||||||
currentItem = (currentItem - 1 + MENU_ITEM_COUNT) % MENU_ITEM_COUNT;
|
currentItem = (currentItem - 1 + LCD_ROWS) % LCD_ROWS;
|
||||||
return true;
|
return true;
|
||||||
case JoystickDirection::DOWN:
|
case JoystickDirection::DOWN:
|
||||||
currentItem = (currentItem + 1) % MENU_ITEM_COUNT;
|
currentItem = (currentItem + 1) % LCD_ROWS;
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user