From 153d90c2e5c246d44d765aa5cb6cd8f9bad4fd32 Mon Sep 17 00:00:00 2001 From: Jason Wen Date: Sun, 5 Oct 2025 21:35:41 -0400 Subject: [PATCH] Mazda: Parse cruise control buttons (#307) * Mazda: parse cruise control buttons * use them properly * lint * Reapply "Mazda: Intelligent Cruise Button Management support (#303)" (#306) This reverts commit e345c21761ad401ed352cbaff6158a119b736620. * should be append instead * should be extend * just btns --- opendbc/car/mazda/carstate.py | 14 ++++++-- opendbc/sunnypilot/car/mazda/__init__.py | 0 opendbc/sunnypilot/car/mazda/carstate_ext.py | 36 ++++++++++++++++++++ opendbc/sunnypilot/car/mazda/values.py | 20 +++++++++++ 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 opendbc/sunnypilot/car/mazda/__init__.py create mode 100644 opendbc/sunnypilot/car/mazda/carstate_ext.py create mode 100644 opendbc/sunnypilot/car/mazda/values.py diff --git a/opendbc/car/mazda/carstate.py b/opendbc/car/mazda/carstate.py index f594dce8..97542c84 100644 --- a/opendbc/car/mazda/carstate.py +++ b/opendbc/car/mazda/carstate.py @@ -4,12 +4,15 @@ from opendbc.car.common.conversions import Conversions as CV from opendbc.car.interfaces import CarStateBase from opendbc.car.mazda.values import DBC, LKAS_LIMITS +from opendbc.sunnypilot.car.mazda.carstate_ext import CarStateExt + ButtonType = structs.CarState.ButtonEvent.Type -class CarState(CarStateBase): +class CarState(CarStateBase, CarStateExt): def __init__(self, CP, CP_SP): - super().__init__(CP, CP_SP) + CarStateBase.__init__(self, CP, CP_SP) + CarStateExt.__init__(self, CP, CP_SP) can_define = CANDefine(DBC[CP.carFingerprint][Bus.pt]) self.shifter_values = can_define.dv["GEAR"]["GEAR"] @@ -113,8 +116,13 @@ class CarState(CarStateBase): self.cam_laneinfo = cp_cam.vl["CAM_LANEINFO"] ret.steerFaultPermanent = cp_cam.vl["CAM_LKAS"]["ERR_BIT_1"] == 1 + CarStateExt.update(self, ret, ret_sp, can_parsers) + # TODO: add button types for inc and dec - ret.buttonEvents = create_button_events(self.distance_button, prev_distance_button, {1: ButtonType.gapAdjustCruise}) + ret.buttonEvents = [ + *create_button_events(self.distance_button, prev_distance_button, {1: ButtonType.gapAdjustCruise}), + *self.button_events, + ] return ret, ret_sp diff --git a/opendbc/sunnypilot/car/mazda/__init__.py b/opendbc/sunnypilot/car/mazda/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendbc/sunnypilot/car/mazda/carstate_ext.py b/opendbc/sunnypilot/car/mazda/carstate_ext.py new file mode 100644 index 00000000..4686fcca --- /dev/null +++ b/opendbc/sunnypilot/car/mazda/carstate_ext.py @@ -0,0 +1,36 @@ + +""" +Copyright (c) 2021-, Haibin Wen, sunnypilot, and a number of other contributors. + +This file is part of sunnypilot and is licensed under the MIT License. +See the LICENSE.md file in the root directory for more details. +""" + +from enum import StrEnum + +from opendbc.car import Bus, structs +from opendbc.can.parser import CANParser +from opendbc.sunnypilot.car.mazda.values import BUTTONS + + +class CarStateExt: + def __init__(self, CP, CP_SP): + self.CP = CP + self.CP_SP = CP_SP + + self.button_events = [] + self.button_states = {button.event_type: False for button in BUTTONS} + + def update(self, ret: structs.CarState, ret_sp: structs.CarStateSP, can_parsers: dict[StrEnum, CANParser]): + cp = can_parsers[Bus.pt] + + button_events = [] + for button in BUTTONS: + state = (cp.vl[button.can_addr][button.can_msg] in button.values) + if self.button_states[button.event_type] != state: + event = structs.CarState.ButtonEvent.new_message() + event.type = button.event_type + event.pressed = state + button_events.append(event) + self.button_states[button.event_type] = state + self.button_events = button_events diff --git a/opendbc/sunnypilot/car/mazda/values.py b/opendbc/sunnypilot/car/mazda/values.py new file mode 100644 index 00000000..d2b9c597 --- /dev/null +++ b/opendbc/sunnypilot/car/mazda/values.py @@ -0,0 +1,20 @@ +""" +Copyright (c) 2021-, Haibin Wen, sunnypilot, and a number of other contributors. + +This file is part of sunnypilot and is licensed under the MIT License. +See the LICENSE.md file in the root directory for more details. +""" + +from collections import namedtuple + +from opendbc.car import structs + +ButtonType = structs.CarState.ButtonEvent.Type +Button = namedtuple('Button', ['event_type', 'can_addr', 'can_msg', 'values']) + +BUTTONS = [ + Button(ButtonType.accelCruise, "CRZ_BTNS", "SET_P", [1]), + Button(ButtonType.decelCruise, "CRZ_BTNS", "SET_M", [1]), + Button(ButtonType.cancel, "CRZ_BTNS", "CAN_OFF", [1]), + Button(ButtonType.resumeCruise, "CRZ_BTNS", "RES", [1]), +]