Class CanandDevice

java.lang.Object
com.reduxrobotics.canand.CanandDevice
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
Canandcoder, Canandcolor

public abstract class CanandDevice extends Object implements AutoCloseable
Base class for Redux CAN devices. The ReduxLib vendordep does all CAN message parsing in the Java/C++ classes themselves. CanandDevice provides general facilities for sending and receiving CAN messages (abstracted from the actual underlying buses) as well as helper functions and constants common for all Redux CAN products. Classes implementing CanandDevice need to do the following:
  • Constructor Details

    • CanandDevice

      public CanandDevice()
      Default constructor for CanandDevices that just adds the device to the incoming CAN message listener.
  • Method Details

    • handleMessage

      public abstract void handleMessage(CanandMessage msg)
      A callback called when a Redux CAN message is received and should be parsed. Subclasses of CanandDevice 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 by getAddress().

      Parameters:
      msg - a CanandMessage representing the received message.
    • getAddress

      public abstract CanandAddress getAddress()
      Returns the CanandAddress 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 a CanandDevice 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 id
      value - 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 a CanandDevice 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 id
      value - 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 a handleMessage(com.reduxrobotics.canand.CanandMessage) implementation to notify confirmSetSetting(byte,long,double) calls of a setting value receipt.
      Parameters:
      settingIdx - Setting index received (typically msg.getData()[0])
      settingValue - Setting value -- one can use CanandUtils.extractLong(BitSet.valueOf(msg.getData()), 8, 56, false) to get this value from a message
    • confirmSetSetting

      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. In order for this method to work on subclasses, handleSettingRecv(byte, long) must be called in its handleMessage(com.reduxrobotics.canand.CanandMessage) implementation when CanandDeviceDetails.kMsg_ReportSetting is received.
      Parameters:
      settingIdx - Setting index to set and listen for
      payload - 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

      protected Long fetchSetting(byte settingIdx, double timeout)
      Fetches a setting from the device and returns the received result.
      Parameters:
      settingIdx - Setting index to fetch
      timeout - 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 send
      data - 1-8 byte bytes payload
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable