Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
85714b4520
|
|||
|
924b2806fb
|
|||
|
fb7fbfe6c4
|
|||
|
dd643c1c10
|
@@ -5,6 +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)
|
||||||
|
|
||||||
## 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`.
|
||||||
|
|||||||
@@ -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;
|
||||||
|
};
|
||||||
@@ -19,3 +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
|
||||||
|
|||||||
@@ -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
@@ -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);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user