Class CanandDevice
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
Canandcoder
,Canandcolor
- Implement
getAddress()
(usually by instantiating aCanandAddress
in the constructor and returning it) - Implement
handleMessage(com.reduxrobotics.canand.CanandMessage)
which will be called asynchronously whenever new CAN messages matching the object'sCanandAddress
get received by the robot - Run super() in the constructor so
handleMessage(com.reduxrobotics.canand.CanandMessage)
actually gets called at runtime
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor for CanandDevices that just adds the device to the incoming CAN message listener. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
protected Long
confirmSetSetting
(byte settingIdx, long payload, double timeout) Potentially blocking operation to send a setting and wait for a report setting message to be received to confirm the operation.protected Long
fetchSetting
(byte settingIdx, double timeout) Fetches a setting from the device and returns the received result.abstract CanandAddress
Returns theCanandAddress
representing the combination of CAN bus and CAN device ID that this CanandDevice refers to.abstract void
A callback called when a Redux CAN message is received and should be parsed.protected void
handleSettingRecv
(byte settingIdx, long settingValue) Method to be called in ahandleMessage(com.reduxrobotics.canand.CanandMessage)
implementation to notifyconfirmSetSetting(byte,long,double)
calls of a setting value receipt.protected void
sendCANMessage
(int apiIndex, byte[] data) Directly sends a CAN message to the device.protected void
sendSettingCommand
(byte settingCmdIdx) Sends a setting command with no arguments.protected void
setSettingById
(int settingId, byte[] value) Directly sends a CAN message to aCanandDevice
to set a setting by index.protected void
setSettingById
(int settingId, long value) Directly sends a CAN message to aCanandDevice
to set a setting by index.
-
Constructor Details
-
CanandDevice
public CanandDevice()Default constructor for CanandDevices that just adds the device to the incoming CAN message listener.
-
-
Method Details
-
handleMessage
A callback called when a Redux CAN message is received and should be parsed. Subclasses ofCanandDevice
should override this to update their internal state accordingly.handleMessage will be called on all Redux CAN packets received by the vendordep that match the
CanandAddress
returned bygetAddress()
.- Parameters:
msg
- aCanandMessage
representing the received message.
-
getAddress
Returns theCanandAddress
representing the combination of CAN bus and CAN device ID that this CanandDevice refers to.Implementing device subclasses should likely construct a new
CanandAddress
in their constructor and return it here.- Returns:
- the
CanandAddress
for the device.
-
setSettingById
protected void setSettingById(int settingId, long value) Directly sends a CAN message to aCanandDevice
to set a setting by index. This function does not block nor check if a report settings message is sent in response.Device subclasses will usually have a more user-friendly settings interface, eliminating the need to call this function directly in the vast majority of cases.
- Parameters:
settingId
- the setting idvalue
- the raw numerical value. Only the lower 48 bits will be used.
-
setSettingById
protected void setSettingById(int settingId, byte[] value) Directly sends a CAN message to aCanandDevice
to set a setting by index. This function does not block nor check if a report settings message is sent in response.Device subclasses will usually have a more user-friendly settings interface, eliminating the need to call this function directly in the vast majority of cases.
- Parameters:
settingId
- the setting idvalue
- a value byte array. Up to the first 6 bytes will be used.
-
handleSettingRecv
protected void handleSettingRecv(byte settingIdx, long settingValue) Method to be called in ahandleMessage(com.reduxrobotics.canand.CanandMessage)
implementation to notifyconfirmSetSetting(byte,long,double)
calls of a setting value receipt.- Parameters:
settingIdx
- Setting index received (typicallymsg.getData()[0]
)settingValue
- Setting value -- one can useCanandUtils.extractLong(BitSet.valueOf(msg.getData()), 8, 56, false)
to get this value from a message
-
confirmSetSetting
Potentially blocking operation to send a setting and wait for a report setting message to be received to confirm the operation. In order for this method to work on subclasses,handleSettingRecv(byte, long)
must be called in itshandleMessage(com.reduxrobotics.canand.CanandMessage)
implementation whenCanandDeviceDetails.kMsg_ReportSetting
is received.- Parameters:
settingIdx
- Setting index to set and listen forpayload
- the long value to send. Only lower 48 bits are used.timeout
- the timeout to wait before giving up in seconds. Passing in 0 will return instantly (not block)- Returns:
- the value received by the report setting packet if existent or null otherwise. If timeout = 0, return "payload" (assume success)
-
fetchSetting
Fetches a setting from the device and returns the received result.- Parameters:
settingIdx
- Setting index to fetchtimeout
- timeout to wait before giving up in seconds. Passing in 0 will return null (failure)- Returns:
- value returned if successful or null otherwise.
-
sendSettingCommand
protected void sendSettingCommand(byte settingCmdIdx) Sends a setting command with no arguments.- Parameters:
settingCmdIdx
- the index of the setting command to send.
-
sendCANMessage
protected void sendCANMessage(int apiIndex, byte[] data) Directly sends a CAN message to the device.Implementing device classes should call this function to send messages to the device, or use
getAddress().sendCANMessage(int, byte[])
, which this aliases.- Parameters:
apiIndex
- the individual API index to value to senddata
- 1-8 byte bytes payload
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-