diff options
Diffstat (limited to 'software/communication/physical.go')
-rw-r--r-- | software/communication/physical.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/software/communication/physical.go b/software/communication/physical.go index 853e4c4..0ba3690 100644 --- a/software/communication/physical.go +++ b/software/communication/physical.go @@ -1,6 +1,9 @@ package communication import ( + "fmt" + "log" + "go.bug.st/serial" "go.bug.st/serial/enumerator" ) @@ -9,6 +12,35 @@ const ( ST_VID = `0483` ) +type physical struct { + port serial.Port + rx chan byte +} + +func newPhysical() (physical, error) { + p := physical{} + + devices, err := getStSerials() + if err != nil { + return p, err + } + + if len(devices) != 1 { + return p, fmt.Errorf("Require exactly one serial device from STMicroelectronics but %d attached", len(devices)) + } + device := devices[0] + + p.port, err = openSerial(device) + if err != nil { + return p, err + } + + p.rx = make(chan byte) + go p.Receive() + + return p, nil +} + func getStSerials() ([]string, error) { retval := make([]string, 0) @@ -39,3 +71,16 @@ func openSerial(device string) (serial.Port, error) { } return port, nil } + +func (p *physical) Receive() { + for { + buff := make([]byte, 100) + n, err := p.port.Read(buff) + if err != nil { + log.Fatal(err) + } + for i := 0; i < n; i++ { + p.rx <- buff[i] + } + } +} |