Skip to content

Commit 060556e

Browse files
authored
added support for deadzone on joystick due to rest position drift (#27)
1 parent 53aa89e commit 060556e

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/Modulino.h

+19-3
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,21 @@ class ModulinoJoystick : public Module {
166166
bool update() {
167167
uint8_t buf[3];
168168
auto res = read((uint8_t*)buf, 3);
169-
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];
169+
auto x = buf[0];
170+
auto y = buf[1];
171+
map_value(x, y);
172+
auto ret = res && (x != last_status[0] || buf[1] != y || buf[2] != last_status[2]);
173+
if (!ret) {
174+
return false;
175+
}
176+
last_status[0] = x;
177+
last_status[1] = y;
172178
last_status[2] = buf[2];
173179
return ret;
174180
}
181+
void setDeadZone(uint8_t dz_th) {
182+
_dz_threshold = dz_th;
183+
}
175184
PinStatus isPressed() {
176185
return last_status[2] ? HIGH : LOW;
177186
}
@@ -189,7 +198,14 @@ class ModulinoJoystick : public Module {
189198
}
190199
return 0xFF;
191200
}
201+
void map_value(uint8_t &x, uint8_t &y) {
202+
if (x > 128 - _dz_threshold && x < 128 + _dz_threshold && y > 128 - _dz_threshold && y < 128 + _dz_threshold) {
203+
x = 128;
204+
y = 128;
205+
}
206+
}
192207
private:
208+
uint8_t _dz_threshold = 26;
193209
uint8_t last_status[3];
194210
protected:
195211
uint8_t match[1] ={ 0x58 }; // same as fw main.c

0 commit comments

Comments
 (0)