Android 东西 - 如何在命令 mLedGpio.setValue(true) 后保持 LED 不间断亮起
Android things - How to keep the led uninterruptedly ON after command mLedGpio.setValue(true)
我完成了这个项目:sample-button
但我需要将其更改为在我按下按钮时保持 LED 亮起,并在我再次按下按钮后将其熄灭。
我该怎么做?
尝试替换 ButtonActivity.java
中的以下回调方法
static boolean mToggle ;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SPACE) {
// Turn on the LED
mToggle = !mToggle;
setLedValue(mToggle);
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SPACE) {
// Turn off the LED
// setLedValue(false);
return true;
}
return super.onKeyUp(keyCode, event);
}
删除 onKeyDown
覆盖方法中的代码并将 setLedValue
更改为:
private void setLedValue(boolean value) {
try {
mLedGpio.setValue(!mLedGpio.getValue());
} catch (IOException e) {
Log.e(TAG, "Error updating GPIO value", e);
}
}
(为了获得更好的用户体验,删除 onKeyDown 比删除 onKeyUp 更好。从用户体验的角度来看,在释放按键时更改某些内容比在首次按下时执行操作要好得多。)
如果您想查看整个 class 更改,它看起来像这样:
public class ButtonActivity extends Activity {
private Gpio mLedGpio;
private ButtonInputDriver mButtonInputDriver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PeripheralManager pioService = new PeripheralManager.getInstance();
try {
mLedGpio = pioService.openGpio(BoardDefaults.getGPIOForLED());
mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
mButtonInputDriver = new ButtonInputDriver(
BoardDefaults.getGPIOForButton(),
Button.LogicState.PRESSED_WHEN_LOW,
KeyEvent.KEYCODE_SPACE);
mButtonInputDriver.register();
} catch (IOException e) {
throw new IllegalStateException("Error configuring GPIO pins", e);
}
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SPACE) {
toggleLedValue();
return true;
}
return super.onKeyUp(keyCode, event);
}
/**
* Update the value of the LED output.
*/
private void toggleLedValue() {
try {
mLedGpio.setValue(!mLedGpio.getValue());
} catch (IOException e) {
Log.e(TAG, "Error updating GPIO value", e);
}
}
@Override
protected void onDestroy(){
super.onDestroy();
mButtonInputDriver.unregister();
try {
mButtonInputDriver.close();
} catch (IOException e) {
Log.e(TAG, "Error closing Button driver", e);
}
try {
mLedGpio.close();
} catch (IOException e) {
Log.e(TAG, "Error closing LED GPIO", e);
}
}
}
我完成了这个项目:sample-button
但我需要将其更改为在我按下按钮时保持 LED 亮起,并在我再次按下按钮后将其熄灭。
我该怎么做?
尝试替换 ButtonActivity.java
中的以下回调方法static boolean mToggle ;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SPACE) {
// Turn on the LED
mToggle = !mToggle;
setLedValue(mToggle);
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SPACE) {
// Turn off the LED
// setLedValue(false);
return true;
}
return super.onKeyUp(keyCode, event);
}
删除 onKeyDown
覆盖方法中的代码并将 setLedValue
更改为:
private void setLedValue(boolean value) {
try {
mLedGpio.setValue(!mLedGpio.getValue());
} catch (IOException e) {
Log.e(TAG, "Error updating GPIO value", e);
}
}
(为了获得更好的用户体验,删除 onKeyDown 比删除 onKeyUp 更好。从用户体验的角度来看,在释放按键时更改某些内容比在首次按下时执行操作要好得多。)
如果您想查看整个 class 更改,它看起来像这样:
public class ButtonActivity extends Activity {
private Gpio mLedGpio;
private ButtonInputDriver mButtonInputDriver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PeripheralManager pioService = new PeripheralManager.getInstance();
try {
mLedGpio = pioService.openGpio(BoardDefaults.getGPIOForLED());
mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
mButtonInputDriver = new ButtonInputDriver(
BoardDefaults.getGPIOForButton(),
Button.LogicState.PRESSED_WHEN_LOW,
KeyEvent.KEYCODE_SPACE);
mButtonInputDriver.register();
} catch (IOException e) {
throw new IllegalStateException("Error configuring GPIO pins", e);
}
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SPACE) {
toggleLedValue();
return true;
}
return super.onKeyUp(keyCode, event);
}
/**
* Update the value of the LED output.
*/
private void toggleLedValue() {
try {
mLedGpio.setValue(!mLedGpio.getValue());
} catch (IOException e) {
Log.e(TAG, "Error updating GPIO value", e);
}
}
@Override
protected void onDestroy(){
super.onDestroy();
mButtonInputDriver.unregister();
try {
mButtonInputDriver.close();
} catch (IOException e) {
Log.e(TAG, "Error closing Button driver", e);
}
try {
mLedGpio.close();
} catch (IOException e) {
Log.e(TAG, "Error closing LED GPIO", e);
}
}
}