Files
ardugotchi/include/joystick.hpp
T

40 lines
1015 B
C++

#pragma once
#include <Arduino.h>
/*
* A helper class to read values from the KY-023 joystick module.
* It provides methods to get the X and Y positions of the joystick, as well as whether the button is pressed.
* The joystick is connected to the following pins:
* - VRx (X-axis) connected to A0
* - VRy (Y-axis) connected to A1
* - SW (button) connected to D4
*
* Note: The button is active LOW and requires a pull-up resistor, which can either be external (10kΩ)
* or the internal pull-up resistor of the microcontroller (default).
*/
enum JoystickDirection {
CENTER,
UP,
DOWN,
LEFT,
RIGHT
};
class Joystick {
public:
Joystick();
double getX() const;
double getY() const;
bool isPressed() const;
JoystickDirection getDirection() const;
private:
uint8_t vrx;
uint8_t vry;
uint8_t sw;
const uint8_t xOffset = 13;
const uint8_t yOffset = 10;
const uint8_t deadzone = 15; // Deadzone threshold to prevent jitter around the center position
};