Skip to content

Commit

Permalink
Fix warnings from gometalinter
Browse files Browse the repository at this point in the history
  • Loading branch information
ecc1 committed May 13, 2017
1 parent 732daa5 commit c20ad02
Show file tree
Hide file tree
Showing 19 changed files with 338 additions and 322 deletions.
51 changes: 51 additions & 0 deletions cgm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Package dexcom provides functions to access a Dexcom G4 Share
CGM receiver over a BLE or USB connection.
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
Receive([]byte) error
Close()
}

// CGM represents a CGM connection.
type CGM struct {
Connection
err error
}

// Open first attempts to open a USB connection;
// if that fails it tries a BLE connection.
func Open() *CGM {
conn, err := OpenUSB()
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}
}

// Error returns the error state of the CGM.
func (cgm *CGM) Error() error {
return cgm.err
}

// SetError sets the error state of the CGM.
func (cgm *CGM) SetError(err error) {
cgm.err = err
}
6 changes: 3 additions & 3 deletions cmd/demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ func main() {
log.Fatal(cgm.Error())
}
fmt.Println("display time:", cgm.ReadDisplayTime())
fmt.Println("transmitter ID:", string(cgm.Cmd(dexcom.READ_TRANSMITTER_ID)))
fmt.Println("transmitter ID:", string(cgm.Cmd(dexcom.ReadTransmitterID)))
printXMLInfo("firmware header", cgm.ReadFirmwareHeader())

hw := cgm.ReadXMLRecord(dexcom.MANUFACTURING_DATA)
hw := cgm.ReadXMLRecord(dexcom.ManufacturingData)
printXMLInfo("manufacturing data", hw.XML)
fmt.Printf(" %+v\n", hw.Timestamp)

