feat(display): add display demo

This commit is contained in:
2026-04-24 15:48:11 +02:00
parent fb7fbfe6c4
commit 924b2806fb
3 changed files with 28 additions and 1 deletions
+1
View File
@@ -10,6 +10,7 @@ class Display {
Display(); Display();
bool begin(); bool begin();
void drawJoystick(double x, double y, bool pressed);
private: private:
U8G2_SSD1306_128X64_NONAME_F_HW_I2C oled; U8G2_SSD1306_128X64_NONAME_F_HW_I2C oled;
}; };
+17
View File
@@ -15,3 +15,20 @@ bool Display::begin() {
oled.setFont(u8g2_font_profont22_mn); oled.setFont(u8g2_font_profont22_mn);
return true; 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 <Arduino.h>
#include <LibPrintf.h> #include <LibPrintf.h>
#include "joystick.hpp" #include "joystick.hpp"
#include "display.hpp"
Joystick joystick; Joystick joystick;
Display display;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
@@ -10,8 +12,14 @@ void setup() {
delay(10); delay(10);
} }
// In case of OLED initialization failure, enter infinite loop
if (!display.begin()) {
while (true) {
delay(1000);
}
}
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
Serial.println("configured");
} }
void loop() { void loop() {
@@ -20,6 +28,7 @@ void loop() {
bool pressed = joystick.isPressed(); bool pressed = joystick.isPressed();
printf("X: %.2f | Y: %.2f | Pressed: %s\n", x, y, pressed ? "Yes" : "No"); printf("X: %.2f | Y: %.2f | Pressed: %s\n", x, y, pressed ? "Yes" : "No");
display.drawJoystick(x, y, pressed);
digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW); digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);