feat(display): migrate to HD44780 character lcd

This commit is contained in:
2026-04-26 03:11:07 +02:00
parent 48a1cd5ee6
commit 1a8047d5e0
5 changed files with 21 additions and 38 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ An open-source implementation of the Tamagotchi virtual pet game, designed to ru
## Bill of Materials ## Bill of Materials
- Arduino Nano (ATmega328P) - Arduino Nano (ATmega328P)
- KY-023 Joystick Module - KY-023 Joystick Module
- SSD1306 OLED Display (128x64) - HD44780 Character LCD
## Development ## Development
After cloning the repository, navigate to the project directory and run `make build` to compile the code. To upload the compiled firmware to your Arduino Nano, use `make upload`. After cloning the repository, navigate to the project directory and run `make build` to compile the code. To upload the compiled firmware to your Arduino Nano, use `make upload`.
+5 -5
View File
@@ -1,16 +1,16 @@
#pragma once #pragma once
#include <Arduino.h> #include <Arduino.h>
#include <U8g2lib.h> #include <LiquidCrystal_I2C.h>
/* /*
* A helper class to facilitate drawing on the SSD1306 OLED display using the U8g2 library. * A helper class to facilitate drawing on a HD44780 LCD display.
*/ */
class Display { class Display {
public: public:
Display(); Display(uint8_t addr = 0x27, uint8_t cols = 16, uint8_t rows = 2);
bool begin(); void begin();
void drawJoystick(double x, double y, bool pressed); void drawJoystick(double x, double y, bool pressed);
private: private:
U8G2_SSD1306_128X64_NONAME_F_HW_I2C oled; LiquidCrystal_I2C lcd;
}; };
+1 -1
View File
@@ -19,4 +19,4 @@ monitor_speed = 9600
; External libraries ; External libraries
lib_deps = lib_deps =
embeddedartistry/LibPrintf@1.2.13 embeddedartistry/LibPrintf@1.2.13
olikraus/U8g2@2.36.18 marcoschwartz/LiquidCrystal_I2C@1.1.4
+13 -24
View File
@@ -1,34 +1,23 @@
#include "display.hpp" #include "display.hpp"
// Initialize the OLED display with the appropriate settings for the SSD1306 128x64 display utilizing hardware I2C. // Initialize the LCD display with the specified number of columns and rows, using the I2C address.
// The constructor initializes the U8g2 library with the correct parameters for the display and communication method. Display::Display(uint8_t addr, uint8_t cols, uint8_t rows) : lcd(addr, cols, rows) {}
Display::Display() : oled(U8G2_R0, U8X8_PIN_NONE) {}
// Try to initialize the OLED display. If initialization fails, print an error message to the serial console and return false. void Display::begin() {
// Otherwise, set the default font and return true. lcd.init();
bool Display::begin() {
if (!oled.begin()) {
Serial.println("Failed to initialize OLED display");
return false;
}
oled.setFont(u8g2_font_profont22_mn);
return true;
} }
// TODO: Remove this method in the future // TODO: Remove this method in the future
void Display::drawJoystick(double x, double y, bool pressed) { void Display::drawJoystick(double x, double y, bool pressed) {
oled.clearBuffer(); lcd.clear();
// Map the joystick values from -100 to 100 to the display coordinates (0 to 128 for X and 0 to 64 for Y) lcd.setCursor(0, 0);
int16_t centerX = oled.getWidth() / 2; lcd.print("X:");
int16_t centerY = oled.getHeight() / 2; lcd.print(x, 2); // Print X value with 2 decimal places
int16_t posX = map(x, -100, 100, centerX - 50, centerX + 50); lcd.print(" Y:");
int16_t posY = map(y, -100, 100, centerY + 50, centerY - 50); // Invert Y-axis for display lcd.print(y, 2); // Print Y value with 2 decimal places
// Draw the joystick position as a filled circle lcd.setCursor(0, 1);
oled.setDrawColor(1); // Set color to white lcd.print("Pressed: ");
oled.drawCircle(posX, posY, pressed ? 10 : 5); // Larger circle if pressed lcd.print(pressed ? "Yes" : "No");
oled.sendBuffer();
} }
+1 -7
View File
@@ -12,13 +12,7 @@ void setup() {
delay(10); delay(10);
} }
// In case of OLED initialization failure, enter infinite loop display.begin();
if (!display.begin()) {
while (true) {
delay(1000);
}
}
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
} }