-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframes.go
181 lines (151 loc) · 3.78 KB
/
frames.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
176
177
178
179
180
181
package as1130
import (
"fmt"
"image"
"image/color"
"image/draw"
)
var (
Off = color.Gray{0} // Min off
On = color.Gray{255} // Max on
)
// Framer renders an image into frame data.
type Framer interface {
draw.Image
OnOffBytes() ([24]byte, error)
PWMBytes() ([132]byte, error)
SetPWMSet(uint8)
PWMSet() uint8
}
// rect12x11 is the size of Frame12x11.
func rect12x11() image.Rectangle {
return image.Rect(0, 0, 12, 11)
}
// Frame12x11 is a frame for a 12x11 matrix with every LED connected.
type Frame12x11 struct {
*image.Gray
pwmSet uint8
}
// NewFrame12x11 creates a new Frame12x11 of the correct size.
func NewFrame12x11() *Frame12x11 {
return &Frame12x11{
Gray: image.NewGray(rect12x11()),
}
}
// OnOffBytes renders On/Off LED data. Pixels with colour values greater
// than 0 are considered on or blinking.
func (f *Frame12x11) OnOffBytes() ([24]byte, error) {
data := [24]byte{}
if actual, expected := f.Bounds(), rect12x11(); actual != expected {
return data, fmt.Errorf("doesn't match size %s: %s", actual, expected)
}
size := f.Bounds()
for x := size.Min.X; x < size.Max.X; x++ {
var shift uint8
segmentAddr := x * 2
for y := size.Min.Y; y < size.Max.Y; y++ {
if shift == 8 {
shift = 0
segmentAddr++
}
if f.GrayAt(x, y).Y > 0 {
data[segmentAddr] |= 1 << shift
}
shift++
}
}
return data, nil
}
// PWMBytes renders PWM (brightness) LED data. Each pixel has 255 steps.
func (f *Frame12x11) PWMBytes() ([132]byte, error) {
data := [132]byte{}
if f.Bounds() != rect12x11() {
return data, fmt.Errorf("XXX")
}
var dataIndex uint8
size := f.Bounds()
for x := size.Min.X; x < size.Max.X; x++ {
for y := size.Min.Y; y < size.Max.Y; y++ {
data[dataIndex] = f.GrayAt(x, y).Y
dataIndex++
}
}
return data, nil
}
// PWMSet returns the PWM set associated with the frame.
func (f *Frame12x11) PWMSet() uint8 {
return f.pwmSet
}
// SetPWMSet sets the PWM set to associate with the frame.
func (f *Frame12x11) SetPWMSet(set uint8) {
f.pwmSet = set
}
// rect24x5 is the size of Frame24x5.
func rect24x5() image.Rectangle {
return image.Rect(0, 0, 24, 5)
}
// Frame24x5 is a frame for a 24x5 matrix where the last LED in each segment
// is disconnected.
type Frame24x5 struct {
*image.Gray
pwmSet uint8
}
// NewFrame24x5 creates a new Frame24x5 of the correct size.
func NewFrame24x5() *Frame24x5 {
return &Frame24x5{
Gray: image.NewGray(rect24x5()),
}
}
// OnOffBytes renders On/Off LED data. Pixels with colour values greater
// than 0 are considered on or blinking.
func (f *Frame24x5) OnOffBytes() ([24]byte, error) {
data := [24]byte{}
if actual, expected := f.Bounds(), rect24x5(); actual != expected {
return data, fmt.Errorf("doesn't match size %s: %s", actual, expected)
}
size := f.Bounds()
for x := size.Min.X; x < size.Max.X; x += 2 {
var shift uint8
segmentAddr := x
for xOffset := 0; xOffset < 2; xOffset++ {
for y := size.Min.Y; y < size.Max.Y; y++ {
if shift == 8 {
shift = 0
segmentAddr++
}
if f.GrayAt(x+xOffset, y).Y > 0 {
data[segmentAddr] |= 1 << shift
}
shift++
}
}
}
return data, nil
}
// PWMBytes renders PWM (brightness) LED data. Each pixel has 255 steps.
func (f *Frame24x5) PWMBytes() ([132]byte, error) {
data := [132]byte{}
if f.Bounds() != rect24x5() {
return data, fmt.Errorf("XXX")
}
var dataIndex uint8
size := f.Bounds()
for x := size.Min.X; x < size.Max.X; x++ {
for y := size.Min.Y; y < size.Max.Y; y++ {
data[dataIndex] = f.GrayAt(x, y).Y
dataIndex++
}
if x%2 == 1 {
dataIndex++
}
}
return data, nil
}
// PWMSet returns the PWM set associated with the frame.
func (f *Frame24x5) PWMSet() uint8 {
return f.pwmSet
}
// SetPWMSet sets the PWM set to associate with the frame.
func (f *Frame24x5) SetPWMSet(set uint8) {
f.pwmSet = set
}