Reader small image

You're reading from  Android Things Quick Start Guide

Product typeBook
Published inAug 2018
PublisherPackt
ISBN-139781789341799
Edition1st Edition
Right arrow
Author (1)
Raul Portales
Raul Portales
author image
Raul Portales

Raul Portales is a software engineer who has had a love for computers, electronics, and gadgets in general for as long as he remembers. He jumped into Android as soon as it was released. Raul has worked on social networks, education, healthcare, and even founded a gaming studio and a consultancy company. Specializing in mobile and UX, he speaks frequently at conferences and meetups. Raul's love for electronics reignited when Google announced Android Things. He started tinkering with it with the first Developer Preview, which lead to adding the IoT category on his Google Developer expert profile.
Read more about Raul Portales

Right arrow

SPI - Faster Bidirectional Communication

SPI is the last of the protocols to handle peripherals that we will be exploring in this book. It is similar to I2C, but also has a few differences, so we will start with an overview of the protocol. As usual, we will look at the component of the Rainbow HAT that is handled using the protocol -in this case, the RGB LED strip-. Finally, we will look at a couple of examples where SPI is commonly used: displays.

The topics covered in this chapter are:

  • Overview of SPI
  • LED strip
  • Usage on displays

Let's start by learning about the protocol itself.

Technical requirements

You will be required to have Android Studio and Android Things installed on a developer kit. You also will require many hardware components to effectively perform the tasks given in this chapter. The components are very interesting to have, just to see them working, but the Rainbow HAT is particularly important. We go into details about the developer kits and how to pick the right one, as a part of Chapter 1, Introducing Android Things. Finally, to use the Git repository of this book, you need to install Git.

The code files of this chapter can be found on GitHub:
https://github.com/PacktPublishing/Android-Things-Quick-Start-Guide.

Check out the following video to see the code in action:

http://bit.ly/2MGJ2bi.

Overview of SPI

Serial Peripheral Interface (SPI) is typically used on external non-volatile memory and graphical displays, where faster data transfer rates and high bandwidth are required. Many sensor devices support SPI in addition to I2C.

SPI shares some characteristics with I2C. It is a synchronous serial interface, which means it relies on a shared clock signal to synchronize data transfer between devices. Because of the shared clock signal, SPI also has a master-slave architecture, where the master controls the triggering of the clock signal and all other connected peripherals are slaves.

SPI is faster than I2C. The clock signal for data transfer is typically in the range of 16 MHz to 25 MHz.

Another similarity with I2C is that it supports multiple slaves.

But there are also some differences. While I2C is semi-duplex, SPI supports full-duplex data transfer, meaning the...

LED strip

As we have done in previous chapters, let's remove the dependency of the Rainbow HAT meta driver and add only the driver that we need. In the case of the RGB LED strip, that driver is called driver-apa102:

dependencies {
implementation 'com.google.android.things.contrib:driver-apa102:+'
}

And, as we have been doing so far, let's use a BoardDefaults object to be able to run our code independently of the developer kit pinout naming:

val spiBus: String
get() = when (Build.DEVICE) {
DEVICE_RPI3 -> "SPI0.0"
DEVICE_IMX7D_PICO -> "SPI3.1"
else -> throw IllegalStateException("Unknown Build.DEVICE ${Build.DEVICE}")
}

There are a couple of important things to note here. First is that the exposed SPI bus on the Raspberry Pi is called 0, while the iMX7D one is 3. It is also important to note...

Usage on displays

We used an LCD display and an OLED screen in the I2C chapter. This time we will look at an LED matrix and the same OLED screen again, but wired to SPI so that you can see the differences:

Note that the Rainbow HAT does have SPI pins exposed. Be aware that early versions of the Rainbow HAT have these pins wrongly labeled (the wiring is consistent, just the label is incorrect). Double check the correct labels or just connect the peripherals directly to your developer board:

Incorrect and correct labeling of the SPI pins on the Rainbow HAT
Double-check the SPI pin labels on the Rainbow HAT, as early versions have the incorrect label.

LED matrix

This peripheral is an 8x8 LED matrix. It is great for retro-style...

Summary

In this chapter we have learned about the SPI protocol and its similarities and differences with I2C. We have also checked how to use the RGD LED strip of the Rainbow HAT without the meta driver and we have seen how to work with a couple of screen components that are wired over SPI: LED matrix and OLED screens.

Now that we have completed the review of the basic protocols for communicating with peripherals, let's explore some areas where Android Things really sets itself on a different level in terms of libraries and utilities that allow us to build awesome IoT projects. It is time to look into the real power of Android Things.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Things Quick Start Guide
Published in: Aug 2018Publisher: PacktISBN-13: 9781789341799
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Raul Portales

Raul Portales is a software engineer who has had a love for computers, electronics, and gadgets in general for as long as he remembers. He jumped into Android as soon as it was released. Raul has worked on social networks, education, healthcare, and even founded a gaming studio and a consultancy company. Specializing in mobile and UX, he speaks frequently at conferences and meetups. Raul's love for electronics reignited when Google announced Android Things. He started tinkering with it with the first Developer Preview, which lead to adding the IoT category on his Google Developer expert profile.
Read more about Raul Portales