Skip to content

Commit 43b8621

Browse files
committed
update implementation of vibrate on press
1 parent 56f2c79 commit 43b8621

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

src/hhd/controller/physical/evdev.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ def open(self) -> Sequence[int]:
327327
self.fd = self.dev.fd
328328
self.started = True
329329
self.effect_id = -1
330+
self.effect_on_press_id = -1
330331
self.queue = []
331332
except Exception as e:
332333
# Prevent leftover rules in case of error
@@ -365,7 +366,7 @@ def consume(self, events: Sequence[Event]):
365366

366367
for ev in events:
367368
match ev["type"]:
368-
case "rumble" | "vibrate_on_press":
369+
case "rumble":
369370
if not self.supports_vibration:
370371
continue
371372

@@ -384,7 +385,7 @@ def consume(self, events: Sequence[Event]):
384385
int(ev["weak_magnitude"] * 0xFFFF), 0xFFFF
385386
),
386387
)
387-
duration_ms = 10000 if ev["type"] == "rumble" else 1
388+
duration_ms = 10000
388389

389390
effect = ff.Effect(
390391
getattr(ecodes, "FF_RUMBLE"),
@@ -397,6 +398,38 @@ def consume(self, events: Sequence[Event]):
397398
self.effect_id = self.dev.upload_effect(effect)
398399
self.dev.write(getattr(ecodes, "EV_FF"), self.effect_id, 1)
399400

401+
case "vibrate_on_press":
402+
if not self.supports_vibration:
403+
continue
404+
405+
# Erase old effect
406+
if self.effect_on_press_id != -1:
407+
self.dev.erase_effect(self.effect_on_press_id)
408+
self.effect_on_press_id = -1
409+
410+
if ev["magnitude"] > 0:
411+
magnitude = min(
412+
int(ev["magnitude"] * 0xFFFF), 0xFFFF
413+
)
414+
415+
rumble = ff.Rumble(
416+
strong_magnitude=magnitude,
417+
weak_magnitude=magnitude,
418+
)
419+
420+
duration_ms = 80
421+
422+
effect = ff.Effect(
423+
getattr(ecodes, "FF_RUMBLE"),
424+
-1,
425+
0,
426+
ff.Trigger(0, 0),
427+
ff.Replay(duration_ms, 0),
428+
ff.EffectType(ff_rumble_effect=rumble),
429+
)
430+
self.effect_on_press_id = self.dev.upload_effect(effect)
431+
self.dev.write(getattr(ecodes, "EV_FF"), self.effect_on_press_id, 1)
432+
400433
def produce(self, fds: Sequence[int]) -> Sequence[Event]:
401434
out: list[Event] = []
402435
curr = time.time()
@@ -431,13 +464,13 @@ def produce(self, fds: Sequence[int]) -> Sequence[Event]:
431464
while can_read(self.fd):
432465
for e in self.dev.read():
433466
if self.vibrate_on_press > 0:
434-
vibration_delay = 0.001
435-
self.queue.append(({
436-
"type": "vibrate_on_press",
437-
"code": "main",
438-
"strong_magnitude": self.vibrate_on_press,
439-
"weak_magnitude": self.vibrate_on_press,
440-
}, curr + vibration_delay))
467+
out.append(
468+
{
469+
"type": "vibrate_on_press",
470+
"code": "main",
471+
"magnitude": self.vibrate_on_press,
472+
}
473+
)
441474

442475
if e.type == B("EV_KEY"):
443476
if e.code == B("KEY_LEFTMETA"):

0 commit comments

Comments
 (0)