Skip to content
This repository has been archived by the owner on Nov 13, 2021. It is now read-only.

Commit

Permalink
HC-SR04: update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
leinardi committed Jan 2, 2018
1 parent 2a1c544 commit 8ba4ad9
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions driver-hcsr04/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,67 @@ dependencies {
### Sample usage

```java
public class DistanceActivity extends Activity {
public class DistanceActivity extends Activity implements SensorEventListener {
private static final String TAG = DistanceActivity.class.getSimpleName();

private Hcsr04SensorDriver mProximitySensorDriver;
private SensorManager mSensorManager;

private SensorManager.DynamicSensorCallback mDynamicSensorCallback = new SensorManager
.DynamicSensorCallback() {
@Override
public void onDynamicSensorConnected(Sensor sensor) {
if (sensor.getType() == Sensor.TYPE_PROXIMITY) {
mSensorManager.registerListener(DistanceActivity.this,
sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerDynamicSensorCallback(mDynamicSensorCallback);

try {
mDevice = new Hcsr04("BCM17", "BCM4");
mDevice.setEnabled(enabled);
mProximitySensorDriver = new Hcsr04SensorDriver(trigPin, echoPin);
mProximitySensorDriver.registerProximitySensor();
} catch (IOException e) {
// couldn't configure the device...
}

}

private void readDistance(){
float distance = mDevice.readDistance();
}

@Override
protected void onDestroy() {
super.onDestroy();
try {
mDevice.close();
} catch (IOException e) {
// error closing sensor
} finally {
mDevice = null;
if (mProximitySensorDriver != null) {
mSensorManager.unregisterDynamicSensorCallback(mDynamicSensorCallback);
mSensorManager.unregisterListener(this);
mProximitySensorDriver.unregisterProximitySensor();
try {
mProximitySensorDriver.close();
} catch (IOException e) {
// error closing sensor
} finally {
mProximitySensorDriver = null;
}
}
}

}
@Override
public void onSensorChanged(SensorEvent event) {
Log.i(TAG, String.format(Locale.getDefault(), "sensor changed: [%f]", event.values[0]));
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
```

To use it with the `SensorManager` check the [sample project](https://github.com/leinardi/androidthings-drivers/tree/hcsr04/sample-hcsr04).

## License

Copyright 2017 Roberto Leinardi
Expand Down

0 comments on commit 8ba4ad9

Please sign in to comment.