Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit eb2b3f3

Browse files
committedJul 21, 2021
use switch case
1 parent e80400b commit eb2b3f3

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed
 

Diff for: ‎hub.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func checkCmd(m []byte) {
127127
}
128128
// pass in buffer type now as string. if user does not
129129
// ask for a buffer type pass in empty string
130-
bufferAlgorithm := ""
130+
bufferAlgorithm := "default" // use the default buffer if none is specified
131131
if len(args) > 3 {
132132
// cool. we got a buffer type request
133133
buftype := strings.Replace(args[3], "\n", "", -1)

Diff for: ‎serialport.go

+23-22
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (p *serport) reader(buftype string) {
103103

104104
n, err := p.portIo.Read(ch)
105105

106-
//if we detect that port is closing, break out o this for{} loop.
106+
//if we detect that port is closing, break out of this for{} loop.
107107
if p.isClosing {
108108
strmsg := "Shutting down reader on " + p.portConf.Name
109109
log.Println(strmsg)
@@ -118,16 +118,20 @@ func (p *serport) reader(buftype string) {
118118
}
119119

120120
// read can return legitimate bytes as well as an error
121-
// so process the bytes if n > 0
121+
// so process the n bytes if n > 0
122122
if n > 0 {
123123
log.Print("Read " + strconv.Itoa(n) + " bytes ch: " + string(ch))
124124

125125
data := ""
126-
if buftype == "timedraw" {
126+
switch buftype {
127+
case "timedraw", "timed": // data is handled differently inside BufferFlowTimed (bufferedOutput is string) and BufferFlowTimedRaw (bufferedOutputRaw is []byte)
127128
data = string(ch[:n])
128-
} else {
129+
p.bufferwatcher.OnIncomingData(data)
130+
case "timedbinary":
131+
p.bufferwatcher.OnIncomingDataBinary(ch[:n])
132+
case "default":
129133
for i, w := 0, 0; i < n; i += w {
130-
runeValue, width := utf8.DecodeRune(ch[i:n])
134+
runeValue, width := utf8.DecodeRune(ch[i:n]) // try to decode the first i bytes in the buffer (UTF8 runes do not have a fixed lenght)
131135
if runeValue == utf8.RuneError {
132136
buffered_ch.Write(append(ch[i:n]))
133137
break
@@ -138,20 +142,14 @@ func (p *serport) reader(buftype string) {
138142
data += string(runeValue)
139143
w = width
140144
}
141-
}
142-
143-
//log.Print("The data i will convert to json is:")
144-
//log.Print(data)
145-
146-
// give the data to our bufferflow so it can do it's work
147-
// to read/translate the data to see if it wants to block
148-
// writes to the serialport. each bufferflow type will decide
149-
// this on its own based on its logic, i.e. tinyg vs grbl vs others
150-
//p.b.bufferwatcher..OnIncomingData(data)
151-
if buftype == "timedbinary" {
152-
p.bufferwatcher.OnIncomingDataBinary(ch[:n])
153-
} else {
145+
// give the data to our bufferflow so it can do it's work
146+
// to read/translate the data to see if it wants to block
147+
// writes to the serialport. each bufferflow type will decide
148+
// this on its own based on its logic, i.e. tinyg vs grbl vs others
149+
//p.b.bufferwatcher..OnIncomingData(data)
154150
p.bufferwatcher.OnIncomingData(data)
151+
default:
152+
log.Panicf("unknown buffer type %s", buftype)
155153
}
156154

157155
// see if the OnIncomingData handled the broadcast back
@@ -330,14 +328,17 @@ func spHandlerOpen(portname string, baud int, buftype string) {
330328

331329
var bw Bufferflow
332330

333-
if buftype == "timed" {
331+
switch buftype {
332+
case "timed":
334333
bw = &BufferflowTimed{Name: "timed", Port: portname, Output: h.broadcastSys, Input: make(chan string)}
335-
} else if buftype == "timedraw" {
334+
case "timedraw":
336335
bw = &BufferflowTimedRaw{Name: "timedraw", Port: portname, Output: h.broadcastSys, Input: make(chan string)}
337-
} else if buftype == "timedbinary" {
336+
case "timedbinary":
338337
bw = &BufferflowTimedBinary{Name: "timedbinary", Port: portname, Output: h.broadcastSys, Input: make(chan []byte)}
339-
} else {
338+
case "default":
340339
bw = &BufferflowDefault{Port: portname}
340+
default:
341+
log.Panicf("unknown buffer type: %s", buftype)
341342
}
342343

343344
bw.Init()

0 commit comments

Comments
 (0)
Please sign in to comment.