Class AnalogInputDevice

  • All Implemented Interfaces:
    DeviceInterface, DeviceEventConsumer<AnalogInputEvent>, 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 Detail

      • AnalogInputDevice

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

        public AnalogInputDevice​(int adcNumber,
                                 float range)
                          throws RuntimeIOException
        Parameters:
        adcNumber - 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 adcNumber,
                                 float range)
                          throws RuntimeIOException
        Parameters:
        deviceFactory - The device factory to use to provision this device.
        adcNumber - 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,
                                 PinInfo pinInfo,
                                 float range)
                          throws RuntimeIOException
        Parameters:
        deviceFactory - The device factory to use to provision this device.
        pinInfo - 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.