Skip to content

Add bufferflow timedraw #30

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
Mar 3, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add bufferflow timedraw
encodes data in base64 so it needs to be decoded on the other side
  • Loading branch information
facchinm committed Feb 3, 2016
commit 1130ea86cd477959efa06606125671347b944328
2 changes: 1 addition & 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", "timed"}
var availableBufferAlgorithms = []string{"default", "timed", "timedraw"}

type BufferMsg struct {
Cmd string
Expand Down
102 changes: 102 additions & 0 deletions bufferflow_timedraw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package main

import (
"encoding/json"
"time"

log "github.com/Sirupsen/logrus"
)

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

var (
bufferedOutputRaw []byte
)

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

go func() {
for data := range b.Input {
bufferedOutputRaw = append(bufferedOutputRaw, []byte(data)...)
}
}()

go func() {
b.ticker = time.NewTicker(16 * time.Millisecond)
for _ = range b.ticker.C {
if len(bufferedOutputRaw) != 0 {
m := SpPortMessageRaw{bufferedOutputRaw}
buf, _ := json.Marshal(m)
// data is now encoded in base64 format
// need a decoder on the other side
b.Output <- []byte(buf)
bufferedOutputRaw = nil
}
}
}()

}

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

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

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

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

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

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

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

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

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

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

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

func (b *BufferflowTimedRaw) ReleaseLock() {
}

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

func (b *BufferflowTimedRaw) Close() {
b.ticker.Stop()
close(b.Input)
}
7 changes: 7 additions & 0 deletions serialport.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ type SpPortMessage struct {
D string // the data, i.e. G0 X0 Y0
}

type SpPortMessageRaw struct {
// P string // the port, i.e. com22
D []byte // the data, i.e. G0 X0 Y0
}

func (p *serport) reader() {

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

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