Ver código fonte

fix(touch): add CPUQuota limit and fix evtest binary mode buffering

- Add CPUQuota=25% to systemd service as hard limit safety net
- Switch evtest to binary mode to fix select() buffering issues
- Skip evtest initialization output before monitoring for events
- Prevents tight loop during evtest startup device info dump

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
tuanchris 1 semana atrás
pai
commit
c4673a7ebc

+ 19 - 5
dune-weaver-touch/backend.py

@@ -1269,13 +1269,27 @@ class Backend(QObject):
 
                 if evtest_available:
                     # Use evtest which is more sensitive to single touches
-                    print("👆 Using evtest for touch detection (non-blocking)")
+                    # Run in binary mode to avoid Python text buffering issues with select()
+                    print("👆 Using evtest for touch detection (binary mode)")
                     process = subprocess.Popen(['sudo', 'evtest', touch_device],
                                              stdout=subprocess.PIPE,
-                                             stderr=subprocess.DEVNULL,
-                                             text=True)
+                                             stderr=subprocess.DEVNULL)
+
+                    # Skip initial device info output (wait for "Testing ..." line)
+                    # This prevents tight looping during evtest startup
+                    print("👆 Waiting for evtest initialization...")
+                    init_timeout = time.time() + 5  # 5 second timeout for init
+                    while time.time() < init_timeout:
+                        if self._stop_touch_monitor.is_set() or self._screen_on:
+                            break
+                        ready, _, _ = select.select([process.stdout], [], [], 0.5)
+                        if ready:
+                            line = process.stdout.readline()
+                            if line and b'Testing' in line:
+                                print("👆 evtest initialized, now monitoring for touch events")
+                                break
 
-                    # Wait for any event line using select for non-blocking reads
+                    # Now monitor for actual touch events
                     while not self._screen_on and not self._stop_touch_monitor.is_set():
                         # Check if process exited
                         if process.poll() is not None:
@@ -1287,7 +1301,7 @@ class Backend(QObject):
                             ready, _, _ = select.select([process.stdout], [], [], 0.5)
                             if ready:
                                 line = process.stdout.readline()
-                                if line and 'Event:' in line:
+                                if line and b'Event:' in line:
                                     print("👆 Touch detected via evtest - waking screen")
                                     self._turn_screen_on()
                                     self._reset_activity_timer()

+ 1 - 0
dune-weaver-touch/dune-weaver-touch.service

@@ -19,6 +19,7 @@ Environment=QT_QPA_EGLFS_KMS_ATOMIC=1
 # CPU isolation: Pin touch app to core 3, lower priority to prevent starving motion backend
 # Backend runs in Docker pinned to cores 0-2 for serial I/O timing reliability
 Nice=10
+CPUQuota=25%
 ExecStart=/usr/bin/taskset -c 3 /home/pi/dune-weaver-touch/venv/bin/python /home/pi/dune-weaver-touch/main.py
 Restart=always
 RestartSec=10