tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/xpt2046/main.go (about)

     1  package main
     2  
     3  import (
     4  	"machine"
     5  	"time"
     6  
     7  	"tinygo.org/x/drivers/xpt2046"
     8  )
     9  
    10  func main() {
    11  
    12  	clk := machine.GPIO0
    13  	cs := machine.GPIO1
    14  	din := machine.GPIO2
    15  	dout := machine.GPIO3
    16  	irq := machine.GPIO4
    17  
    18  	touchScreen := xpt2046.New(clk, cs, din, dout, irq)
    19  
    20  	touchScreen.Configure(&xpt2046.Config{
    21  		Precision: 10, //Maximum number of samples for a single ReadTouchPoint to improve accuracy.
    22  	})
    23  
    24  	for {
    25  
    26  		//Wait for a touch
    27  		for !touchScreen.Touched() {
    28  			time.Sleep(50 * time.Millisecond)
    29  		}
    30  
    31  		touch := touchScreen.ReadTouchPoint()
    32  		//X and Y are 16 bit with 12 bit resolution and need to be scaled for the display size
    33  		//Z is 24 bit and is typically > 2000 for a touch
    34  		println("touch:", touch.X, touch.Y, touch.Z)
    35  		//Example of scaling for a 240x320 display
    36  		println("screen:", (touch.X*240)>>16, (touch.Y*320)>>16)
    37  
    38  		//Wait for touch to end
    39  		for touchScreen.Touched() {
    40  			time.Sleep(50 * time.Millisecond)
    41  		}
    42  
    43  	}
    44  }