This commit is contained in:
rusefillc 2024-02-26 14:51:01 -05:00
parent 66eadaf43d
commit 8bd5e5c6f5
3 changed files with 38 additions and 6 deletions

View File

@ -2,7 +2,7 @@
* @file can_msg_tx.cpp * @file can_msg_tx.cpp
* *
* CAN message transmission * CAN message transmission
* *
* @date Mar 13, 2020 * @date Mar 13, 2020
* @author Matthew Kennedy, (c) 2012-2020 * @author Matthew Kennedy, (c) 2012-2020
*/ */
@ -113,6 +113,7 @@ void CanTxMessage::setBus(size_t bus) {
busIndex = bus; busIndex = bus;
} }
// LSB Little-endian System, "Intel"
void CanTxMessage::setShortValue(uint16_t value, size_t offset) { void CanTxMessage::setShortValue(uint16_t value, size_t offset) {
m_frame.data8[offset] = value & 0xFF; m_frame.data8[offset] = value & 0xFF;
m_frame.data8[offset + 1] = value >> 8; m_frame.data8[offset + 1] = value >> 8;

View File

@ -2,7 +2,7 @@
* @file can_msg_tx.h * @file can_msg_tx.h
* *
* CAN message transmission * CAN message transmission
* *
* @date Mar 13, 2020 * @date Mar 13, 2020
* @author Matthew Kennedy, (c) 2012-2020 * @author Matthew Kennedy, (c) 2012-2020
*/ */
@ -17,7 +17,7 @@
/** /**
* Represent a message to be transmitted over CAN. * Represent a message to be transmitted over CAN.
* *
* Usage: * Usage:
* * Create an instance of CanTxMessage * * Create an instance of CanTxMessage
* * Set any data you'd like to transmit either using the subscript operator to directly access bytes, or any of the helper functions. * * Set any data you'd like to transmit either using the subscript operator to directly access bytes, or any of the helper functions.
@ -53,7 +53,7 @@ public:
uint8_t& operator[](size_t); uint8_t& operator[](size_t);
/** /**
* @brief Write a 16-bit short value to the buffer. Note: this writes in big endian byte order. * @brief Write a 16-bit short value to the buffer. Note: this writes in little endian byte order.
*/ */
void setShortValue(uint16_t value, size_t offset); void setShortValue(uint16_t value, size_t offset);
@ -98,8 +98,8 @@ public:
#if EFI_CAN_SUPPORT #if EFI_CAN_SUPPORT
/** /**
* Access members of the templated type. * Access members of the templated type.
* *
* So you can do: * So you can do:
* CanTxTyped<MyType> d; * CanTxTyped<MyType> d;
* d->memberOfMyType = 23; * d->memberOfMyType = 23;

View File

@ -0,0 +1,31 @@
package com.rusefi.core;
import org.junit.jupiter.api.Test;
import java.nio.ByteBuffer;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class FileUtilTest {
static void setShortValue(byte[] data8, int value, int offset) {
data8[offset] = (byte) (value & 0xFF);
data8[offset + 1] = (byte) (value >> 8);
}
@Test
public void damnItWhichOneIsLittleEndian() {
byte[] testArrayForLittleEndianByteBuffer = new byte[2];
// LSB Little-endian System, "Intel"
ByteBuffer littleEndianBuffer = FileUtil.littleEndianWrap(testArrayForLittleEndianByteBuffer, 0, 2);
short testValue = 0x1122;
littleEndianBuffer.putShort(testValue);
assertEquals(testArrayForLittleEndianByteBuffer[0], 0x22);
byte[] testArrayForJavaCodeWhichMimicsCanTxMessageSetShortValue = new byte[2];
setShortValue(testArrayForJavaCodeWhichMimicsCanTxMessageSetShortValue, testValue, 0);
assertEquals(testArrayForJavaCodeWhichMimicsCanTxMessageSetShortValue[0], 0x22);
assertEquals(testArrayForJavaCodeWhichMimicsCanTxMessageSetShortValue[1], testArrayForLittleEndianByteBuffer[1]);
}
}