140 lines
4.6 KiB
Python
140 lines
4.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Copyright (c) 2025, Rick Lan
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, and/or sublicense,
|
|
for non-commercial purposes only, subject to the following conditions:
|
|
|
|
- The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
- Commercial use (e.g. use in a product, service, or activity intended to
|
|
generate revenue) is prohibited without explicit written permission from
|
|
the copyright holder.
|
|
|
|
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
"""
|
|
|
|
import subprocess
|
|
import time
|
|
from cereal import car, messaging
|
|
from openpilot.common.realtime import Ratekeeper
|
|
import threading
|
|
|
|
AudibleAlert = car.CarControl.HUDControl.AudibleAlert
|
|
|
|
class Beepd:
|
|
def __init__(self):
|
|
self.current_alert = AudibleAlert.none
|
|
self.enable_gpio()
|
|
|
|
def enable_gpio(self):
|
|
try:
|
|
subprocess.run("echo 42 | sudo tee /sys/class/gpio/export",
|
|
shell=True,
|
|
stderr=subprocess.DEVNULL,
|
|
stdout=subprocess.DEVNULL,
|
|
encoding='utf8')
|
|
except Exception:
|
|
pass
|
|
subprocess.run("echo \"out\" | sudo tee /sys/class/gpio/gpio42/direction",
|
|
shell=True,
|
|
stderr=subprocess.DEVNULL,
|
|
stdout=subprocess.DEVNULL,
|
|
encoding='utf8')
|
|
|
|
def _beep(self, on):
|
|
val = "1" if on else "0"
|
|
subprocess.run(f"echo \"{val}\" | sudo tee /sys/class/gpio/gpio42/value",
|
|
shell=True,
|
|
stderr=subprocess.DEVNULL,
|
|
stdout=subprocess.DEVNULL,
|
|
encoding='utf8')
|
|
|
|
def engage(self):
|
|
self._beep(True)
|
|
time.sleep(0.05)
|
|
self._beep(False)
|
|
|
|
def disengage(self):
|
|
for _ in range(2):
|
|
self._beep(True)
|
|
time.sleep(0.01)
|
|
self._beep(False)
|
|
time.sleep(0.01)
|
|
|
|
def warning(self):
|
|
for _ in range(3):
|
|
self._beep(True)
|
|
time.sleep(0.01)
|
|
self._beep(False)
|
|
time.sleep(0.01)
|
|
|
|
def warning_immediate(self):
|
|
for _ in range(5):
|
|
self._beep(True)
|
|
time.sleep(0.01)
|
|
self._beep(False)
|
|
time.sleep(0.01)
|
|
|
|
def dispatch_beep(self, func):
|
|
threading.Thread(target=func, daemon=True).start()
|
|
|
|
def update_alert(self, new_alert):
|
|
current_alert_played_once = self.current_alert == AudibleAlert.none
|
|
if self.current_alert != new_alert and (new_alert != AudibleAlert.none or current_alert_played_once):
|
|
self.current_alert = new_alert
|
|
# if new_alert == AudibleAlert.engage:
|
|
# self.dispatch_beep(self.engage)
|
|
# if new_alert == AudibleAlert.disengage:
|
|
# self.dispatch_beep(self.disengage)
|
|
if new_alert == AudibleAlert.warningImmediate:
|
|
self.dispatch_beep(self.warning_immediate)
|
|
|
|
def get_audible_alert(self, sm):
|
|
if sm.updated['selfdriveState']:
|
|
new_alert = sm['selfdriveState'].alertSound.raw
|
|
self.update_alert(new_alert)
|
|
|
|
def test_beepd_thread(self):
|
|
frame = 0
|
|
rk = Ratekeeper(20)
|
|
pm = messaging.PubMaster(['selfdriveState'])
|
|
while True:
|
|
cs = messaging.new_message('selfdriveState')
|
|
if frame == 20:
|
|
cs.selfdriveState.alertSound = AudibleAlert.engage
|
|
if frame == 40:
|
|
cs.selfdriveState.alertSound = AudibleAlert.disengage
|
|
if frame == 60:
|
|
cs.selfdriveState.alertSound = AudibleAlert.prompt
|
|
if frame == 80:
|
|
cs.selfdriveState.alertSound = AudibleAlert.disengage
|
|
if frame == 85:
|
|
cs.selfdriveState.alertSound = AudibleAlert.prompt
|
|
|
|
pm.send("selfdriveState", cs)
|
|
frame += 1
|
|
rk.keep_time()
|
|
|
|
def beepd_thread(self, test=False):
|
|
if test:
|
|
threading.Thread(target=self.test_beepd_thread, daemon=True).start()
|
|
|
|
sm = messaging.SubMaster(['selfdriveState'])
|
|
rk = Ratekeeper(20)
|
|
|
|
while True:
|
|
sm.update(0)
|
|
self.get_audible_alert(sm)
|
|
rk.keep_time()
|
|
|
|
def main():
|
|
s = Beepd()
|
|
s.beepd_thread(test=False)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|