33import math
44
55from opendbc .can import CANDefine , CANParser
6- from opendbc .car import Bus , create_button_events , structs
6+ from opendbc .car import Bus , create_button_events , structs , ButtonEvent , ButtonMap
77from opendbc .car .common .conversions import Conversions as CV
88from opendbc .car .hyundai .hyundaicanfd import CanBus
99from opendbc .car .hyundai .values import HyundaiFlags , CAR , DBC , Buttons , CarControllerParams
1717
1818# Cancel button can sometimes be ACC pause/resume button, main button can also enable on some cars
1919ENABLE_BUTTONS = (Buttons .RES_ACCEL , Buttons .SET_DECEL , Buttons .CANCEL )
20- BUTTONS_DICT = {Buttons .RES_ACCEL : ButtonType .accelCruise , Buttons .SET_DECEL : ButtonType .decelCruise ,
21- Buttons .GAP_DIST : ButtonType .gapAdjustCruise , Buttons .CANCEL : ButtonType .cancel }
20+ BUTTON_MAPS : list [ButtonMap ] = (
21+ ButtonMap (ButtonType .accelCruise , Buttons .RES_ACCEL ),
22+ ButtonMap (ButtonType .decelCruise , Buttons .SET_DECEL ),
23+ ButtonMap (ButtonType .gapAdjustCruise , Buttons .GAP_DIST ),
24+ ButtonMap (ButtonType .cancel , Buttons .CANCEL )
25+ )
2226
2327
2428class CarState (CarStateBase ):
@@ -28,6 +32,7 @@ def __init__(self, CP):
2832
2933 self .cruise_buttons : deque = deque ([Buttons .NONE ] * PREV_BUTTON_SAMPLES , maxlen = PREV_BUTTON_SAMPLES )
3034 self .main_buttons : deque = deque ([Buttons .NONE ] * PREV_BUTTON_SAMPLES , maxlen = PREV_BUTTON_SAMPLES )
35+ self .main_btn_map = [ButtonMap (ButtonType .mainCruise )]
3136 self .lda_button = 0
3237
3338 self .gear_msg_canfd = "ACCELERATOR" if CP .flags & HyundaiFlags .EV else \
@@ -181,17 +186,16 @@ def update(self, can_parsers) -> structs.CarState:
181186 self .lkas11 = copy .copy (cp_cam .vl ["LKAS11" ])
182187 self .clu11 = copy .copy (cp .vl ["CLU11" ])
183188 self .steer_state = cp .vl ["MDPS12" ]["CF_Mdps_ToiActive" ] # 0 NOT ACTIVE, 1 ACTIVE
184- prev_cruise_buttons = self .cruise_buttons [- 1 ]
185- prev_main_buttons = self .main_buttons [- 1 ]
186- prev_lda_button = self .lda_button
187189 self .cruise_buttons .extend (cp .vl_all ["CLU11" ]["CF_Clu_CruiseSwState" ])
188190 self .main_buttons .extend (cp .vl_all ["CLU11" ]["CF_Clu_CruiseSwMain" ])
189191 if self .CP .flags & HyundaiFlags .HAS_LDA_BUTTON :
190192 self .lda_button = cp .vl ["BCM_PO_11" ]["LDA_BTN" ]
191193
192- ret .buttonEvents = [* create_button_events (self .cruise_buttons [- 1 ], prev_cruise_buttons , BUTTONS_DICT ),
193- * create_button_events (self .main_buttons [- 1 ], prev_main_buttons , {1 : ButtonType .mainCruise }),
194- * create_button_events (self .lda_button , prev_lda_button , {1 : ButtonType .lkas })]
194+ create_button_events (ret , [
195+ ButtonEvent (self .cruise_buttons [- 1 ], BUTTON_MAPS ),
196+ ButtonEvent (self .main_buttons [- 1 ], self .main_btn_map ),
197+ ButtonEvent (self .lda_button , self .lkas_btn_map )
198+ ])
195199
196200 ret .blockPcmEnable = not self .recent_button_interaction ()
197201
@@ -274,9 +278,6 @@ def update_canfd(self, can_parsers) -> structs.CarState:
274278 if self .CP .flags & HyundaiFlags .EV :
275279 ret .cruiseState .nonAdaptive = cp .vl ["MANUAL_SPEED_LIMIT_ASSIST" ]["MSLA_ENABLED" ] == 1
276280
277- prev_cruise_buttons = self .cruise_buttons [- 1 ]
278- prev_main_buttons = self .main_buttons [- 1 ]
279- prev_lda_button = self .lda_button
280281 self .cruise_buttons .extend (cp .vl_all [self .cruise_btns_msg_canfd ]["CRUISE_BUTTONS" ])
281282 self .main_buttons .extend (cp .vl_all [self .cruise_btns_msg_canfd ]["ADAPTIVE_CRUISE_MAIN_BTN" ])
282283 self .lda_button = cp .vl [self .cruise_btns_msg_canfd ]["LDA_BTN" ]
@@ -287,9 +288,11 @@ def update_canfd(self, can_parsers) -> structs.CarState:
287288 self .lfa_block_msg = copy .copy (cp_cam .vl ["CAM_0x362" ] if self .CP .flags & HyundaiFlags .CANFD_LKA_STEERING_ALT
288289 else cp_cam .vl ["CAM_0x2a4" ])
289290
290- ret .buttonEvents = [* create_button_events (self .cruise_buttons [- 1 ], prev_cruise_buttons , BUTTONS_DICT ),
291- * create_button_events (self .main_buttons [- 1 ], prev_main_buttons , {1 : ButtonType .mainCruise }),
292- * create_button_events (self .lda_button , prev_lda_button , {1 : ButtonType .lkas })]
291+ create_button_events (ret , [
292+ ButtonEvent (self .cruise_buttons [- 1 ], BUTTON_MAPS ),
293+ ButtonEvent (self .main_buttons [- 1 ], self .main_btn_map ),
294+ ButtonEvent (self .lda_button , self .lkas_btn_map )
295+ ])
293296
294297 ret .blockPcmEnable = not self .recent_button_interaction ()
295298
0 commit comments