forked from didn0t/PicoCGM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
237 lines (206 loc) · 6.94 KB
/
main.py
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import network
import rp2
import time
import machine
import requests
import ntptime
from pimoroni import Button
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2, PEN_P4
def wifi_connect():
display_message('Connecting...')
rp2.country('NZ')
wlan = network.WLAN(network.STA_IF)
wlan.config(hostname = 'PicoCGM')
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
timeout = 15
while timeout > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
timeout -= 1
print(f'[*] Waiting for connection... {timeout}')
time.sleep(1)
if wlan.status() != 3:
machine.reset()
else:
display_message('WIFI Up')
print(f"[+] Connected to {WIFI_SSID} with IP address {wlan.ifconfig()[0]}")
print("[+] Setting Time via NTP...")
ntptime.settime()
print("[+] UTC time after synchronization:%s" %str(time.localtime()))
return wlan
def scale_to_range(number, old_min, old_max, new_min, new_max):
if number < old_min:
number = old_min
elif number > old_max:
number = old_max
old_range = old_max - old_min
new_range = new_max - new_min
scaled_number = int(((number - old_min) / old_range) * new_range + new_min)
return scaled_number
def severity(BG):
if BG <= 72:
return BLUE
elif BG <= 180:
return GREEN
elif BG <= 260:
return YELLOW
else:
return RED
def fetch_nightscout_data(delta):
api = f"/api/v1/entries/sgv.json?find[date][$gte]={delta}&count={WIDTH//8}"
url = NIGHTSCOUT_URL + api
print(f"[*] Requesting data from NightScout API: {url}")
headers = {"API-SECRET": f"{NIGHTSCOUT_TOKEN}"}
try:
response = requests.get(url, headers=headers)
return response.json()
except Exception as err:
print(f"[-] Unexpected {err=}, {type(err)=}")
return None
def draw_arrow(direction, x, y, pen):
direction_map = {
"SingleUp": (0,2,1,0,2,2),
"FortyFiveUp": (0,0,2,0,2,2),
"Flat": (0,0,1,1,0,2),
"FortyFiveDown":(0,2,2,0,2,2),
"SingleDown": (0,0,1,2,2,0),
}
scale = 12
x1, y1, x2, y2, x3, y3 = direction_map[direction]
display.set_pen(pen)
display.triangle(x+x1*scale, y+y1*scale, x+x2*scale, y+y2*scale, x+x3*scale, y+y3*scale)
def display_text(bg, obg, direction, end):
scale = 4
prefix=""
display.set_pen(severity(bg))
diff = bg-obg
if mmol:
bg = round(bg/18, 1)
diff = round(diff/18, 1)
display.text(str(bg), 0, 0, 0, scale)
pad = display.measure_text(str(bg), scale)
draw_arrow(direction, pad, 0, BLUE)
if diff > 0:
prefix = "+"
diff = prefix + str(diff)
display.text(diff, pad+25, 0, 0, scale)
# Display Time
_, _, _, hour, min, _, _, _ = time.gmtime()
if DST:
hour += 1
hour += UTC_OFFSET
hour %= 24
last = round((time.time() - end) / 60)
if last > 60:
last = str(round(last / 60)) + "h"
else:
last = str(last) + "m"
text = (f"{hour}:{min:02d} {last}")
pad = display.measure_text(text, scale)
display.set_pen(GREY)
display.text(text, WIDTH-pad, 0, -1, scale)
def display_graph(data):
if data != None:
start = data[-1]['date']//1000
end = data[0]['date']//1000
display.set_pen(BLACK)
display.clear()
for entry in data:
sgv = entry.get('sgv', 0)
date = (entry.get('date', 0)//1000) - start
if date < 0: # We need enough data for the width but it may return data before the time we asked for!
continue
y_scaled = scale_to_range(sgv, y_scale_min, y_scale_max, 2, HEIGHT-10)
x_scaled = scale_to_range(date, 0, time.time()-start, 0, WIDTH-1)
y = HEIGHT - y_scaled
#print(f"[DEBUG] {date}->{x_scaled}: {sgv}->{y_scaled} ({y})")
display.set_pen(severity(sgv))
display.circle(x_scaled, y, 2)
display_text(data[0]['sgv'], data[1]['sgv'], data[0]['direction'], end)
display.update()
def display_message(message):
width = display.measure_text(message,4)
print(f"[+] Displaying Message: {message} with length {width}")
display.set_pen(BLACK)
display.clear()
display.set_pen(YELLOW)
pad = int((WIDTH - width) / 2) # Center Text
display.text(message, pad, 20, -1, 4)
display.update()
time.sleep(1)
def check_buttons(data):
global mmol, DST, backlight
if button_x.read():
mmol = not mmol # Toggle
if mmol:
display_message("mmol/L")
else:
display_message("mg/dL")
display_graph(data)
elif button_y.read():
DST = not DST # Toggle
if DST:
display_message("DST Enabled")
else:
display_message("DST Disabled")
display_graph(data)
elif button_a.read() and backlight <= 0.9:
backlight += 0.1
display.set_backlight(backlight)
elif button_b.read() and backlight >= 0.1:
backlight -= 0.1
display.set_backlight(backlight)
################################################################
# Main Program
################################################################
# Configure Display Buttons
button_a = Button(12)
button_b = Button(13)
button_x = Button(14)
button_y = Button(15)
# create the rtc object
rtc = machine.RTC()
# Initialize PicoGraphics for Display
backlight = 0.5
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2, pen_type=PEN_P4, rotate=0)
display.set_backlight(backlight)
display.set_font("bitmap8")
WIDTH, HEIGHT = display.get_bounds()
BLACK = display.create_pen(0, 0, 0)
GREY = display.create_pen(150, 150, 150)
RED = display.create_pen(255, 0, 0)
GREEN = display.create_pen(0, 255, 0)
BLUE = display.create_pen(0, 0, 255)
YELLOW = display.create_pen(255, 255, 0)
################################################################
# User Configurable Settings
################################################################
# Wi-FI Settings
WIFI_SSID = 'Hoops'
WIFI_PASSWORD = 'H00p3rw1r3l355?'
# NightScout Settings
NIGHTSCOUT_URL = "https://nightscout.hoops.nz"
NIGHTSCOUT_TOKEN = "display-6a046d5c21d47346"
# TimeZone Configuration
UTC_OFFSET = +12
DST = True # Daylight Saving Time
# Min/Max Scale for Blood Glucose in mg/dL
y_scale_min = 54 # 3mmol
y_scale_max = 414 # 23mmol
mmol = True # Controls if to display mmol/L instead of mg/dL
wlan = wifi_connect()
last_execution_time = 0
interval_ms = 60 * 1000
data = None
# Main loop
while True:
current_time = time.ticks_ms()
if current_time - last_execution_time >= interval_ms:
end = time.time()
start = end - (WIDTH * 300) # Libre logs every 5m so grab data going back that far...
data = fetch_nightscout_data(start)
display_graph(data)
last_execution_time = current_time
check_buttons(data)
time.sleep_ms(100)