-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathmain.go
95 lines (78 loc) · 1.89 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
package main
import (
"fmt"
"image/color"
"strings"
"time"
"tinygo.org/x/drivers/examples/ili9341/initdisplay"
"tinygo.org/x/drivers/ili9341"
"tinygo.org/x/drivers/image/jpeg"
"tinygo.org/x/drivers/image/png"
)
var (
black = color.RGBA{0, 0, 0, 255}
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}
)
var (
display *ili9341.Device
)
func main() {
err := run()
for err != nil {
errorMessage(err)
}
}
func run() error {
display = initdisplay.InitDisplay()
width, height := display.Size()
if width < 320 || height < 240 {
display.SetRotation(ili9341.Rotation270)
}
display.FillScreen(black)
for {
err := drawJpeg(display)
if err != nil {
return err
}
time.Sleep(time.Second)
err = drawPng(display)
if err != nil {
return err
}
time.Sleep(time.Second)
}
return nil
}
// Define the buffer required for the callback. In most cases, this setting
// should be sufficient. For jpeg, the callback will always be called every
// 3*8*8*4 pix. png will be called every line, i.e. every width pix.
var buffer [3 * 8 * 8 * 4]uint16
func drawPng(display *ili9341.Device) error {
p := strings.NewReader(pngImage)
png.SetCallback(buffer[:], func(data []uint16, x, y, w, h, width, height int16) {
err := display.DrawRGBBitmap(x, y, data[:w*h], w, h)
if err != nil {
errorMessage(fmt.Errorf("error drawPng: %s", err))
}
})
return png.Decode(p)
}
func drawJpeg(display *ili9341.Device) error {
p := strings.NewReader(jpegImage)
jpeg.SetCallback(buffer[:], func(data []uint16, x, y, w, h, width, height int16) {
err := display.DrawRGBBitmap(x, y, data[:w*h], w, h)
if err != nil {
errorMessage(fmt.Errorf("error drawJpeg: %s", err))
}
})
return jpeg.Decode(p)
}
func errorMessage(err error) {
for {
fmt.Printf("%s\r\n", err.Error())
time.Sleep(5 * time.Second)
}
}