-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
252 lines (226 loc) · 5.66 KB
/
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package insta
import (
"bytes"
"encoding/binary"
"image"
"image/draw"
"log"
"net"
"time"
"fmt"
)
const (
syncPort = 8500
dataPort = 9410
)
type pkt struct {
head [34]byte
panelLeft [486]uint8
brightnessLeft uint8
contrastLeft uint8
afterglowLeft uint8
unknownLeft uint8
trailLeft [49]byte
panelRight [486]uint8
brightnessRight uint8
contrastRight uint8
afterglowRight uint8
unknownRight uint8
trailRight [49]byte
}
func newPkt() *pkt {
p := pkt{}
copy(p.head[:], []byte("INSTA-INET\x00\x01\x00\x00\x01\xac\x10\x05\x00\x00"+
/* img 1, sync 8: */ "\x01"+"\x00\x01"+
/* frame counter */ "\x00\x00"+"\x00\x00\x00\x00\x01\xe6\x00\x1a\x00"))
copy(p.trailLeft[:], []byte("\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14\x91\x49\x45\x01"+
/* 00|01|02 cycle */ "\x00"+"\x00\x01"+
/* e3|6c|e0 cycle */ "\xe3"+"\x80\x16\x08"+
/* "checksum" (cycle+ip) */ "\x00\x00\x00\x00\x00\x00\x00\x00\x00"+"\x00\x00\x00\x00\x00\x00"))
copy(p.trailRight[:], p.trailLeft[:])
p.brightnessLeft = 128
p.brightnessRight = 128
p.contrastLeft = 128
p.contrastRight = 128
p.afterglowLeft = 80
p.afterglowRight = 80
p.unknownLeft = 0
p.unknownRight = 0
return &p
}
type syncPkt struct {
head [27]byte
}
func newSyncPkt() *syncPkt {
s := syncPkt{}
copy(s.head[:], []byte("INSTA-INET\x00\x01\x00\x00\x01\xac\x10\x05\x00\x00"+
/* img 1, sync 8: */ "\x08"+"\x00\x00\x00\x00\x00\x00"))
return &s
}
type Client interface {
SetScreen(*Screen)
SetScreenImmediate(*Screen)
SetFPS(int)
Run()
SetAfterglow(float64)
}
type syncedImage struct {
image image.Image
syncAt time.Time
}
type InstaClient struct {
syncSock *net.UDPConn
dataSock *net.UDPConn
panelAddrs []*net.UDPAddr
syncBytes []byte
dataPkt *pkt
imgs chan syncedImage
fps int
nextSync time.Time
}
func NewInstaClient(addrs []string) (*InstaClient, error) {
c := InstaClient{imgs: make(chan syncedImage, 1), fps: 50}
if len(addrs) != PanelsX*PanelsY {
return nil, fmt.Errorf("invalid number of addresses, got %d for %dx%d panels",
len(addrs), PanelsX, PanelsY)
}
for _, addr := range addrs {
udpAddr := &net.UDPAddr{IP: net.ParseIP(addr), Port: dataPort}
c.panelAddrs = append(c.panelAddrs, udpAddr)
}
var err error
c.dataSock, err = net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4zero, Port: 0})
if err != nil {
return nil, err
}
lip, err := localSourceIP(net.ParseIP(addrs[0]))
if err != nil {
return nil, err
}
c.syncSock, err = net.DialUDP("udp4",
&net.UDPAddr{IP: lip, Port: syncPort},
&net.UDPAddr{IP: net.IPv4(255, 255, 255, 255), Port: dataPort})
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
syncPkt := newSyncPkt()
if err := binary.Write(buf, binary.LittleEndian, syncPkt); err != nil {
return nil, err
}
c.syncBytes = buf.Bytes()
c.dataPkt = newPkt()
return &c, nil
}
func (c *InstaClient) SetScreen(s *Screen) {
dur := time.Second / time.Duration(c.fps)
// wait till previous frame was synced, in case we are to fast
if c.nextSync.After(time.Now()) {
time.Sleep(c.nextSync.Sub(time.Now()))
}
// step c.nextSync time for next frame
c.nextSync = c.nextSync.Add(dur)
// is next frame in the past? forward to next c.nextSync in the future
if c.nextSync.Before(time.Now()) {
log.Println("dropped frame")
c.nextSync = time.Now().Add(dur)
}
select {
case c.imgs <- syncedImage{image: s.Copy(), syncAt: c.nextSync}:
default: // skip screen
}
}
func (c *InstaClient) SetScreenImmediate(s *Screen) {
select {
case c.imgs <- syncedImage{image: s.Copy()}:
default: // skip screen
}
}
func (c *InstaClient) SetAfterglow(v float64) {
if v < 0 {
v = 0
}
if v > 1 {
v = 1
}
c.dataPkt.afterglowLeft = uint8(v * 255)
c.dataPkt.afterglowRight = uint8(v * 255)
}
func (c *InstaClient) send(img image.Image) error {
scr := NewScreen() // TODO, create image2panel function
draw.Draw(scr, scr.Bounds(), img, image.ZP, draw.Over)
i := 0
buf := new(bytes.Buffer)
for y := 0; y < PanelsY; y++ {
for x := 0; x < PanelsX; x++ {
c.dataPkt.panelLeft, c.dataPkt.panelRight = scr.Panel(x, y)
err := binary.Write(buf, binary.LittleEndian, c.dataPkt)
if err != nil {
return err
}
n, err := c.dataSock.WriteTo(buf.Bytes(), c.panelAddrs[i])
if err != nil {
return err
}
if n != buf.Len() {
return fmt.Errorf("not all bytes sent: %d of %d", n, buf.Len())
}
buf.Reset()
i += 1
}
}
return nil
}
func (c *InstaClient) SetFPS(fps int) {
if fps < 0 {
fps = 50
}
c.fps = fps
}
func (c *InstaClient) sync() error {
n, err := c.syncSock.Write(c.syncBytes)
if err != nil {
return err
}
if n != len(c.syncBytes) {
return fmt.Errorf("not all bytes sent: %d of %d", n, len(c.syncBytes))
}
return nil
}
func (c *InstaClient) Run() {
for syncedImg := range c.imgs {
if err := c.send(syncedImg.image); err != nil {
log.Printf("error: while sending packages: %v", err)
time.Sleep(time.Second)
}
if !syncedImg.syncAt.IsZero() {
wait := syncedImg.syncAt.Sub(time.Now())
time.Sleep(wait)
}
if err := c.sync(); err != nil {
log.Printf("error: while sending packages: %v", err)
}
}
}
// localSourceIP returns the local IP address that is in the same network as target
func localSourceIP(target net.IP) (net.IP, error) {
ifaces, err := net.Interfaces()
if err != nil {
return net.IPv4zero, err
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
return net.IPv4zero, err
}
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
if v.Contains(target) {
return v.IP, nil
}
}
}
}
return net.IPv4zero, fmt.Errorf("found no interface for %v", target)
}