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 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
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)
}
4 changes: 4 additions & 0 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ func checkCmd(m []byte) {
// kill the running process (assumes singleton for now)
go spHandlerProgramKill()

} else if strings.HasPrefix(sl, "sendjsonraw") {
// will catch sendjsonraw
go spWriteJsonRaw(s)

} else if strings.HasPrefix(sl, "sendjson") {
// will catch sendjson
go spWriteJson(s)
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ const homeTemplateHtml = `<!DOCTYPE html>
return false;
}
socket.emit("command", msg.val());
if (msg.val().indexOf("log off") != -1) {only_log = true;}
if (msg.val().indexOf("log on") != -1) {only_log = false;}
if (msg.val().indexOf("log off") != -1) {only_log = true}
if (msg.val().indexOf("log on") != -1) {only_log = false}
msg.val("");
return false
});
Expand Down
86 changes: 79 additions & 7 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,24 @@ type writeRequestJson struct {
Data []writeRequestJsonData
}

type writeRequestJsonRaw struct {
p *serport
P string
Data []writeRequestJsonDataRaw
}

type writeRequestJsonData struct {
D string
Id string
Buf string
}

type writeRequestJsonDataRaw struct {
D []byte
Id string
Buf string
}

type qReportJson struct {
Cmd string
QCnt int
Expand Down Expand Up @@ -73,7 +85,8 @@ type serialhub struct {
unregister chan *serport

// regexp for json trimming
reJsonTrim *regexp.Regexp
reJsonTrim *regexp.Regexp
reJsonRawTrim *regexp.Regexp
}

type SpPortList struct {
Expand Down Expand Up @@ -103,12 +116,13 @@ var NetworkPorts SpPortList

var sh = serialhub{
//write: make(chan *serport, chan []byte),
write: make(chan writeRequest),
writeJson: make(chan writeRequestJson),
register: make(chan *serport),
unregister: make(chan *serport),
ports: make(map[*serport]bool),
reJsonTrim: regexp.MustCompile("sendjson"),
write: make(chan writeRequest),
writeJson: make(chan writeRequestJson),
register: make(chan *serport),
unregister: make(chan *serport),
ports: make(map[*serport]bool),
reJsonTrim: regexp.MustCompile("sendjson"),
reJsonRawTrim: regexp.MustCompile("sendjsonraw"),
}

func (sh *serialhub) run() {
Expand Down Expand Up @@ -615,6 +629,64 @@ func spWriteJson(arg string) {
sh.writeJson <- m
}

func spWriteJsonRaw(arg string) {

log.Printf("spWriteJson. arg:%v\n", arg)

// remove sendjson string
arg = sh.reJsonRawTrim.ReplaceAllString(arg, "")
//log.Printf("string we're going to parse:%v\n", arg)

// this is a structured command now for sending in serial commands multiple at a time
// with an ID so we can send back the ID when the command is done
var m writeRequestJsonRaw
/*
m.P = "COM22"
var data writeRequestJsonData
data.Id = "234"
str := "yeah yeah"
data.D = str //[]byte(str) //[]byte(string("blah blah"))
m.Data = append(m.Data, data)
//m.Data = append(m.Data, data)
bm, err2 := json.Marshal(m)
if err2 == nil {
log.Printf("Test json serialize:%v\n", string(bm))
}
*/

err := json.Unmarshal([]byte(arg), &m)

if err != nil {
log.Printf("Problem decoding json. giving up. json:%v, err:%v\n", arg, err)
spErr(fmt.Sprintf("Problem decoding json. giving up. json:%v, err:%v", arg, err))
return
}

// see if we have this port open
portname := m.P
myport, isFound := findPortByName(portname)

if !isFound {
// we couldn't find the port, so send err
spErr("We could not find the serial port " + portname + " that you were trying to write to.")
return
}

// we found our port
m.p = myport

var mr writeRequestJson

mr.p = m.p
mr.P = m.P
var data writeRequestJsonData
data.D = string(m.Data[0].D)
mr.Data = append(mr.Data, data)

// send it to the writeJson channel
sh.writeJson <- mr
}

func spWrite(arg string) {
// we will get a string of comXX asdf asdf asdf
log.Println("Inside spWrite arg: " + arg)
Expand Down
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