feat(joystick): add directional input
This commit is contained in:
+11
-1
@@ -12,13 +12,22 @@
|
||||
* 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;
|
||||
@@ -26,4 +35,5 @@ class Joystick {
|
||||
|
||||
const uint8_t xOffset = 13;
|
||||
const uint8_t yOffset = 10;
|
||||
const uint8_t deadzone = 15; // Deadzone threshold to prevent jitter around the center position
|
||||
};
|
||||
|
||||
@@ -20,3 +20,22 @@ double Joystick::getY() const {
|
||||
bool Joystick::isPressed() const {
|
||||
return digitalRead(sw) == LOW;
|
||||
}
|
||||
|
||||
// Determine the direction of the joystick based on the X and Y values, considering a deadzone to prevent jitter
|
||||
JoystickDirection Joystick::getDirection() const {
|
||||
double x = getX();
|
||||
double y = getY();
|
||||
|
||||
if (abs(x) < deadzone && abs(y) < deadzone) {
|
||||
return CENTER;
|
||||
} else if (y > deadzone) {
|
||||
return UP;
|
||||
} else if (y < -deadzone) {
|
||||
return DOWN;
|
||||
} else if (x > deadzone) {
|
||||
return RIGHT;
|
||||
} else if (x < -deadzone) {
|
||||
return LEFT;
|
||||
}
|
||||
return CENTER; // Default to CENTER if no direction is detected
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user