-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
blank_canvas.go
158 lines (130 loc) · 3.08 KB
/
blank_canvas.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
package board
import (
"context"
"image"
"image/color"
"sync"
"go.uber.org/atomic"
"go.uber.org/zap"
)
// BlankCanvas is a board.Canvas type that does not render
type BlankCanvas struct {
width int
height int
pixels []uint32
enabled *atomic.Bool
log *zap.Logger
sync.Mutex
}
// NewBlankCanvas ...
func NewBlankCanvas(width int, height int, logger *zap.Logger) *BlankCanvas {
i := &BlankCanvas{
width: width,
height: height,
pixels: make([]uint32, (width * height)),
enabled: atomic.NewBool(false),
log: logger,
}
return i
}
// Name ...
func (i *BlankCanvas) Name() string {
return "BlankCanvas"
}
// Scrollable ...
func (i *BlankCanvas) Scrollable() bool {
return false
}
// Close ...
func (i *BlankCanvas) Close() error {
return nil
}
// SetWidth ...
func (i *BlankCanvas) SetWidth(x int) {}
// GetWidth ...
func (i *BlankCanvas) GetWidth() int {
return i.width
}
// Clear sets the canvas to all black
func (i *BlankCanvas) Clear() error {
i.blackOut()
return i.Render(context.Background())
}
// AlwaysRender ...
func (i *BlankCanvas) AlwaysRender() bool {
return true
}
func (i *BlankCanvas) blackOut() {
for x := range i.pixels {
i.pixels[x] = colorToUint32(color.Black)
}
}
// Render stores the state of the image as a PNG
func (i *BlankCanvas) Render(ctx context.Context) error {
return nil
}
// ColorModel returns the canvas' color model, always color.RGBAModel
func (i *BlankCanvas) ColorModel() color.Model {
return color.RGBAModel
}
// Bounds return the topology of the Canvas
func (i *BlankCanvas) Bounds() image.Rectangle {
return image.Rect(0, 0, i.width, i.height)
}
// At returns the color of the pixel at (x, y)
func (i *BlankCanvas) At(x, y int) color.Color {
pos := i.position(x, y)
if pos > len(i.pixels)-1 || pos < 0 {
i.log.Debug("imgcanvas no pixel", zap.Int("x", x), zap.Int("y", y))
return color.Black
}
return uint32ToColor(i.pixels[pos])
}
// Set set LED at position x,y to the provided 24-bit color value
func (i *BlankCanvas) Set(x, y int, clr color.Color) {
pos := i.position(x, y)
if pos > len(i.pixels)-1 || pos < 0 {
return
}
i.pixels[pos] = colorToUint32(clr)
}
// Enabled ...
func (i *BlankCanvas) Enabled() bool {
return i.enabled.Load()
}
// Enable ...
func (i *BlankCanvas) Enable() bool {
return i.enabled.CompareAndSwap(false, true)
}
// Disable ...
func (i *BlankCanvas) Disable() bool {
return i.enabled.CompareAndSwap(true, false)
}
func (i *BlankCanvas) Store(s bool) bool {
return i.enabled.CompareAndSwap(!s, s)
}
func (i *BlankCanvas) SetStateChangeCallback(s func()) {
}
func (i *BlankCanvas) position(x, y int) int {
return x + (y * i.width)
}
// GetHTTPHandlers ...
func (i *BlankCanvas) GetHTTPHandlers() ([]*HTTPHandler, error) {
return nil, nil
}
func uint32ToColor(u uint32) color.Color {
return color.RGBA{
uint8(u>>16) & 255,
uint8(u>>8) & 255,
uint8(u>>0) & 255,
255,
}
}
func colorToUint32(c color.Color) uint32 {
if c == nil {
return 0
}
// A color's RGBA method returns values in the range [0, 65535]
red, green, blue, _ := c.RGBA()
return (red>>8)<<16 | (green>>8)<<8 | blue>>8
}