-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapped.go
More file actions
109 lines (95 loc) · 3.09 KB
/
wrapped.go
File metadata and controls
109 lines (95 loc) · 3.09 KB
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
package lights
import (
"context"
"go.uber.org/zap"
"github.com/akrantz01/lights/api/database"
"github.com/akrantz01/lights/api/lights/pb"
)
var logger = zap.L().Named("controller")
// Set changes the color of pixels at the given indexes
func (c *Controller) Set(ctx context.Context, indexes []uint32, color database.Color) {
_, err := c.inner.Set(ctx, &pb.SetArgs{
Indexes: indexes,
Color: &pb.Color{
R: uint32(color.Red),
G: uint32(color.Green),
B: uint32(color.Blue),
},
})
if err != nil {
logger.Error("failed to set index(es)", zap.Uint32s("indexes", indexes), zap.Any("color", color), zap.Error(err))
}
}
// Fill changes the color of the entire strip
func (c *Controller) Fill(ctx context.Context, color database.Color) {
_, err := c.inner.Fill(ctx, &pb.Color{
R: uint32(color.Red),
G: uint32(color.Green),
B: uint32(color.Blue),
})
if err != nil {
logger.Error("failed to fill strip", zap.Any("color", color), zap.Error(err))
}
}
// Brightness sets the brightness of the entire strip
func (c *Controller) Brightness(ctx context.Context, brightness uint8) {
_, err := c.inner.Brightness(ctx, &pb.BrightnessArgs{
Brightness: uint32(brightness),
})
if err != nil {
logger.Error("failed to set brightness", zap.Uint8("level", brightness), zap.Error(err))
}
}
// SetAll sets all the pixels to the specified colors at once
func (c *Controller) SetAll(ctx context.Context, pixels []database.Color) {
var pbColors []*pb.Color
for _, pixel := range pixels {
pbColors = append(pbColors, &pb.Color{
R: uint32(pixel.Red),
G: uint32(pixel.Green),
B: uint32(pixel.Blue),
})
}
_, err := c.inner.SetAll(ctx, &pb.SetAllArgs{
Colors: pbColors,
})
if err != nil {
logger.Error("failed to set all pixels", zap.Any("pixels", pixels), zap.Error(err))
}
}
// StartAnimation starts the specified animation (if it exists)
func (c *Controller) StartAnimation(ctx context.Context, id string) {
_, err := c.inner.StartAnimation(ctx, &pb.StartAnimationArgs{
Id: id,
})
if err != nil {
logger.Error("failed to start animation", zap.String("id", id), zap.Error(err))
}
}
// StopAnimation stops the currently running animation (if it exists)
func (c *Controller) StopAnimation(ctx context.Context) {
if _, err := c.inner.StopAnimation(ctx, &pb.Empty{}); err != nil {
logger.Error("failed to stop animation", zap.Error(err))
}
}
// RegisterAnimation creates a new animation on the controller and compiles it
func (c *Controller) RegisterAnimation(ctx context.Context, id string, wasm []byte) bool {
result, err := c.inner.RegisterAnimation(ctx, &pb.RegisterAnimationArgs{
Id: id,
Wasm: wasm,
})
if err != nil {
logger.Error("failed to register animation", zap.String("id", id), zap.Error(err))
return false
}
return result.Success
}
// UnregisterAnimation removes an animation from the controller (if it exists)
func (c *Controller) UnregisterAnimation(ctx context.Context, id string) {
_, err := c.inner.UnregisterAnimation(ctx, &pb.UnregisterAnimationArgs{
Id: id,
})
if err != nil {
logger.Error("failed to unregister animation", zap.String("id", id), zap.Error(err))
}
}