package main

import (
	"machine"
	"time"

	"image/color"

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

func main() {

	machine.SPI0.Configure(machine.SPIConfig{
		SCK:       machine.P1_01,
		SDO:       machine.P1_02,
		Frequency: 2000000,
		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,
		//FrameRate:  st7789.FRAMERATE_111,
		//VSyncLines: st7789.MAX_VSYNC_SCANLINES,
	})

	width, height := display.Size()
	width = 240
	height = 135
	println(width, height)
	white := color.RGBA{255, 255, 255, 255}
	red := color.RGBA{255, 0, 0, 255}
	blue := color.RGBA{0, 0, 255, 255}
	green := color.RGBA{0, 255, 0, 255}
	black := color.RGBA{0, 0, 0, 255}

	machine.InitADC()
	ax := machine.ADC{Pin: machine.P0_29}
	ay := machine.ADC{Pin: machine.P0_02}
	ax.Configure(machine.ADCConfig{})
	ay.Configure(machine.ADCConfig{})

	display.FillScreen(black)

	display.FillRectangle(0, 0, width/2, height/2, white)
	display.FillRectangle(width/2, 0, width/2, height/2, red)
	display.FillRectangle(0, height/2, width/2, height/2, green)
	display.FillRectangle(width/2, height/2, width/2, height/2, blue)
	display.FillRectangle(width/4, height/4, width/2, height/2, black)

	for {
		println(ax.Get(), ay.Get())
		time.Sleep(100 * time.Millisecond)
	}
}
