Rename msgpackpp->msgpack, merge device+protocol->wire, absorb picoserial into client
This commit is contained in:
@@ -7,19 +7,22 @@ import (
|
||||
|
||||
"github.com/theater/picomap/lib/halfsiphash"
|
||||
"github.com/theater/picomap/lib/msgpack"
|
||||
"github.com/theater/picomap/lib/transport"
|
||||
"io"
|
||||
)
|
||||
|
||||
var HashKey = [8]byte{}
|
||||
|
||||
type Client struct {
|
||||
transport transport.Transport
|
||||
timeout time.Duration
|
||||
nextID atomic.Uint32
|
||||
type transport interface {
|
||||
Send(data []byte) error
|
||||
SetReadTimeout(timeout time.Duration)
|
||||
Reader() io.Reader
|
||||
Close() error
|
||||
}
|
||||
|
||||
func New(t transport.Transport, timeout time.Duration) *Client {
|
||||
return &Client{transport: t, timeout: timeout}
|
||||
type Client struct {
|
||||
transport transport
|
||||
timeout time.Duration
|
||||
nextID atomic.Uint32
|
||||
}
|
||||
|
||||
func (c *Client) Close() error {
|
||||
|
||||
53
lib/client/serial.go
Normal file
53
lib/client/serial.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"go.bug.st/serial"
|
||||
"go.bug.st/serial/enumerator"
|
||||
)
|
||||
|
||||
func ListSerial() ([]string, error) {
|
||||
ports, err := enumerator.GetDetailedPortsList()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("enumerating ports: %w", err)
|
||||
}
|
||||
var result []string
|
||||
for _, p := range ports {
|
||||
if p.IsUSB {
|
||||
result = append(result, p.Name)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type serialTransport struct {
|
||||
port serial.Port
|
||||
}
|
||||
|
||||
func NewSerial(portName string, timeout time.Duration) (*Client, error) {
|
||||
port, err := serial.Open(portName, &serial.Mode{BaudRate: 115200})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("opening %s: %w", portName, err)
|
||||
}
|
||||
return &Client{transport: &serialTransport{port: port}, timeout: timeout}, nil
|
||||
}
|
||||
|
||||
func (t *serialTransport) Send(data []byte) error {
|
||||
_, err := t.port.Write(data)
|
||||
return err
|
||||
}
|
||||
|
||||
func (t *serialTransport) SetReadTimeout(timeout time.Duration) {
|
||||
t.port.SetReadTimeout(timeout)
|
||||
}
|
||||
|
||||
func (t *serialTransport) Reader() io.Reader {
|
||||
return t.port
|
||||
}
|
||||
|
||||
func (t *serialTransport) Close() error {
|
||||
return t.port.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user