forked from mcuadros/go-rpi-rgb-led-matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.go
79 lines (67 loc) · 1.82 KB
/
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
package rgbmatrix
import (
"image"
"image/color"
"image/draw"
)
// Canvas is a image.Image representation of a WS281x matrix, it implements
// image.Image interface and can be used with draw.Draw for example
type Canvas struct {
w, h int
m Matrix
//closed bool // Unused ?
}
// NewCanvas returns a new Canvas using the given width and height and creates
// a new WS281x matrix using the given config
func NewCanvas(m Matrix) *Canvas {
w, h := m.Geometry()
return &Canvas{
w: w,
h: h,
m: m,
}
}
// Render update the display with the data from the LED buffer
func (c *Canvas) Render() error {
return c.m.Render()
}
// ColorModel returns the canvas' color model, always color.RGBAModel
func (c *Canvas) ColorModel() color.Model {
return color.RGBAModel
}
// Bounds return the topology of the Canvas
func (c *Canvas) Bounds() image.Rectangle {
return image.Rect(0, 0, c.w, c.h)
}
// At returns the color of the pixel at (x, y)
func (c *Canvas) At(x, y int) color.Color {
return c.m.At(c.position(x, y))
}
// Set set LED at position x,y to the provided 24-bit color value
func (c *Canvas) Set(x, y int, color color.Color) {
c.m.Set(c.position(x, y), color)
}
func (c *Canvas) position(x, y int) int {
return x + (y * c.w)
}
// Clear set all the leds on the matrix with color.Black
func (c *Canvas) Clear() error {
draw.Draw(c, c.Bounds(), &image.Uniform{color.Black}, image.Point{}, draw.Src)
return c.m.Render()
}
// Close clears the matrix and close the matrix
func (c *Canvas) Close() error {
c.Clear()
return c.m.Close()
}
// Matrix is an interface that represent any RGB matrix, very useful for testing
type Matrix interface {
Geometry() (width, height int)
At(position int) color.Color
Set(position int, c color.Color)
Apply([]color.Color) error
Render() error
Close() error
GetBrightness() int
SetBrightness(int)
}