LEDs

In this section we will see how to handle LEDs directly, learn a bit more about PeripheralManager (the class you use to access peripherals), and understand how GPIO works for output.

So, let's begin by replacing the utility method from the Rainbow HAT meta driver with some code that accesses the pins directly.

Given that we are already using a Gpio object to handle the LEDs, the only part that we need to change is how to open it. We already looked at openLedRed() from RainbowHat:

public static Gpio openLedRed() throws IOException {
return openLed(BOARD.getLedR());
}

We also looked at BOARD.getLedR() and how it gives us the pin name which is connected to the Rainbow HAT red LED according to the board. The other part is a generic function to open an LED. Let's look at that code:

public static Gpio openLed(String pin) throws IOException {
PeripheralManager pioService = PeripheralManager.getInstance();
Gpio ledGpio = pioService.openGpio(pin);
ledGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
return ledGpio;
}

Here we have the real deal. First we get an instance of PeripheralManager and then we use it to open a Gpio pin by name.

PeripheralManager, as the name indicates, is the class we use to manage all peripherals.

 

Once we have the Gpio object, we call setDirection, passing Gpio.DIRECTION_OUT_INITIALLY_LOW. This step is very important. Since GPIO can be used for input or output, when we set the direction, we are indicating how we want to use that particular pin. Unless we specify the direction we want, the system cannot consider the pin open since it will not know how to handle it.

In this case we are setting it as output, but also indicating the initial state. As you may have guessed, Gpio.DIRECTION_OUT_INITIALLY_HIGH also exists.

If we now replace the code inside one of our examples of the previous chapter, it would look like this:

class BlinkTimerActivity : Activity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
led = PeripheralManager.getInstance().openGpio(BoardDefaults.ledR)
led.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW)
timer.schedule(timerTask {
led.value = !led.value
}, 0, 1000)
}

[...]
}

The only difference with the previous chapter is that now we open it using PeripheralManager directly, since we were already using a Gpio reference to handle the LED.

PeripheralManager is one the most important classes of the Android Things library, it is the one we use to interact with all the peripherals. It was used behind the scenes on the Rainbow HAT driver, and it is usually done that way on drivers. 

There is one last method we haven't discussed yet: setActiveTypeNormally you would want to have active as high, but not always:

led.setActiveType(Gpio.ACTIVE_HIGH)

The active type maps the value of true or false to the value of high and low, as you can see in the following diagram

Just to double-check: when we set active as low, a value of false means that we do have signal on the pin.

Let's move on to something more interesting to do with our digital outputs: controlling anything with a relay.

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

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