15#define JOYSTICK_DEADZONE 150
16#define JOYSTICK_RESOLUTION_BITS 12
17#define JOYSTICK_SAMPLES 5
33 int _center_x, _center_y;
39 int _read_raw(uint8_t pin)
const;
46 int _process_axis(
int raw_val,
int center)
const;
54 int _map_speed(
int val,
int min_out,
int max_out)
const;
69 Joystick(uint8_t pin_x, uint8_t pin_y, uint8_t pin_z,
int deadzone = JOYSTICK_DEADZONE)
70 : _pin_x(pin_x), _pin_y(pin_y), _pin_z(pin_z), _deadzone(deadzone), _center_x(0), _center_y(0) {};
86 int get_x()
const {
return _process_axis(this->_read_raw(this->_pin_x), this->_center_x); }
89 int get_y()
const {
return _process_axis(this->_read_raw(this->_pin_y), this->_center_y); }
100 bool is_z_held()
const {
return digitalRead(this->_pin_z) == LOW; }
112 int get_speed_x(
int min_out = -255,
int max_out = 255) {
return this->_map_speed(
get_x(), min_out, max_out); }
118 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:27
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:69
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:100
int get_y() const
Definition joystick.h:89
int get_speed_y(int min_out=-255, int max_out=255)
Maps Y deflection to a motor speed range.
Definition joystick.h:118
int get_speed_x(int min_out=-255, int max_out=255)
Maps X deflection to a motor speed range.
Definition joystick.h:112
int get_x() const
Definition joystick.h:86