Skip to content

Commit 1130ea8

Browse files
committed
Add bufferflow timedraw
encodes data in base64 so it needs to be decoded on the other side
1 parent 3037740 commit 1130ea8

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

bufferflow.go

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

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

1010
type BufferMsg struct {
1111
Cmd string

bufferflow_timedraw.go

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

serialport.go

+7
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ type SpPortMessage struct {
8888
D string // the data, i.e. G0 X0 Y0
8989
}
9090

91+
type SpPortMessageRaw struct {
92+
// P string // the port, i.e. com22
93+
D []byte // the data, i.e. G0 X0 Y0
94+
}
95+
9196
func (p *serport) reader() {
9297

9398
//var buf bytes.Buffer
@@ -344,6 +349,8 @@ func spHandlerOpen(portname string, baud int, buftype string, isSecondary bool)
344349

345350
if buftype == "timed" {
346351
bw = &BufferflowTimed{Name: "timed", Port: portname, Output: h.broadcastSys, Input: make(chan string)}
352+
} else if buftype == "timedraw" {
353+
bw = &BufferflowTimedRaw{Name: "timedraw", Port: portname, Output: h.broadcastSys, Input: make(chan string)}
347354
} else {
348355
bw = &BufferflowDefault{Port: portname}
349356
}

0 commit comments

Comments
 (0)