package main

import (
	"image/color"
	"machine"
	"time"

	"tinygo.org/x/drivers/ws2812"
)

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)

	// A cycles forward, B cycles backward through the rainbow
	btnA := machine.P1_06
	btnB := machine.P1_04
	btnA.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
	btnB.Configure(machine.PinConfig{Mode: machine.PinInputPullup})

	var k uint8
	for {
		if !btnA.Get() {
			k++
		}
		if !btnB.Get() {
			k--
		}

		ledColors[0] = getRainbowRGB(k)
		ledColors[1] = getRainbowRGB(k + 10)
		leds.WriteColors(ledColors)

		time.Sleep(10 * time.Millisecond)
	}
}

func getRainbowRGB(i uint8) color.RGBA {
	if i < 85 {
		return color.RGBA{i * 3, 255 - i*3, 0, 255}
	} else if i < 170 {
		i -= 85
		return color.RGBA{255 - i*3, 0, i * 3, 255}
	}
	i -= 170
	return color.RGBA{0, i * 3, 255 - i*3, 255}
}
