Skip to content

Commit 7c55c5e

Browse files
committed
Merge pull request arduino#30 from facchinm/timedraw
Add bufferflow timedraw
2 parents f5f5180 + 6c1c6fa commit 7c55c5e

File tree

6 files changed

+195
-10
lines changed

6 files changed

+195
-10
lines changed

bufferflow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
//"time"
66
)
77

8-
var availableBufferAlgorithms = []string{"default", "timed"}
8+
var availableBufferAlgorithms = []string{"default", "timed", "timedraw"}
99

1010
type BufferMsg struct {
1111
Cmd string

bufferflow_timedraw.go

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

hub.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ func checkCmd(m []byte) {
171171
// kill the running process (assumes singleton for now)
172172
go spHandlerProgramKill()
173173

174+
} else if strings.HasPrefix(sl, "sendjsonraw") {
175+
// will catch sendjsonraw
176+
go spWriteJsonRaw(s)
177+
174178
} else if strings.HasPrefix(sl, "sendjson") {
175179
// will catch sendjson
176180
go spWriteJson(s)

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ const homeTemplateHtml = `<!DOCTYPE html>
335335
return false;
336336
}
337337
socket.emit("command", msg.val());
338-
if (msg.val().indexOf("log off") != -1) {only_log = true;}
339-
if (msg.val().indexOf("log on") != -1) {only_log = false;}
338+
if (msg.val().indexOf("log off") != -1) {only_log = true}
339+
if (msg.val().indexOf("log on") != -1) {only_log = false}
340340
msg.val("");
341341
return false
342342
});

serial.go

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,24 @@ type writeRequestJson struct {
2626
Data []writeRequestJsonData
2727
}
2828

29+
type writeRequestJsonRaw struct {
30+
p *serport
31+
P string
32+
Data []writeRequestJsonDataRaw
33+
}
34+
2935
type writeRequestJsonData struct {
3036
D string
3137
Id string
3238
Buf string
3339
}
3440

41+
type writeRequestJsonDataRaw struct {
42+
D []byte
43+
Id string
44+
Buf string
45+
}
46+
3547
type qReportJson struct {
3648
Cmd string
3749
QCnt int
@@ -73,7 +85,8 @@ type serialhub struct {
7385
unregister chan *serport
7486

7587
// regexp for json trimming
76-
reJsonTrim *regexp.Regexp
88+
reJsonTrim *regexp.Regexp
89+
reJsonRawTrim *regexp.Regexp
7790
}
7891

7992
type SpPortList struct {
@@ -103,12 +116,13 @@ var NetworkPorts SpPortList
103116

104117
var sh = serialhub{
105118
//write: make(chan *serport, chan []byte),
106-
write: make(chan writeRequest),
107-
writeJson: make(chan writeRequestJson),
108-
register: make(chan *serport),
109-
unregister: make(chan *serport),
110-
ports: make(map[*serport]bool),
111-
reJsonTrim: regexp.MustCompile("sendjson"),
119+
write: make(chan writeRequest),
120+
writeJson: make(chan writeRequestJson),
121+
register: make(chan *serport),
122+
unregister: make(chan *serport),
123+
ports: make(map[*serport]bool),
124+
reJsonTrim: regexp.MustCompile("sendjson"),
125+
reJsonRawTrim: regexp.MustCompile("sendjsonraw"),
112126
}
113127

114128
func (sh *serialhub) run() {
@@ -615,6 +629,64 @@ func spWriteJson(arg string) {
615629
sh.writeJson <- m
616630
}
617631

632+
func spWriteJsonRaw(arg string) {
633+
634+
log.Printf("spWriteJson. arg:%v\n", arg)
635+
636+
// remove sendjson string
637+
arg = sh.reJsonRawTrim.ReplaceAllString(arg, "")
638+
//log.Printf("string we're going to parse:%v\n", arg)
639+
640+
// this is a structured command now for sending in serial commands multiple at a time
641+
// with an ID so we can send back the ID when the command is done
642+
var m writeRequestJsonRaw
643+
/*
644+
m.P = "COM22"
645+
var data writeRequestJsonData
646+
data.Id = "234"
647+
str := "yeah yeah"
648+
data.D = str //[]byte(str) //[]byte(string("blah blah"))
649+
m.Data = append(m.Data, data)
650+
//m.Data = append(m.Data, data)
651+
bm, err2 := json.Marshal(m)
652+
if err2 == nil {
653+
log.Printf("Test json serialize:%v\n", string(bm))
654+
}
655+
*/
656+
657+
err := json.Unmarshal([]byte(arg), &m)
658+
659+
if err != nil {
660+
log.Printf("Problem decoding json. giving up. json:%v, err:%v\n", arg, err)
661+
spErr(fmt.Sprintf("Problem decoding json. giving up. json:%v, err:%v", arg, err))
662+
return
663+
}
664+
665+
// see if we have this port open
666+
portname := m.P
667+
myport, isFound := findPortByName(portname)
668+
669+
if !isFound {
670+
// we couldn't find the port, so send err
671+
spErr("We could not find the serial port " + portname + " that you were trying to write to.")
672+
return
673+
}
674+
675+
// we found our port
676+
m.p = myport
677+
678+
var mr writeRequestJson
679+
680+
mr.p = m.p
681+
mr.P = m.P
682+
var data writeRequestJsonData
683+
data.D = string(m.Data[0].D)
684+
mr.Data = append(mr.Data, data)
685+
686+
// send it to the writeJson channel
687+
sh.writeJson <- mr
688+
}
689+
618690
func spWrite(arg string) {
619691
// we will get a string of comXX asdf asdf asdf
620692
log.Println("Inside spWrite arg: " + arg)

serialport.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ type SpPortMessage struct {
8888
D string // the data, i.e. G0 X0 Y0
8989
}
9090

91+
type SpPortMessageRaw struct {
92+
// P string // the port, i.e. com22
93+
D []byte // the data, i.e. G0 X0 Y0
94+
}
95+
9196
func (p *serport) reader() {
9297

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

345350
if buftype == "timed" {
346351
bw = &BufferflowTimed{Name: "timed", Port: portname, Output: h.broadcastSys, Input: make(chan string)}
352+
} else if buftype == "timedraw" {
353+
bw = &BufferflowTimedRaw{Name: "timedraw", Port: portname, Output: h.broadcastSys, Input: make(chan string)}
347354
} else {
348355
bw = &BufferflowDefault{Port: portname}
349356
}

0 commit comments

Comments
 (0)