Compare commits

..

4 Commits

Author SHA1 Message Date
zvonimir 85714b4520 docs: add SSD1306 display to bill of materials 2026-04-24 15:48:23 +02:00
zvonimir 924b2806fb feat(display): add display demo 2026-04-24 15:48:11 +02:00
zvonimir fb7fbfe6c4 feat(display): add display class 2026-04-24 15:46:48 +02:00
zvonimir dd643c1c10 chore: add U8g2 library 2026-04-24 15:46:32 +02:00
5 changed files with 62 additions and 1 deletions
+1
View File
@@ -5,6 +5,7 @@ An open-source implementation of the Tamagotchi virtual pet game, designed to ru
## Bill of Materials
- Arduino Nano (ATmega328P)
- KY-023 Joystick Module
- SSD1306 OLED Display (128x64)
## 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`.
+16
View File
@@ -0,0 +1,16 @@
#pragma once
#include <Arduino.h>
#include <U8g2lib.h>
/*
* A helper class to facilitate drawing on the SSD1306 OLED display using the U8g2 library.
*/
class Display {
public:
Display();
bool begin();
void drawJoystick(double x, double y, bool pressed);
private:
U8G2_SSD1306_128X64_NONAME_F_HW_I2C oled;
};
+1
View File
@@ -19,3 +19,4 @@ monitor_speed = 9600
; External libraries
lib_deps =
embeddedartistry/LibPrintf@1.2.13
olikraus/U8g2@2.36.18
+34
View File
@@ -0,0 +1,34 @@
#include "display.hpp"
// Initialize the OLED display with the appropriate settings for the SSD1306 128x64 display utilizing hardware I2C.
// The constructor initializes the U8g2 library with the correct parameters for the display and communication method.
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.
// Otherwise, set the default font and return true.
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
void Display::drawJoystick(double x, double y, bool pressed) {
oled.clearBuffer();
// Map the joystick values from -100 to 100 to the display coordinates (0 to 128 for X and 0 to 64 for Y)
int16_t centerX = oled.getWidth() / 2;
int16_t centerY = oled.getHeight() / 2;
int16_t posX = map(x, -100, 100, centerX - 50, centerX + 50);
int16_t posY = map(y, -100, 100, centerY + 50, centerY - 50); // Invert Y-axis for display
// Draw the joystick position as a filled circle
oled.setDrawColor(1); // Set color to white
oled.drawCircle(posX, posY, pressed ? 10 : 5); // Larger circle if pressed
oled.sendBuffer();
}
+10 -1
View File
@@ -1,8 +1,10 @@
#include <Arduino.h>
#include <LibPrintf.h>
#include "joystick.hpp"
#include "display.hpp"
Joystick joystick;
Display display;
void setup() {
Serial.begin(9600);
@@ -10,8 +12,14 @@ void setup() {
delay(10);
}
// In case of OLED initialization failure, enter infinite loop
if (!display.begin()) {
while (true) {
delay(1000);
}
}
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("configured");
}
void loop() {
@@ -20,6 +28,7 @@ void loop() {
bool pressed = joystick.isPressed();
printf("X: %.2f | Y: %.2f | Pressed: %s\n", x, y, pressed ? "Yes" : "No");
display.drawJoystick(x, y, pressed);
digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);