/
tosocket.go
71 lines (60 loc) · 1.56 KB
/
tosocket.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
//target:tosocket
package main
import (
"net"
"flag"
"os"
"github.com/jbrukh/goneuro"
)
var tcpPort *string = flag.String("port", "9999", "port for the socket")
var serialPort *string = flag.String("serial", "/dev/tty.MindBand", "serial port for the device")
func main() {
flag.Parse()
// start a socket listener
addr := "localhost:" + *tcpPort
for {
listener, err := net.Listen("tcp", addr)
if err != nil {
println("couldn't listen: ", err)
os.Exit(1)
}
// wait for an incoming connection
println("listening on address: ", addr)
conn, err := listener.Accept()
if err != nil {
println("couldn't establish TCP connection: ", err)
os.Exit(1)
}
// write the raw signal bytes to the socket channel
println("getting connection to device...")
// this channel better be asynchronous, or else
// the loop below is going to stop reading it,
// the processing will block, and you won't
// be able to disconnect
toSocket := make(chan []byte, 1024)
handler := &goneuro.ThinkGearListener{
RawSignal: func(a, b byte) {
//fmt.Println("raw:", int16(a)<<8|int16(b))
toSocket <- []byte{a, b}
},
}
disconnect, err := goneuro.Connect(*serialPort, handler)
if err != nil {
println("couldn't connect: ", *serialPort)
continue
}
for {
val := <-toSocket
_, err := conn.Write(val)
// TODO: check bytes written
// when tcp connection closes,
// close the socket and start again
if err != nil {
println("couldn't write to socket anymore...")
break
}
}
disconnect <- true
print("cycling... ")
}
}