From fb51fc986fbfd30e4cd23c8725fd1eb314ad8a3b Mon Sep 17 00:00:00 2001 From: Rocketct Date: Tue, 15 Apr 2025 10:28:46 +0200 Subject: [PATCH 1/2] added support for deadzone on joystick due to rest position drift --- src/Modulino.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Modulino.h b/src/Modulino.h index 54c00a0..2ccf36b 100644 --- a/src/Modulino.h +++ b/src/Modulino.h @@ -167,11 +167,17 @@ class ModulinoJoystick : public Module { 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); + 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; } @@ -189,7 +195,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 From 3639b9f1a99820d110091fb64cf4bc4a14134b6f Mon Sep 17 00:00:00 2001 From: Rocketct Date: Wed, 16 Apr 2025 10:43:23 +0200 Subject: [PATCH 2/2] added check of updated values after mapping in order to avoid dead zone updating --- src/Modulino.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Modulino.h b/src/Modulino.h index 2ccf36b..08fbf7b 100644 --- a/src/Modulino.h +++ b/src/Modulino.h @@ -166,10 +166,13 @@ 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]); 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];