Button driver

First, we will remove the Rainbow HAT meta driver from dependencies and add an entry for the button driver instead. This driver includes both the button and the button input drivers.

dependencies {
[...]
implementation 'com.google.android.things.contrib:driver-button:+'
}

In a similar way as we did for LEDs, let's look at the code of the wrapper function:

public static Button openButtonA() throws IOException {
return openButton(BOARD.getButtonA());
}

And, as we saw for the LEDs, a BOARD object is used to provide the correct name of the pin depending on the development board and then call a generic function that opens a button. Let's see the code of the generic openButton:

public static final Button.LogicState BUTTON_LOGIC_STATE = Button.LogicState.PRESSED_WHEN_LOW;

public static
Button openButton(String pin) throws IOException {
return new Button(pin, BUTTON_LOGIC_STATE);
}

Since we are using a driver, this step is very simple; the only thing the meta driver hides from us is the logic state of the buttons. Since they are capacitive, they are pressed when the GPIO is low (which is the reverse of normal press buttons). This is the equivalent of the active type we saw with inputs in the previous section.

So, if we just put everything together, we can just collapse it on a single line:

buttonA = Button(BoardDefaults.buttonA, Button.LogicState.PRESSED_WHEN_LOW)

And then, just for completeness, we add a new value to the BoardDefaults object for the pin where the button is connected:

val buttonA: String
get() = when (Build.DEVICE) {
DEVICE_RPI3 -> "BCM21"
DEVICE_IMX7D_PICO -> "GPIO6_IO14"
else -> throw IllegalStateException("Unknown Build.DEVICE ${Build.DEVICE}")
}

Let's take the example of the previous chapter that used a button and an LED and see how it will look when we completely remove the abstraction for the Rainbow HAT:

class ButtonDriverActivity : Activity() {

private lateinit var redLed: Gpio
private lateinit var buttonA: Button

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

redLed = PeripheralManager.getInstance().openGpio(BoardDefaults.ledR)
redLed.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW)

buttonA = Button(BoardDefaults.buttonA, Button.LogicState.PRESSED_WHEN_LOW)
buttonA.setOnButtonEventListener { button: Button, state: Boolean ->
redLed.value = state
}
}

override fun onDestroy() {
super.onDestroy()
redLed.close()
buttonA.close()
}
}

There is not much of a difference, except that now we need to know more about our components, such as the logic state of the button and the pins where they are connected.

Note that the button driver hides the invocation to PeripheralManagerwhich we needed to use for the LED. It is still there, but enclosed inside the code of the driver. Taking care of opening the pins and interacting with PeripheralManager is a common pattern that most drivers follow.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.85.33