Skip to content

Bufferflowtimed #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bufferflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
//"time"
)

var availableBufferAlgorithms = []string{"default"}
var availableBufferAlgorithms = []string{"default", "timed"}

type BufferMsg struct {
Cmd string
Expand All @@ -16,6 +16,7 @@ type BufferMsg struct {
}

type Bufferflow interface {
Init()
BlockUntilReady(cmd string, id string) (bool, bool) // implement this method
//JustQueue(cmd string, id string) bool // implement this method
OnIncomingData(data string) // implement this method
Expand Down
98 changes: 98 additions & 0 deletions bufferflow_timed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"encoding/json"
log "github.com/Sirupsen/logrus"
"time"
)

type BufferflowTimed struct {
Name string
Port string
Output chan []byte
Input chan string
ticker *time.Ticker
}

var (
bufferedOutput string
)

func (b *BufferflowTimed) Init() {
log.Println("Initting timed buffer flow (output once every 16ms)")
bufferedOutput = ""

go func() {
for data := range b.Input {
bufferedOutput = bufferedOutput + data
}
}()

go func() {
b.ticker = time.NewTicker(16 * time.Millisecond)
for _ = range b.ticker.C {
m := SpPortMessage{bufferedOutput}
buf, _ := json.Marshal(m)
b.Output <- []byte(buf)
bufferedOutput = ""
}
}()

}

func (b *BufferflowTimed) BlockUntilReady(cmd string, id string) (bool, bool) {
//log.Printf("BlockUntilReady() start\n")
return true, false
}

func (b *BufferflowTimed) OnIncomingData(data string) {
b.Input <- data
}

// Clean out b.sem so it can truly block
func (b *BufferflowTimed) ClearOutSemaphore() {
}

func (b *BufferflowTimed) BreakApartCommands(cmd string) []string {
return []string{cmd}
}

func (b *BufferflowTimed) Pause() {
return
}

func (b *BufferflowTimed) Unpause() {
return
}

func (b *BufferflowTimed) SeeIfSpecificCommandsShouldSkipBuffer(cmd string) bool {
return false
}

func (b *BufferflowTimed) SeeIfSpecificCommandsShouldPauseBuffer(cmd string) bool {
return false
}

func (b *BufferflowTimed) SeeIfSpecificCommandsShouldUnpauseBuffer(cmd string) bool {
return false
}

func (b *BufferflowTimed) SeeIfSpecificCommandsShouldWipeBuffer(cmd string) bool {
return false
}

func (b *BufferflowTimed) SeeIfSpecificCommandsReturnNoResponse(cmd string) bool {
return false
}

func (b *BufferflowTimed) ReleaseLock() {
}

func (b *BufferflowTimed) IsBufferGloballySendingBackIncomingData() bool {
return true
}

func (b *BufferflowTimed) Close() {
b.ticker.Stop()
close(b.Input)
}
10 changes: 8 additions & 2 deletions serialport.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,15 @@ func spHandlerOpen(portname string, baud int, buftype string, isSecondary bool)
// we can go up to 256,000 lines of gcode in the buffer
p := &serport{sendBuffered: make(chan Cmd, 256000), sendNoBuf: make(chan Cmd), portConf: conf, portIo: sp, BufferType: buftype, IsPrimary: isPrimary, IsSecondary: isSecondary}

bw := &BufferflowDefault{}
var bw Bufferflow

if buftype == "timed" {
bw = &BufferflowTimed{Name: "timed", Port: portname, Output: h.broadcastSys, Input: make(chan string)}
} else {
bw = &BufferflowDefault{Port: portname}
}

bw.Init()
bw.Port = portname
p.bufferwatcher = bw

sh.register <- p
Expand Down