mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-07-23 18:42:10 +08:00
refactor
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
import cereal.messaging as messaging
|
||||
from enum import Enum
|
||||
from openpilot.common.params import Params
|
||||
from openpilot.common.swaglog import cloudlog
|
||||
from cereal import car, custom
|
||||
@@ -12,6 +13,11 @@ ButtonTypeSP = custom.CarStateSP.ButtonEvent.Type
|
||||
# TODO-SP: Use common python enum when we move to raylib?
|
||||
CUSTOM_MAPPING_BOOKMARK = 1 # Custom button mapping value for bookmark action
|
||||
|
||||
class ButtonPressType(Enum):
|
||||
NONE = 'NONE'
|
||||
LKAS = 'LKAS'
|
||||
CUSTOM = 'CUSTOM'
|
||||
|
||||
|
||||
def main():
|
||||
params = Params()
|
||||
@@ -21,49 +27,29 @@ def main():
|
||||
block_num = 0
|
||||
waiting_for_release = False
|
||||
early_stop_triggered = False
|
||||
custom_button_mapping = params.get("SteeringCustomButtonMapping")
|
||||
|
||||
while True:
|
||||
sm.update()
|
||||
|
||||
if sm.frame % 60 == 0: # update params once every 60 frames
|
||||
custom_button_mapping = params.get("SteeringCustomButtonMapping")
|
||||
|
||||
custom_mapped = custom_button_mapping == CUSTOM_MAPPING_BOOKMARK
|
||||
should_send_bookmark = False
|
||||
btn_pressed = False
|
||||
btn_pressed = get_button_event(sm, params)
|
||||
|
||||
# use custom button mapping if available
|
||||
use_custom = custom_mapped and sm.updated['carStateSP']
|
||||
# only allow the LKAS button to record feedback when MADS is disabled & custom button mapping is not set
|
||||
use_lkas = sm.updated['carState'] and sm['carState'].canValid and not sm['selfdriveStateSP'].mads.available and not custom_mapped
|
||||
|
||||
if use_custom:
|
||||
for be in sm['carStateSP'].buttonEvents:
|
||||
if be.type == ButtonTypeSP.customButton:
|
||||
btn_pressed = be.pressed
|
||||
elif use_lkas:
|
||||
for be in sm['carState'].buttonEvents:
|
||||
if be.type == ButtonType.lkas:
|
||||
btn_pressed = be.pressed
|
||||
|
||||
if btn_pressed:
|
||||
if btn_pressed is not ButtonPressType.NONE:
|
||||
if not should_record_audio:
|
||||
if params.get_bool("RecordAudioFeedback"): # Start recording on first press if toggle set
|
||||
should_record_audio = True
|
||||
block_num = 0
|
||||
waiting_for_release = False
|
||||
early_stop_triggered = False
|
||||
cloudlog.info(f"{'LKAS' if use_lkas else 'CUSTOM'} button pressed - starting 10-second audio feedback")
|
||||
cloudlog.info(f"{btn_pressed.value} button pressed - starting 10-second audio feedback")
|
||||
else:
|
||||
should_send_bookmark = True # immediately send bookmark if toggle false
|
||||
cloudlog.info(f"{'LKAS' if use_lkas else 'CUSTOM'} button pressed - bookmarking")
|
||||
cloudlog.info(f"{btn_pressed.value} button pressed - bookmarking")
|
||||
elif should_record_audio and not waiting_for_release: # Wait for release of second press to stop recording early
|
||||
waiting_for_release = True
|
||||
elif waiting_for_release: # Second press released
|
||||
waiting_for_release = False
|
||||
early_stop_triggered = True
|
||||
cloudlog.info(f"{'LKAS' if use_lkas else 'CUSTOM'} button released - ending recording early")
|
||||
cloudlog.info(f"{btn_pressed.value} button released - ending recording early")
|
||||
|
||||
if should_record_audio and sm.updated['rawAudioData']:
|
||||
raw_audio = sm['rawAudioData']
|
||||
@@ -88,5 +74,27 @@ def main():
|
||||
pm.send('userBookmark', msg)
|
||||
|
||||
|
||||
def get_button_event(sm, params):
|
||||
|
||||
custom_button_mapping = params.get("SteeringCustomButtonMapping")
|
||||
custom_mapped = custom_button_mapping == CUSTOM_MAPPING_BOOKMARK
|
||||
btn_pressed = ButtonPressType.NONE
|
||||
|
||||
# use custom button mapping if available
|
||||
use_custom = custom_mapped and sm.updated['carStateSP']
|
||||
# only allow the LKAS button to record feedback when MADS is disabled & custom button mapping is not set
|
||||
use_lkas = sm.updated['carState'] and sm['carState'].canValid and not sm['selfdriveStateSP'].mads.available and not custom_mapped
|
||||
|
||||
if use_custom:
|
||||
for be in sm['carStateSP'].buttonEvents:
|
||||
if be.type == ButtonTypeSP.customButton:
|
||||
btn_pressed = ButtonPressType.CUSTOM if be.pressed else ButtonPressType.NONE
|
||||
elif use_lkas:
|
||||
for be in sm['carState'].buttonEvents:
|
||||
if be.type == ButtonType.lkas:
|
||||
btn_pressed = ButtonPressType.LKAS if be.pressed else ButtonPressType.NONE
|
||||
|
||||
return btn_pressed
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user