-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathmain.go
114 lines (85 loc) · 2.73 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"image/color"
"machine"
"strconv"
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/st7789"
// "tinygo.org/x/drivers/xpt2046"
"github.com/gregoster/tinygo-drivers/xpt2046"
"tinygo.org/x/tinyfont"
"tinygo.org/x/tinyfont/freemono"
)
func initSPI() drivers.SPI {
machine.SPI1.Configure(machine.SPIConfig{
Frequency: 3000000,
Mode: 0,
})
return machine.SPI1
}
func initDisplay(bus drivers.SPI) (drivers.Displayer, st7789.Device) {
// https://www.waveshare.com/wiki/Pico-ResTouch-LCD-2.8
display := st7789.New(bus,
machine.GPIO15, // TFT_RESET - reset pin
machine.GPIO8, // TFT_DC - Data/Command
machine.GPIO9, // TFT_CS - Chip Select
machine.GPIO13) // TFT_LITE - Backlite pin
display.Configure(st7789.Config{
Rotation: st7789.ROTATION_270,
RowOffset: 0,
FrameRate: st7789.FRAMERATE_60,
VSyncLines: st7789.MAX_VSYNC_SCANLINES,
Width: 240,
Height: 320,
})
return &display, display
}
func initTouch(bus drivers.SPI) xpt2046.Device {
// clk := machine.GPIO10 // TP_CLK
cs := machine.GPIO16 // TP_CS
// din := machine.GPIO11 // MOSI
// dout := machine.GPIO12 // MISO
irq := machine.GPIO17 // TP_IRQ
touchScreen := xpt2046.New(bus, cs, irq)
touchScreen.Configure(&xpt2046.Config{
Precision: 10, //Maximum number of samples for a single ReadTouchPoint to improve accuracy.
})
return touchScreen
}
func main() {
SPI := initSPI()
touchScreen := initTouch(SPI)
display, _ := initDisplay(SPI)
width, height := display.Size()
//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}
// yellow := color.RGBA{255,255,0,255}
tinyfont.WriteLine(display, &freemono.Regular9pt7b, 30,
80, strconv.Itoa(int(width))+"x"+strconv.Itoa(int(height)), green)
prev := ""
// tinyfont.WriteLine(display, &freemono.Regular9pt7b, 0, 160, "HERE!", red)
// tinyfont.WriteLine(display, &freemono.Regular9pt7b, 0, 200, strconv.Itoa(int(machine.SPI1.GetBaudRate())), red)
for {
//Wait for a touch
for !touchScreen.Touched() {
time.Sleep(50 * time.Millisecond)
}
if prev != "" {
tinyfont.WriteLine(display, &freemono.Regular9pt7b, 0, 180, prev, black)
}
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
//Example of scaling for a 240x320 display
prev = strconv.Itoa(int((touch.X*240)>>16)) + "x" + strconv.Itoa(int(touch.Y*320)>>16)
tinyfont.WriteLine(display, &freemono.Regular9pt7b, 0, 180, prev, blue)
//Wait for touch to end
for touchScreen.Touched() {
time.Sleep(50 * time.Millisecond)
}
}
}