Android Things onTouch 不释放 GPIO

Android Things onTouch doesnt release GPIO

我正在使用 Android Things v1 并尝试使用屏幕上的 Button 来启动电机,只要 button 被按下(点击并长按)。

我遇到的问题是一旦按下 button 电机就不会停止。我希望它在 button 被释放后停止。

这里是 button 代码:

    mtrbtnGD.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            try {
                mtrGpio = manager.openGpio("BCM24");
                mtrGpio.setEdgeTriggerType(Gpio.EDGE_NONE);
                mtrGpio.setActiveType(Gpio.ACTIVE_HIGH);
                mtrGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
                mtrGpio.setValue(true);
                Log.i(TAG, "Motor started");

            }
            catch (IOException e) {
                Log.w(TAG, "Unable to access GPIO", e);
            }
           return true;
        }
    });

编辑:

这是 Sam 回复的新代码:

public class MainActivity extends Activity {

private Gpio mtrGpio;
private GestureDetector mtrbtnGD;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button mtrbtnGD = (Button) findViewById(R.id.mtrbtn);
    Button closebtn = (Button) findViewById(R.id.closebtn);
    Button stopmtrbtn = (Button) findViewById(R.id.stopmtrbtn);

    final PeripheralManager manager = PeripheralManager.getInstance();
    List<String> portList = manager.getGpioList();
    if (portList.isEmpty()) {
        Log.i(TAG, "No GPIO port available on this device.");
    } else {
        Log.i(TAG, "List of available ports: " + portList);
    }

    mtrbtnGD.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            try {

                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // PRESSED
                        mtrGpio = manager.openGpio("BCM24");
                        mtrGpio.setEdgeTriggerType(Gpio.EDGE_NONE);
                        mtrGpio.setActiveType(Gpio.ACTIVE_HIGH);
                        mtrGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
                        mtrGpio.setValue(true);
                        Log.i(TAG, "Motor started");
                        return true; // if you want to handle the touch event
                    case MotionEvent.ACTION_UP:
                        // RELEASED
                        mtrGpio.close();

                        return true; // if you want to handle the touch event
                }



            }
            catch (IOException e) {
                Log.w(TAG, "Unable to access GPIO", e);
            }
           return true;
        }
    });

当我松开屏幕按钮时,电机仍然没有停止。

您可以在 onTouch 方法中使用此代码并检查 event.getAction() :

switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // PRESSED
                return true; // if you want to handle the touch event
            case MotionEvent.ACTION_UP:
                // RELEASED
                return true; // if you want to handle the touch event
        }

close() 关闭您与 GPIO 外围设备的连接,它在关闭之前不会更改该连接的值。您需要这样使用 setValue(false);

               switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // PRESSED
                        mtrGpio = manager.openGpio("BCM24");
                        mtrGpio.setEdgeTriggerType(Gpio.EDGE_NONE);
                        mtrGpio.setActiveType(Gpio.ACTIVE_HIGH);
                        mtrGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
                        mtrGpio.setValue(true);
                        Log.i(TAG, "Motor started");
                        return true; // if you want to handle the touch event
                    case MotionEvent.ACTION_UP:
                        // RELEASED
                        mtrGpio.setValue(false); // ADD THIS
                        mtrGpio.close();

                        return true; // if you want to handle the touch event
                }

理想情况下,如果您希望电机经常打开和关闭,则应保持连接打开。

public class MainActivity extends Activity {

private Gpio mtrGpio;
private GestureDetector mtrbtnGD;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button mtrbtnGD = (Button) findViewById(R.id.mtrbtn);
    try {
      PeripheralManager manager = PeripheralManager.getInstance();
      mtrGpio = manager.openGpio("BCM24");
      mtrGpio.setEdgeTriggerType(Gpio.EDGE_NONE);
      mtrGpio.setActiveType(Gpio.ACTIVE_HIGH);
      mtrGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
    } catch (IOException e) {
      throw new IllegalStateException("cannot open gpio", e);
    }

    mtrbtnGD.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            try {
               switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // PRESSED
                        mtrGpio.setValue(true);
                        Log.i(TAG, "Motor started");
                        return true; 
                    case MotionEvent.ACTION_UP:
                        // RELEASED
                        mtrGpio.setValue(false);
                        return true; 
                }
            } catch (IOException e) {
                Log.w(TAG, "Unable to access GPIO", e);
            }
           return true;
        }
    });
}

@Override
protected void onDestroy() {
   try {
      mtrGpio.close();
   } catch (IOException ignore) {
      Log.w(TAG, "Unable to close GPIO", ignore);
   }
   super.onDestroy();
}