Skip to content

Commit afbf7d3

Browse files
Rocketctfacchinm
authored andcommitted
added support for deadzone on joystick due to rest position drift (#27)
1 parent 4d9593c commit afbf7d3

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/Modulino.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,21 @@ class ModulinoJoystick : public Module {
189189
bool update() {
190190
uint8_t buf[3];
191191
auto res = read((uint8_t*)buf, 3);
192-
auto ret = res && (buf[0] != last_status[0] || buf[1] != last_status[1] || buf[2] != last_status[2]);
193-
last_status[0] = buf[0];
194-
last_status[1] = buf[1];
192+
auto x = buf[0];
193+
auto y = buf[1];
194+
map_value(x, y);
195+
auto ret = res && (x != last_status[0] || y != last_status[1] || buf[2] != last_status[2]);
196+
if (!ret) {
197+
return false;
198+
}
199+
last_status[0] = x;
200+
last_status[1] = y;
195201
last_status[2] = buf[2];
196202
return ret;
197203
}
204+
void setDeadZone(uint8_t dz_th) {
205+
_dz_threshold = dz_th;
206+
}
198207
PinStatus isPressed() {
199208
return last_status[2] ? HIGH : LOW;
200209
}
@@ -212,7 +221,14 @@ class ModulinoJoystick : public Module {
212221
}
213222
return 0xFF;
214223
}
224+
void map_value(uint8_t &x, uint8_t &y) {
225+
if (x > 128 - _dz_threshold && x < 128 + _dz_threshold && y > 128 - _dz_threshold && y < 128 + _dz_threshold) {
226+
x = 128;
227+
y = 128;
228+
}
229+
}
215230
private:
231+
uint8_t _dz_threshold = 26;
216232
uint8_t last_status[3];
217233
protected:
218234
uint8_t match[1] = { 0x58 }; // same as fw main.c

0 commit comments

Comments
 (0)