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:
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);
}
}
gpio
Constructor and Description |
---|
AnalogInputDevice(AnalogInputDeviceFactoryInterface deviceFactory,
int gpio) |
AnalogInputDevice(AnalogInputDeviceFactoryInterface deviceFactory,
int gpio,
float range) |
AnalogInputDevice(int gpio) |
AnalogInputDevice(int gpio,
float range) |
Modifier and Type | Method and Description |
---|---|
void |
addListener(InputEventListener<AnalogInputEvent> listener,
float percentChange)
Register a listener for value changes, will check for changes every 50ms.
|
void |
addListener(InputEventListener<AnalogInputEvent> listener,
float percentChange,
int pollInterval)
Register a listener for value changes, will check for changes every 50ms.
|
void |
close() |
protected void |
disableDeviceListener() |
protected void |
enableDeviceListener() |
float |
getRange() |
float |
getScaledValue()
Get the scaled value in the range 0..range (if unsigned) or -range..range
(if signed)
|
float |
getUnscaledValue()
Get the unscaled normalised value in the range 0..1 (if unsigned) or
-1..1 (if signed)
|
void |
run() |
void |
valueChanged(AnalogInputEvent event) |
addListener, removeAllListeners, removeListener
getGpio
public AnalogInputDevice(int gpio) throws RuntimeIOException
gpio
- GPIO to which the device is connected.RuntimeIOException
- If an I/O error occurred.public AnalogInputDevice(int gpio, float range) throws RuntimeIOException
gpio
- GPIO to which the device is connected.range
- To be used for taking scaled readings for this device.RuntimeIOException
- If an I/O error occurred.public AnalogInputDevice(AnalogInputDeviceFactoryInterface deviceFactory, int gpio) throws RuntimeIOException
deviceFactory
- The device factory to use to provision this device.gpio
- GPIO to which the device is connected.RuntimeIOException
- If an I/O error occurred.public AnalogInputDevice(AnalogInputDeviceFactoryInterface deviceFactory, int gpio, float range) throws RuntimeIOException
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.RuntimeIOException
- If an I/O error occurred.public float getRange()
public void close() throws RuntimeIOException
close
in interface Closeable
close
in interface AutoCloseable
RuntimeIOException
protected void enableDeviceListener()
enableDeviceListener
in class GpioInputDevice<AnalogInputEvent>
protected void disableDeviceListener()
disableDeviceListener
in class GpioInputDevice<AnalogInputEvent>
public void valueChanged(AnalogInputEvent event)
valueChanged
in interface InputEventListener<AnalogInputEvent>
valueChanged
in class GpioInputDevice<AnalogInputEvent>
public float getUnscaledValue() throws RuntimeIOException
RuntimeIOException
- if there was an I/O errorpublic float getScaledValue() throws RuntimeIOException
RuntimeIOException
- if there was an I/O errorpublic void addListener(InputEventListener<AnalogInputEvent> listener, float percentChange)
listener
- The listener callback.percentChange
- Degree of change required to trigger an event.public void addListener(InputEventListener<AnalogInputEvent> listener, float percentChange, int pollInterval)
listener
- The listener callback.percentChange
- Degree of change required to trigger an event.pollInterval
- Time in milliseconds at which reading should be taken.Copyright © 2016–2018 mattjlewis. All rights reserved.