Skip to content

Commit e80400b

Browse files
committed
binary support (WIP)
1 parent 6a50f87 commit e80400b

6 files changed

+136
-3
lines changed

bufferflow.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
//"time"
66
)
77

8-
var availableBufferAlgorithms = []string{"default", "timed", "timedraw"}
8+
var availableBufferAlgorithms = []string{"default", "timed", "timedraw", "timedbinary"}
99

1010
type BufferMsg struct {
1111
Cmd string
@@ -19,6 +19,7 @@ type Bufferflow interface {
1919
Init()
2020
BlockUntilReady(cmd string, id string) (bool, bool) // implement this method
2121
//JustQueue(cmd string, id string) bool // implement this method
22+
OnIncomingDataBinary(data []byte)
2223
OnIncomingData(data string) // implement this method
2324
ClearOutSemaphore() // implement this method
2425
BreakApartCommands(cmd string) []string // implement this method

bufferflow_default.go

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ func (b *BufferflowDefault) BlockUntilReady(cmd string, id string) (bool, bool)
2020
return true, false
2121
}
2222

23+
// not implemented, we are gonna use OnIncomingData
24+
func (b *BufferflowDefault) OnIncomingDataBinary(data []byte) {
25+
}
26+
2327
func (b *BufferflowDefault) OnIncomingData(data string) {
2428
//log.Printf("OnIncomingData() start. data:%v\n", data)
2529
}

bufferflow_timed.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type BufferflowTimed struct {
1818

1919
var (
2020
bufferedOutput string
21-
sPort string
21+
sPort string
2222
)
2323

2424
func (b *BufferflowTimed) Init() {
@@ -61,6 +61,10 @@ func (b *BufferflowTimed) BlockUntilReady(cmd string, id string) (bool, bool) {
6161
return true, false
6262
}
6363

64+
// not implemented, we are gonna use OnIncomingData
65+
func (b *BufferflowTimed) OnIncomingDataBinary(data []byte) {
66+
}
67+
6468
func (b *BufferflowTimed) OnIncomingData(data string) {
6569
b.Input <- data
6670
}

bufferflow_timedbinary.go

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"time"
6+
7+
log "github.com/sirupsen/logrus"
8+
)
9+
10+
type BufferflowTimedBinary struct {
11+
Name string
12+
Port string
13+
Output chan []byte
14+
Input chan []byte
15+
done chan bool
16+
ticker *time.Ticker
17+
}
18+
19+
var (
20+
bufferedOutputBinary []byte
21+
sPortBinary string
22+
)
23+
24+
func (b *BufferflowTimedBinary) Init() {
25+
log.Println("Initting timed buffer binary flow (output once every 16ms)")
26+
bufferedOutputBinary = nil
27+
sPortBinary = ""
28+
29+
go func() {
30+
b.ticker = time.NewTicker(16 * time.Millisecond)
31+
b.done = make(chan bool)
32+
Loop:
33+
for {
34+
select {
35+
case data := <-b.Input:
36+
bufferedOutputBinary = append(bufferedOutputBinary, []byte(data)...)
37+
sPortBinary = b.Port
38+
case <-b.ticker.C:
39+
if bufferedOutputBinary != nil {
40+
m := SpPortMessageRaw{sPortBinary, bufferedOutputBinary}
41+
buf, _ := json.Marshal(m)
42+
b.Output <- []byte(buf)
43+
bufferedOutputBinary = nil
44+
sPortBinary = ""
45+
}
46+
case <-b.done:
47+
break Loop
48+
}
49+
}
50+
51+
close(b.Input)
52+
}()
53+
}
54+
55+
func (b *BufferflowTimedBinary) BlockUntilReady(cmd string, id string) (bool, bool) {
56+
//log.Printf("BlockUntilReady() start\n")
57+
return true, false
58+
}
59+
60+
func (b *BufferflowTimedBinary) OnIncomingDataBinary(data []byte) {
61+
b.Input <- data
62+
}
63+
64+
// not implemented, we are gonna use OnIncomingDataBinary
65+
func (b *BufferflowTimedBinary) OnIncomingData(data string) {
66+
}
67+
68+
// Clean out b.sem so it can truly block
69+
func (b *BufferflowTimedBinary) ClearOutSemaphore() {
70+
}
71+
72+
func (b *BufferflowTimedBinary) BreakApartCommands(cmd string) []string {
73+
return []string{cmd}
74+
}
75+
76+
func (b *BufferflowTimedBinary) Pause() {
77+
return
78+
}
79+
80+
func (b *BufferflowTimedBinary) Unpause() {
81+
return
82+
}
83+
84+
func (b *BufferflowTimedBinary) SeeIfSpecificCommandsShouldSkipBuffer(cmd string) bool {
85+
return false
86+
}
87+
88+
func (b *BufferflowTimedBinary) SeeIfSpecificCommandsShouldPauseBuffer(cmd string) bool {
89+
return false
90+
}
91+
92+
func (b *BufferflowTimedBinary) SeeIfSpecificCommandsShouldUnpauseBuffer(cmd string) bool {
93+
return false
94+
}
95+
96+
func (b *BufferflowTimedBinary) SeeIfSpecificCommandsShouldWipeBuffer(cmd string) bool {
97+
return false
98+
}
99+
100+
func (b *BufferflowTimedBinary) SeeIfSpecificCommandsReturnNoResponse(cmd string) bool {
101+
return false
102+
}
103+
104+
func (b *BufferflowTimedBinary) ReleaseLock() {
105+
}
106+
107+
func (b *BufferflowTimedBinary) IsBufferGloballySendingBackIncomingData() bool {
108+
return true
109+
}
110+
111+
func (b *BufferflowTimedBinary) Close() {
112+
b.ticker.Stop()
113+
close(b.Input)
114+
}

bufferflow_timedraw.go

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ func (b *BufferflowTimedRaw) BlockUntilReady(cmd string, id string) (bool, bool)
5959
return true, false
6060
}
6161

62+
// not implemented, we are gonna use OnIncomingData
63+
func (b *BufferflowTimedRaw) OnIncomingDataBinary(data []byte) {
64+
}
65+
6266
func (b *BufferflowTimedRaw) OnIncomingData(data string) {
6367
b.Input <- data
6468
}

serialport.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@ func (p *serport) reader(buftype string) {
148148
// writes to the serialport. each bufferflow type will decide
149149
// this on its own based on its logic, i.e. tinyg vs grbl vs others
150150
//p.b.bufferwatcher..OnIncomingData(data)
151-
p.bufferwatcher.OnIncomingData(data)
151+
if buftype == "timedbinary" {
152+
p.bufferwatcher.OnIncomingDataBinary(ch[:n])
153+
} else {
154+
p.bufferwatcher.OnIncomingData(data)
155+
}
152156

153157
// see if the OnIncomingData handled the broadcast back
154158
// to the user. this option was added in case the OnIncomingData wanted
@@ -330,6 +334,8 @@ func spHandlerOpen(portname string, baud int, buftype string) {
330334
bw = &BufferflowTimed{Name: "timed", Port: portname, Output: h.broadcastSys, Input: make(chan string)}
331335
} else if buftype == "timedraw" {
332336
bw = &BufferflowTimedRaw{Name: "timedraw", Port: portname, Output: h.broadcastSys, Input: make(chan string)}
337+
} else if buftype == "timedbinary" {
338+
bw = &BufferflowTimedBinary{Name: "timedbinary", Port: portname, Output: h.broadcastSys, Input: make(chan []byte)}
333339
} else {
334340
bw = &BufferflowDefault{Port: portname}
335341
}

0 commit comments

Comments
 (0)