feat(display): add display demo
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
+10
-1
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user