Skip to content

added support for deadzone on joystick due to rest position drift #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/Modulino.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,21 @@ class ModulinoJoystick : public Module {
bool update() {
uint8_t buf[3];
auto res = read((uint8_t*)buf, 3);
auto ret = res && (buf[0] != last_status[0] || buf[1] != last_status[1] || buf[2] != last_status[2]);
last_status[0] = buf[0];
last_status[1] = buf[1];
auto x = buf[0];
auto y = buf[1];
map_value(x, y);
auto ret = res && (x != last_status[0] || buf[1] != y || buf[2] != last_status[2]);
if (!ret) {
return false;
}
last_status[0] = x;
last_status[1] = y;
last_status[2] = buf[2];
return ret;
}
void setDeadZone(uint8_t dz_th) {
_dz_threshold = dz_th;
}
PinStatus isPressed() {
return last_status[2] ? HIGH : LOW;
}
Expand All @@ -189,7 +198,14 @@ class ModulinoJoystick : public Module {
}
return 0xFF;
}
void map_value(uint8_t &x, uint8_t &y) {
if (x > 128 - _dz_threshold && x < 128 + _dz_threshold && y > 128 - _dz_threshold && y < 128 + _dz_threshold) {
x = 128;
y = 128;
}
}
private:
uint8_t _dz_threshold = 26;
uint8_t last_status[3];
protected:
uint8_t match[1] ={ 0x58 }; // same as fw main.c
Expand Down