Class TrcSerialBusDevice

java.lang.Object
trclib.robotcore.TrcSerialBusDevice
Direct Known Subclasses:
FtcI2cDevice

public abstract class TrcSerialBusDevice extends Object
This class implements a platform independent serial bus device. This class is intended to be inherited by a platform dependent serial bus device such as I2C device or Serial Port device that provides synchronous methods to access the device. Optionally, it creates a request queue to allow both synchronous and asynchronous requests to be queued for processing. The request queue is processed by a separate thread for asynchronous access. If no request queue is specified, only synchronous requests are allowed.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    This class implements a request.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final String
     
    private final TrcEvent
     
     
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    TrcSerialBusDevice(String instanceName, boolean useRequestQueue)
    Constructor: Creates an instance of the object.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    asyncRead(Object requestId, int length, boolean repeat, TrcEvent event)
    This method is doing an asynchronous read from the device with the specified length to read.
    void
    asyncRead(Object requestId, int address, int length, boolean repeat, TrcEvent completionEvent)
    This method is doing an asynchronous read from the device with the specified length to read.
    void
    asyncRead(Object requestId, int address, int length, boolean repeat, TrcEvent.Callback callback, Object callbackContext)
    This method is doing an asynchronous read from the device with the specified length to read.
    void
    asyncRead(Object requestId, int address, int length, TrcEvent event)
    This method is doing an asynchronous read from the device with the specified length to read.
    void
    asyncRead(Object requestId, int length, TrcEvent event)
    This method is doing an asynchronous read from the device with the specified length to read.
    void
    asyncWrite(Object requestId, byte[] data, int length, TrcEvent event)
    This method is doing an asynchronous write to the device with the specified data and length
    void
    asyncWrite(Object requestId, int address, byte[] data, int length, TrcEvent completionEvent)
    This method is doing an asynchronous write to the device with the specified data and length
    void
    asyncWrite(Object requestId, int address, byte[] data, int length, TrcEvent.Callback callback, Object callbackContext)
    This method is doing an asynchronous write to the device with the specified data and length
    boolean
    This method checks if the serial bus device is enabled.
    void
    preemptiveWrite(int address, byte[] data, int length)
    This method writes the data to the device preemptively bypassing the queue.
    abstract byte[]
    readData(int address, int length)
    This method is called to read data from the device synchronously with the specified length.
    private void
    This method processes a request.
    void
    sendByteCommand(int address, byte command, boolean waitForCompletion)
    This method sends a byte command to the device.
    void
    sendWordCommand(int address, short command, boolean waitForCompletion)
    This method sends a 16-bit command to the device.
    void
    setEnabled(boolean enabled)
    This method enables/disables the serial bus device thread.
    byte[]
    syncRead(int length)
    This method is doing a synchronous read from the device with the specified length to read.
    byte[]
    syncRead(int address, int length)
    This method is doing a synchronous read from the device with the specified length to read.
    int
    syncWrite(byte[] data, int length)
    This method is doing a synchronous write to the device with the specified data and length.
    int
    syncWrite(int address, byte[] data, int length)
    This method is doing a synchronous write to the device with the specified data and length.
    This method returns the instance name.
    abstract int
    writeData(int address, byte[] buffer, int length)
    This method is called to write data to the device synchronously with the specified data buffer and length.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • TrcSerialBusDevice

      public TrcSerialBusDevice(String instanceName, boolean useRequestQueue)
      Constructor: Creates an instance of the object.
      Parameters:
      instanceName - specifies the instance name.
  • Method Details

    • readData

      public abstract byte[] readData(int address, int length)
      This method is called to read data from the device synchronously with the specified length.
      Parameters:
      address - specifies the data address if any, can be -1 if no address is required.
      length - specifies the number of bytes to read.
      Returns:
      a byte array containing the data read.
    • writeData

      public abstract int writeData(int address, byte[] buffer, int length)
      This method is called to write data to the device synchronously with the specified data buffer and length.
      Parameters:
      address - specifies the data address if any, can be -1 if no address is required.
      buffer - specifies the buffer containing the data to be written to the device.
      length - specifies the number of bytes to write.
      Returns:
      number of bytes written.
    • toString

      public String toString()
      This method returns the instance name.
      Overrides:
      toString in class Object
      Returns:
      instance name.
    • setEnabled

      public void setEnabled(boolean enabled)
      This method enables/disables the serial bus device thread.
      Parameters:
      enabled - specifies true to enable device thread, false to disable.
    • isEnabled

      public boolean isEnabled()
      This method checks if the serial bus device is enabled.
      Returns:
      true if serial bus device is enabled, false if disabled.
    • syncRead

      public byte[] syncRead(int address, int length)
      This method is doing a synchronous read from the device with the specified length to read.
      Parameters:
      address - specifies the data address if any, can be -1 if no address is required.
      length - specifies the number of bytes to read.
      Returns:
      data read as an array of bytes.
    • syncRead

      public byte[] syncRead(int length)
      This method is doing a synchronous read from the device with the specified length to read.
      Parameters:
      length - specifies the number of bytes to read.
      Returns:
      data read as an array of bytes.
    • syncWrite

      public int syncWrite(int address, byte[] data, int length)
      This method is doing a synchronous write to the device with the specified data and length.
      Parameters:
      address - specifies the data address if any, can be -1 if no address is required.
      data - specifies the data to write to the device.
      length - specifies the number of bytes to write.
      Returns:
      number of bytes written.
    • syncWrite

      public int syncWrite(byte[] data, int length)
      This method is doing a synchronous write to the device with the specified data and length.
      Parameters:
      data - specifies the data to write to the device.
      length - specifies the number of bytes to write.
      Returns:
      number of bytes written.
    • asyncRead

      public void asyncRead(Object requestId, int address, int length, boolean repeat, TrcEvent completionEvent)
      This method is doing an asynchronous read from the device with the specified length to read.
      Parameters:
      requestId - specifies the request ID and is not interpreted by the TrcSerialBusDevice class. it is just passed back to the requester's notification handler.
      address - specifies the data address if any, can be -1 if no address is required.
      length - specifies the number of bytes to read.
      repeat - specifies true to re-queue the request when completed.
      completionEvent - specifies the event to signal when the request is completed, can be null if none specified.
    • asyncRead

      public void asyncRead(Object requestId, int address, int length, boolean repeat, TrcEvent.Callback callback, Object callbackContext)
      This method is doing an asynchronous read from the device with the specified length to read.
      Parameters:
      requestId - specifies the request ID and is not interpreted by the TrcSerialBusDevice class. it is just passed back to the requester's notification handler.
      address - specifies the data address if any, can be -1 if no address is required.
      length - specifies the number of bytes to read.
      repeat - specifies true to re-queue the request when completed.
      callback - specifies the callback handler when the request is completed.
      callbackContext - specifies the object to pass back to the callback.
    • asyncRead

      public void asyncRead(Object requestId, int address, int length, TrcEvent event)
      This method is doing an asynchronous read from the device with the specified length to read.
      Parameters:
      requestId - specifies the request ID and is not interpreted by the TrcSerialBusDevice class. it is just passed back to the requester's notification handler.
      address - specifies the data address if any, can be -1 if no address is required.
      length - specifies the number of bytes to read.
      event - specifies the event to signal when the request is completed, can be null if none specified.
    • asyncRead

      public void asyncRead(Object requestId, int length, boolean repeat, TrcEvent event)
      This method is doing an asynchronous read from the device with the specified length to read.
      Parameters:
      requestId - specifies the request ID and is not interpreted by the TrcSerialBusDevice class. it is just passed back to the requester's notification handler.
      length - specifies the number of bytes to read.
      repeat - specifies true to re-queue the request when completed.
      event - specifies the event to signal when the request is completed, can be null if none specified.
    • asyncRead

      public void asyncRead(Object requestId, int length, TrcEvent event)
      This method is doing an asynchronous read from the device with the specified length to read.
      Parameters:
      requestId - specifies the request context and is not interpreted by the TrcSerialBusDevice class. it is just passed back to the requester's notification handler.
      length - specifies the number of bytes to read.
      event - specifies the event to signal when the request is completed, can be null if none specified.
    • asyncWrite

      public void asyncWrite(Object requestId, int address, byte[] data, int length, TrcEvent completionEvent)
      This method is doing an asynchronous write to the device with the specified data and length
      Parameters:
      requestId - specifies the request ID and is not interpreted by the TrcSerialBusDevice class. it is just passed back to the requester's notification handler.
      address - specifies the data address if any, can be -1 if no address is required.
      data - specifies the buffer containing the data to write to the device.
      length - specifies the number of bytes to write.
      completionEvent - specifies the event to signal when the request is completed, can be null if none specified.
    • asyncWrite

      public void asyncWrite(Object requestId, int address, byte[] data, int length, TrcEvent.Callback callback, Object callbackContext)
      This method is doing an asynchronous write to the device with the specified data and length
      Parameters:
      requestId - specifies the request ID and is not interpreted by the TrcSerialBusDevice class. it is just passed back to the requester's notification handler.
      address - specifies the data address if any, can be -1 if no address is required.
      data - specifies the buffer containing the data to write to the device.
      length - specifies the number of bytes to write.
      callback - specifies the callback handler when the request is completed.
      callbackContext - specifies the object to pass back to the callback.
    • asyncWrite

      public void asyncWrite(Object requestId, byte[] data, int length, TrcEvent event)
      This method is doing an asynchronous write to the device with the specified data and length
      Parameters:
      requestId - specifies the request ID and is not interpreted by the TrcSerialBusDevice class. it is just passed back to the requester's notification handler.
      data - specifies the buffer containing the data to write to the device.
      length - specifies the number of bytes to write.
      event - specifies the event to signal when the request is completed, can be null if none specified.
    • preemptiveWrite

      public void preemptiveWrite(int address, byte[] data, int length)
      This method writes the data to the device preemptively bypassing the queue.
      Parameters:
      address - specifies the data address if any, can be -1 if no address is required.
      data - specifies the buffer containing the data to write to the device.
      length - specifies the number of bytes to write.
    • sendByteCommand

      public void sendByteCommand(int address, byte command, boolean waitForCompletion)
      This method sends a byte command to the device.
      Parameters:
      address - specifies the data address if any, can be -1 if no address is required.
      command - specifies the command byte.
      waitForCompletion - specifies true to wait for write completion.
    • sendWordCommand

      public void sendWordCommand(int address, short command, boolean waitForCompletion)
      This method sends a 16-bit command to the device.
      Parameters:
      address - specifies the data address if any, can be -1 if no address is required.
      command - specifies the 16-bit command.
      waitForCompletion - specifies true to wait for write completion.
    • requestHandler

      private void requestHandler(Object context)
      This method processes a request.
      Parameters:
      context - specifies the request entry to be processed.