hardwared: non blocking read for touch events (#34263)

* slow

* slow

* non blocking

* 10

* try

* simple

* int

* test

* get

* try

* clean

* read all

* nested

* simpler

* indent

* cleanup
pull/34267/head
Maxime Desroches 4 months ago committed by GitHub
parent a984903298
commit 71b02f8001
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 41
      system/hardware/hardwared.py

@ -1,4 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import fcntl
import os import os
import json import json
import queue import queue
@ -61,6 +62,8 @@ def set_offroad_alert_if_changed(offroad_alert: str, show_alert: bool, extra_tex
set_offroad_alert(offroad_alert, show_alert, extra_text) set_offroad_alert(offroad_alert, show_alert, extra_text)
def touch_thread(end_event): def touch_thread(end_event):
count = 0
pm = messaging.PubMaster(["touch"]) pm = messaging.PubMaster(["touch"])
event_format = "llHHi" event_format = "llHHi"
@ -68,23 +71,29 @@ def touch_thread(end_event):
event_frame = [] event_frame = []
with open("/dev/input/by-path/platform-894000.i2c-event", "rb") as event_file: with open("/dev/input/by-path/platform-894000.i2c-event", "rb") as event_file:
fcntl.fcntl(event_file, fcntl.F_SETFL, os.O_NONBLOCK)
while not end_event.is_set(): while not end_event.is_set():
event = event_file.read(event_size) if (count % int(1. / DT_HW)) == 0:
if event: event = event_file.read(event_size)
(sec, usec, etype, code, value) = struct.unpack(event_format, event) if event:
if etype != 0 or code != 0 or value != 0: (sec, usec, etype, code, value) = struct.unpack(event_format, event)
touch = log.Touch.new_message() if etype != 0 or code != 0 or value != 0:
touch.sec = sec touch = log.Touch.new_message()
touch.usec = usec touch.sec = sec
touch.type = etype touch.usec = usec
touch.code = code touch.type = etype
touch.value = value touch.code = code
event_frame.append(touch) touch.value = value
else: # end of frame, push new log event_frame.append(touch)
msg = messaging.new_message('touch', len(event_frame), valid=True) else: # end of frame, push new log
msg.touch = event_frame msg = messaging.new_message('touch', len(event_frame), valid=True)
pm.send('touch', msg) msg.touch = event_frame
event_frame = [] pm.send('touch', msg)
event_frame = []
continue
count += 1
time.sleep(DT_HW)
def hw_state_thread(end_event, hw_queue): def hw_state_thread(end_event, hw_queue):

Loading…
Cancel
Save