Skip to content
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

feat: remove bufferAlgorithm param from the open command #1002

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
aff40ab
WIP: print to understand
dido18 Jan 10, 2025
bae632a
refactor: remove unused Bufferflow implementations and simplify buffe…
dido18 Jan 10, 2025
818f79f
refactor: simplify serial test functions by removing unused parameters
dido18 Jan 10, 2025
baf2ff4
fix(ci): update package installation command to use correct version o…
dido18 Jan 10, 2025
e63527a
fix(ci): update libwebkit2gtk version in dependency installation
dido18 Jan 10, 2025
98b6376
fix: remove unused buffer slice and simplify data reading in serial port
dido18 Jan 10, 2025
0ce39a5
small changes
dido18 Jan 10, 2025
ec5049d
fix: remove debug print statement from wsHandler function
dido18 Jan 13, 2025
2fe3c28
fix: add warning log for unexpected arguments in open command
dido18 Jan 13, 2025
1849a1c
Update hub.go
dido18 Jan 13, 2025
294de02
fix: update log message for buffer flow initialization
dido18 Jan 14, 2025
99a31e6
fix: update log message for clarity in buffer flow initialization
dido18 Jan 14, 2025
2308b5f
fix: ignore all tags in check-certificates workflow on push
dido18 Jan 14, 2025
8688ec6
fix: update macOS-13 version in publish-go-tester-task workflow
dido18 Jan 14, 2025
894c139
fix(release): add Go installation step in release workflow
dido18 Jan 14, 2025
3a34186
fix: correct JSON formatting in broadcast message for port registration
dido18 Jan 16, 2025
62e324b
Merge branch 'main' into remove-open-not-used-buffer-algorithm
dido18 Jan 17, 2025
c042f10
fix: update application identity in release workflow to use Massimo B…
dido18 Jan 20, 2025
43bd6ea
resolve conflict
dido18 Jan 21, 2025
8f5fa3b
revert application_identity
dido18 Jan 21, 2025
6684e7c
ci: remove tags-ignore from check-certificates workflow
dido18 Jan 21, 2025
fb39e25
Merge branch 'main' into remove-open-not-used-buffer-algorithm
dido18 Jan 21, 2025
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
5 changes: 5 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ jobs:
-k "${{ env.KEYCHAIN_PASSWORD }}" \
"${{ env.KEYCHAIN }}"

- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}

- name: Install gon for code signing
uses: actions/checkout@v4
with:
Expand Down
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.

6 changes: 3 additions & 3 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 All @@ -48,7 +48,7 @@ func NewBufferflowTimed(port string, output chan<- []byte) *BufferflowTimed {

// Init will initialize the bufferflow
func (b *BufferflowTimed) Init() {
log.Println("Initting timed buffer flow (output once every 16ms)")
log.Println("Start consuming from serial port (output once every 16ms)")
go b.consumeInput()
}

Expand Down
89 changes: 0 additions & 89 deletions bufferflow_timedraw.go

This file was deleted.

14 changes: 6 additions & 8 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var h = hub{
const commands = `{
"Commands": [
"list",
"open <portName> <baud> [bufferAlgorithm: ({default}, timed, timedraw)]",
"open <portName> <baud>",
"(send, sendnobuf, sendraw) <portName> <cmd>",
"close <portName>",
"restart",
Expand Down Expand Up @@ -146,15 +146,13 @@ 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

// Ignore extra "buffer type" param for backward compatibility
if len(args) > 3 {
dido18 marked this conversation as resolved.
Show resolved Hide resolved
// cool. we got a buffer type request
buftype := strings.Replace(args[3], "\n", "", -1)
bufferAlgorithm = buftype
log.Warn(fmt.Sprintf("Unexpected arguments for the open command. Ignored arguments: '%s'.", args[3:]))
}
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
68 changes: 9 additions & 59 deletions serialport.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"strconv"
"sync/atomic"
"time"
"unicode/utf8"

log "github.com/sirupsen/logrus"
serial "go.bug.st/serial"
Expand Down Expand Up @@ -57,10 +56,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 @@ -75,15 +71,13 @@ 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 {
n, err := p.portIo.Read(serialBuffer)
bufferPart := serialBuffer[:n]

//if we detect that port is closing, break out of this for{} loop.
if p.isClosing.Load() {
Expand All @@ -96,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)
}
log.Print("Read " + strconv.Itoa(n) + " bytes ch: " + string(serialBuffer[:n]))
p.bufferFlow.OnIncomingData(string(serialBuffer[:n]))
}

// double check that we got characters in the buffer
Expand Down Expand Up @@ -273,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 @@ -312,23 +275,10 @@ 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.Init()
p.bufferwatcher = bw
p.bufferFlow = NewBufferFlowTimed(portname, h.broadcastSys)
p.bufferFlow.Init()

sh.Register(p)
defer sh.Unregister(p)
Expand All @@ -343,15 +293,15 @@ 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.Store(true)

p.bufferwatcher.Close()
p.bufferFlow.Close()
p.portIo.Close()
serialPorts.MarkPortAsClosed(p.portName)
serialPorts.List()
Expand Down
Loading
Loading