Skip to content

Commit 19f9a6d

Browse files
committed
Use a bufferflow that sends collected messages every 16ms
1 parent 53f6f8b commit 19f9a6d

File tree

3 files changed

+106
-3
lines changed

3 files changed

+106
-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"}
8+
var availableBufferAlgorithms = []string{"default", "timed"}
99

1010
type BufferMsg struct {
1111
Cmd string
@@ -16,6 +16,7 @@ type BufferMsg struct {
1616
}
1717

1818
type Bufferflow interface {
19+
Init()
1920
BlockUntilReady(cmd string, id string) (bool, bool) // implement this method
2021
//JustQueue(cmd string, id string) bool // implement this method
2122
OnIncomingData(data string) // implement this method

bufferflow_timed.go

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

serialport.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,15 @@ func spHandlerOpen(portname string, baud int, buftype string, isSecondary bool)
340340
// we can go up to 256,000 lines of gcode in the buffer
341341
p := &serport{sendBuffered: make(chan Cmd, 256000), sendNoBuf: make(chan Cmd), portConf: conf, portIo: sp, BufferType: buftype, IsPrimary: isPrimary, IsSecondary: isSecondary}
342342

343-
bw := &BufferflowDefault{}
343+
var bw Bufferflow
344+
345+
if buftype == "timed" {
346+
bw = &BufferflowTimed{Name: "timed", Port: portname, Output: h.broadcastSys, Input: make(chan string)}
347+
} else {
348+
bw = &BufferflowDefault{Port: portname}
349+
}
350+
344351
bw.Init()
345-
bw.Port = portname
346352
p.bufferwatcher = bw
347353

348354
sh.register <- p

0 commit comments

Comments
 (0)