Class SensorHubI2CConnection


public class SensorHubI2CConnection extends I2CConnection
I2C connection to the configurable Arduino code that's prebuilt for this library.
  • Field Details

    • DEFAULT_BUS

      public static final int DEFAULT_BUS
      Default bus to use for the I2C connection.
      See Also:
    • REQUEST_TIMING

      public static final int REQUEST_TIMING
      How often to request data from the Arduino. This is in milliseconds.
      See Also:
    • HIGH

      public static final byte[] HIGH
      The value of "HIGH" in the Arduino.
    • LOW

      public static final byte[] LOW
      The value of "LOW" in the Arduino.
  • Constructor Details

    • SensorHubI2CConnection

      public SensorHubI2CConnection(String id, int bus, int device)
      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

      public static String generateId(int bus, int device)
      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

      public static SensorHubI2CConnection getOrCreateConnection(String id, int bus, int device)
      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

      public void subscribeSensor(Sensor sensor, int... pins)
      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.