For the last part, we have the RGB LED strip, which is the one that gives the Rainbow HAT its name. It has seven RGB LEDs that we can set to any combination of colors.
For this example, we will just use the color red, because we are going to build a moving LED like that of KITT from Knight Rider or, if you prefer, from a Cylon eye. Having such pop culture references at hand, our example could not be any other one.
For this example, we will use a timer and keep track of the current position that is lit up, and also check edge conditions to bounce and change direction.
Let's start with the initialization and cleanup code:
class KnightRiderSimpleActivity : Activity() {
private val timer: Timer = Timer()
private var goingUp = true
private var currentPos = 0
private val interval: Long = 100
val colors = IntArray(RainbowHat.LEDSTRIP_LENGTH)
...