Last active
April 19, 2017 14:35
-
-
Save PaulTR/0f09b2f8fdc2e45fa96aa53a77dabc05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2016-2017 Paul Trebilcox-Ruiz | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
package com.awkwardelk.hardware; | |
import com.google.android.things.pio.Gpio; | |
import com.google.android.things.pio.GpioCallback; | |
import com.google.android.things.pio.PeripheralManagerService; | |
import java.io.IOException; | |
@SuppressWarnings({"unused", "WeakerAccess"}) | |
public class HCSR501 implements AutoCloseable { | |
public enum State { | |
STATE_HIGH, | |
STATE_LOW; | |
} | |
public interface OnMotionDetectedEventListener { | |
void onMotionDetectedEvent(State state); | |
} | |
private Gpio mMotionDetectorGpio; | |
private OnMotionDetectedEventListener mOnMotionDetectedEventListener; | |
private boolean mLastState; | |
public HCSR501(String pin) throws IOException { | |
PeripheralManagerService pioService = new PeripheralManagerService(); | |
Gpio HCSR501Gpio = pioService.openGpio(pin); | |
try { | |
connect(HCSR501Gpio); | |
} catch( IOException | RuntimeException e ) { | |
close(); | |
throw e; | |
} | |
} | |
private void connect(Gpio HCSR501Gpio) throws IOException { | |
mMotionDetectorGpio = HCSR501Gpio; | |
mMotionDetectorGpio.setDirection(Gpio.DIRECTION_IN); | |
mMotionDetectorGpio.setEdgeTriggerType(Gpio.EDGE_BOTH); | |
mLastState = mMotionDetectorGpio.getValue(); | |
mMotionDetectorGpio.setActiveType(mLastState ? Gpio.ACTIVE_HIGH : Gpio.ACTIVE_LOW); | |
mMotionDetectorGpio.registerGpioCallback(mInterruptCallback); | |
} | |
private void performMotionEvent(State state) { | |
if( mOnMotionDetectedEventListener != null ) { | |
mOnMotionDetectedEventListener.onMotionDetectedEvent(state); | |
} | |
} | |
private GpioCallback mInterruptCallback = new GpioCallback() { | |
@Override | |
public boolean onGpioEdge(Gpio gpio) { | |
try { | |
if( gpio.getValue() != mLastState ) { | |
mLastState = gpio.getValue(); | |
performMotionEvent(mLastState ? State.STATE_HIGH : State.STATE_LOW); | |
} | |
} catch( IOException e ) { | |
} | |
return true; | |
} | |
}; | |
public void setOnMotionDetectedEventListener(OnMotionDetectedEventListener listener) { | |
mOnMotionDetectedEventListener = listener; | |
} | |
@Override | |
public void close() throws IOException { | |
mOnMotionDetectedEventListener = null; | |
if (mMotionDetectorGpio != null) { | |
mMotionDetectorGpio.unregisterGpioCallback(mInterruptCallback); | |
try { | |
mMotionDetectorGpio.close(); | |
} finally { | |
mMotionDetectorGpio = null; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not using the UserSensor framework. Will need to be reworked to support proper driver usage. Good enough for simple use, however.