forked from ecc1/medtronic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunits.go
106 lines (85 loc) · 2.1 KB
/
units.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package medtronic
import (
"fmt"
"log"
)
// Carbs values are represented as either grams or 10x exchanges.
type Carbs int
const (
CarbUnits Command = 0x88
GlucoseUnits Command = 0x89
)
type CarbUnitsType byte
//go:generate stringer -type CarbUnitsType
const (
Grams CarbUnitsType = 1
Exchanges CarbUnitsType = 2
)
// Glucose values are represented as either mg/dL or μmol/L,
// so all conversions must include a GlucoseUnitsType parameter.
type Glucose int
type GlucoseUnitsType byte
const (
MgPerDeciLiter GlucoseUnitsType = 1
MicromolPerLiter GlucoseUnitsType = 2
)
func (u GlucoseUnitsType) String() string {
switch u {
case MgPerDeciLiter:
return "mg/dL"
case MicromolPerLiter:
return "μmol/L"
default:
log.Panicf("unknown glucose unit %d", u)
}
panic("unreachable")
}
func (pump *Pump) whichUnits(cmd Command) byte {
data := pump.Execute(cmd)
if pump.Error() != nil {
return 0
}
if len(data) < 2 || data[0] != 1 {
pump.BadResponse(cmd, data)
return 0
}
return data[1]
}
func intToGlucose(n int, t GlucoseUnitsType) Glucose {
if t == MicromolPerLiter {
// Convert 10x mmol/L to μmol/L
return Glucose(n) * 100
} else {
return Glucose(n)
}
}
func byteToGlucose(n byte, t GlucoseUnitsType) Glucose {
return intToGlucose(int(n), t)
}
func (pump *Pump) CarbUnits() CarbUnitsType {
return CarbUnitsType(pump.whichUnits(CarbUnits))
}
func (pump *Pump) GlucoseUnits() GlucoseUnitsType {
return GlucoseUnitsType(pump.whichUnits(GlucoseUnits))
}
// Quantities and rates of insulin delivery are represented in milliunits.
type Insulin int
func (r Insulin) String() string {
return fmt.Sprintf("%g", float64(r)/1000)
}
func milliUnitsPerStroke(newerPump bool) Insulin {
if newerPump {
return 25
} else {
return 100
}
}
func intToInsulin(strokes int, newerPump bool) Insulin {
return Insulin(strokes) * milliUnitsPerStroke(newerPump)
}
func byteToInsulin(strokes uint8, newerPump bool) Insulin {
return intToInsulin(int(strokes), newerPump)
}
func twoByteInsulin(data []byte, newerPump bool) Insulin {
return Insulin(twoByteUint(data)) * milliUnitsPerStroke(newerPump)
}