nicebadge/tutorial/basics/step3/main.go

70 lines
1.2 KiB
Go
Raw Permalink Normal View History

2026-04-18 11:01:06 +00:00
package main
import (
"image/color"
"machine"
"time"
"tinygo.org/x/drivers/ws2812"
)
const (
Red = iota
Green
Blue
Yellow
Cyan
Purple
White
Off
)
var colors = [...]color.RGBA{
{255, 0, 0, 255}, // Red
{0, 255, 0, 255}, // Green
{0, 0, 255, 255}, // Blue
{255, 255, 0, 255}, // Yellow
{0, 255, 255, 255}, // Cyan
{255, 0, 255, 255}, // Purple
{255, 255, 255, 255}, // White
{0, 0, 0, 255}, // Off
}
func main() {
// WS2812 LEDs are on pin P1_11
neo := machine.P1_11
neo.Configure(machine.PinConfig{Mode: machine.PinOutput})
leds := ws2812.New(neo)
ledColors := make([]color.RGBA, 2)
// buttons: active LOW (internal pull-up)
btnA := machine.P1_06
btnB := machine.P1_04
btnRot := machine.P0_22 // rotary encoder push button
btnA.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
btnB.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
btnRot.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
c := Off
for {
if !btnA.Get() {
c = Red
}
if !btnB.Get() {
c = Blue
}
if !btnRot.Get() {
c = Green
}
for i := range ledColors {
ledColors[i] = colors[c]
}
leds.WriteColors(ledColors)
time.Sleep(30 * time.Millisecond)
}
}