dexcom

package module
v0.0.0-...-91fd5a1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2018 License: MIT Imports: 12 Imported by: 0

README

dexcom

The dexcom package provides functions to connect to Dexcom G4 and Dexcom G4 Share continuous glucose monitor (CGM) systems. It connects by either USB or Bluetooth LE to the Dexcom receiver.

Wireless connection (Bluetooth LE; G4 Share only)

For BLE connections, the serial number of the Dexcom receiver must be passed in the DEXCOM_CGM_ID environment variable.

For the initial connection to the receiver, use the Settings > Share menu to Forget Device, then turn Share back on. The cmd/g4ping program can be used to confirm connection with the receiver.

Wired connection (USB serial)

For serial connections, the host must have the cdc_acm kernel module loaded. Look for the creation of /dev/ttyACM0 in the system log when the receiver is attached.

Utility programs

The cmd directory contains some simple utility programs:

  • g4ping pings the receiver (first connecting if necessary) and exits with a success or failure status.
  • glucose retrieves CGM data and prints it in various formats.
  • backfill finds gaps in Nightscout CGM data, retrieves the missing data from the receiver, and uploads it. Note that a USB connection works much faster for gaps that are hours or days in the past, and can be done from any Linux machine, not just an OpenAPS rig.
  • g4setclock sets the receiver's date and time.
  • g4update retrieves CGM data, with options to update a local JSON file and upload to Nightscout.
Documentation

https://godoc.org/github.com/ecc1/dexcom

Documentation

Overview

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

Index

Constants

View Source
const (
	EGVDisplayOnly = 1 << 15
	EGVValueMask   = 0x3FF
	EGVNoiseMask   = 0x70
	EGVTrendMask   = 0xF
)

Constants used to extract EGV, noise, and trend.

View Source
const (
	// JSONTimeLayout specifies the format for JSON time values.
	JSONTimeLayout = time.RFC3339

	// UserTimeLayout specifies a consistent, human-readable format for local time.
	UserTimeLayout = "2006-01-02 15:04:05"
)

Variables

View Source
var IterationDone = errors.New("iteration done") // nolint

IterationDone can be returned by a RecordFunc to indicate that iteration is complete and no further records need to be processed.

Functions

func IsSpecial

func IsSpecial(glucose uint16) bool

IsSpecial checks whether a glucose value falls in the SpecialGlucose range.

func NightscoutEntries

func NightscoutEntries(records Records) nightscout.Entries

NightscoutEntries converts records (in reverse-chronological order) into a Nightscout entries. Neighboring Sensor and EGV records are merged.

Types

type CGM

type CGM struct {
	Connection
	// contains filtered or unexported fields
}

CGM represents a CGM connection.

func Open

func Open() *CGM

Open first attempts to open a USB connection; if that fails it tries a BLE connection.

func (*CGM) Cmd

func (cgm *CGM) Cmd(cmd Command, params ...byte) []byte

Cmd creates a Dexcom packet with the given command and parameters, sends it to the device, and returns the response.

func (*CGM) Error

func (cgm *CGM) Error() error

Error returns the error state of the CGM.

func (*CGM) IterRecords

func (cgm *CGM) IterRecords(pageType PageType, firstPage, lastPage int, recordFn RecordFunc)

IterRecords reads the specified page range and applies recordFn to each record in each page. Pages are visited in reverse order to facilitate scanning for recent records.

func (*CGM) ReadCount

func (cgm *CGM) ReadCount(pageType PageType, count int) Records

ReadCount returns a specified number of most recent records.

func (*CGM) ReadDisplayTime

func (cgm *CGM) ReadDisplayTime() time.Time

ReadDisplayTime returns the Dexcom receiver's display time.

SystemTime = RTC + SystemTimeOffset
DisplayTime = SystemTime + DisplayTimeOffset

func (*CGM) ReadFirmwareHeader

func (cgm *CGM) ReadFirmwareHeader() XMLInfo

