mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-10-28 09:03:46 +08:00
a82935f326
Integrate onroad views, configurable widgets, local settings, steering-profile contracts, Bluetooth and Android Auto with their device and runtime services. Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> Co-authored-by: Daniel Koepping <elkoled@gmail.com> Co-authored-by: Danny <dannyirwin31@yahoo.com> Co-authored-by: DimitriChrysafis <Dimitri.chrysafis@gmail.com> Co-authored-by: Dom <168790843+firestar5683@users.noreply.github.com> Co-authored-by: Harald Schäfer <harald.the.engineer@gmail.com> Co-authored-by: Prabhaav Pillai <prabhaav@msu.edu> Co-authored-by: Shane Smiskol <shane@smiskol.com> Co-authored-by: dirwin31 <83434411+dirwin31@users.noreply.github.com> Co-authored-by: firestarsdog <229254897+firestarsdog@users.noreply.github.com> Co-authored-by: stef <19478336+stefpi@users.noreply.github.com> Co-authored-by: whoisdomi <dcantu83@gmail.com> Co-authored-by: Intelli <contact@intelli.software> Co-authored-by: pharmacomaniac <blittle65@gmail.com> Co-authored-by: prabhaavp <prabhaav@msu.edu>
54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
"""Publish already-observed native camera frames without UI-thread file I/O."""
|
|
|
|
import threading
|
|
|
|
from openpilot.starpilot.galaxy.camera_request import boot_ns, mark_frame
|
|
|
|
|
|
class CameraAvailability:
|
|
def __init__(self, *, clock=boot_ns, write=mark_frame):
|
|
self.clock, self.write = clock, write
|
|
self.last_frame_ns = None
|
|
self.last_queued_ns = None
|
|
self.pending = None
|
|
self.lock = threading.Lock()
|
|
self.changed = threading.Event()
|
|
self.stopped = False
|
|
self.worker = threading.Thread(target=self._run, name="camera-availability", daemon=True)
|
|
self.worker.start()
|
|
|
|
def observe(self):
|
|
now = self.clock()
|
|
self.last_frame_ns = now
|
|
if self.last_queued_ns is not None and 0 <= now - self.last_queued_ns < 1_000_000_000:
|
|
return
|
|
self.last_queued_ns = now
|
|
with self.lock:
|
|
if not self.stopped:
|
|
self.pending = now
|
|
self.changed.set()
|
|
|
|
def available(self):
|
|
return self.last_frame_ns is not None and 0 <= self.clock() - self.last_frame_ns <= 500_000_000
|
|
|
|
def _run(self):
|
|
while True:
|
|
self.changed.wait()
|
|
with self.lock:
|
|
if self.stopped:
|
|
return
|
|
stamp, self.pending = self.pending, None
|
|
self.changed.clear()
|
|
if stamp is not None:
|
|
try:
|
|
self.write(clock=lambda stamp=stamp: stamp)
|
|
except (OSError, ValueError):
|
|
pass
|
|
|
|
def close(self):
|
|
with self.lock:
|
|
self.stopped = True
|
|
self.pending = None
|
|
self.changed.set()
|
|
self.worker.join(timeout=.1)
|