-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev_2in9.go
175 lines (149 loc) · 3.62 KB
/
dev_2in9.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package epd
import "image"
var (
dev2in9LutFullUpdate = []byte{
0x02, 0x02, 0x01, 0x11, 0x12, 0x12, 0x22, 0x22,
0x66, 0x69, 0x69, 0x59, 0x58, 0x99, 0x99, 0x88,
0x00, 0x00, 0x00, 0x00, 0xF8, 0xB4, 0x13, 0x51,
0x35, 0x51, 0x51, 0x19, 0x01, 0x00,
}
dev2in9LutPartialUpdate = []byte{
0x10, 0x18, 0x18, 0x08, 0x18, 0x18, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x13, 0x14, 0x44, 0x12,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
dev2in9Width = 128
dev2in9Height = 296
)
type dev2in9 struct {
board *board
}
func newDev2in9(board *board) device {
return &dev2in9{board}
}
/***************************************** interface functions ****************************************/
func (d *dev2in9) init(particalUpdate bool) error {
if err := d.board.init(); err != nil {
return err
}
if particalUpdate {
d.initLut(dev2in9LutPartialUpdate)
} else {
d.initLut(dev2in9LutFullUpdate)
}
return nil
}
func (d *dev2in9) clear() {
d.setWindow(0, 0, uint(dev2in9Width-1), uint(dev2in9Height-1))
for j := 0; j < dev2in9Height; j++ {
d.setCursor(0, uint(j))
d.sendCmd(0x24)
for i := 0; i < dev2in9Width/8; i++ {
d.sendData(0xff)
}
}
d.turnOnDisplay()
}
func (d *dev2in9) display(img image.Image) {
d.setWindow(0, 0, uint(dev2in9Width-1), uint(dev2in9Height-1))
for j := 0; j < dev2in9Height; j++ {
d.setCursor(0, uint(j))
d.sendCmd(0x24)
for i := 0; i < dev2in9Width/8; i++ {
d.sendData(getImageByte(j, i, img))
}
}
d.turnOnDisplay()
}
func (d *dev2in9) sleep() {
d.sendCmd(0x10)
d.sendData(0x01)
d.board.cleanup()
}
/***************************************** private functions ****************************************/
func (d *dev2in9) initLut(lut []byte) {
d.reset()
d.sendCmd(0x01)
d.sendData(byte((dev2in9Height - 1) & 0xff))
d.sendData(byte(((dev2in9Height - 1) >> 8) & 0xff))
d.sendData(0x00)
d.sendCmd(0x0c)
d.sendData(0xd7)
d.sendData(0xd6)
d.sendData(0x9d)
d.sendCmd(0x2c)
d.sendData(0xa8)
d.sendCmd(0x3a)
d.sendData(0x1a)
d.sendCmd(0x3b)
d.sendData(0x08)
d.sendCmd(0x11)
d.sendData(0x03)
d.sendCmd(0x32)
d.sendData(lut...)
}
func (d *dev2in9) reset() {
d.board.writeRST(true)
d.board.delayms(200)
d.board.writeRST(false)
d.board.delayms(10)
d.board.writeRST(true)
d.board.delayms(200)
}
func (d *dev2in9) sendCmd(b byte) {
d.board.writeDC(false)
d.board.writeCS(false)
d.board.writeSPI(b)
d.board.writeCS(true)
}
func (d *dev2in9) sendData(b ...byte) {
d.board.writeDC(true)
d.board.writeCS(false)
d.board.writeSPI(b...)
d.board.writeCS(true)
}
func (d *dev2in9) turnOnDisplay() {
d.sendCmd(0x22)
d.sendData(0xc4)
d.sendCmd(0x20)
d.sendCmd(0xff)
d.board.readBUSY()
}
func (d *dev2in9) setWindow(xStart, yStart, xEnd, yEnd uint) {
d.sendCmd(0x44)
// x point must be the multiple of 8 or the last 3 bits will be ignored
d.sendData(byte((xStart >> 3) & 0xff))
d.sendData(byte((xEnd >> 3) & 0xff))
d.sendCmd(0x45)
d.sendData(byte(yStart & 0xff))
d.sendData(byte((yStart >> 8) & 0xff))
d.sendData(byte(yEnd & 0xff))
d.sendData(byte((yEnd >> 8) & 0xff))
}
func (d *dev2in9) setCursor(x, y uint) {
d.sendCmd(0x4e)
// x point must be the multiple of 8 or the last 3 bits will be ignored
d.sendData(byte((x >> 3) & 0xff))
d.sendCmd(0x4f)
d.sendData(byte(y & 0xff))
d.sendData(byte((y >> 8) & 0xff))
d.board.readBUSY()
}
func getImageByte(j, i int, img image.Image) byte {
var b byte
for x := 0; x < 8; x++ {
xx := i*8 + x
if getPixelValue(xx, j, img) > 0 {
b = b | (1 << uint(7-x))
}
}
return b
}
func getPixelValue(x, y int, img image.Image) int {
c := img.At(x, y)
if convertColorToBlackWhite(c) {
return 1
}
return 0
}