-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMisc.pas
157 lines (126 loc) · 3.59 KB
/
Misc.pas
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
unit Misc;
interface
{-------------------------------- TGlucoseUnit --------------------------------}
type
TGlucoseUnit = (guMMOL, guMGDL);
const
GlucoseUnitText: array[TGlucoseUnit] of string = ('mmol/L', 'mg/dL');
{-------------------------------- TInsulinUnit --------------------------------}
type
TInsulinUnit = (iuHalf, iuWhole);
function RoundInsulin(Value: Double): Double;
{-------------------------------- TInsulinTime --------------------------------}
type
TInsulinTime = (itMorning, itDay, itEvening);
function InsulinTime: TInsulinTime;
{--------------------------------- MMOL/MGDL ----------------------------------}
type
MMOL = Double;
MGDL = Integer;
function MMOL2MGDL(Value: MMOL): MGDL;
function MGDL2MMOL(Value: MGDL): MMOL;
{------------------------------- TBloodGlucose --------------------------------}
type
TBloodGlucose = record
private
Inner: Double;
public
constructor Create(Value: Double);
function Format: string;
function ToMMOL: MMOL;
function ToMGDL: MGDL;
class operator Equal(const Left: TBloodGlucose; Right: Double): Boolean;
class operator GreaterThanOrEqual(const Left: TBloodGlucose; Right: Double): Boolean;
class operator Implicit(const Value: TBloodGlucose): Double;
class operator LessThan(const Left, Right: TBloodGlucose): Boolean;
class operator Subtract(const Left: TBloodGlucose; Right: Double): Double;
end;
implementation
uses
// Delphi
System.SysUtils,
// Project
Settings;
function RoundInsulin(Value: Double): Double;
begin
if Value < 0 then
begin
Result := 0;
EXIT;
end;
Result := Value;
const settings = Settings.Get;
if settings.InsulinUnit = iuWhole then
Result := Round(Result)
else if settings.InsulinUnit = iuHalf then
begin
const frac = Frac(Result);
if frac < 0.25 then
Result := Int(Result)
else if frac > 0.75 then
Result := Round(Result)
else
Result := Int(Result) + 0.5;
end;
end;
function InsulinTime: TInsulinTime;
begin
Result := itMorning;
if Time > 11/24 then
if Time > 16/24 then
Result := itEvening
else
Result := itDay;
end;
function MMOL2MGDL(Value: MMOL): MGDL;
begin
Result := Round(Value * 18.0182);
end;
function MGDL2MMOL(Value: MGDL): MMOL;
begin
Result := Value / 18.0182;
end;
constructor TBloodGlucose.Create(Value: Double);
begin
Self.Inner := Value;
end;
function TBloodGlucose.ToMMOL: MMOL;
begin
Result := Self.Inner;
if Settings.Get.GlucoseUnit = guMGDL then
Result := MGDL2MMOL(Round(Self.Inner));
end;
function TBloodGlucose.ToMGDL: MGDL;
begin
Result := Round(Self.Inner);
if Settings.Get.GlucoseUnit = guMMOL then
Result := MMOL2MGDL(Self.Inner);
end;
function TBloodGlucose.Format: string;
begin
case Settings.Get.GlucoseUnit of
guMMOL: Result := System.SysUtils.Format('%.1f', [Self.Inner], TSettings.Format);
guMGDL: Result := System.SysUtils.Format('%.0f', [Self.Inner], TSettings.Format);
end;
end;
class operator TBloodGlucose.Equal(const Left: TBloodGlucose; Right: Double): Boolean;
begin
Result := Left.Inner = Right;
end;
class operator TBloodGlucose.GreaterThanOrEqual(const Left: TBloodGlucose; Right: Double): Boolean;
begin
Result := left.Inner >= Right;
end;
class operator TBloodGlucose.Implicit(const Value: TBloodGlucose): Double;
begin
Result := Value.Inner;
end;
class operator TBloodGlucose.LessThan(const Left, Right: TBloodGlucose): Boolean;
begin
Result := Left.Inner < Right.Inner;
end;
class operator TBloodGlucose.Subtract(const Left: TBloodGlucose; Right: Double): Double;
begin
Result := Left.Inner - Right;
end;
end.