Class AnalogInputDevice

All Implemented Interfaces:
DeviceEventConsumer<AnalogInputEvent>, Closeable, AutoCloseable, Runnable, Consumer<AnalogInputEvent>
Direct Known Subclasses:
GP2Y0A21YK, LDR, Potentiometer, TMP36

public class AnalogInputDevice
extends GpioInputDevice<AnalogInputEvent>
implements Runnable

The AnalogInputDevice base class encapsulates logic for interfacing with analog devices. This class provides access to unscaled (-1..1) and scaled (e.g. voltage, temperature, distance) readings. For scaled readings is important that the device factory is configured correctly - all raw analog readings are normalised (i.e. -1..1).

Note: The Raspberry Pi does not natively support analog input devices, see McpAdc for connecting to analog-to-digital converters.

Example: Temperature readings using an MCP3008 and TMP36:

MCP3008 TMP36

Code taken from TMP36Test:

 
try (McpAdc adc = new McpAdc(type, chipSelect);
	TMP36 tmp36 = new TMP36(adc, pin, vRef, tempOffset)) {
	for (int i=0; i<ITERATIONS; i++) {
		double tmp = tmp36.getTemperature();
		Logger.info("Temperature: {}", String.format("%.2f", Double.valueOf(tmp)));
		SleepUtil.sleepSeconds(.5);
	}
}

 
  • Constructor Details

    • AnalogInputDevice

      public AnalogInputDevice​(int gpio) throws RuntimeIOException
      Parameters:
      gpio - GPIO to which the device is connected.
      Throws:
      RuntimeIOException - If an I/O error occurred.
    • AnalogInputDevice

      public AnalogInputDevice​(int gpio, float range) throws RuntimeIOException
      Parameters:
      gpio - GPIO to which the device is connected.
      range - To be used for taking scaled readings for this device.
      Throws:
      RuntimeIOException - If an I/O error occurred.
    • AnalogInputDevice

      public AnalogInputDevice​(AnalogInputDeviceFactoryInterface deviceFactory, int gpio) throws RuntimeIOException
      Parameters:
      deviceFactory - The device factory to use to provision this device.
      gpio - GPIO to which the device is connected.
      Throws:
      RuntimeIOException - If an I/O error occurred.
    • AnalogInputDevice

      public AnalogInputDevice​(AnalogInputDeviceFactoryInterface deviceFactory, int gpio, float range) throws RuntimeIOException
      Parameters:
      deviceFactory - The device factory to use to provision this device.
      gpio - GPIO to which the device is connected.
      range - To be used for taking scaled readings for this device.
      Throws:
      RuntimeIOException - If an I/O error occurred.
  • Method Details

    • close

      public void close() throws RuntimeIOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      RuntimeIOException
    • enableDeviceListener

      protected void enableDeviceListener()
      Specified by:
      enableDeviceListener in class GpioInputDevice<AnalogInputEvent>
    • disableDeviceListener

      protected void disableDeviceListener()
      Specified by:
      disableDeviceListener in class GpioInputDevice<AnalogInputEvent>
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • accept

      public void accept​(AnalogInputEvent event)
      Specified by:
      accept in interface Consumer<AnalogInputEvent>
      Overrides:
      accept in class GpioInputDevice<AnalogInputEvent>
    • getRange

      public float getRange()
      Get the analog range for this input device as used by getScaledValue() and convertToScaledValue(float)
      Returns:
      the analog range for this input device
    • getUnscaledValue

      public float getUnscaledValue() throws RuntimeIOException
      Get the unscaled normalised value in the range 0..1 (if unsigned) or -1..1 (if signed)
      Returns:
      the unscaled value
      Throws:
      RuntimeIOException - if there was an I/O error
    • getScaledValue

      public float getScaledValue() throws RuntimeIOException
      Get the scaled value in the range 0..range (if unsigned) or -range..range (if signed)
      Returns:
      the scaled value (-range..range)
      Throws:
      RuntimeIOException - if there was an I/O error
    • convertToScaledValue

      public float convertToScaledValue​(float unscaledValue)
      Convert the specified unscaled value (-1..1) to a scaled one (-range..range).
      Parameters:
      unscaledValue - the unscaled value in the range -1..1
      Returns:
      the scaled value in -range..range
      See Also:
      getRange()
    • addListener

      public void addListener​(DeviceEventConsumer<AnalogInputEvent> listener, float percentChange)
      Register a listener for value changes, will check for changes every 50ms.
      Parameters:
      listener - The listener callback.
      percentChange - Degree of change required to trigger an event.
    • addListener

      public void addListener​(DeviceEventConsumer<AnalogInputEvent> listener, float percentChange, int pollInterval)
      Register a listener for value changes, will check for changes every 50ms.
      Parameters:
      listener - The listener callback.
      percentChange - Degree of change required to trigger an event.
      pollInterval - Time in milliseconds at which reading should be taken.