Skip to content

Commit 650968e

Browse files
Rocketctfacchinm
authored andcommitted
added support for deadzone on joystick due to rest position drift (#27)
1 parent 6d60bd5 commit 650968e

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
@@ -190,12 +190,21 @@ class ModulinoJoystick : public Module {
190190
bool update() {
191191
uint8_t buf[3];
192192
auto res = read((uint8_t*)buf, 3);
193-
auto ret = res && (buf[0] != last_status[0] || buf[1] != last_status[1] || buf[2] != last_status[2]);
194-
last_status[0] = buf[0];
195-
last_status[1] = buf[1];
193+
auto x = buf[0];
194+
auto y = buf[1];
195+
map_value(x, y);
196+
auto ret = res && (x != last_status[0] || buf[1] != y || buf[2] != last_status[2]);
197+
if (!ret) {
198+
return false;
199+
}
200+
last_status[0] = x;
201+
last_status[1] = y;
196202
last_status[2] = buf[2];
197203
return ret;
198204
}
205+
void setDeadZone(uint8_t dz_th) {
206+
_dz_threshold = dz_th;
207+
}
199208
PinStatus isPressed() {
200209
return last_status[2] ? HIGH : LOW;
201210
}
@@ -213,7 +222,14 @@ class ModulinoJoystick : public Module {
213222
}
214223
return 0xFF;
215224
}
225+
void map_value(uint8_t &x, uint8_t &y) {
226+
if (x > 128 - _dz_threshold && x < 128 + _dz_threshold && y > 128 - _dz_threshold && y < 128 + _dz_threshold) {
227+
x = 128;
228+
y = 128;
229+
}
230+
}
216231
private:
232+
uint8_t _dz_threshold = 26;
217233
uint8_t last_status[3];
218234
protected:
219235
uint8_t match[1] ={ 0x58 }; // same as fw main.c

0 commit comments

Comments
 (0)