reducing code duplication, making API available

This commit is contained in:
rusefillc 2024-09-19 11:06:48 -04:00
parent 591daa471d
commit df87937e30
4 changed files with 19 additions and 16 deletions

View File

@ -16,7 +16,7 @@
#if EFI_CAN_SUPPORT
#include "can.h"
#include "can_rx.h"
#include "obd2.h"
#include "can_sensor.h"
#include "can_vss.h"
@ -117,12 +117,16 @@ void registerCanSensor(CanSensorBase& sensor) {
#define MM5_10_MB_YAW_Y_CANID 0x150
#define MM5_10_MB_ROLL_X_CANID 0x151
static uint16_t getLSB_intel(const CANRxFrame& frame, int offset) {
uint16_t getTwoBytesLsb(const CANRxFrame& frame, int offset) {
return (frame.data8[offset + 1] << 8) + frame.data8[offset];
}
uint16_t getTwoBytesMsb(const CANRxFrame& frame, int offset) {
return (frame.data8[offset] << 8) + frame.data8[offset + 1];
}
static int16_t getShiftedLSB_intel(const CANRxFrame& frame, int offset) {
return getLSB_intel(frame, offset) - 0x8000;
return getTwoBytesLsb(frame, offset) - 0x8000;
}
static void processCanRxImu_BoschM5_10_YawY(const CANRxFrame& frame) {

View File

@ -0,0 +1,7 @@
#include "pch.h"
#include "can.h"
#pragma once
uint16_t getTwoBytesLsb(const CANRxFrame& frame, int offset);
uint16_t getTwoBytesMsb(const CANRxFrame& frame, int offset);

View File

@ -10,7 +10,7 @@
#include "pch.h"
#if EFI_CAN_SUPPORT
#include "can.h"
#include "can_rx.h"
#include "dynoview.h"
#include "stored_value_sensor.h"
@ -73,18 +73,6 @@ static uint16_t look_up_second_vss_can_id(can_vss_nbc_e type) {
}
}
static int getTwoBytesLsb(const CANRxFrame& frame, int index) {
uint8_t low = frame.data8[index];
uint8_t high = frame.data8[index + 1] & 0xFF;
return low | (high << 8);
}
static int getTwoBytesMsb(const CANRxFrame& frame, int index) {
uint8_t low = frame.data8[index + 1];
uint8_t high = frame.data8[index] & 0x0FF;
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) {
@ -113,6 +101,7 @@ float processBMW_e90(const CANRxFrame& frame) {
}
float processW202(const CANRxFrame& frame) {
// todo: reuse one of the getTwoBytes methods!
uint16_t tmp = (frame.data8[2] << 8);
tmp |= frame.data8[3];
return tmp * 0.0625;

View File

@ -65,6 +65,7 @@ end \
"
// LSB (Least Significant Byte comes first) "Intel"
// see also getTwoBytesLsb
#define TWO_BYTES_LSB "function getTwoBytesLSB(data, offset, factor)\
return (data[offset + 2] * 256 + data[offset + 1]) * factor \n\
end\n\
@ -80,11 +81,13 @@ end \
"
// MOTOROLA order, MSB (Most Significant Byte/Big Endian) comes first.
// see also getTwoBytesMsb
#define TWO_BYTES_MSB "function getTwoBytesMSB(data, offset, factor) \
return (data[offset + 1] * 256 + data[offset + 2]) * factor \
end \
"
// see also CanTxMessage#setShortValueMsb
#define SET_TWO_BYTES_MSB " function setTwoBytesMsb(data, offset, value) \
value = math.floor(value) \
data[offset + 1] = value >> 8 \