sw := cgm.ReadXMLRecord(dexcom.PC_SOFTWARE_PARAMETER)
sw := cgm.ReadXMLRecord(dexcom.SoftwareData)
printXMLInfo("PC software parameter", sw.XML)
fmt.Printf(" %+v\n", sw.Timestamp)
}
2 changes: 1 addition & 1 deletion cmd/g4ping/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func main() {
cgm := dexcom.Open()
cgm.Cmd(dexcom.PING)
cgm.Cmd(dexcom.Ping)
if cgm.Error() != nil {
log.Fatal(cgm.Error())
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/glucose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ var (
flag *bool
page dexcom.PageType
}{
{egv, dexcom.EGV_DATA},
{sensor, dexcom.SENSOR_DATA},
{calibration, dexcom.CAL_SET},
{meter, dexcom.METER_DATA},
{egv, dexcom.EGVData},
{sensor, dexcom.SensorData},
{calibration, dexcom.CalibrationData},
{meter, dexcom.MeterData},
}
)

Expand Down Expand Up @@ -65,9 +65,9 @@ func main() {
}
var v []dexcom.Record
// Special case when both EGV and sensor records are requested.
if t.page == dexcom.EGV_DATA && *sensor {
if t.page == dexcom.EGVData && *sensor {
v = cgm.GlucoseReadings(cutoff)
} else if t.page == dexcom.SENSOR_DATA && *egv {
} else if t.page == dexcom.SensorData && *egv {
continue
} else {
v = cgm.ReadHistory(t.page, cutoff)
Expand Down
2 changes: 1 addition & 1 deletion cmd/show/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

var (
pageTypeFlag = flag.Int("p", int(dexcom.EGV_DATA), "`page type` to read")
pageTypeFlag = flag.Int("p", int(dexcom.EGVData), "`page type` to read")
numRecords = flag.Int("n", 10, "number of `records` to get")
all = flag.Bool("a", false, "get all records")
)
Expand Down
89 changes: 45 additions & 44 deletions commands.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
package dexcom

// A Command specifies an operation to be performed by the Dexcom CGM receiver.
// Command represents a Dexcom CGM receiver command.
type Command byte

//go:generate stringer -type Command

// Dexcom G4 receiver commands.
const (
NULL Command = 0
ACK Command = 1
NAK Command = 2
INVALID_COMMAND Command = 3
INVALID_PARAM Command = 4
INCOMPLETE_PACKET_RECEIVED Command = 5
RECEIVER_ERROR Command = 6
INVALID_MODE Command = 7
PING Command = 10
READ_FIRMWARE_HEADER Command = 11
READ_DATABASE_PARTITION_INFO Command = 15
READ_DATABASE_PAGE_RANGE Command = 16
READ_DATABASE_PAGES Command = 17
READ_DATABASE_PAGE_HEADER Command = 18
READ_TRANSMITTER_ID Command = 25
WRITE_TRANSMITTER_ID Command = 26
READ_LANGUAGE Command = 27
WRITE_LANGUAGE Command = 28
READ_DISPLAY_TIME_OFFSET Command = 29
WRITE_DISPLAY_TIME_OFFSET Command = 30
READ_RTC Command = 31
RESET_RECEIVER Command = 32
READ_BATTERY_LEVEL Command = 33
READ_SYSTEM_TIME Command = 34
READ_SYSTEM_TIME_OFFSET Command = 35
WRITE_SYSTEM_TIME Command = 36
READ_GLUCOSE_UNIT Command = 37
WRITE_GLUCOSE_UNIT Command = 38
READ_BLINDED_MODE Command = 39
WRITE_BLINDED_MODE Command = 40
READ_CLOCK_MODE Command = 41
WRITE_CLOCK_MODE Command = 42
READ_DEVICE_MODE Command = 43
ERASE_DATABASE Command = 45
SHUTDOWN_RECEIVER Command = 46
WRITE_PC_PARAMETERS Command = 47
READ_BATTERY_STATE Command = 48
READ_HARDWARE_BOARD_ID Command = 49
READ_FIRMWARE_SETTINGS Command = 54
READ_ENABLE_SETUP_WIZARD_FLAG Command = 55
READ_SETUP_WIZARD_STATE Command = 57
READ_CHARGER_CURRENT_SETTING Command = 59
WRITE_CHARGER_CURRENT_SETTING Command = 60
Null Command = 0
Ack Command = 1
Nak Command = 2
InvalidCommand Command = 3
InvalidParam Command = 4
IncompletePacketReceived Command = 5
ReceiverError Command = 6
InvalidMode Command = 7
Ping Command = 10
ReadFirmwareHeader Command = 11
ReadDatabasePartitionInfo Command = 15
ReadDatabasePageRange Command = 16
ReadDatabasePages Command = 17
ReadDatabasePageHeader Command = 18
ReadTransmitterID Command = 25
WriteTransmitterID Command = 26
ReadLanguage Command = 27
WriteLanguage Command = 28
ReadDisplayTimeOffset Command = 29
WriteDisplayTimeOffset Command = 30
ReadRTC Command = 31
ResetReceiver Command = 32
ReadBatteryLevel Command = 33
ReadSystemTime Command = 34
ReadSystemTimeOffset Command = 35
WriteSystemTime Command = 36
ReadGlucoseUnits Command = 37
WriteGlucoseUnits Command = 38
ReadBlindMode Command = 39
WriteBlindMode Command = 40
ReadClockMode Command = 41
WriteClockMode Command = 42
ReadDeviceMode Command = 43
EraseDatabase Command = 45
ShutdownReceiver Command = 46
WriteSoftwareParameters Command = 47
ReadBatteryState Command = 48
ReadHardwareID Command = 49
ReadFirmwareSettings Command = 54
ReadEnableSetupWizardFlag Command = 55
ReadSetupWizardState Command = 57
ReadChargerCurrentSetting Command = 59
WriteChargerCurrentSetting Command = 60
)
48 changes: 0 additions & 48 deletions connection.go

This file was deleted.

13 changes: 8 additions & 5 deletions history.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const (
userTimeLayout = "2006-01-02 15:04:05"
)

// ReadHistory returns records since the specified time.
func (cgm *CGM) ReadHistory(pageType PageType, since time.Time) []Record {
first, last := cgm.ReadPageRange(pageType)
if cgm.Error() != nil {
Expand All @@ -28,6 +29,7 @@ func (cgm *CGM) ReadHistory(pageType PageType, since time.Time) []Record {
return results
}

// ReadCount returns a specified number of most recent records.
func (cgm *CGM) ReadCount(pageType PageType, count int) []Record {
first, last := cgm.ReadPageRange(pageType)
if cgm.Error() != nil {
Expand All @@ -42,8 +44,8 @@ func (cgm *CGM) ReadCount(pageType PageType, count int) []Record {
return results
}

// Merge slices of records that are already in reverse chronological order
// into a single ordered slice.
// MergeHistory merges slices of records that are already
// in reverse chronological order into a single ordered slice.
func MergeHistory(slices ...[]Record) []Record {
n := len(slices)
if n == 0 {
Expand All @@ -60,7 +62,7 @@ func MergeHistory(slices ...[]Record) []Record {
}
results := make([]Record, total)
index := make([]int, n)
for next, _ := range results {
for next := range results {
// Find slice with latest current value.
which := -1
max := time.Time{}
Expand All @@ -84,12 +86,13 @@ const (
glucoseReadingWindow = 10 * time.Second
)

// GlucoseReadings returns sensor and EGV records since the specified time.
func (cgm *CGM) GlucoseReadings(since time.Time) []Record {
sensor := cgm.ReadHistory(SENSOR_DATA, since)
sensor := cgm.ReadHistory(SensorData, since)
if cgm.Error() != nil {
return nil
}
egv := cgm.ReadHistory(EGV_DATA, since)
egv := cgm.ReadHistory(EGVData, since)
if cgm.Error() != nil {
return nil
}
Expand Down
50 changes: 22 additions & 28 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,46 @@ import (
"math"
)

// MarshalUint16 marshals a uint16 value into 2 bytes in little-endian order.
func MarshalUint16(n uint16) []byte {
// Marshaling and unmarshaling of ints and floats in little-endian order.

func marshalUint16(n uint16) []byte {
return []byte{byte(n & 0xFF), byte(n >> 8)}
}

// MarshalInt16 marshals an int16 value into 2 bytes in little-endian order.
func MarshalInt16(n int16) []byte {
return MarshalUint16(uint16(n))
// nolint
func marshalInt16(n int16) []byte {
return marshalUint16(uint16(n))
}

// MarshalUint32 marshals a uint32 value into 4 bytes in little-endian order.
func MarshalUint32(n uint32) []byte {
return append(MarshalUint16(uint16(n&0xFFFF)), MarshalUint16(uint16(n>>16))...)
func marshalUint32(n uint32) []byte {
return append(marshalUint16(uint16(n&0xFFFF)), marshalUint16(uint16(n>>16))...)
}

// MarshalInt32 marshals an int32 value into 4 bytes in little-endian order.
func MarshalInt32(n int32) []byte {
return MarshalUint32(uint32(n))
func marshalInt32(n int32) []byte {
return marshalUint32(uint32(n))
}

// UnmarshalUint16 unmarshals 2 bytes in little-endian order into a uint16 value.
func UnmarshalUint16(v []byte) uint16 {
func unmarshalUint16(v []byte) uint16 {
return uint16(v[0]) | uint16(v[1])<<8
}

// UnmarshalInt16 unmarshals 2 bytes in little-endian order into an int16 value.
func UnmarshalInt16(v []byte) int16 {
return int16(UnmarshalUint16(v))
// nolint
func unmarshalInt16(v []byte) int16 {
return int16(unmarshalUint16(v))
}

// UnmarshalUint32 unmarshals 4 bytes in little-endian order into a uint32 value.
func UnmarshalUint32(v []byte) uint32 {
return uint32(UnmarshalUint16(v[0:2])) | uint32(UnmarshalUint16(v[2:4]))<<16
func unmarshalUint32(v []byte) uint32 {
return uint32(unmarshalUint16(v[0:2])) | uint32(unmarshalUint16(v[2:4]))<<16
}

// UnmarshalInt32 unmarshals 4 bytes in little-endian order into an int32 value.
func UnmarshalInt32(v []byte) int32 {
return int32(UnmarshalUint32(v))
func unmarshalInt32(v []byte) int32 {
return int32(unmarshalUint32(v))
}

// UnmarshalUint64 unmarshals 8 bytes in little-endian order into a uint64 value.
func UnmarshalUint64(v []byte) uint64 {
return uint64(UnmarshalUint32(v[0:4])) | uint64(UnmarshalUint32(v[4:8]))<<32
func unmarshalUint64(v []byte) uint64 {
return uint64(unmarshalUint32(v[0:4])) | uint64(unmarshalUint32(v[4:8]))<<32
}

// UnmarshalFloat64 unmarshals 8 bytes in little-endian order into a float64 value.
func UnmarshalFloat64(v []byte) float64 {
return math.Float64frombits(UnmarshalUint64(v))
func unmarshalFloat64(v []byte) float64 {
return math.Float64frombits(unmarshalUint64(v))
}
Loading

0 comments on commit c20ad02

Please sign in to comment.