32 lines
907 B
C++
32 lines
907 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).
|
|
*
|
|
* TODO: Add "deadzone" handling to prevent small joystick movements from being registered as input.
|
|
*/
|
|
class Joystick {
|
|
public:
|
|
Joystick();
|
|
|
|
double getX() const;
|
|
double getY() const;
|
|
bool isPressed() const;
|
|
private:
|
|
uint8_t vrx;
|
|
uint8_t vry;
|
|
uint8_t sw;
|
|
|
|
const uint8_t xOffset = 13;
|
|
const uint8_t yOffset = 10;
|
|
};
|