-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGarminSDView.mc
448 lines (404 loc) · 13.2 KB
/
GarminSDView.mc
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
Garmin_sd - a data source for OpenSeizureDetector that runs on a
Garmin ConnectIQ watch.
See http://openseizuredetector.org for more information.
Copyright Graham Jones, 2019, 2022
This file is part of Garmin_sd.
Garmin_sd is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Garmin_sd is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Garmin_sd. If not, see <http://www.gnu.org/licenses/>.
*/
using Toybox.WatchUi as Ui;
using Toybox.Graphics as Gfx;
using Toybox.System;
using Toybox.Time;
using Toybox.Application as App;
import Toybox.Application.Storage;
import Toybox.Lang;
class GarminSDView extends Ui.View {
var accelHandler as GarminSDDataHandler;
var width as Number = 0;
var halfWidth as Number = 0;
var height as Number = 0;
var mSdState as GarminSDState;
var beatsPerMinuteAbbrev as String = "";
var batteryAbbrev as String = "";
var muteLabel as String = "";
var fontSizeClock as Gfx.FontDefinition = Gfx.FONT_SYSTEM_NUMBER_HOT;
var fontHrO2Str as Gfx.FontDefinition = Gfx.FONT_SYSTEM_NUMBER_HOT;
var heightScaleLine1 as Float = 0.0;
var heightScaleLine2 as Float = 0.0;
var heightScaleLine3 as Float = 0.0;
var heightScaleLine4 as Float = 0.0;
var heightScaleLine5 as Float = 0.0;
var lastUpdatedMinute as Number = -1;
function initialize(sdState as GarminSDState) {
writeLog("GarminSDView.initialize()", "");
View.initialize();
mSdState = sdState;
accelHandler = new GarminSDDataHandler(
Ui.loadResource(Rez.Strings.VersionId).toString()
);
//loading resources locally
beatsPerMinuteAbbrev = Ui.loadResource(Rez.Strings.Beats_per_minute_abbrev).toString();
batteryAbbrev = Ui.loadResource(Rez.Strings.Battery_abbrev).toString();
muteLabel = Ui.loadResource(Rez.Strings.Mute_label).toString();
writeLog("GarminSDView.initialize()", "Complete");
}
function onTick() as Void {
/**
Called by GarminSDView every second in case we need to do anything timed.
*/
//writeLog("GarminSDView.onTick()", "Start");
accelHandler.onTick();
var currentMinute = System.getClockTime().min;
if ((accelHandler.mComms.needs_update == true)||(currentMinute != lastUpdatedMinute)){
//writeLog("GarminSDView.onTick()", "update view");
Ui.requestUpdate();
lastUpdatedMinute = currentMinute;
accelHandler.mComms.needs_update = false;
}
}
// Load your resources here
function onLayout(dc) {
//writeLog("GarminSDView.onLayout()", "");
width = dc.getWidth();
halfWidth = width/2;
height = dc.getHeight();
// Nominal height of display for positioning text - values are for a 240px high display.
var heightScale = height / 240.0;
heightScaleLine1=heightScale*20;
heightScaleLine2=heightScale*45;
heightScaleLine3=heightScale*120;
heightScaleLine4=heightScale*150;
heightScaleLine5=heightScale*180;
//precalculate font size
// There is an issue with some devices having different font sizes, so
// we check the width of the text for our preferred font size, and if it is too long
// we use a smaller font.
var timeTextDims = dc.getTextDimensions(
"23:59:52",
Gfx.FONT_SYSTEM_NUMBER_HOT
);
if (timeTextDims[0] < width) {
fontSizeClock = Gfx.FONT_SYSTEM_NUMBER_HOT;
}
else{
fontSizeClock = Gfx.FONT_SYSTEM_NUMBER_MEDIUM;
}
//precalculate HRO2 string size from dummy values
var hrO2Str = "";
if (accelHandler.mO2SensorIsEnabled == true){
hrO2Str = Lang.format("150 $1$ / 100% Ox", [beatsPerMinuteAbbrev]);
}
else {
hrO2Str = Lang.format("150 $1$", [beatsPerMinuteAbbrev]);
}
var hrTextDims = dc.getTextDimensions(hrO2Str, Gfx.FONT_LARGE);
if (hrTextDims[0] < width) {
fontHrO2Str = Gfx.FONT_LARGE;
}
else{
fontHrO2Str = Gfx.FONT_SMALL;
}
writeLog("GarminSDView.on_layout()", "Start accelHandler");
accelHandler.onStart();
}
// Restore the state of the app and prepare the view to be shown
function onShow() {
}
// Update the view
function onUpdate(dc) {
var myTime = System.getClockTime();
var timeString = Lang.format("$1$:$2$", [
myTime.hour.format("%02d"),
myTime.min.format("%02d")
]);
var sysStats = System.getSystemStats();
var hrO2Str = "";
if (accelHandler.mO2SensorIsEnabled == true){
hrO2Str = Lang.format("$1$ $2$ / $3$% Ox", [
accelHandler.mHR,
beatsPerMinuteAbbrev,
accelHandler.mO2sat,
]);
}
else {
hrO2Str = Lang.format("$1$ $2$", [
accelHandler.mHR,
beatsPerMinuteAbbrev,
]);
}
var hrBatStr = Lang.format("$1$: $2$%", [
batteryAbbrev,
sysStats.battery.format("%02.0f"),
]);
dc.setColor(Gfx.COLOR_BLACK, Gfx.COLOR_WHITE);
dc.clear();
dc.setColor(Gfx.COLOR_BLACK, Gfx.COLOR_TRANSPARENT);
dc.drawText(
halfWidth,
0,
Gfx.FONT_MEDIUM,
"OpenSeizure",
Gfx.TEXT_JUSTIFY_CENTER
);
dc.drawText(
halfWidth,
heightScaleLine1,
Gfx.FONT_MEDIUM,
"Detector",
Gfx.TEXT_JUSTIFY_CENTER
);
dc.drawText(
halfWidth,
heightScaleLine2,
fontSizeClock,
timeString,
Gfx.TEXT_JUSTIFY_CENTER
);
dc.drawText(
halfWidth,
heightScaleLine3,
fontHrO2Str,
hrO2Str,
Gfx.TEXT_JUSTIFY_CENTER
);
dc.drawText(
halfWidth,
heightScaleLine4,
fontHrO2Str,
hrBatStr,
Gfx.TEXT_JUSTIFY_CENTER
);
if (accelHandler.mMute) {
dc.drawText(
halfWidth,
heightScaleLine5,
Gfx.FONT_LARGE,
muteLabel,
Gfx.TEXT_JUSTIFY_CENTER
);
} else {
dc.drawText(
halfWidth,
heightScaleLine5,
Gfx.FONT_LARGE,
accelHandler.mStatusStr,
Gfx.TEXT_JUSTIFY_CENTER
);
}
}
// Called when this View is removed from the screen. Save the
// state of your app here.
function onHide() {
}
}
class SdDelegate extends Ui.BehaviorDelegate {
var mSdView as GarminSDView;
var mSdState as GarminSDState;
var mQuitDlgOpenTime as Number = 0;
const QUIT_TIMEOUT = 10; // Seconds
const QUIT_TIMEOUT_BENMODE = 1; // Second
const MUTE_TIMEOUT = 10; // Seconds
function initialize(sdView as GarminSDView, sdState as GarminSDState) {
writeLog("SdDelegate.initialize()", "");
mSdView = sdView;
mSdState = sdState;
mSdState.setMode(MODE_RUNNING);
// Set default values for settings if necessary
if (Storage.getValue(MENUITEM_BENMODE) == null) {
Storage.setValue(MENUITEM_BENMODE, 0);
}
if (Storage.getValue(MENUITEM_SOUND) == null) {
Storage.setValue(MENUITEM_SOUND, 0);
}
if (Storage.getValue(MENUITEM_VIBRATION) == null) {
Storage.setValue(MENUITEM_VIBRATION, 0);
}
if (Storage.getValue(MENUITEM_LIGHT) == null) {
Storage.setValue(MENUITEM_LIGHT, 0);
}
if (Storage.getValue(MENUITEM_O2SENSOR) == null) {
Storage.setValue(MENUITEM_O2SENSOR, 1);
}
BehaviorDelegate.initialize();
}
function onTick() as Void {
//System.println("SdDelegate.timerCallback()");
// Handle Timeout of Quit Dialog
if (mSdState.getMode() == MODE_QUITDLG) {
//System.println("SdDelegate.timerCalback - Quit Dialog Displayed");
var timeoutSecs = Storage.getValue(MENUITEM_BENMODE)
? QUIT_TIMEOUT_BENMODE
: QUIT_TIMEOUT;
var dlgOpenSecs = Time.now().value() - mQuitDlgOpenTime;
//System.println("dlgOpenMs="+dlgOpenSecs);
if (dlgOpenSecs > timeoutSecs) {
writeLog("timerCallback()" , "Quit Dialog Timedout - closing");
mSdState.setMode(MODE_RUNNING);
Ui.popView(Ui.SLIDE_IMMEDIATE);
}
}
//mSdView.accelHandler.onTick();
}
function onMenu() {
// Display a menu of configurable options
var menu = new GarminSDSettingsMenu();
var boolean;
//var boolean = Storage.getValue(1) ? true : false;
/*var boolean = mSdView.accelHandler.mMute ? true : false;
menu.addItem(
new Ui.ToggleMenuItem(
Ui.loadResource(Rez.Strings.Mute_title).toString(),
Ui.loadResource(Rez.Strings.Mute_desc).toString(),
MENUITEM_MUTE,
boolean,
null
)
);
*/
boolean = Storage.getValue(MENUITEM_VIBRATION) ? true : false;
menu.addItem(
new Ui.ToggleMenuItem(
Ui.loadResource(Rez.Strings.Vibration_title).toString(),
Ui.loadResource(Rez.Strings.Vibration_desc).toString(),
MENUITEM_VIBRATION,
boolean,
null
)
);
boolean = Storage.getValue(MENUITEM_SOUND) ? true : false;
menu.addItem(
new Ui.ToggleMenuItem(
Ui.loadResource(Rez.Strings.Sound_title).toString(),
Ui.loadResource(Rez.Strings.Sound_desc).toString(),
MENUITEM_SOUND,
boolean,
null
)
);
boolean = Storage.getValue(MENUITEM_LIGHT) ? true : false;
menu.addItem(
new Ui.ToggleMenuItem(
Ui.loadResource(Rez.Strings.Light_title).toString(),
Ui.loadResource(Rez.Strings.Light_desc).toString(),
MENUITEM_LIGHT,
boolean,
null
)
);
boolean = Storage.getValue(MENUITEM_O2SENSOR) ? true : false;
menu.addItem(
new Ui.ToggleMenuItem(
Ui.loadResource(Rez.Strings.O2_sensor_title).toString(),
Ui.loadResource(Rez.Strings.O2_sensor_desc).toString(),
MENUITEM_O2SENSOR,
boolean,
null
)
);
boolean = Storage.getValue(MENUITEM_BENMODE) ? true : false;
menu.addItem(
new Ui.ToggleMenuItem(
Ui.loadResource(Rez.Strings.BenMode_title).toString(),
Ui.loadResource(Rez.Strings.BenMode_desc).toString(),
MENUITEM_BENMODE,
boolean,
null
)
);
Ui.pushView(
menu,
new GarminSDSettingsMenuDelegate(mSdView.accelHandler),
Ui.SLIDE_IMMEDIATE
);
return true;
}
function onBack() {
// Display a quit confirmation dialog, which times out after a given period
// Handled by setting mMode to MODE_QUITDLG and initialising the time that we open the dialog.
// timeout is handled in the timerCallback function.
writeLog("SdDelegate.onBack()", "");
var quitString = Ui.loadResource(Rez.Strings.Exit_app_confirmation).toString();
var cd = new Ui.Confirmation(quitString);
mSdState.setMode(MODE_QUITDLG);
mQuitDlgOpenTime = Time.now().value();
Ui.pushView(cd, new QuitDelegate(mSdView, mSdState), Ui.SLIDE_IMMEDIATE);
return true;
}
// Detect Menu button input
function onKey(keyEvent) {
writeLog("onKey()", " key="+keyEvent.getKey()); // e.g. KEY_MENU = 7
if (keyEvent.getKey() == KEY_ENTER) {
if (mSdView.accelHandler.mMute) {
mSdView.accelHandler.mMute = false;
writeLog( "onKey()", "Mute="+mSdView.accelHandler.mMute);
} else {
mSdView.accelHandler.mMute = true;
writeLog("onKey()", "Mute="+mSdView.accelHandler.mMute);
}
Ui.requestUpdate();
return true;
}
return true;
}
}
class QuitDelegate extends Ui.ConfirmationDelegate {
// Handles user response to the quit confirmation dialog.
var mResponseReceived as Boolean;
var mSdView as GarminSDView;
var mSdState as GarminSDState;
function initialize(sdView as GarminSDView, sdState as GarminSDState) {
writeLog("QuitDelegate.initialize()", "");
mSdView = sdView;
mSdState = sdState;
Ui.ConfirmationDelegate.initialize();
mResponseReceived = false;
}
function onResponse(value) {
writeLog("QuitDelegate.onResponse()", "Response = " + value);
mResponseReceived = true;
if (value == CONFIRM_YES) {
// pop the confirmation dialog associated with this delegate
writeLog("quitDelegate.onResponse()", "Exiting app");
Ui.popView(Ui.SLIDE_IMMEDIATE);
} else {
// the system will automatically pop the top level dialog so we do not have to close it ourselves
writeLog("quitDelegate.onResponse()", "Closing quit dalog and returning to running state");
mSdState.setMode(MODE_RUNNING);
}
return true;
}
}
//! The app settings menu
class GarminSDSettingsMenu extends Ui.Menu2 {
//! Constructor
public function initialize() {
Menu2.initialize({ :title => "Settings" });
}
}
//! Input handler for the app settings menu
class GarminSDSettingsMenuDelegate extends Ui.Menu2InputDelegate {
var mAccelHandler as GarminSDDataHandler;
//! Constructor
public function initialize(accelHandler as GarminSDDataHandler) {
Menu2InputDelegate.initialize();
mAccelHandler = accelHandler;
}
//! Handle a menu item being selected
//! @param menuItem The menu item selected
public function onSelect(menuItem as Ui.MenuItem) as Void {
if (menuItem instanceof Ui.ToggleMenuItem) {
writeLog("menuDelegate.onSelect()", "id=" + menuItem.getId());
Storage.setValue(menuItem.getId() as Lang.Number, menuItem.isEnabled());
}
}
}