From f90fa6a737dabd42621078a085f8c56e8a96113b Mon Sep 17 00:00:00 2001 From: Andrey Date: Tue, 12 Dec 2023 23:56:32 -0500 Subject: [PATCH] refactoring: reducing code duplication --- firmware/controllers/can/can_vss.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/firmware/controllers/can/can_vss.cpp b/firmware/controllers/can/can_vss.cpp index e75ba7b00e..735ea21489 100644 --- a/firmware/controllers/can/can_vss.cpp +++ b/firmware/controllers/can/can_vss.cpp @@ -31,21 +31,24 @@ static expected look_up_can_id(can_vss_nbc_e type) { } } -/* Module specitifc processing functions */ +static int getTwoBytesLsb(const CANRxFrame& frame, int index) { + uint8_t low = frame.data8[index]; + uint8_t high = frame.data8[index + 1] & 0x0F; + return low | (high << 8); +} + +/* Module specific processing functions */ /* source: http://z4evconversion.blogspot.com/2016/07/completely-forgot-but-it-does-live-on.html */ float processBMW_e46(const CANRxFrame& frame) { // average the rear wheels since those are the driven ones (more accurate gear detection!) - uint16_t left = (((frame.data8[5] & 0x0f) << 8) | frame.data8[4]); - uint16_t right = (((frame.data8[7] & 0x0f) << 8) | frame.data8[6]); + uint16_t left = getTwoBytesLsb(frame, 4); + uint16_t right = getTwoBytesLsb(frame, 6); return (left + right) / (16 * 2); } float processBMW_e90(const CANRxFrame& frame) { - uint8_t low = frame.data8[0]; - uint8_t high = frame.data8[1] & 0x0F; - - return 0.1f * (low | (high << 8)); + return 0.1f * getTwoBytes(frame, 0); } float processW202(const CANRxFrame& frame) {