@@ -21,7 +21,6 @@ import (
21
21
"io"
22
22
"strconv"
23
23
"time"
24
- "unicode/utf8"
25
24
26
25
log "github.com/sirupsen/logrus"
27
26
serial "go.bug.st/serial"
@@ -56,10 +55,7 @@ type serport struct {
56
55
// channel containing raw base64 encoded binary data (outbound messages)
57
56
sendRaw chan string
58
57
59
- // Do we have an extra channel/thread to watch our buffer?
60
- BufferType string
61
- //bufferwatcher *BufferflowDummypause
62
- bufferwatcher Bufferflow
58
+ bufferFlow * BufferflowTimed
63
59
}
64
60
65
61
// SpPortMessage is the serial port message
@@ -74,10 +70,9 @@ type SpPortMessageRaw struct {
74
70
D []byte // the data, i.e. G0 X0 Y0
75
71
}
76
72
77
- func (p * serport ) reader (buftype string ) {
73
+ func (p * serport ) reader () {
78
74
79
75
timeCheckOpen := time .Now ()
80
- var bufferedCh bytes.Buffer
81
76
82
77
serialBuffer := make ([]byte , 1024 )
83
78
for {
@@ -95,39 +90,8 @@ func (p *serport) reader(buftype string) {
95
90
// read can return legitimate bytes as well as an error
96
91
// so process the n bytes red, if n > 0
97
92
if n > 0 && err == nil {
98
-
99
93
log .Print ("Read " + strconv .Itoa (n ) + " bytes ch: " + string (bufferPart [:n ]))
100
-
101
- data := ""
102
- switch buftype {
103
- case "timedraw" , "timed" :
104
- data = string (bufferPart [:n ])
105
- // give the data to our bufferflow so it can do it's work
106
- // to read/translate the data to see if it wants to block
107
- // writes to the serialport. each bufferflow type will decide
108
- // this on its own based on its logic
109
- p .bufferwatcher .OnIncomingData (data )
110
- case "default" : // the bufferbuftype is actually called default 🤷♂️
111
- // save the left out bytes for the next iteration due to UTF-8 encoding
112
- bufferPart = append (bufferedCh .Bytes (), bufferPart [:n ]... )
113
- n += len (bufferedCh .Bytes ())
114
- bufferedCh .Reset ()
115
- for i , w := 0 , 0 ; i < n ; i += w {
116
- runeValue , width := utf8 .DecodeRune (bufferPart [i :n ]) // try to decode the first i bytes in the buffer (UTF8 runes do not have a fixed length)
117
- if runeValue == utf8 .RuneError {
118
- bufferedCh .Write (bufferPart [i :n ])
119
- break
120
- }
121
- if i == n {
122
- bufferedCh .Reset ()
123
- }
124
- data += string (runeValue )
125
- w = width
126
- }
127
- p .bufferwatcher .OnIncomingData (data )
128
- default :
129
- log .Panicf ("unknown buffer type %s" , buftype )
130
- }
94
+ p .bufferFlow .OnIncomingData (string (bufferPart [:n ]))
131
95
}
132
96
133
97
// double check that we got characters in the buffer
@@ -272,7 +236,7 @@ func (p *serport) writerRaw() {
272
236
h .broadcastSys <- []byte (msgstr )
273
237
}
274
238
275
- func spHandlerOpen (portname string , baud int , buftype string ) {
239
+ func spHandlerOpen (portname string , baud int ) {
276
240
277
241
log .Print ("Inside spHandler" )
278
242
@@ -311,23 +275,12 @@ func spHandlerOpen(portname string, baud int, buftype string) {
311
275
portConf : conf ,
312
276
portIo : sp ,
313
277
portName : portname ,
314
- BufferType : buftype }
315
-
316
- var bw Bufferflow
317
-
318
- switch buftype {
319
- case "timed" :
320
- bw = NewBufferflowTimed (portname , h .broadcastSys )
321
- case "timedraw" :
322
- bw = NewBufferflowTimedRaw (portname , h .broadcastSys )
323
- case "default" :
324
- bw = NewBufferflowDefault (portname , h .broadcastSys )
325
- default :
326
- log .Panicf ("unknown buffer type: %s" , buftype )
327
278
}
328
279
280
+ bw := NewBufferFlowTimed (portname , h .broadcastSys )
329
281
bw .Init ()
330
- p .bufferwatcher = bw
282
+
283
+ p .bufferFlow = bw
331
284
332
285
sh .Register (p )
333
286
defer sh .Unregister (p )
@@ -342,14 +295,14 @@ func spHandlerOpen(portname string, baud int, buftype string) {
342
295
// this is thread to send to serial port but with base64 decoding
343
296
go p .writerRaw ()
344
297
345
- p .reader (buftype )
298
+ p .reader ()
346
299
347
300
serialPorts .List ()
348
301
}
349
302
350
303
func (p * serport ) Close () {
351
304
p .isClosing = true
352
- p .bufferwatcher .Close ()
305
+ p .bufferFlow .Close ()
353
306
p .portIo .Close ()
354
307
serialPorts .MarkPortAsClosed (p .portName )
355
308
serialPorts .List ()
0 commit comments