Package com.diozero.devices.sandpit
Class HCSR04UsingEvents
- java.lang.Object
-
- com.diozero.devices.sandpit.HCSR04UsingEvents
-
- All Implemented Interfaces:
DeviceInterface
,DeviceEventConsumer<DigitalInputEvent>
,DistanceSensorInterface
,SensorInterface
,AutoCloseable
,Consumer<DigitalInputEvent>
public class HCSR04UsingEvents extends Object implements DistanceSensorInterface, DeviceEventConsumer<DigitalInputEvent>
Note this version doesn't work as well as the polling-based HCSR04 version. User's manual: https://docs.google.com/document/d/1Y-yZnNhMYy7rwhAgyL_pfa39RsB-x2qR4vP8saG73rE/edit# Product specification: http://www.micropik.com/PDF/HCSR04.pdf Provides 2cm - 400cm non-contact measurement function, the ranging accuracy can reach to 3mm. You only need to supply a short 10uS pulse to trigger input to start the ranging, and then the module will send out an 8 cycle burst of ultrasound at 40 kHz and raise its echo. The Echo is a distance object that is pulse width and the range in proportion. We suggest to use over 60ms measurement cycle, in order to prevent trigger signal to the echo signal.
-
-
Constructor Summary
Constructors Constructor Description HCSR04UsingEvents(int triggerGpioNum, int echoGpioNum)
Initialise GPIO to echo and trigger pins
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
accept(DigitalInputEvent event)
void
close()
Free device GPIOsfloat
getDistanceCm()
Send a pulse to HCSR04 and compute the echo to obtain distance
-
-
-
Constructor Detail
-
HCSR04UsingEvents
public HCSR04UsingEvents(int triggerGpioNum, int echoGpioNum) throws RuntimeIOException
Initialise GPIO to echo and trigger pins- Parameters:
triggerGpioNum
- GPIO connected to the HC-SR04 trigger pinechoGpioNum
- GPIO connected to the HC-SR04 echo pin- Throws:
RuntimeIOException
- if an I/O error occurs
-
-
Method Detail
-
getDistanceCm
public float getDistanceCm() throws RuntimeIOException
Send a pulse to HCSR04 and compute the echo to obtain distance- Specified by:
getDistanceCm
in interfaceDistanceSensorInterface
- Returns:
- distance in cm
- Throws:
RuntimeIOException
- if an I/O error occurs
-
close
public void close()
Free device GPIOs- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceDeviceInterface
-
accept
public void accept(DigitalInputEvent event)
- Specified by:
accept
in interfaceConsumer<DigitalInputEvent>
-
-