all: use interfaces for UART objects

This makes it possible to replace UART objects with dummy
implementations, for example. Or allows changing the `machine.UART` type
to `*machine.UART` without breaking compatibility.
This commit is contained in:
Ayke van Laethem
2021-03-06 00:27:42 +01:00
committed by Ron Evans
parent 1345bc2161
commit e77e9249cd
3 changed files with 17 additions and 6 deletions
+3 -3
View File
@@ -20,17 +20,17 @@ package espat // import "tinygo.org/x/drivers/espat"
import ( import (
"errors" "errors"
"machine"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/net" "tinygo.org/x/drivers/net"
) )
// Device wraps UART connection to the ESP8266/ESP32. // Device wraps UART connection to the ESP8266/ESP32.
type Device struct { type Device struct {
bus machine.UART bus drivers.UART
// command responses that come back from the ESP8266/ESP32 // command responses that come back from the ESP8266/ESP32
response []byte response []byte
@@ -43,7 +43,7 @@ type Device struct {
var ActiveDevice *Device var ActiveDevice *Device
// New returns a new espat driver. Pass in a fully configured UART bus. // New returns a new espat driver. Pass in a fully configured UART bus.
func New(b machine.UART) *Device { func New(b drivers.UART) *Device {
return &Device{bus: b, response: make([]byte, 512), socketdata: make([]byte, 0, 1024)} return &Device{bus: b, response: make([]byte, 512), socketdata: make([]byte, 0, 1024)}
} }
+2 -3
View File
@@ -4,7 +4,6 @@ package gps // import "tinygo.org/x/drivers/gps"
import ( import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"machine"
"strings" "strings"
"time" "time"
@@ -21,13 +20,13 @@ type Device struct {
buffer []byte buffer []byte
bufIdx int bufIdx int
sentence strings.Builder sentence strings.Builder
uart *machine.UART uart drivers.UART
bus drivers.I2C bus drivers.I2C
address uint16 address uint16
} }
// NewUART creates a new UART GPS connection. The UART must already be configured. // NewUART creates a new UART GPS connection. The UART must already be configured.
func NewUART(uart *machine.UART) Device { func NewUART(uart drivers.UART) Device {
return Device{ return Device{
uart: uart, uart: uart,
buffer: make([]byte, bufferSize), buffer: make([]byte, bufferSize),
+12
View File
@@ -0,0 +1,12 @@
package drivers
import "io"
// UART represents a UART connection. It is implemented by the machine.UART
// type.
type UART interface {
io.Reader
io.Writer
Buffered() int
}