Package com.reduxrobotics.canand
Class CanandUtils
java.lang.Object
com.reduxrobotics.canand.CanandUtils
Series of utility functions for CAN messaging and bit manipulation.
For more information, see https://docs.wpilib.org/en/stable/docs/software/can-devices/can-addressing.html
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic long
Shorthand forBitSet.toLongArray()[0]
, except zero length bitsets return 0.static long
bytesToLong
(byte[] data) Convert bytes to long without allocating new bytebuffers.static int
constructMessageId
(int deviceType, int prodId, int devId, int msgId) Construct a CAN message id to send to a Redux device.static float
extractFloat
(byte[] v, int bstart) Extracts a float from a byte array.static long
extractLong
(byte[] v, int start, int end, boolean signed) More scope-limited long-extracting helper that only works on up to 8-length byte arrays.static long
extractLong
(BitSet v, int start, int end, boolean signed) Extracts a long from aBitSet
, optionally sign-extending the output.static int
getApiIndex
(int fullId) Extracts the 5-bit API index from a full message id.static int
getDeviceId
(int fullId) Extracts 6-bit device id from a full message id This is the "CAN id" that end users will see and care about.static int
getDeviceType
(int fullId) Extracts 5-bit device type code from a full message idstatic int
getProdId
(int fullId) Extracts 5-bit product id/API class from a full message id Instead of doing a 6bit/4bit split for api class/api index, we use an even 5 bit split.static boolean
idMatches
(int idToCompare, int deviceType, int prodId, int devId) Checks if a full CAN id will match against device type, product id, and device id We use this to determine if a message is intended for a specific device.static BitSet
signExtend
(BitSet v, int s) static void
writeFloatToBytes
(float v, byte[] dest, int bstart) Writes a float to a byte array.
-
Constructor Details
-
CanandUtils
public CanandUtils()
-
-
Method Details
-
getDeviceType
public static int getDeviceType(int fullId) Extracts 5-bit device type code from a full message id- Parameters:
fullId
- the full 29-bit message id- Returns:
- the device type code
-
getProdId
public static int getProdId(int fullId) Extracts 5-bit product id/API class from a full message id Instead of doing a 6bit/4bit split for api class/api index, we use an even 5 bit split.- Parameters:
fullId
- the full 29-bit message id- Returns:
- the product id code
-
getApiIndex
public static int getApiIndex(int fullId) Extracts the 5-bit API index from a full message id. Instead of doing a 6bit/4bit split for api class/api index, we use an even 5 bit split.- Parameters:
fullId
- the full 29-bit message id- Returns:
- the product id code
-
getDeviceId
public static int getDeviceId(int fullId) Extracts 6-bit device id from a full message id This is the "CAN id" that end users will see and care about.- Parameters:
fullId
- the full 29-bit message id- Returns:
- the device CAN id
-
idMatches
public static boolean idMatches(int idToCompare, int deviceType, int prodId, int devId) Checks if a full CAN id will match against device type, product id, and device id We use this to determine if a message is intended for a specific device.- Parameters:
idToCompare
- full 29-bit iddeviceType
- device id codeprodId
- product iddevId
- device id- Returns:
- whether the parameters matches the message id
-
constructMessageId
public static int constructMessageId(int deviceType, int prodId, int devId, int msgId) Construct a CAN message id to send to a Redux device.- Parameters:
deviceType
- the device id codeprodId
- API product iddevId
- CAN device idmsgId
- API message id- Returns:
- a 29-bit full CAN message id
-
bytesToLong
public static long bytesToLong(byte[] data) Convert bytes to long without allocating new bytebuffers.- Parameters:
data
- byte array- Returns:
- long made of the first 8 bytes
-
signExtend
- Parameters:
v
- the BitSet to sign extend.s
- the index of the first bit to apply sign extension to. Bits-1
will be sampled to determine the sign value.- Returns:
- v, the input BitSet
-
bsToLong
Shorthand forBitSet.toLongArray()[0]
, except zero length bitsets return 0.- Parameters:
v
- the inputBitSet
- Returns:
- v interpreted as a long
-
extractLong
Extracts a long from aBitSet
, optionally sign-extending the output.- Parameters:
v
- input BitSet to extract fromstart
- index of the first bit to extractend
- index after the last bit to extractsigned
- whether the long is signed- Returns:
- an extracted long
-
extractLong
public static long extractLong(byte[] v, int start, int end, boolean signed) More scope-limited long-extracting helper that only works on up to 8-length byte arrays. Use the BitSet version for values that are across 9 or more bytes.- Parameters:
v
- byte array that is 0-8 bytes longstart
- index of the first bit to extractend
- index after the last bit to extractsigned
- whether to sign extend the result- Returns:
- an extracted long
-
extractFloat
public static float extractFloat(byte[] v, int bstart) Extracts a float from a byte array.- Parameters:
v
- byte array, length should be >= 4bstart
- byte index to start reading from where bstart + 4 < v.length- Returns:
- float value
-
writeFloatToBytes
public static void writeFloatToBytes(float v, byte[] dest, int bstart) Writes a float to a byte array.- Parameters:
v
- the value to writedest
- the byte array to write intobstart
- index to start writing at
-