Skip to content

Commit a2635c4

Browse files
authored
transports: use typed UART device instead of string port in MCUTransport (#8)
1 parent 1fff721 commit a2635c4

2 files changed

Lines changed: 15 additions & 16 deletions

File tree

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,18 @@ func main() {
598598
println("Starting servo example...")
599599
ctx := context.Background()
600600

601+
// Create a new servo transport
602+
transport, err := transports.OpenSerial(transports.SerialConfig{
603+
Device: machine.UART0,
604+
BaudRate: 1000000,
605+
})
606+
if err != nil {
607+
failure("Failed to open serial transport:" + err.Error())
608+
}
601609
// Create a new servo bus
602610
bus, err := feetech.NewBus(feetech.BusConfig{
603-
Port: "0",
604-
BaudRate: 1000000,
605-
Protocol: feetech.ProtocolSTS,
611+
Transport: transport,
612+
Protocol: feetech.ProtocolSTS,
606613
})
607614
if err != nil {
608615
failure("Failed to create bus:" + err.Error())

transports/serial_mcu.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package transports
44

55
import (
66
"errors"
7-
"fmt"
87
"machine"
98
"time"
109
)
@@ -14,7 +13,8 @@ type MCUTransport struct {
1413
}
1514

1615
type SerialConfig struct {
17-
Port string
16+
Device *machine.UART
17+
Port string // Ignored on MCU, but required for interface compatibility
1818
BaudRate int
1919
Timeout time.Duration
2020
}
@@ -23,8 +23,8 @@ var currentTransport MCUTransport
2323

2424
// OpenSerial gets a UART port with the given configuration.
2525
func OpenSerial(cfg SerialConfig) (*MCUTransport, error) {
26-
if cfg.Port == "" {
27-
return nil, errors.New("serial port path is required")
26+
if cfg.Device == nil {
27+
return nil, errors.New("UART device is required")
2828
}
2929

3030
if cfg.BaudRate == 0 {
@@ -35,15 +35,7 @@ func OpenSerial(cfg SerialConfig) (*MCUTransport, error) {
3535
cfg.Timeout = time.Second
3636
}
3737

38-
switch cfg.Port {
39-
case "0":
40-
currentTransport = MCUTransport{machine.UART0}
41-
case "1":
42-
currentTransport = MCUTransport{machine.UART1}
43-
default:
44-
return nil, fmt.Errorf("unknown UART %s", cfg.Port)
45-
}
46-
38+
currentTransport = MCUTransport{cfg.Device}
4739
currentTransport.SetBaudRate(uint32(cfg.BaudRate))
4840

4941
return &currentTransport, nil

0 commit comments

Comments
 (0)