diff --git a/bufferflow.go b/bufferflow.go index d5059989b..ff5bcf1b7 100644 --- a/bufferflow.go +++ b/bufferflow.go @@ -5,7 +5,7 @@ import ( //"time" ) -var availableBufferAlgorithms = []string{"default"} +var availableBufferAlgorithms = []string{"default", "timed"} type BufferMsg struct { Cmd string @@ -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 diff --git a/bufferflow_timed.go b/bufferflow_timed.go new file mode 100644 index 000000000..6dd58dfa4 --- /dev/null +++ b/bufferflow_timed.go @@ -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) +} diff --git a/serialport.go b/serialport.go index a776e5251..9aafcfbc9 100755 --- a/serialport.go +++ b/serialport.go @@ -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