-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
399 lines (338 loc) · 7.06 KB
/
main.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package main
import (
"fmt"
"github.com/tarm/serial"
"math/rand"
"net"
"os"
"strconv"
"strings"
"time"
)
const START = 0x7f
const STOP = 0x7e
const ESCAPE = 0x20
const BAUD = 115200
const BLANKTIME = 3
var lastFireTime time.Time
var interstitialId = 0
var r1 *rand.Rand
func main() {
addr := net.UDPAddr{
Port: 1075,
IP: net.IPv4zero,
}
conn, err := net.ListenUDP("udp", &addr)
defer conn.Close()
if err != nil {
panic(err)
}
port := os.Args[1]
sc := &serial.Config{Name: port, Baud: BAUD}
ser, err := serial.OpenPort(sc)
if err != nil {
panic(err)
}
lastFireTime = time.Now()
s1 := rand.NewSource(time.Now().UnixNano())
r1 = rand.New(s1)
fmt.Printf("Sending on %s. Monitor with: cu -l /dev/ttyUSB? -s %d\n", port, BAUD)
Serve(conn, ser)
}
func Serve(conn *net.UDPConn, ser *serial.Port) {
for {
u := make([]byte, 1024)
n, err := conn.Read(u)
if err != nil {
panic(err)
}
bytes, err := handleUdp(u, n)
if err != nil {
panic(err)
}
//bytes[0] = 0xff
//bytes[1] = 0xff
//bytes[2] = 0xff
//bytes[3] = 0xff
fmt.Printf("Serial send: ")
for i := 0; i < len(bytes); i++ {
fmt.Printf("%02x", bytes[i])
}
fmt.Printf("\n")
bf := make([]byte, len(bytes)*2+3)
bf_i := 0
/*
for b := 0; b < 8; b++ {
bf[bf_i] = START
bf_i += 1
}
*/
bf[bf_i] = START
bf_i += 1
var csum byte
for b := 0; b < len(bytes); b++ {
if bytes[b] == START || bytes[b] == ESCAPE || bytes[b] == STOP {
fmt.Printf("frame: %d, ESCAPED\n", bf_i)
bf[bf_i] = ESCAPE
bf_i += 1
bf[bf_i] = bytes[b] ^ ESCAPE
} else {
bf[bf_i] = bytes[b]
}
bf_i += 1
csum ^= bytes[b]
}
bf[bf_i] = csum
bf_i += 1
bf[bf_i] = STOP
bf_i += 1
fmt.Printf("Framed send: ")
for i := 0; i < bf_i; i++ {
fmt.Printf("%02x", bf[i])
}
fmt.Printf("\n")
n, err = ser.Write(bf[0:bf_i])
if err != nil {
panic(err)
}
if n != bf_i {
panic(fmt.Errorf("Not enough bytes written: wrote %d, expected %d", n, len(bytes)))
}
}
}
func getInt(str string) int {
val, _ := strconv.ParseInt(str, 0, 32)
return int(val)
}
func handleUdp(b []byte, n int) ([]byte, error) {
fmt.Printf("Received %d UDP bytes\n", n)
parts := strings.SplitN(string(b[0:n]), "\n", 4)
ver := getInt(parts[0])
cols := getInt(parts[1])
rows := getInt(parts[2])
board := parts[3]
if ver != 0 && ver != 1 {
return nil, fmt.Errorf("Unsupported version %d", ver)
}
if cols < 1 || cols > 10 {
return nil, fmt.Errorf("Unsupported width %d", cols)
}
if rows < 1 || rows > 20 {
return nil, fmt.Errorf("Unsupported height %d", rows)
}
if ver == 1 {
// In v1, an extra row is added at the beginning to
// control the victory poofer.
rows += 1
}
lines := strings.SplitN(board, "\n", rows+1)
if len(lines) < rows {
return nil, fmt.Errorf("Not enough lines: received %d, expected %d", len(lines), rows)
}
for l := 0; l < rows; l++ {
line := lines[l]
if len(line) != cols {
return nil, fmt.Errorf("Line %d length wrong: received %d, expected %d", l, len(line), cols)
}
}
if allBlank(lines[0:rows]) {
blankFor := time.Now().Sub(lastFireTime).Seconds()
if blankFor >= BLANKTIME {
fmt.Printf("INTERSTITIAL after %vs\n", blankFor)
ver = 0
rows = 20
if interstitialId == 0 {
interstitialId = r1.Intn(len(INTERSTITIALS)) + 1
}
lines = INTERSTITIALS[interstitialId-1]
}
} else {
lastFireTime = time.Now()
interstitialId = 0
}
if cols < 10 || rows < 20 {
lines = extendLines(lines[0:rows])
}
if ver == 0 {
return linesToBytes(lines[0:20], 20, 10, false)
} else {
vp, err := lineToVP(lines[0])
if err != nil {
return nil, err
}
return linesToBytes(lines[1:20], 20, 10, vp)
}
}
func allBlank(lines []string) bool {
for _, line := range lines {
if strings.Contains(line, "1") {
return false
}
}
return true
}
func extendLines(lines []string) []string {
newLines := make([]string, 20)
inRows := len(lines)
for l := 0; l < len(newLines); l++ {
line := lines[l%inRows]
inCols := len(line)
newLine := strings.Repeat(line, 10/inCols+1)
newLines[l] = newLine[0:10]
}
return newLines
}
var INTERSTITIALS = [][]string{
{
"0100000010",
"1100000011",
"1100000011",
"1110110111",
"1111111111",
"1100110011",
"1100110011",
"0111111110",
"0111001110",
"0011111100",
"0010000100",
"0010000100",
"0000000000",
"0000000000",
"0000000000",
"0000000000",
"0000000000",
"0000000000",
"0000000000",
"0000000000",
},
{
"1010000000",
"1010111000",
"1110101000",
"0010111000",
"1110000101",
"0000000101",
"0011100111",
"0010100000",
"0011101110",
"0000001010",
"1111001010",
"1000000000",
"1110000000",
"1001000011",
"1000011010",
"1001010011",
"1001010010",
"1001010011",
"0000000000",
"1111111111",
},
{
"0000110000",
"0011111100",
"0110110110",
"1101111011",
"0001111001",
"0010110100",
"0110110110",
"0000110000",
"0000110000",
"0000110000",
"0000110000",
"0000110000",
"0000110000",
"0000110000",
"0000110000",
"0000110000",
"0000110000",
"0000110000",
"0001111000",
"1111111111",
},
}
func lineToVP(line string) (bool, error) {
if strings.Contains(line, "1") {
return true, nil
}
return false, nil
}
func linesToBytes(lines []string, rows int, cols int, vp bool) ([]byte, error) {
// dmxfires wiring:
// master: 1- 5 -> col1, 6-10 -> col2, 11-15 -> col3
// slave: 17-21 -> col4, 22-26 -> col5, 27 -> victory poofer
// Lower values correspond to bottom solenoids.
// Panel layout: GH
// EF
// CD
// AB
// RS-485 output is:
// start byte
// 4 bytes per panel (in alphabetical order):
// solenoids 1- 8
// solenoids 9-16
// solenoids 17-25
// solenoids 26-32
// checksum (xor of bits)
// stop byte
// PPP escaping is performed (does not affect checksum)
grid := make([][]uint8, cols)
for c := 0; c < cols; c++ {
grid[c] = make([]uint8, rows)
}
for l, line := range lines {
fmt.Printf("%02d: ", l)
for c, col := range line {
if col == '0' {
grid[c][rows-l-1] = 0
fmt.Printf("-")
} else {
grid[c][rows-l-1] = 1
fmt.Printf("*")
}
}
fmt.Printf("\n")
}
if vp {
fmt.Printf("VICTORY POOF!\n")
}
const outlen = 256
outbits := make([]uint8, outlen)
for p := 0; p < 8; p++ {
start_col := (p % 2) * 5
start_row := (p / 2) * 5
byte := p * 32
//fmt.Printf("panel: %d, srow: %02d, scol: %02d, byte: %03d\n", p, start_row, start_col, byte)
for c := 0; c < 3; c++ {
for r := 0; r < 5; r++ {
outbits[byte] = grid[start_col+c][start_row+r]
byte++
}
}
byte++ // skip over output 16, which doesn't connect anywhere
for c := 3; c < 5; c++ {
for r := 0; r < 5; r++ {
outbits[byte] = grid[start_col+c][start_row+r]
byte++
}
}
if vp {
outbits[byte] = 1
}
}
/*
for t := 0; t < len(outbits); t++ {
fmt.Printf("%d", outbits[t])
if t%8 == 7 {
fmt.Printf("\n")
}
}
*/
outbytes := make([]byte, outlen/8)
var b uint
for b = 0; b < outlen; b++ {
bi := b % 8
by := b / 8
outbytes[by] |= outbits[b] << bi
}
return outbytes, nil
}