15#define JOYSTICK_DEADZONE 150
16#define JOYSTICK_RESOLUTION_BITS 12
17#define JOYSTICK_SAMPLES 5
18#define BUTTON_DEBOUNCE_MS 50
34 int _center_x, _center_y;
38 unsigned long _last_debounce_time;
44 int _read_raw(uint8_t pin)
const;
51 int _process_axis(
int raw_val,
int center)
const;
59 int _map_speed(
int val,
int min_out,
int max_out)
const;
74 Joystick(uint8_t pin_x, uint8_t pin_y, uint8_t pin_z,
int deadzone = JOYSTICK_DEADZONE)
75 : _pin_x(pin_x), _pin_y(pin_y), _pin_z(pin_z), _deadzone(deadzone), _center_x(0), _center_y(0),
76 _last_btn_state(HIGH), _last_debounce_time(0){};
92 int get_x()
const {
return _process_axis(this->_read_raw(this->_pin_x), this->_center_x); }
95 int get_y()
const {
return _process_axis(this->_read_raw(this->_pin_y), this->_center_y); }
117 bool is_z_held()
const {
return digitalRead(this->_pin_z) == LOW; }
129 int get_speed_x(
int min_out = -255,
int max_out = 255) {
return this->_map_speed(
get_x(), min_out, max_out); }
135 int get_speed_y(
int min_out = -255,
int max_out = 255) {
return this->_map_speed(
get_y(), min_out, max_out); }
High-level interface for 12-bit analog joystick input.
Definition joystick.h:28
bool is_active() const
Checks if the joystick is currently deflected beyond the deadzone.
Definition joystick.cpp:93
Joystick(uint8_t pin_x, uint8_t pin_y, uint8_t pin_z, int deadzone=JOYSTICK_DEADZONE)
Construct a new Joystick object.
Definition joystick.h:74
void begin()
Configures hardware and performs auto-calibration.
Definition joystick.cpp:3
bool is_z_held() const
Check the current physical state of the button.
Definition joystick.h:117
int get_y() const
Definition joystick.h:95
bool is_z_pressed()
Detects a button press (Falling Edge).
Definition joystick.cpp:59
int get_speed_y(int min_out=-255, int max_out=255)
Maps Y deflection to a motor speed range.
Definition joystick.h:135
int get_speed_x(int min_out=-255, int max_out=255)
Maps X deflection to a motor speed range.
Definition joystick.h:129
int get_x() const
Definition joystick.h:92