-
Notifications
You must be signed in to change notification settings - Fork 4
/
epdfuse.go
138 lines (120 loc) · 2.76 KB
/
epdfuse.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
// Copyright 2017 Whit Marbut. All rights reserved.
// License information may be found in the LICENSE file.
package epdfuse
import (
"bytes"
"github.com/wmarbut/goxbm"
"image"
"io"
"os"
"path"
)
const DISPLAY_PATH = "/BE/display"
const EPD_COMMAND_PATH = "/command"
const EPD_DEFAULT_PATH = "/dev/epd"
const EPD_DEFAULT_WIDTH = 200
const EPD_DEFAULT_HEIGHT = 96
type EpdCommand byte
const (
// EPD Command to trigger a display update
COMMAND_UPDATE EpdCommand = 'U'
// EPD Command to clear display
COMMAND_CLEAR EpdCommand = 'C'
// EPD Command to trigger a partial display update
COMMAND_PARTIAL EpdCommand = 'P'
)
// Configures EPD Fuse
type EpdFuse struct {
EpdPath string
Width int
Height int
}
// Create a new EpdFuse to interact with your display
func NewEpdFuse() EpdFuse {
return EpdFuse{
EPD_DEFAULT_PATH,
EPD_DEFAULT_WIDTH,
EPD_DEFAULT_HEIGHT,
}
}
// Create a new EpdFuse to interact with your display
func NewCustomEpdFuse(path string, width, height int) EpdFuse {
return EpdFuse{
path,
width,
height,
}
}
// Write wrapped text to your PaPiRus display
func (epd *EpdFuse) WriteText(text string) error {
img, err := epd.buildTextImage(text)
if err != nil {
return err
}
return epd.WriteImage(img)
}
// Write image to your PaPiRus display
func (epd *EpdFuse) WriteImage(img image.Image) error {
img = epd.scaleAndPlaceImage(img)
err := epd.writeDisplay(goxbm.ToRawXBMBytes(img))
if err != nil {
return err
}
return epd.Update()
}
// Write partial image to your PaPiRus display
func (epd *EpdFuse) WriteImagePartial(img image.Image) error {
img = epd.scaleAndPlaceImage(img)
err := epd.writeDisplay(goxbm.ToRawXBMBytes(img))
if err != nil {
return err
}
return epd.PartialUpdate()
}
// Force an update of the PaPiRus display
func (epd *EpdFuse) Update() error {
return epd.update(COMMAND_UPDATE)
}
// Clear the PaPiRus display
func (epd *EpdFuse) Clear() error {
return epd.update(COMMAND_CLEAR)
}
// Perform a partial update
func (epd *EpdFuse) PartialUpdate() error {
return epd.update(COMMAND_PARTIAL)
}
func (epd *EpdFuse) update(command EpdCommand) error {
cmdPath := path.Join(epd.EpdPath, EPD_COMMAND_PATH)
f, err := os.OpenFile(cmdPath, os.O_WRONLY|os.O_APPEND, 0222)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write([]byte{byte(command)})
if err != nil {
return err
}
err = f.Sync()
if err != nil {
return err
}
return nil
}
func (epd *EpdFuse) writeDisplay(data []byte) error {
displayPath := path.Join(epd.EpdPath, DISPLAY_PATH)
f, err := os.OpenFile(displayPath, os.O_WRONLY, 0666)
if err != nil {
return err
}
defer f.Close()
dataReader := bytes.NewReader(data)
_, err = io.Copy(f, dataReader)
if err != nil {
return err
}
err = f.Sync()
if err != nil {
return err
}
return nil
}