ReadFirmwareHeader gets the firmware header from the Dexcom CGM receiver and returns it as XMLInfo.

func (*CGM) ReadHistory

func (cgm *CGM) ReadHistory(pageType PageType, since time.Time) Records

ReadHistory returns records since the specified time.

func (*CGM) ReadPage

func (cgm *CGM) ReadPage(pageType PageType, pageNumber int) []byte

ReadPage reads the specified page.

func (*CGM) ReadPageRange

func (cgm *CGM) ReadPageRange(pageType PageType) (int, int)

ReadPageRange returns the starting and ending page for a given PageType. The page numbers can be -1 if there are no entries (for example, USER_EVENT_DATA).

func (*CGM) ReadRawRecords

func (cgm *CGM) ReadRawRecords(pageType PageType, pageNumber int) [][]byte

ReadRawRecords reads the specified page and returns its records as raw byte slices.

func (*CGM) ReadRecords

func (cgm *CGM) ReadRecords(pageType PageType, pageNumber int) Records

ReadRecords reads the specified page and returns its records.

func (*CGM) ReadXMLRecord

func (cgm *CGM) ReadXMLRecord(pageType PageType) Record

ReadXMLRecord gets the given XML record type from the Dexcom CGM receiver.

func (*CGM) SetDisplayTime

func (cgm *CGM) SetDisplayTime(t time.Time)

SetDisplayTime sets the Dexcom receiver's display time.

func (*CGM) SetError

func (cgm *CGM) SetError(err error)

SetError sets the error state of the CGM.

type CRCError

type CRCError struct {
	Kind               string
	Received, Computed uint16
	PageType           PageType
	PageNumber         int
	Data               []byte
}

CRCError indicates that a CRC error was detected.

func (CRCError) Error

func (e CRCError) Error() string

type CalibrationInfo

type CalibrationInfo struct {
	Slope     float64
	Intercept float64
	Scale     float64
	Decay     float64
	Data      []CalibrationRecord
}

CalibrationInfo represents a calibration event.

type CalibrationRecord

type CalibrationRecord struct {
	TimeEntered time.Time
	Glucose     int32
	Raw         int32
	TimeApplied time.Time
}

CalibrationRecord represents a calibration data point.

type Command

type Command byte

Command represents a Dexcom CGM receiver command.

const (
	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
)

Dexcom G4 receiver commands.

func (Command) String

func (i Command) String() string

type Connection

type Connection interface {
	Send([]byte) error
	Receive([]byte) error
	Close()
}

Connection is the interface satisfied by a CGM connection.

func OpenBLE

func OpenBLE() (Connection, error)

OpenBLE makes a BLE connection to a Dexcom G4 Share receiver.

func OpenUSB

func OpenUSB() (Connection, error)

OpenUSB opens the USB serial device for a Dexcom G4 receiver.

type EGVInfo

type EGVInfo struct {
	Glucose     uint16
	DisplayOnly bool
	Noise       uint8
	Trend       Trend
}

EGVInfo represents an estimated glucose value.

type InsertionInfo

type InsertionInfo struct {
	SystemTime time.Time
	Event      SensorChange
}

InsertionInfo represents a sensor change event.

type MeterInfo

type MeterInfo struct {
	Glucose   uint16
	MeterTime time.Time
}

MeterInfo represents a meter reading.

type PageInfo

type PageInfo struct {
	Type    PageType
	Number  int
	Records [][]byte
}

PageInfo represents a page of raw records.

func UnmarshalPage

func UnmarshalPage(v []byte) (*PageInfo, error)

UnmarshalPage validates the CRC of the given page data and uses the page type to slice the data into raw records.

type PageType

type PageType byte

PageType specifies a record page type stored by the Dexcom G4 receiver.

