Skip to content

Commit 2511a6e

Browse files
committed
added support for deadzone on joystick due to rest position drift
1 parent 53aa89e commit 2511a6e

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/Modulino.h

+14-2
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,17 @@ class ModulinoJoystick : public Module {
167167
uint8_t buf[3];
168168
auto res = read((uint8_t*)buf, 3);
169169
auto ret = res && (buf[0] != last_status[0] || buf[1] != last_status[1] || buf[2] != last_status[2]);
170-
last_status[0] = buf[0];
171-
last_status[1] = buf[1];
170+
auto x = buf[0];
171+
auto y = buf[1];
172+
map_value(x, y);
173+
last_status[0] = x;
174+
last_status[1] = y;
172175
last_status[2] = buf[2];
173176
return ret;
174177
}
178+
void setDeadZone(uint8_t dz_th) {
179+
_dz_threshold = dz_th;
180+
}
175181
PinStatus isPressed() {
176182
return last_status[2] ? HIGH : LOW;
177183
}
@@ -189,6 +195,12 @@ class ModulinoJoystick : public Module {
189195
}
190196
return 0xFF;
191197
}
198+
void map_value(uint8_t &x, uint8_t &y) {
199+
if (x > 128 - _dz_threshold && x < 128 + _dz_threshold && y > 128 - _dz_threshold && y < 128 + _dz_threshold) {
200+
x = 128;
201+
y = 128;
202+
}
203+
}
192204
private:
193205
uint8_t last_status[3];
194206
protected:

0 commit comments

Comments
 (0)