diff --git a/include/display.hpp b/include/display.hpp index ba3680f..7409aa2 100644 --- a/include/display.hpp +++ b/include/display.hpp @@ -10,6 +10,7 @@ class Display { Display(); bool begin(); + void drawJoystick(double x, double y, bool pressed); private: U8G2_SSD1306_128X64_NONAME_F_HW_I2C oled; }; diff --git a/src/display.cpp b/src/display.cpp index 338ba33..a577cfa 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -15,3 +15,20 @@ bool Display::begin() { 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(); +} diff --git a/src/main.cpp b/src/main.cpp index ce3efcc..caa221c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,10 @@ #include #include #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);