From 0f62b35cb1a1ff69e8c29285322a251010cf5ee1 Mon Sep 17 00:00:00 2001 From: xengineering Date: Thu, 30 May 2024 17:37:44 +0200 Subject: software: communication: Use Go channels Go channels help to decouple the communication layers defined by the OSI model [1]. The physical layer can be abstracted by an RX and TX byte channel and the data link layer by two frame channels. [1]: https://en.wikipedia.org/wiki/OSI_model --- software/communication/interface.go | 33 +++++---------------------------- 1 file changed, 5 insertions(+), 28 deletions(-) (limited to 'software/communication/interface.go') diff --git a/software/communication/interface.go b/software/communication/interface.go index 672617e..ebd8d6b 100644 --- a/software/communication/interface.go +++ b/software/communication/interface.go @@ -2,28 +2,17 @@ package communication import ( "fmt" - - "go.bug.st/serial" ) type SerialInterface struct { - port serial.Port + phy physical } func NewSerialInterface() (SerialInterface, error) { iface := SerialInterface{} + var err error - devices, err := getStSerials() - if err != nil { - return iface, err - } - - if len(devices) != 1 { - return iface, fmt.Errorf("Require exactly one serial device from STMicroelectronics but %d attached", len(devices)) - } - device := devices[0] - - iface.port, err = openSerial(device) + iface.phy, err = newPhysical() if err != nil { return iface, err } @@ -31,21 +20,9 @@ func NewSerialInterface() (SerialInterface, error) { return iface, nil } -func (i *SerialInterface) Cat() error { +func (i *SerialInterface) Cat() { for { - data, err := i.Read() - if err != nil { - return err - } + data := <-i.phy.rx fmt.Printf("%s", string(data)) } } - -func (i *SerialInterface) Read() ([]byte, error) { - buff := make([]byte, 100) - n, err := i.port.Read(buff) - if err != nil { - return []byte{}, err - } - return buff[:n], nil -} -- cgit v1.2.3-70-g09d2