Skip to content

Commit

Permalink
refactor: remove unused Bufferflow implementations and simplify buffe…
Browse files Browse the repository at this point in the history
…r handling
  • Loading branch information
dido18 committed Jan 10, 2025
1 parent 5e631b9 commit d915288
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 253 deletions.
23 changes: 0 additions & 23 deletions bufferflow.go

This file was deleted.

72 changes: 0 additions & 72 deletions bufferflow_default.go

This file was deleted.

4 changes: 2 additions & 2 deletions bufferflow_timed.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type BufferflowTimed struct {
bufferedOutput string
}

// NewBufferflowTimed will create a new timed bufferflow
func NewBufferflowTimed(port string, output chan<- []byte) *BufferflowTimed {
// NewBufferFlowTimed will create a new timed bufferflow
func NewBufferFlowTimed(port string, output chan<- []byte) *BufferflowTimed {
return &BufferflowTimed{
port: port,
output: output,
Expand Down
89 changes: 0 additions & 89 deletions bufferflow_timedraw.go

This file was deleted.

1 change: 1 addition & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func wsHandler() *WsServer {
c := &connection{send: make(chan []byte, 256*10), ws: so}
h.register <- c
so.On("command", func(message string) {
fmt.Println("command:", message)
h.broadcast <- []byte(message)
})

Expand Down
12 changes: 2 additions & 10 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,8 @@ func checkCmd(m []byte) {
go spErr("Problem converting baud rate " + args[2])
return
}
// pass in buffer type now as string. if user does not
// ask for a buffer type pass in empty string
bufferAlgorithm := "default" // use the default buffer if none is specified
if len(args) > 3 {
// cool. we got a buffer type request
buftype := strings.Replace(args[3], "\n", "", -1)
bufferAlgorithm = buftype
}
fmt.Println("bufferAlgorithm: ", bufferAlgorithm)
go spHandlerOpen(args[1], baud, bufferAlgorithm)

go spHandlerOpen(args[1], baud)

} else if strings.HasPrefix(sl, "close") {

Expand Down
2 changes: 1 addition & 1 deletion serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var sh = serialhub{
func (sh *serialhub) Register(port *serport) {
sh.mu.Lock()
//log.Print("Registering a port: ", p.portConf.Name)
h.broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + ",\"BufferType\":\"" + port.BufferType + "\"}")
h.broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + "\"}")
sh.ports[port] = true
sh.mu.Unlock()
}
Expand Down
65 changes: 9 additions & 56 deletions serialport.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"io"
"strconv"
"time"
"unicode/utf8"

log "github.com/sirupsen/logrus"
serial "go.bug.st/serial"
Expand Down Expand Up @@ -56,10 +55,7 @@ type serport struct {
// channel containing raw base64 encoded binary data (outbound messages)
sendRaw chan string

// Do we have an extra channel/thread to watch our buffer?
BufferType string
//bufferwatcher *BufferflowDummypause
bufferwatcher Bufferflow
bufferFlow *BufferflowTimed
}

// SpPortMessage is the serial port message
Expand All @@ -74,10 +70,9 @@ type SpPortMessageRaw struct {
D []byte // the data, i.e. G0 X0 Y0
}

func (p *serport) reader(buftype string) {
func (p *serport) reader() {

timeCheckOpen := time.Now()
var bufferedCh bytes.Buffer

serialBuffer := make([]byte, 1024)
for {
Expand All @@ -95,39 +90,8 @@ func (p *serport) reader(buftype string) {
// read can return legitimate bytes as well as an error
// so process the n bytes red, if n > 0
if n > 0 && err == nil {

log.Print("Read " + strconv.Itoa(n) + " bytes ch: " + string(bufferPart[:n]))

data := ""
switch buftype {
case "timedraw", "timed":
data = string(bufferPart[:n])
// give the data to our bufferflow so it can do it's work
// to read/translate the data to see if it wants to block
// writes to the serialport. each bufferflow type will decide
// this on its own based on its logic
p.bufferwatcher.OnIncomingData(data)
case "default": // the bufferbuftype is actually called default 🤷‍♂️
// save the left out bytes for the next iteration due to UTF-8 encoding
bufferPart = append(bufferedCh.Bytes(), bufferPart[:n]...)
n += len(bufferedCh.Bytes())
bufferedCh.Reset()
for i, w := 0, 0; i < n; i += w {
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)
if runeValue == utf8.RuneError {
bufferedCh.Write(bufferPart[i:n])
break
}
if i == n {
bufferedCh.Reset()
}
data += string(runeValue)
w = width
}
p.bufferwatcher.OnIncomingData(data)
default:
log.Panicf("unknown buffer type %s", buftype)
}
p.bufferFlow.OnIncomingData(string(bufferPart[:n]))
}

// double check that we got characters in the buffer
Expand Down Expand Up @@ -272,7 +236,7 @@ func (p *serport) writerRaw() {
h.broadcastSys <- []byte(msgstr)
}

func spHandlerOpen(portname string, baud int, buftype string) {
func spHandlerOpen(portname string, baud int) {

log.Print("Inside spHandler")

Expand Down Expand Up @@ -311,23 +275,12 @@ func spHandlerOpen(portname string, baud int, buftype string) {
portConf: conf,
portIo: sp,
portName: portname,
BufferType: buftype}

var bw Bufferflow

switch buftype {
case "timed":
bw = NewBufferflowTimed(portname, h.broadcastSys)
case "timedraw":
bw = NewBufferflowTimedRaw(portname, h.broadcastSys)
case "default":
bw = NewBufferflowDefault(portname, h.broadcastSys)
default:
log.Panicf("unknown buffer type: %s", buftype)
}

bw := NewBufferFlowTimed(portname, h.broadcastSys)
bw.Init()
p.bufferwatcher = bw

p.bufferFlow = bw

sh.Register(p)
defer sh.Unregister(p)
Expand All @@ -342,14 +295,14 @@ func spHandlerOpen(portname string, baud int, buftype string) {
// this is thread to send to serial port but with base64 decoding
go p.writerRaw()

p.reader(buftype)
p.reader()

serialPorts.List()
}

func (p *serport) Close() {
p.isClosing = true
p.bufferwatcher.Close()
p.bufferFlow.Close()
p.portIo.Close()
serialPorts.MarkPortAsClosed(p.portName)
serialPorts.List()
Expand Down

0 comments on commit d915288

Please sign in to comment.