-
Notifications
You must be signed in to change notification settings - Fork 3
/
draw.go
44 lines (40 loc) · 989 Bytes
/
draw.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
package goi2coled
import (
"fmt"
"image"
"image/color"
)
func (i *I2c) convertImageToOLEDData() ([]byte, error) {
bounds := i.Img.Bounds()
if bounds.Max.X != i.screen.w || i.screen.h != bounds.Max.Y {
panic(fmt.Sprintf("Error: Size of image is not %dx%d pixels.", i.screen.w, i.screen.h))
}
size := i.screen.w * i.screen.h / PIXSIZE
data := make([]byte, size)
for page := 0; page < i.screen.h/8; page++ {
for x := 0; x < i.screen.w; x++ {
bits := uint8(0)
for bit := 0; bit < 8; bit++ {
y := page*8 + 7 - bit
if y < i.screen.h {
col := color.GrayModel.Convert(i.Img.At(x, y)).(color.Gray)
if col.Y > 127 {
bits = (bits << 1) | 1
} else {
bits = bits << 1
}
}
}
index := page*i.screen.w + x
data[index] = byte(bits)
}
}
return data, nil
}
func (i *I2c) DrawImage(image *image.RGBA) {
i.Img = image
i.buffer, _ = i.convertImageToOLEDData()
}
func (i *I2c) Draw() {
i.buffer, _ = i.convertImageToOLEDData()
}