-
Notifications
You must be signed in to change notification settings - Fork 29
/
carbratios.go
104 lines (94 loc) · 2.32 KB
/
carbratios.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
package medtronic
import (
"log"
"time"
)
// CarbRatio represents an entry in a carb ratio schedule.
type CarbRatio struct {
Start TimeOfDay
Ratio Ratio
Units CarbUnitsType
}
// Newer pumps store carb ratios as 10x grams/unit or 1000x units/exchange.
// Older pumps store carb ratios as grams/unit or 10x units/exchange.
// Ratio represents a carb ratio using the higher resolution:
// 10x grams/unit or 1000x units/exchange.
type Ratio int
func intToRatio(n int, u CarbUnitsType, family Family) Ratio {
if family <= 22 {
// Convert to higher-resolution representation.
switch u {
case Grams:
return Ratio(10 * n)
case Exchanges:
return Ratio(100 * n)
default:
log.Panicf("unknown carb unit %d", u)
}
}
// Use representation as-is.
return Ratio(n)
}
// CarbRatioSchedule represents a carb ratio schedule.
type CarbRatioSchedule []CarbRatio
func carbRatioStep(family Family) int {
if family <= 22 {
return 2
}
return 3
}
func decodeCarbRatioSchedule(data []byte, units CarbUnitsType, family Family) CarbRatioSchedule {
var sched []CarbRatio
step := carbRatioStep(family)
for i := 0; i <= len(data)-step; i += step {
start := halfHoursToTimeOfDay(data[i])
if start == 0 && len(sched) != 0 {
break
}
var value int
if family <= 22 {
value = int(data[i+1])
} else {
value = twoByteInt(data[i+1 : i+3])
}
sched = append(sched, CarbRatio{
Start: start,
Ratio: intToRatio(value, units, family),
Units: units,
})
}
return sched
}
// CarbRatios returns the pump's carb ratio schedule..
func (pump *Pump) CarbRatios() CarbRatioSchedule {
data := pump.Execute(carbRatios)
if pump.Error() != nil {
return CarbRatioSchedule{}
}
if len(data) < 2 {
pump.BadResponse(carbRatios, data)
return CarbRatioSchedule{}
}
// Format of response depends on the pump family.
family := pump.Family()
n := int(data[0]) - 1
step := carbRatioStep(family)
if n%step != 0 {
pump.BadResponse(carbRatios, data)
return CarbRatioSchedule{}
}
units := CarbUnitsType(data[1])
return decodeCarbRatioSchedule(data[step:step+n], units, family)
}
// CarbRatioAt returns the carb ratio in effect at the given time.
func (s CarbRatioSchedule) CarbRatioAt(t time.Time) CarbRatio {
d := SinceMidnight(t)
last := CarbRatio{}
for _, v := range s {
if v.Start > d {
break
}
last = v
}
return last
}