|
@@ -0,0 +1,77 @@
|
|
|
+package com.hanghui.senic.service.usbserialdemo.utile.X5;
|
|
|
+
|
|
|
+import android.preference.Preference;
|
|
|
+
|
|
|
+import com.hanghui.senic.service.usbserialdemo.utile.loacat.AppLogUtils;
|
|
|
+import com.innohi.YNHAPI;
|
|
|
+
|
|
|
+public class GpioUtile implements Preference.OnPreferenceChangeListener{
|
|
|
+ private static GpioUtile instance;
|
|
|
+ private static OnGpioListener listener;
|
|
|
+ private static final String KEY_GPIO_MODE = "gpio_mode";
|
|
|
+ private static final String KEY_SET_GPIO_STATE = "set_gpio_state";
|
|
|
+ private static final String KEY_GPIO_STATE = "gpio_state";
|
|
|
+ private YNHAPI.GpioListenerCallback mCallback = null;
|
|
|
+ private YNHAPI mYNHAPI = YNHAPI.getInstance();
|
|
|
+ private GpioUtile() {
|
|
|
+ }
|
|
|
+ public static GpioUtile getInstance() {
|
|
|
+ synchronized (GpioUtile.class) {
|
|
|
+ if (instance == null) {
|
|
|
+ instance = new GpioUtile();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return instance;
|
|
|
+ }
|
|
|
+ public void setListener(OnGpioListener listener) {
|
|
|
+ this.listener = listener;
|
|
|
+ }
|
|
|
+ public void init(){
|
|
|
+ YNHAPI.GpioMode mode = mYNHAPI.getGpioMode(YNHAPI.GPIO_5);
|
|
|
+ YNHAPI.GpioState state = mYNHAPI.getGpioState(YNHAPI.GPIO_5);
|
|
|
+ AppLogUtils.i(true,"紫鹏门磁信号初始化","init: mode =" + mode + ", state = " + state);
|
|
|
+ if (mode == YNHAPI.GpioMode.UNKNOWN || state == YNHAPI.GpioState.UNKNOWN) {
|
|
|
+ AppLogUtils.e(true,"紫鹏门磁信号初始化","无法正确获取获取GPIO的模式/状态!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ listenInputGpioState();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void listenInputGpioState() {
|
|
|
+ if (mCallback == null) {
|
|
|
+ mCallback = (index, oldState, newState) -> {
|
|
|
+ AppLogUtils.i(true,"紫鹏门磁信号回调","index = YNHAPI.GPIO_5" + ", old state = " + oldState + ", new state = " + newState);
|
|
|
+ //newState 1 - 断开 0 - 短接
|
|
|
+ listener.OnGpioListener(oldState,newState);
|
|
|
+ };
|
|
|
+ mYNHAPI.listenGpio(YNHAPI.GPIO_5, mCallback);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void unlistenGpioState() {
|
|
|
+ if (mCallback != null) {
|
|
|
+ mYNHAPI.unlistenGpio(YNHAPI.GPIO_5, mCallback);
|
|
|
+ mCallback = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
|
|
|
+ switch (preference.getKey()) {
|
|
|
+ case KEY_GPIO_MODE:
|
|
|
+ boolean output = "1".equals(String.valueOf(newValue));
|
|
|
+ mYNHAPI.setGpioMode(YNHAPI.GPIO_5,YNHAPI.GpioMode.INPUT);
|
|
|
+ return true;
|
|
|
+
|
|
|
+ case KEY_SET_GPIO_STATE:
|
|
|
+ boolean high = "1".equals(String.valueOf(newValue));
|
|
|
+ mYNHAPI.setGpioState(YNHAPI.GPIO_5, high ? YNHAPI.GpioState.HIGH : YNHAPI.GpioState.LOW);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public interface OnGpioListener {
|
|
|
+ void OnGpioListener(int oldState,int newState);
|
|
|
+ }
|
|
|
+}
|