Package me.autobot.lib.hardware.i2c
Class SensorHubI2CConnection
java.lang.Object
me.autobot.lib.hardware.Connection
me.autobot.lib.hardware.i2c.I2CConnection
me.autobot.lib.hardware.i2c.SensorHubI2CConnection
I2C connection to the configurable Arduino code that's prebuilt for this library.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault bus to use for the I2C connection.static final byte[]The value of "HIGH" in the Arduino.static final byte[]The value of "LOW" in the Arduino.static final intHow often to request data from the Arduino.Fields inherited from class me.autobot.lib.hardware.i2c.I2CConnection
default_bus, i2cVerboseLevel, THIS_DEVICE_ADDRESS -
Constructor Summary
ConstructorsConstructorDescriptionSensorHubI2CConnection(String id, int bus, int device) Creates a new I2C connection to the default Arduino code with the given bus and device address. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringgenerateId(int bus, int device) Generates an ID for the I2C connection for the Pi4J context.static SensorHubI2CConnectiongetOrCreateConnection(String id, int bus, int device) Gets or creates a connection to the I2C device with the given ID, bus, and device.voidping()Pings the I2C device.voidreadPin(int index) Sends a request to read the value of a specific pin.protected voidCreates a new reading thread that reads data from the I2C device.voidsubscribeSensor(Sensor sensor, int... pins) Subscribes the given sensor to the given sensor pins.voidsubscribeToPin(int pin) Sends a request to subscribe to a specific pin.voidwriteToPin(int index, byte[] value) Writes a byte list to a specific pin.voidwriteToPin(int index, float value) Writes a value to a specific pin.Methods inherited from class me.autobot.lib.hardware.i2c.I2CConnection
disableConnections, getBus, getDeviceAddress, read, write
-
Field Details
-
DEFAULT_BUS
public static final int DEFAULT_BUSDefault bus to use for the I2C connection.- See Also:
-
REQUEST_TIMING
public static final int REQUEST_TIMINGHow often to request data from the Arduino. This is in milliseconds.- See Also:
-
HIGH
public static final byte[] HIGHThe value of "HIGH" in the Arduino. -
LOW
public static final byte[] LOWThe value of "LOW" in the Arduino.
-
-
Constructor Details
-
SensorHubI2CConnection
Creates a new I2C connection to the default Arduino code with the given bus and device address.- Parameters:
id- The ID of the I2C connection.bus- The bus of the I2C connection.device- The device address of the I2C connection.
-
-
Method Details
-
generateId
Generates an ID for the I2C connection for the Pi4J context.- Parameters:
bus- The bus of the I2C connection.device- The device of the I2C connection.- Returns:
- The ID of the I2C connection.
-
getOrCreateConnection
Gets or creates a connection to the I2C device with the given ID, bus, and device.- Parameters:
id- The ID of the I2C device. Uses this as reference for the Pi4J context.bus- The bus of the I2C device.device- The device of the I2C device.- Returns:
- The I2C connection to the device.
-
subscribeSensor
Subscribes the given sensor to the given sensor pins.- Parameters:
sensor- The sensor to subscribe.pins- The pins to subscribe the sensor to.
-
setupReadThread
protected void setupReadThread()Creates a new reading thread that reads data from the I2C device. This is a separate thread that runs in the background.- See Also:
-
ping
public void ping()Pings the I2C device. Won't return anything, but will let the device know that the Pi is still connected. -
readPin
public void readPin(int index) Sends a request to read the value of a specific pin.- Parameters:
index- The index of the pin to read.
-
subscribeToPin
public void subscribeToPin(int pin) Sends a request to subscribe to a specific pin.- Parameters:
pin- The pin to subscribe to.
-
writeToPin
public void writeToPin(int index, float value) Writes a value to a specific pin. The value is a float.- Parameters:
index- The index of the pin to write to.value- The value to write to the pin.- See Also:
-
writeToPin
public void writeToPin(int index, byte[] value) Writes a byte list to a specific pin. Should be 4 bytes long (a float!)- Parameters:
index- The index of the pin to write to.value- The byte list (length=4) to write to the pin.
-