const (
	ManufacturingData PageType = 0
	FirmwareData      PageType = 1
	SoftwareData      PageType = 2
	SensorData        PageType = 3
	EGVData           PageType = 4
	CalibrationData   PageType = 5
	DeviationData     PageType = 6
	InsertionTimeData PageType = 7
	ReceiverLogData   PageType = 8
	ReceiverErrorData PageType = 9
	MeterData         PageType = 10
	UserEventData     PageType = 11
	UserSettingData   PageType = 12

	FirstPageType = ManufacturingData
	LastPageType  = UserSettingData

	InvalidPage PageType = 0xFF
)

Types of CGM records stored by the Dexcom G4 receiver.

func (PageType) String

func (i PageType) String() string

type Record

type Record struct {
	Timestamp   Timestamp
	XML         XMLInfo          `json:",omitempty"`
	Sensor      *SensorInfo      `json:",omitempty"`
	EGV         *EGVInfo         `json:",omitempty"`
	Calibration *CalibrationInfo `json:",omitempty"`
	Insertion   *InsertionInfo   `json:",omitempty"`
	Meter       *MeterInfo       `json:",omitempty"`
}

Record represents a time-stamped Dexcom receiver record.

func (Record) Glucose

func (r Record) Glucose() uint16

Glucose returns the glucose field from an EGV record.

func (Record) Time

func (r Record) Time() time.Time

Time returns the record's display time.

type RecordFunc

type RecordFunc func(Record) error

RecordFunc represents a function that IterRecords applies to each record.

type Records

type Records []Record

Records represents a sequence of records.

func MergeHistory

func MergeHistory(slices ...Records) Records

MergeHistory merges slices of records that are already in reverse chronological order into a single ordered slice.

func UnmarshalRecords

func UnmarshalRecords(pageType PageType, data [][]byte) (Records, error)

UnmarshalRecords unmarshals raw records into records of the appropriate type.

func (Records) Len

func (v Records) Len() int

Len returns the number of records.

func (Records) Time

func (v Records) Time(i int) time.Time

Time returns the time of the record at index i.

type SensorChange

type SensorChange byte

SensorChange represents a sensor change.

const (
	Stopped SensorChange = 1
	Started SensorChange = 7
)

Sensor change values.

func (SensorChange) String

func (i SensorChange) String() string

type SensorInfo

type SensorInfo struct {
	Unfiltered uint32
	Filtered   uint32
	RSSI       int8
	Unknown    byte
}

SensorInfo represents a sensor reading.

type SpecialGlucose

type SpecialGlucose uint16

SpecialGlucose represents a glucose value that indicates an exceptional condition.

const (
	SensorNotActive     SpecialGlucose = 1
	MinimalDeviation    SpecialGlucose = 2
	NoAntenna           SpecialGlucose = 3
	SensorNotCalibrated SpecialGlucose = 5
	CountDeviation      SpecialGlucose = 6
	AbsoluteDeviation   SpecialGlucose = 9
	PowerDeviation      SpecialGlucose = 10
	BadRF               SpecialGlucose = 12
)

Exceptional conditions.

func (SpecialGlucose) String

func (i SpecialGlucose) String() string

type Timestamp

type Timestamp struct {
	SystemTime  time.Time
	DisplayTime time.Time
}

A Timestamp contains system and display time values.

type Trend

type Trend byte

Trend represents a directional arrow displayed by the Dexcom CGM receiver.

const (
	UpUp          Trend = 1
	Up            Trend = 2
	Up45          Trend = 3
	Flat          Trend = 4
	Down45        Trend = 5
	Down          Trend = 6
	DownDown      Trend = 7
	NotComputable Trend = 8
	OutOfRange    Trend = 9
)

Trend arrows.

func (Trend) String

func (i Trend) String() string

func (Trend) Symbol

func (t Trend) Symbol() string

Symbol converts a Trend to a graphical representation.

type XMLInfo

type XMLInfo map[string]string

XMLInfo maps attribute names to values. The Dexcom CGM receiver represents its system data as single XML nodes with multiple attributes, so a tree structure is not required.

func (*XMLInfo) UnmarshalXML

func (ptr *XMLInfo) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML is called by xml.Unmarshal.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL