#pragma once #include #include "constants.hpp" /* * 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(); JoystickDirection getDirection() const; private: class Switch { public: Switch(uint8_t pin); bool isPressed(); private: uint8_t pin; bool lastKnownState; bool lastStableState; uint64_t lastDebounceTime; }; uint8_t vrx; uint8_t vry; Switch sw; uint8_t xOffset; uint8_t yOffset; const uint8_t deadzone = 15; // Deadzone threshold to prevent jitter around the center position };