diff --git a/src/Modulino.h b/src/Modulino.h index 54c00a0..08fbf7b 100644 --- a/src/Modulino.h +++ b/src/Modulino.h @@ -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; } @@ -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