[TUT] Android Things – Zx Gesture Sensor using UART

This blog post is going to show you how you can use the UART protocol to output from your AndroidThings application. We will attach a ZX Sensor and detect some movement hand gestures, minority report style. We’ll use swipe gestures to change our Rainbow Hat LEDs through a range of colours.

Note: All the specific information about this gesture sensor that we are about to discuss, if you are looking for it as a reference, is in the ZXSensor datasheet here.

First we create an instance of PeripheralManagerService. This is the AndroidThings SDK class that allows us to open connections to different board pins using the different available protocols.

3;
PeripheralManagerService service = new PeripheralManagerService();3;

Using this instance, we want to open an UART connection, the open method takes one parameter, this is the name of the UART bus you want to use. The UART pins on the top of the Rainbow Hat are know as “UART0”.

The datasheet tells us whenever we configure a UART bus we need to set the baud rate, data size, parity and stop bits. Baud is the speed of communication, data size is how many bits are expected at once, parity is way to ensure the integrity of the data and stop bits can be used for a time interval between transmissions.

From the datasheet we can see the baud rate is 115,200. The databits are 8, there is no parity and no stop bits. Let’s configure that in oncreate:

Once we have registered, we can read data from the device inside the on uart device data available method.

The datasheet explains to us that we will be sent a Message Code of 1 byte, followed by a data payload. The data payload has a maximum size of two bytes. Therefore we can read the UARTs device buffer with this code, making sure our buffer has a maximum size of 3 bytes.

We want to detect swipe left and swipe right gestures. The datasheet tells us that any data we receive with the message code 0xFC will be a gesture event.

** SHOW datasheet page 6, gestures table **

Then, when we receive the 0xFC message code, the payload will contain 0x1 for a swipe right or 0x2 for a swipe left. Followed by another byte for the speed of the swipe. Handling the gesture event. The code looks like this:

When a gesture event happens let’s give ourselves some visual feedback. Here I have created a class called LedStrip that will turn the rainbowhat LEDs on and off. If you are interested in the details, check out this LedStrip.java class.

We setup this ledStrip in the onCreate. And tear it down in the onDestroy.

// as a field
private final LedStrip ledStrip = new LedStrip();
// in onCreate
ledStrip.setUp();
// in onDestroy
ledStrip.tearDown();

Now when we get a swipe right we will tell the LED strip to change to the next color, and if we get a swipe left we’ll go back to the previous colour.

That’s it! You now understand how the Android Things UART communication with peripherals works.

You can now interface with a gesture sensor reading its buffers and configuring the UART device to identify gestures. This knowledge transfers to reading other datasheets and coding other UART peripherals such as printers, GPS devices or LCD displays.