Skip to content

Commit

Permalink
track multiple buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnyhaibin committed Sep 25, 2024
1 parent d3fbd74 commit 7f83c84
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions opendbc/car/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,23 @@ def apply_hysteresis(val: float, val_steady: float, hyst_gap: float) -> float:

class ButtonTracker:
def __init__(self):
self.prev_btn: int = 0
self.prev_buttons: dict = {}

def create_button_events(self, cur_btn: int, buttons_dict: dict[int, structs.CarState.ButtonEvent.Type],
unpressed_btn: int = 0) -> list[structs.CarState.ButtonEvent]:
events: list[structs.CarState.ButtonEvent] = []

if cur_btn == self.prev_btn:
return events
button = id(buttons_dict)
prev_btn = self.prev_buttons.get(button, unpressed_btn)

# Add events for button presses, multiple when a button switches without going to unpressed
for pressed, btn in ((False, self.prev_btn), (True, cur_btn)):
if btn != unpressed_btn:
events.append(structs.CarState.ButtonEvent(pressed=pressed,
type=buttons_dict.get(btn, ButtonType.unknown)))
if cur_btn != prev_btn:
# Add events for button presses, multiple when a button switches without going to unpressed
for pressed, btn in ((False, prev_btn), (True, cur_btn)):
if btn != unpressed_btn:
events.append(structs.CarState.ButtonEvent(pressed=pressed,
type=buttons_dict.get(btn, ButtonType.unknown)))

self.prev_btn = cur_btn
self.prev_buttons[button] = cur_btn
return events


Expand Down

0 comments on commit 7f83c84

Please sign in to comment.