-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
687 lines (558 loc) · 15.2 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
package main
import (
"bufio"
"encoding/hex"
"errors"
"fmt"
"github.com/boxofrox/cctv-ptz/config"
"github.com/docopt/docopt-go"
"github.com/mikepb/go-serial"
"github.com/simulatedsimian/joystick"
"io"
"math"
"os"
"strconv"
"strings"
"syscall"
"time"
)
var (
VERSION string
BUILD_DATE string
)
// pelco d byte names
const (
SYNC = 0
ADDR = 1
COMMAND_1 = 2
COMMAND_2 = 3
DATA_1 = 4
DATA_2 = 5
CHECKSUM = 6
)
type PelcoDMessage [7]byte
type DelayedMessage struct {
Message PelcoDMessage
Delay time.Duration
}
const (
AxisMax = 32767
MaxSpeed = 0x3f
)
type Axis struct {
Index int32
Min int32 // used for normalizing input -1.0 to 1.0
Max int32
Deadzone int32
Inverted bool // flips normalized input
}
var xbox = struct {
LeftAxisX Axis
LeftAxisY Axis
RightAxisX Axis
RightAxisY Axis
LeftTrigger Axis
RightTrigger Axis
DPadX Axis
DPadY Axis
LeftBumper uint32
RightBumper uint32
A uint32
B uint32
X uint32
Y uint32
Start uint32
Back uint32
XBox uint32
}{
Axis{0, -AxisMax, AxisMax, 8192, false}, // left analog stick
Axis{1, -AxisMax, AxisMax, 8192, true},
Axis{3, -AxisMax, AxisMax, 8192, false}, // right analog stick
Axis{4, -AxisMax, AxisMax, 8192, true},
Axis{2, -AxisMax, AxisMax, 1000, false}, // triggers
Axis{5, -AxisMax, AxisMax, 1000, false},
Axis{6, -AxisMax, AxisMax, 1000, false}, // directional pad
Axis{7, -AxisMax, AxisMax, 1000, false},
1 << 4, // bumpers
1 << 5,
1 << 0, // A
1 << 1, // B
1 << 2, // X
1 << 3, // Y
1 << 7, // start
1 << 6, // back
1 << 8, // xbox button
}
// map xbox controller to pan-tilt-zoom controls and misc app controls
var ptz = struct {
// pan tilt zoom
PanX Axis
PanY Axis
ZoomIn uint32
ZoomOut uint32
OpenIris uint32
CloseIris uint32
OpenMenu uint32
// misc
IncPelcoAddr uint32
DecPelcoAddr uint32
ResetTimer uint32
MarkLeft Axis
MarkRight Axis
}{
xbox.LeftAxisX, // pan x
xbox.RightAxisY, // pan y
xbox.LeftBumper, // zoom in
xbox.RightBumper, // zoom out
xbox.A, // open iris (enter)
xbox.B, // close iris
xbox.Start, // open menu
xbox.Y, // increment pelco address
xbox.X, // decrement pelco address
xbox.Back, // reset timer
xbox.LeftTrigger, // mark
xbox.RightTrigger, // mark
}
func main() {
var (
err error
arguments map[string]interface{}
)
usage := `CCTV Pan-Tilt-Zoom via Xbox Controller
Usage:
cctv-ptz [-v] [-a ADDRESS] [-s FILE] [-j JOYSTICK] [-r FILE] [-b BAUD] [-m MAXSPEED]
cctv-ptz playback [-a ADDRESS] [-s FILE] [-b BAUD] [-v]
cctv-ptz -h
cctv-ptz -V
Options:
-a, --address ADDRESS - Pelco-D address 0-256. (default = 0)
-b, --baud BAUD - set baud rate of serial port. (default = 9600)
-j, --joystick JOYSTICK - use joystick NUM (e.g. /dev/input/jsNUM). (default = 0)
-m, --maxspeed MAXSPEED - set max speed setting 0-100. (default = 100)
-s, --serial FILE - assign serial port for rs485 output. (default = /dev/sttyUSB0)
-r, --record FILE - record rs485 commands to file. (default = /dev/null)
-v, --verbose - prints Pelco-D commands to stdout.
-h, --help - print this help message.
-V, --version - print version info.
`
arguments, err = docopt.Parse(usage, nil, true, version(), false)
// fail if arguments failed to parse
if err != nil {
panic(err)
}
conf := config.Load(arguments)
if arguments["playback"].(bool) {
playback(conf)
} else {
interactive(conf)
}
}
func createSerialOptions(conf config.Config) serial.Options {
return serial.Options{
Mode: serial.MODE_WRITE,
BitRate: conf.BaudRate,
DataBits: 8,
StopBits: 1,
Parity: serial.PARITY_NONE,
FlowControl: serial.FLOWCONTROL_NONE,
}
}
func decodeMessage(text string) (PelcoDMessage, error) {
var (
bytes []byte
err error
)
message := PelcoDMessage{}
if bytes, err = hex.DecodeString(text); err != nil {
return message, err
}
copy(message[:], bytes)
return message, nil
}
func interactive(conf config.Config) {
var (
record *os.File
tty *serial.Port
jsObserver <-chan joystick.State
err error
resetTimer = true
serialEnabled = ("/dev/null" != conf.SerialPort)
hasSerialAccess bool
)
stdinObserver := listenFile(os.Stdin)
js, err := joystick.Open(conf.JoystickNumber)
if err != nil {
fmt.Fprintf(os.Stderr, "cctv-ptz: error opening joystick %d. %s\n", conf.JoystickNumber, err)
jsObserver = listenNothing()
} else {
defer js.Close()
fmt.Fprintf(os.Stderr, "Joystick port opened. /dev/input/js%d\n", conf.JoystickNumber)
fmt.Fprintf(os.Stderr, " Joystick Name: %s\n", js.Name())
fmt.Fprintf(os.Stderr, " Axis Count: %d\n", js.AxisCount())
fmt.Fprintf(os.Stderr, " Button Count: %d\n", js.ButtonCount())
jsTicker := time.NewTicker(100 * time.Millisecond)
jsObserver = listenJoystick(js, jsTicker)
}
hasSerialAccess, err = serialPortAvailable(conf.SerialPort)
if err != nil {
fmt.Fprintf(os.Stderr, "cctv-ptz: cannot open serial port (%s). %s\n", conf.SerialPort, err)
}
if serialEnabled && hasSerialAccess {
ttyOptions := createSerialOptions(conf)
tty, err = ttyOptions.Open(conf.SerialPort)
if err != nil {
fmt.Fprintf(os.Stderr, "cctz-ptz: unable to open tty: %s\n", conf.SerialPort)
os.Exit(1)
}
defer tty.Close()
printSerialPortInfo(conf, tty)
} else {
fmt.Fprintf(os.Stderr, "cctv-ptz: serial port disabled\n")
}
if "-" == conf.RecordFile {
record = os.Stdout
} else {
if record, err = os.Create(conf.RecordFile); err != nil {
panic(err)
}
}
defer record.Close()
// limit rate at which Pelco address may change via joystick
allowAddressChange := make(chan struct{}, 1)
allowAddressChange <- struct{}{} // prime channel to allow first address change
startTime := time.Now()
lastMessage := PelcoDMessage{}
for {
select {
case <-stdinObserver:
return
case state := <-jsObserver:
// adjust Pelco address
if isPressed(state, ptz.DecPelcoAddr) {
limitChange(allowAddressChange, func() { conf.Address -= 1 })
} else if isPressed(state, ptz.IncPelcoAddr) {
limitChange(allowAddressChange, func() { conf.Address += 1 })
}
// reset the clock if user presses Back
if isPressed(state, ptz.ResetTimer) {
resetTimer = true
}
if isMarkTriggered(state, ptz.MarkLeft) {
fmt.Fprintf(record, "# Mark Left\n")
}
if isMarkTriggered(state, ptz.MarkRight) {
fmt.Fprintf(record, "# Mark Right\n")
}
message := pelcoCreate()
message = pelcoTo(message, conf.Address)
message = joystickToPelco(message, state, conf.MaxSpeed)
message = pelcoChecksum(message)
if lastMessage != message {
var millis int64
if resetTimer {
millis = 0
resetTimer = false
startTime = time.Now()
} else {
endTime := time.Now()
millis = (endTime.Sub(startTime)).Nanoseconds() / 1E6
startTime = endTime
}
if conf.Verbose {
fmt.Printf("pelco-d %x %d\n", message, millis)
} else {
fmt.Fprintf(os.Stderr, "\033[Kpelco-d %x %d\r", message, millis)
}
fmt.Fprintf(record, "pelco-d %x %d\n", message, millis)
if serialEnabled {
tty.Write(message[:])
}
lastMessage = message
}
}
}
}
func isPressed(state joystick.State, mask uint32) bool {
return 0 != state.Buttons&mask
}
func joystickToPelco(buffer PelcoDMessage, state joystick.State, maxSpeed int32) PelcoDMessage {
var zoom float32
panX := normalizeAxis(state, ptz.PanX)
panY := normalizeAxis(state, ptz.PanY)
openIris := isPressed(state, ptz.OpenIris)
closeIris := isPressed(state, ptz.CloseIris)
openMenu := isPressed(state, ptz.OpenMenu)
if isPressed(state, ptz.ZoomOut) {
zoom = -1.0
} else if isPressed(state, ptz.ZoomIn) {
zoom = 1.0
}
buffer = pelcoApplyJoystick(buffer, panX, panY, zoom, openIris, closeIris, openMenu, maxSpeed)
return buffer
}
func limitChange(allowAddressChange chan struct{}, proc func()) {
select {
case <-allowAddressChange:
proc()
// delay next signal to allow change
go func() {
<-time.After(125 * time.Millisecond)
allowAddressChange <- struct{}{}
}()
default:
// do nothing
}
}
func listenFile(f io.Reader) <-chan []byte {
io := make(chan []byte)
scanner := bufio.NewScanner(f)
go func() {
defer close(io)
for scanner.Scan() {
bytes := scanner.Bytes()
if len(bytes) == 0 {
break
}
io <- bytes
}
if err := scanner.Err(); err != nil {
panic(err)
}
}()
return io
}
func listenJoystick(js joystick.Joystick, ticker *time.Ticker) <-chan joystick.State {
io := make(chan joystick.State, 20)
go func() {
for range ticker.C {
if state, err := js.Read(); err != nil {
panic(err)
} else {
io <- state
}
time.Sleep(25 * time.Millisecond)
}
}()
return io
}
func listenNothing() <-chan joystick.State {
return make(chan joystick.State)
}
func isMarkTriggered(state joystick.State, axis Axis) bool {
triggerValue := normalizeAxis(state, axis)
return 0.5 < triggerValue
}
func normalizeAxis(state joystick.State, axis Axis) float32 {
var (
value = float32(state.AxisData[axis.Index])
deadzone = float32(axis.Deadzone)
max = float32(axis.Max)
)
if axis.Inverted {
value = -value
}
if value > 0 && value < deadzone {
value = 0
} else if value > deadzone {
value = (value - deadzone) / (max - deadzone)
} else if value < 0 && value > -deadzone {
value = 0
} else if value < -deadzone {
value = (value + deadzone) / (max - deadzone)
}
return value
}
func pelcoCreate() PelcoDMessage {
buffer := PelcoDMessage{}
buffer[SYNC] = 0xff
return buffer
}
// should be last call before sending a pelco message
func pelcoChecksum(buffer PelcoDMessage) PelcoDMessage {
buffer[CHECKSUM] = uint8(buffer[ADDR] + buffer[COMMAND_1] + buffer[COMMAND_2] + buffer[DATA_1] + buffer[DATA_2])
return buffer
}
func pelcoTo(buffer PelcoDMessage, addr int) PelcoDMessage {
buffer[ADDR] = uint8(addr)
return buffer
}
func pelcoApplyJoystick(buffer PelcoDMessage, panX, panY, zoom float32, openIris, closeIris, openMenu bool, maxSpeed int32) PelcoDMessage {
if openMenu {
buffer[COMMAND_1] = 0x00
buffer[COMMAND_2] = 0x03
buffer[DATA_1] = 0x00
buffer[DATA_2] = 0x5F
return buffer
}
if panX > 0 {
buffer[COMMAND_2] |= 1 << 1
} else if panX < 0 {
buffer[COMMAND_2] |= 1 << 2
}
// pan speed
buffer[DATA_1] = uint8(float64(maxSpeed) * math.Abs(float64(panX)))
if panY > 0 {
buffer[COMMAND_2] |= 1 << 3
} else if panY < 0 {
buffer[COMMAND_2] |= 1 << 4
}
// tilt speed
buffer[DATA_2] = uint8(float64(maxSpeed) * math.Abs(float64(panY)))
if zoom > 0 {
buffer[COMMAND_2] |= 1 << 5
} else if zoom < 0 {
buffer[COMMAND_2] |= 1 << 6
}
if openIris {
buffer[COMMAND_1] |= 1 << 1
} else if closeIris {
buffer[COMMAND_1] |= 1 << 2
}
return buffer
}
func playback(conf config.Config) {
var (
message PelcoDMessage
tty *serial.Port
millis uint64
err error
serialEnabled = ("/dev/null" != conf.SerialPort)
hasSerialAccess bool
)
hasSerialAccess, err = serialPortAvailable(conf.SerialPort)
if err != nil {
fmt.Fprintf(os.Stderr, "cctv-ptz: cannot open serial port (%s). %s\n", conf.SerialPort, err)
}
if serialEnabled && hasSerialAccess {
ttyOptions := createSerialOptions(conf)
tty, err = ttyOptions.Open(conf.SerialPort)
if err != nil {
panic(err)
}
defer tty.Close()
printSerialPortInfo(conf, tty)
} else {
fmt.Fprintf(os.Stderr, "Serial port disabled\n")
}
messageChannel := make(chan DelayedMessage)
defer close(messageChannel)
go sendDelayedMessages(messageChannel, tty, conf.Verbose)
lineCount := 0
lineScanner := bufio.NewScanner(os.Stdin)
for lineScanner.Scan() {
text := strings.TrimSpace(lineScanner.Text())
if strings.HasPrefix(text, "#") {
continue
}
words := strings.Fields(text)
lineCount += 1
if 3 > len(words) {
fmt.Fprintf(os.Stderr, "cctv-ptz: error parsing playback. Too few fields. Line %d: %s\n", lineCount, text)
continue
}
if "pelco-d" != words[0] {
fmt.Fprintf(os.Stderr, "cctv-ptz: error parsing playback. Invalid protocol %s. Line %d: %s\n", words[0], lineCount, text)
continue
}
if message, err = decodeMessage(words[1]); err != nil {
fmt.Fprintf(os.Stderr, "cctv-ptz: error parsing playback. Invalid packet %s. Line %d: %s\n", err.Error(), lineCount, text)
continue
}
if millis, err = strconv.ParseUint(words[2], 10, 64); err != nil {
fmt.Fprintf(os.Stderr, "cctv-ptz: error parsing playback. Invalid duration %s. Line %d: %s\n", err.Error(), lineCount, text)
continue
}
messageChannel <- DelayedMessage{message, time.Duration(millis) * time.Millisecond}
if conf.Verbose {
fmt.Fprintf(os.Stderr, "%s\n", text)
}
}
}
func printSerialPortInfo(conf config.Config, tty *serial.Port) {
baud, err := tty.BitRate()
if err != nil {
panic(err)
}
data, err := tty.DataBits()
if err != nil {
panic(err)
}
stop, err := tty.StopBits()
if err != nil {
panic(err)
}
parity, err := tty.Parity()
if err != nil {
panic(err)
}
fmt.Fprintf(os.Stderr, "Serial port opened. %s\n", conf.SerialPort)
fmt.Fprintf(os.Stderr, " Name: %s\n", tty.Name())
fmt.Fprintf(os.Stderr, " Baud rate: %d\n", baud)
fmt.Fprintf(os.Stderr, " Data bits: %d\n", data)
fmt.Fprintf(os.Stderr, " Stop bits: %d\n", stop)
fmt.Fprintf(os.Stderr, " Parity: %d\n", parity)
}
func sendMessage(tty *serial.Port, message PelcoDMessage) {
if nil != tty {
tty.Write(message[:])
}
}
func sendDelayedMessages(c <-chan DelayedMessage, tty *serial.Port, verbose bool) {
var (
pkg DelayedMessage
lastTime time.Time
)
// send first message without delay
pkg = <-c
sendMessage(tty, pkg.Message)
lastTime = time.Now()
// all other messages are delayed wrt preceeding messages
for pkg = range c {
time.Sleep(pkg.Delay)
sendMessage(tty, pkg.Message)
if verbose {
duration := time.Now().Sub(lastTime) / 1E6
delay := pkg.Delay / 1E6
fmt.Fprintf(os.Stderr, "Sent %x after %d millis. target %d millis. offset %d millis\n",
pkg.Message, duration, delay, duration-delay)
}
lastTime = time.Now()
}
}
func serialPortAvailable(serialPort string) (bool, error) {
var err error
goStat, err := os.Stat(serialPort)
if os.IsNotExist(err) || os.IsPermission(err) {
return false, err
}
euid := uint32(os.Geteuid())
unixStat, ok := goStat.Sys().(*syscall.Stat_t)
if !ok {
return false, errors.New("cannot determine file ownership or permissions")
}
if euid == unixStat.Uid && 0 != (0x600&unixStat.Mode) {
// we should have owner access!
return true, nil
}
if 0 != (0x006 & unixStat.Mode) {
// we should have other access!
return true, nil
}
if 0 != (0x060 & unixStat.Mode) {
groups, err := os.Getgroups()
if err != nil {
return false, err
}
// does any group for user match file's group?
for _, gid := range groups {
if uint32(gid) == unixStat.Gid {
// we should have group access!
return true, nil
}
}
}
return false, errors.New(fmt.Sprintf("access denied. uid (%d) gid (%d) mode (%o)", unixStat.Uid, unixStat.Gid, 0xfff & unixStat.Mode))
}
func version() string {
return fmt.Sprintf("%s: version %s, build %s\n\n", os.Args[0], VERSION, BUILD_DATE)
}