/
matrix_rpc_client.go
158 lines (137 loc) · 3.28 KB
/
matrix_rpc_client.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 matrix
import (
"encoding/gob"
. "github.com/gabz57/goledmatrix/canvas"
"image/color"
"log"
"net/rpc"
"strconv"
"time"
)
func init() {
gob.Register(color.RGBA{})
}
type MatrixRpcClient struct {
config *MatrixConfig
client *rpc.Client
lastFrameDuration time.Duration
previous []RpcPixel
}
func NewRpcClient(config *MatrixConfig) (Matrix, error) {
rpcClient, err := rpc.DialHTTP("tcp", config.IpAddress+":8080")
if err != nil {
return nil, err
}
client := MatrixRpcClient{
config: config,
client: rpcClient,
lastFrameDuration: 0 * time.Nanosecond,
}
if err != nil {
return nil, err
}
if err = validateGeometry(config, &client); err != nil {
return nil, err
}
return &client, nil
}
func (m *MatrixRpcClient) Config() *MatrixConfig {
return m.config
}
func (m *MatrixRpcClient) Geometry() (width, height int) {
var reply *GeometryReply
err := m.client.Call("MatrixRPCServer.Geometry", &GeometryArgs{}, &reply)
if err != nil {
panic(err)
}
return reply.Width, reply.Height
}
func (m *MatrixRpcClient) RenderMethod(canvas Canvas) error {
return m.Render(canvas)
}
const frameDurationInNanos = 33333333 // 30 FPS approximated in nanos
func (m *MatrixRpcClient) Render(canvas Canvas) error {
if m.lastFrameDuration < frameDurationInNanos {
start := time.Now()
err := m.doRender(canvas)
duration := time.Now().Sub(start)
log.Println("rpc render took " + strconv.FormatInt(duration.Milliseconds(), 10) + "ms")
m.lastFrameDuration = duration
return err
} else {
log.Println("rpc render skipped")
m.lastFrameDuration = 0 * time.Nanosecond
return nil
}
}
func (m *MatrixRpcClient) doRender(canvas Canvas) error {
var reply *RenderReply
pixels := toRpcPixels(canvas)
if m.sameAsPrevious(pixels) {
println("skip same")
m.previous = pixels
return nil
} else {
m.previous = pixels
err := m.client.Call("MatrixRPCServer.Render", &RenderArgs{
Pixels: pixels,
Timestamp: time.Now().UnixNano(),
}, &reply)
return err
}
}
func toRpcPixels(canvas Canvas) []RpcPixel {
width := canvas.Bounds().Max.X
pixels := make([]RpcPixel, canvas.Bounds().Max.X*canvas.Bounds().Max.Y)
index := 0
for i, c := range *canvas.GetLeds() {
if c != nil && !isBlack(c) {
pixels[index] = RpcPixel{
X: i % width,
Y: i / width,
C: c,
}
index++
}
}
return pixels[:index]
}
func isBlack(c color.Color) bool {
br, bg, bb, _ := color.Black.RGBA()
r, g, b, _ := c.RGBA()
return r == br && g == bg && b == bb
}
func (m *MatrixRpcClient) Close() error {
var reply *CloseReply
return m.client.Call("MatrixRPCServer.Close", &CloseArgs{}, &reply)
}
func (m *MatrixRpcClient) MainThread(_ Canvas, done chan struct{}) {
select {
case <-done:
break
}
}
func (m *MatrixRpcClient) send(_ interface{}) {
// implemented only in emulator which handle data asynchronously
}
func (m *MatrixRpcClient) sameAsPrevious(pixels []RpcPixel) bool {
if m.previous == nil {
return false
}
if len(pixels) != len(m.previous) {
return false
}
for i, pixel := range pixels {
rpcPixel := m.previous[i]
br, bg, bb, _ := rpcPixel.C.RGBA()
r, g, b, _ := rpcPixel.C.RGBA()
if pixel.X != rpcPixel.X ||
pixel.Y != rpcPixel.Y ||
r != br ||
g != bg ||
b != bb {
return false
}
}
return true
}