Smart Camera ESP32
An AI-driven, real-time Sentry Turret platform leveraging asynchronous I/O and computer vision to deliver high-precision autonomous motion tracking on resource-constrained embedded hardware.
Loading...
Searching...
No Matches
joystick.h
1
9#pragma once
10
11#include <Arduino.h>
12
15#define JOYSTICK_DEADZONE 150
16#define JOYSTICK_RESOLUTION_BITS 12
17#define JOYSTICK_SAMPLES 5
18#define BUTTON_DEBOUNCE_MS 50
28{
29private:
30 uint8_t _pin_x;
31 uint8_t _pin_y;
32 uint8_t _pin_z;
33 int _deadzone;
34 int _center_x, _center_y;
35
36 // Button state for Z-axis
37 bool _last_btn_state;
38 unsigned long _last_debounce_time;
39
44 int _read_raw(uint8_t pin) const;
45
51 int _process_axis(int raw_val, int center) const;
52
59 int _map_speed(int val, int min_out, int max_out) const;
60
61public:
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){};
77
83 void begin();
92 int get_x() const { return _process_axis(this->_read_raw(this->_pin_x), this->_center_x); }
93
95 int get_y() const { return _process_axis(this->_read_raw(this->_pin_y), this->_center_y); }
96
101 bool is_active() const;
112 bool is_z_pressed();
113
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); }
130
135 int get_speed_y(int min_out = -255, int max_out = 255) { return this->_map_speed(get_y(), min_out, max_out); }
137};
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