XPT2046 Touch driver (#350)

Co-authored-by: Steven Pearson <steven.pearson.78@gmail.com>
This commit is contained in:
spearson78
2021-12-14 01:14:10 +01:00
committed by GitHub
parent 0f9b9d873b
commit 43099c5d5f
4 changed files with 236 additions and 1 deletions
+44
View File
@@ -0,0 +1,44 @@
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/xpt2046"
)
func main() {
clk := machine.GPIO0
cs := machine.GPIO1
din := machine.GPIO2
dout := machine.GPIO3
irq := machine.GPIO4
touchScreen := xpt2046.New(clk, cs, din, dout, irq)
touchScreen.Configure(&xpt2046.Config{
Precision: 10, //Maximum number of samples for a single ReadTouchPoint to improve accuracy.
})
for {
//Wait for a touch
for !touchScreen.Touched() {
time.Sleep(50 * time.Millisecond)
}
touch := touchScreen.ReadTouchPoint()
//X and Y are 16 bit with 12 bit resolution and need to be scaled for the display size
//Z is 24 bit and is typically > 2000 for a touch
println("touch:", touch.X, touch.Y, touch.Z)
//Example of scaling for a 240x320 display
println("screen:", (touch.X*240)>>16, (touch.Y*320)>>16)
//Wait for touch to end
for touchScreen.Touched() {
time.Sleep(50 * time.Millisecond)
}
}
}