Package trclib.robotcore
Class TrcI2cDevice
java.lang.Object
trclib.robotcore.TrcI2cDevice
This class implements a platform independent I2C device. Typically, this class is extended by a platform dependent
I2C device class. The platform dependent I2C device class must implement the abstract methods required by this
class. The abstract methods allow this class to perform platform independent operations on the I2C device.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
The client of this class provides this interface if it wants to be notified when a read or write operation has been completed.private static enum
Specifies the Port Command state machine states.private static class
This class implements an I2C device request. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate TrcI2cDevice.Request
private byte[]
private double
private final TrcTaskMgr.TaskObject
protected final String
private final TrcStateMachine<TrcI2cDevice.PortCommandState>
private final ConcurrentLinkedQueue<TrcI2cDevice.Request>
final TrcDbgTrace
-
Constructor Summary
ConstructorsConstructorDescriptionTrcI2cDevice
(String instanceName) Constructor: Creates an instance of the object. -
Method Summary
Modifier and TypeMethodDescriptionabstract byte[]
getData()
This method retrieves the data read from the device.private void
i2cDeviceTask
(TrcTaskMgr.TaskType taskType, TrcRobot.RunMode runMode, boolean slowPeriodicLoop) This method is called periodically to run the PortCommand state machines.abstract boolean
This method checks if the I2C port is in write mode.abstract boolean
This method checks if the I2C port is ready for bus transaction.void
read
(int regAddress, int length) This method queues the read request.void
read
(int regAddress, int length, TrcI2cDevice.CompletionHandler handler) This method queues the read request.void
read
(int regAddress, int length, TrcI2cDevice.CompletionHandler handler, double timeout) This method queues the read request.void
sendByteCommand
(int regAddress, byte command) This method sends a byte command to the device.abstract void
sendReadCommand
(int regAddress, int length) This method sends a read command to the device.void
sendWordCommand
(int regAddress, short command) This method sends a 16-bit command to the device.abstract void
sendWriteCommand
(int regAddress, int length, byte[] data) This method sends a write command to the device.private void
setTaskEnabled
(boolean enabled) This method enables/disables the internal port command state machine and its task.toString()
This method returns the instance name of the device.void
write
(int regAddress, int length, byte[] writeBuffer) This method queues the write request.void
write
(int regAddress, int length, byte[] writeBuffer, TrcI2cDevice.CompletionHandler handler) This method queues the write request.void
write
(int regAddress, int length, byte[] writeBuffer, TrcI2cDevice.CompletionHandler handler, double timeout) This method queues the write request.
-
Field Details
-
tracer
-
instanceName
-
i2cDeviceTaskObj
-
portCommandSM
-
requestQueue
-
currRequest
-
expiredTime
private double expiredTime -
dataRead
private byte[] dataRead
-
-
Constructor Details
-
TrcI2cDevice
Constructor: Creates an instance of the object.- Parameters:
instanceName
- specifies the instance name.
-
-
Method Details
-
isPortReady
public abstract boolean isPortReady()This method checks if the I2C port is ready for bus transaction.- Returns:
- true if port is ready, false otherwise.
-
isPortInWriteMode
public abstract boolean isPortInWriteMode()This method checks if the I2C port is in write mode.- Returns:
- true if port is in write mode, false otherwise.
-
sendReadCommand
public abstract void sendReadCommand(int regAddress, int length) This method sends a read command to the device.- Parameters:
regAddress
- specifies the register address.length
- specifies the number of bytes to read.
-
sendWriteCommand
public abstract void sendWriteCommand(int regAddress, int length, byte[] data) This method sends a write command to the device.- Parameters:
regAddress
- specifies the register address.length
- specifies the number of bytes to write.data
- specifies the data buffer containing the data to write to the device.
-
getData
public abstract byte[] getData()This method retrieves the data read from the device.- Returns:
- byte array containing the data read.
-
toString
This method returns the instance name of the device. -
setTaskEnabled
private void setTaskEnabled(boolean enabled) This method enables/disables the internal port command state machine and its task.- Parameters:
enabled
- specifies true to enable the state machine and task, false otherwise.
-
read
public void read(int regAddress, int length, TrcI2cDevice.CompletionHandler handler, double timeout) This method queues the read request.- Parameters:
regAddress
- specifies the register address to read from.length
- specifies the number of bytes to read.handler
- specifies the completion handler to call when done. Can be null if none needed.timeout
- specifies the timeout for the operation in seconds.
-
read
This method queues the read request.- Parameters:
regAddress
- specifies the register address to read from.length
- specifies the number of bytes to read.handler
- specifies the completion handler to call when done. Can be null if none needed.
-
read
public void read(int regAddress, int length) This method queues the read request.- Parameters:
regAddress
- specifies the register address to read from.length
- specifies the number of bytes to read.
-
write
public void write(int regAddress, int length, byte[] writeBuffer, TrcI2cDevice.CompletionHandler handler, double timeout) This method queues the write request.- Parameters:
regAddress
- specifies the register address to write to.length
- specifies the number of bytes to read.writeBuffer
- specifies the buffer containing the data to be written to the device.handler
- specifies the completion handler to call when done. Can be null if none needed.timeout
- specifies the timeout for the operation in seconds.
-
write
public void write(int regAddress, int length, byte[] writeBuffer, TrcI2cDevice.CompletionHandler handler) This method queues the write request.- Parameters:
regAddress
- specifies the register address to write to.length
- specifies the number of bytes to read.writeBuffer
- specifies the buffer containing the data to be written to the device.handler
- specifies the completion handler to call when done. Can be null if none needed.
-
write
public void write(int regAddress, int length, byte[] writeBuffer) This method queues the write request.- Parameters:
regAddress
- specifies the register address to write to.length
- specifies the number of bytes to read.writeBuffer
- specifies the buffer containing the data to be written to the device.
-
sendByteCommand
public void sendByteCommand(int regAddress, byte command) This method sends a byte command to the device.- Parameters:
regAddress
- specifies the register address to write to.command
- specifies the command byte.
-
sendWordCommand
public void sendWordCommand(int regAddress, short command) This method sends a 16-bit command to the device.- Parameters:
regAddress
- specifies the register address to write to.command
- specifies the 16-bit command.
-
i2cDeviceTask
private void i2cDeviceTask(TrcTaskMgr.TaskType taskType, TrcRobot.RunMode runMode, boolean slowPeriodicLoop) This method is called periodically to run the PortCommand state machines.- Parameters:
taskType
- specifies the type of task being run.runMode
- specifies the competition mode that is running.slowPeriodicLoop
- specifies true if it is running the slow periodic loop on the main robot thread, false otherwise.
-