package main

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

	"tinygo.org/x/drivers/st7789"
	"tinygo.org/x/tinydraw"
)

func main() {
	machine.SPI0.Configure(machine.SPIConfig{
		SCK:       machine.P1_01,
		SDO:       machine.P1_02,
		Frequency: 8000000,
		Mode:      0,
	})

	display := st7789.New(machine.SPI0,
		machine.P1_15, // TFT_RESET
		machine.P1_13, // TFT_DC
		machine.P0_10, // TFT_CS
		machine.P0_09) // TFT_LITE

	display.Configure(st7789.Config{
		Rotation:     st7789.ROTATION_90,
		Width:        135,
		Height:       240,
		RowOffset:    40,
		ColumnOffset: 53,
	})

	// 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})

	white := color.RGBA{255, 255, 255, 255}
	circle := color.RGBA{0, 100, 250, 255}
	ring := color.RGBA{200, 0, 0, 255}

	display.FillScreen(white)

	// draw a circle for each button on the 240x135 display
	tinydraw.FilledCircle(&display, 60, 67, 14, circle)  // B (left)
	tinydraw.FilledCircle(&display, 120, 67, 14, circle) // rotary button (center)
	tinydraw.FilledCircle(&display, 180, 67, 14, circle) // A (right)

	for {
		if !btnB.Get() {
			tinydraw.Circle(&display, 60, 67, 16, ring)
		} else {
			tinydraw.Circle(&display, 60, 67, 16, white)
		}
		if !btnRot.Get() {
			tinydraw.Circle(&display, 120, 67, 16, ring)
		} else {
			tinydraw.Circle(&display, 120, 67, 16, white)
		}
		if !btnA.Get() {
			tinydraw.Circle(&display, 180, 67, 16, ring)
		} else {
			tinydraw.Circle(&display, 180, 67, 16, white)
		}

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