Skip to content

Commit

Permalink
refactoring: reducing code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
rusefillc committed Dec 13, 2023
1 parent 3d1285a commit 367a621
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions firmware/controllers/can/can_vss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,24 @@ static expected<uint16_t> 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 * getTwoBytesLsb(frame, 0);
}

float processW202(const CANRxFrame& frame) {
Expand Down

0 comments on commit 367a621

Please sign in to comment.