Files
ardugotchi/src/display.cpp
T

69 lines
1.7 KiB
C++

#include "display.hpp"
// Initialize the LCD display with the specified number of columns and rows, using the I2C address.
Display::Display() : lcd(LCD_I2C_ADDRESS, LCD_COLS, LCD_ROWS) {}
void Display::begin() {
byte CUSTOM_CHAR_PET[] = {
B00100,
B01110,
B11111,
B10101,
B11111,
B11111,
B10001,
B11111
};
lcd.init();
lcd.backlight();
lcd.createChar(0, CUSTOM_CHAR_PET); // Create a custom character for the pet
}
void Display::clear() {
lcd.clear();
}
void Display::drawBuffer(String buffer[]) {
clear();
for (size_t i = 0; i < LCD_ROWS; i++) {
lcd.setCursor(0, i);
lcd.print(buffer[i].substring(0, LCD_COLS)); // Ensure we only print up to the number of columns
}
}
void Display::drawPet(Pet &pet) {
clear();
lcd.setCursor(0, 0);
lcd.print("Hunger: " + String(pet.getHunger()));
lcd.setCursor(0, 1);
lcd.print("Joy: " + String(pet.getJoy()));
lcd.setCursor(0, 2);
lcd.print("Energy: " + String(pet.getEnergy()));
lcd.setCursor(0, 3);
lcd.print("Cleanliness: " + String(pet.getCleanliness()));
lcd.setCursor(LCD_COLS - 2, LCD_ROWS / 2 - 1); // Position the pet
lcd.write(byte(0)); // Draw the custom pet character
}
void Display::drawMenu(Menu &menu) {
clear();
size_t currentItemIndex = menu.getCurrentItemIndex();
for (size_t i = 0; i < LCD_ROWS; i++) {
lcd.setCursor(0, i);
String item = menu.getItemAt(i);
if (i == currentItemIndex) {
lcd.print(("> " + item).substring(0, LCD_COLS)); // Add a ">" to indicate the current item
} else {
lcd.print((" " + item).substring(0, LCD_COLS)); // Indent non-selected items
}
}
}
LiquidCrystal_I2C& Display::getLCD() {
return lcd;
}