/
display.go
129 lines (102 loc) · 2.4 KB
/
display.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package display
import (
"fmt"
"image"
"log"
"sync"
"github.com/golang/freetype/truetype"
"periph.io/x/conn/v3/i2c"
"periph.io/x/conn/v3/i2c/i2creg"
"periph.io/x/devices/v3/ssd1306"
"periph.io/x/devices/v3/ssd1306/image1bit"
"periph.io/x/host/v3"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/math/fixed"
)
type (
OLED struct {
bc i2c.BusCloser
dev *ssd1306.Dev
lock sync.Mutex
}
)
func New() (*OLED, error) {
_, err := host.Init()
if err != nil {
return nil, err
}
bc, err := i2creg.Open("")
if err != nil {
return nil, err
}
dev, err := ssd1306.NewI2C(bc, &ssd1306.DefaultOpts)
if err != nil {
return nil, err
}
return &OLED{bc: bc, dev: dev}, nil
}
func (o *OLED) Close() {
o.bc.Close()
}
func (o *OLED) Clear() {
o.dev.Halt()
}
func (o *OLED) Print(tt, at int, state string) {
o.lock.Lock()
img := image1bit.NewVerticalLSB(o.dev.Bounds())
fontTTF, _ := truetype.Parse(goregular.TTF)
face := truetype.NewFace(fontTTF, &truetype.Options{
Size: 24,
DPI: 72,
})
smallFace := basicfont.Face7x13
//y := img.Bounds().Dy() - 1 - f.Descent
temperature := font.Drawer{
Dst: img,
Src: &image.Uniform{image1bit.On},
Face: face,
Dot: fixed.P(0, 24),
}
legend := font.Drawer{
Dst: img,
Src: &image.Uniform{image1bit.On},
Face: smallFace,
Dot: fixed.P(0, 34),
}
st := font.Drawer{
Dst: img,
Src: &image.Uniform{image1bit.On},
Face: face,
Dot: fixed.P(0, 60),
}
if state == "Off" {
temperature.DrawString(fmt.Sprintf("-- %02d", at))
} else {
temperature.DrawString(fmt.Sprintf("%02d %02d", tt, at))
}
legend.DrawString("target actual")
st.DrawString(fmt.Sprintf(" %s", state))
if err := o.dev.Draw(o.dev.Bounds(), img, image.Point{}); err != nil {
log.Fatal(err)
}
o.lock.Unlock()
}
func (o *OLED) Message(msg string) {
o.lock.Lock()
fontTTF, _ := truetype.Parse(goregular.TTF)
img := image1bit.NewVerticalLSB(o.dev.Bounds())
legend := font.Drawer{
Dst: img,
Src: &image.Uniform{image1bit.On},
Face: truetype.NewFace(fontTTF, &truetype.Options{Size: 16, DPI: 72}),
Dot: fixed.P(0, 34),
}
w := 24
legend.DrawString(fmt.Sprintf(fmt.Sprintf("%%-%ds", w/2), fmt.Sprintf(fmt.Sprintf("%%%ds", w/2), msg)))
if err := o.dev.Draw(o.dev.Bounds(), img, image.Point{}); err != nil {
log.Fatal(err)
}
o.lock.Unlock()
}