mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-09 01:13:40 +00:00
d1b917b835
* Added implementation and example to support software-based SPI for APA102, for use with boards like Adafruit Itsy Bitsy M0 for instance.
85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
// This example demostrates how to control the "Dotstar" (APA102) LED included
|
|
// on the Adafruit Itsy Bitsy M0 board. It implements a "rainbow effect" based
|
|
// on the following example:
|
|
// https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/master/CircuitPython_Essentials/CircuitPython_Internal_RGB_LED_rainbow.py
|
|
package main
|
|
|
|
import (
|
|
"image/color"
|
|
"machine"
|
|
"time"
|
|
|
|
"tinygo.org/x/drivers/apa102"
|
|
)
|
|
|
|
var (
|
|
apa apa102.Device
|
|
|
|
led = machine.PWM{machine.LED}
|
|
leds = make([]color.RGBA, 1)
|
|
wheel = &Wheel{Brightness: 0x10}
|
|
)
|
|
|
|
func init() {
|
|
|
|
// APA102 on Itsy Bitsy is connected to pins that require a software-based
|
|
// SPI implementation.
|
|
apa = apa102.NewSoftwareSPI(machine.PA00, machine.PA01, 1)
|
|
|
|
// Configure the regular on-board LED for PWM fading
|
|
machine.InitPWM()
|
|
led.Configure()
|
|
|
|
}
|
|
|
|
func main() {
|
|
|
|
// We'll fade the on-board LED in a goroutine to show/ensure that the APA102
|
|
// works fine with the scheduler enabled. Comment this out to test this code
|
|
// with the scheduler disabled.
|
|
go func() {
|
|
for i, brightening := uint8(0), false; ; i++ {
|
|
if i == 0 {
|
|
brightening = !brightening
|
|
continue
|
|
}
|
|
var brightness uint16 = uint16(i) << 8
|
|
if !brightening {
|
|
brightness = 0xFFFF - brightness
|
|
}
|
|
led.Set(brightness)
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
}()
|
|
|
|
// Use the "wheel" function from Adafruit's example to cycle the APA102
|
|
for {
|
|
leds[0] = wheel.Next()
|
|
apa.WriteColors(leds)
|
|
time.Sleep(25 * time.Millisecond)
|
|
}
|
|
|
|
}
|
|
|
|
// Wheel is a port of Adafruit's Circuit Python example referenced above.
|
|
type Wheel struct {
|
|
Brightness uint8
|
|
pos uint8
|
|
}
|
|
|
|
// Next increments the internal state of the color and returns the new RGBA
|
|
func (w *Wheel) Next() (c color.RGBA) {
|
|
pos := w.pos
|
|
if w.pos < 85 {
|
|
c = color.RGBA{R: 0xFF - pos*3, G: pos * 3, B: 0x0, A: w.Brightness}
|
|
} else if w.pos < 170 {
|
|
pos -= 85
|
|
c = color.RGBA{R: 0x0, G: 0xFF - pos*3, B: pos * 3, A: w.Brightness}
|
|
} else {
|
|
pos -= 170
|
|
c = color.RGBA{R: pos * 3, G: 0x0, B: 0xFF - pos*3, A: w.Brightness}
|
|
}
|
|
w.pos++
|
|
return
|
|
}
|