From dfc400440cdfd00a37e344ebfad72c040cfab3f0 Mon Sep 17 00:00:00 2001 From: rusefillc Date: Thu, 15 Feb 2024 16:14:54 -0500 Subject: [PATCH] only: lua lib huh --- firmware/controllers/lua/lua_lib.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/firmware/controllers/lua/lua_lib.h b/firmware/controllers/lua/lua_lib.h index b13c0644d0..a8f1c8d3cc 100644 --- a/firmware/controllers/lua/lua_lib.h +++ b/firmware/controllers/lua/lua_lib.h @@ -107,7 +107,7 @@ function getBitRange(data, bitIndex, bitWidth) \n\ return (value >> shift) & mask \n\ end \n\ " - +// todo: do we have a defect with indexing here?! #define GET_BIT_RANGE_MSB " \ function getBitRangeMsb(data, bitIndex, bitWidth) \n\ local byteIndex = bitIndex >> 3 \n\ @@ -138,6 +138,23 @@ function setBitRange(data, totalBitIndex, bitWidth, value) \ end \n\ " +#define SET_BIT_RANGE_MSB " \ +function setBitRangeMsb(data, totalBitIndex, bitWidth, value) \ + local byteIndex = totalBitIndex >> 3 \ + local bitInByteIndex = totalBitIndex - byteIndex * 8 \ + if (bitInByteIndex + bitWidth > 8) then \ + local bitsToHandleNow = 8 - bitInByteIndex \ + setBitRange(data, totalBitIndex + bitsToHandleNow, bitWidth - bitsToHandleNow, value >> bitsToHandleNow) \ + bitWidth = bitsToHandleNow \ + end \ + local mask = (1 << bitWidth) - 1 \ + data[1 + byteIndex] = data[1 + byteIndex] & (~(mask << bitInByteIndex)) \ + local maskedValue = value & mask \ + local shiftedValue = maskedValue << bitInByteIndex \ + data[1 + byteIndex] = data[1 + byteIndex] | shiftedValue \ +end \n\ +" + #define HYUNDAI_SUM_NIBBLES "\ function hyundaiSumNibbles(data, seed) \n\ local sum = seed \n\