Skip to content

Commit

Permalink
Handle changes in serial package
Browse files Browse the repository at this point in the history
  • Loading branch information
ecc1 committed Mar 4, 2018
1 parent f16de6f commit 2f694b8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
10 changes: 0 additions & 10 deletions cgm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ Based on the Python version at github.com/bewest/decoding-dexcom
*/
package dexcom

import (
"log"

"github.com/ecc1/usbserial"
)

// Connection is the interface satisfied by a CGM connection.
type Connection interface {
Send([]byte) error
Expand All @@ -32,10 +26,6 @@ func Open() *CGM {
if err == nil {
return &CGM{Connection: conn}
}
_, notFound := err.(usbserial.DeviceNotFoundError)
if !notFound {
log.Print(err)
}
conn, err = OpenBLE()
return &CGM{Connection: conn, err: err}
}
Expand Down
26 changes: 17 additions & 9 deletions usb.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dexcom

import (
"github.com/ecc1/usbserial"
"log"

"github.com/ecc1/serial"
)

const (
Expand All @@ -10,27 +12,33 @@ const (
dexcomProduct = 0x0047
)

type usbConn struct {
*usbserial.Port
}
type usbConn serial.Port

// OpenUSB opens the USB serial device for a Dexcom G4 receiver.
func OpenUSB() (Connection, error) {
port, err := usbserial.Open(dexcomVendor, dexcomProduct)
return &usbConn{port}, err
device, err := serial.FindUSB(dexcomVendor, dexcomProduct)
if err != nil {
_, notFound := err.(serial.DeviceNotFoundError)
if !notFound {
log.Print(err)
}
return nil, err
}
port, err := serial.Open(device, 115200)
return (*usbConn)(port), err
}

// Send writes data over the USB connection.
func (conn *usbConn) Send(data []byte) error {
return conn.Write(data)
return (*serial.Port)(conn).Write(data)
}

// Receive reads data from the USB connection.
func (conn *usbConn) Receive(data []byte) error {
return conn.Read(data)
return (*serial.Port)(conn).Read(data)
}

// Close closes the USB connection.
func (conn *usbConn) Close() {
_ = conn.Port.Close()
_ = (*serial.Port)(conn).Close()
}

0 comments on commit 2f694b8

Please sign in to comment.