Skip to content

Commit f805f7a

Browse files
committed
fix legion go gyro
1 parent 50130fa commit f805f7a

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/hhd/controller/physical/imu.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ def __init__(
224224
scale: Sequence[str | None] | None,
225225
mappings: dict[str, tuple[Axis, str | None, float, float | None]],
226226
update_trigger: bool = False,
227+
legion_fix: bool = False,
227228
) -> None:
228229
self.types = types
229230
self.attr = attr
@@ -233,6 +234,7 @@ def __init__(
233234
self.update_trigger = update_trigger
234235
self.fd = 0
235236
self.dev = None
237+
self.legion_fix = legion_fix
236238

237239
def open(self):
238240
sens_dir, type = find_sensor(self.types)
@@ -294,6 +296,10 @@ def produce(self, fds: Sequence[int]) -> Sequence[Event]:
294296
# TODO: Implement parsing iio fully, by adding shifting and cutoff
295297
d = data[ofs >> 3 : (ofs >> 3) + (se.storage_bits >> 3)]
296298
d = int.from_bytes(d, byteorder=se.endianness, signed=se.signed)
299+
if self.legion_fix and d == -124:
300+
# Legion go likes to overflow to -124 in both directions
301+
# skip this number to avoid jitters
302+
continue
297303
# d = d >> se.shift
298304
# d &= (1 << se.bits) - 1
299305
d = d * se.scale + se.offset
@@ -330,13 +336,16 @@ def __init__(self, freq=None, scale=None) -> None:
330336

331337

332338
class GyroImu(IioReader):
333-
def __init__(self, freq=None, scale=None, map=None) -> None:
339+
def __init__(
340+
self, freq=None, scale=None, map=None, legion_fix: bool = False
341+
) -> None:
334342
super().__init__(
335343
GYRO_NAMES,
336344
["anglvel"],
337345
[freq] if freq else None,
338346
[scale],
339347
map if map else GYRO_MAPPINGS,
348+
legion_fix=legion_fix,
340349
)
341350

342351

src/hhd/device/legion_go/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def controller_loop_xinput(conf: Config, should_exit: TEvent, updated: TEvent):
200200
"anglvel_z": ("gyro_y", "anglvel", conf["gyro_scaling"].to(int), None),
201201
"timestamp": ("gyro_ts", None, 1, None),
202202
}
203-
d_gyro = GyroImu(map=GYRO_MAPPINGS)
203+
d_gyro = GyroImu(map=GYRO_MAPPINGS, legion_fix=True)
204204

205205
# Inputs
206206
d_xinput = GenericGamepadEvdev(

0 commit comments

Comments
 (0)