/
harbour-laufhelden.qml
624 lines (524 loc) · 19.6 KB
/
harbour-laufhelden.qml
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
* Copyright (C) 2017 Jens Drescher, Germany
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.0
import Sailfish.Silica 1.0
import QtSensors 5.0 as Sensors
import "pages"
import "tools"
import QtFeedback 5.0
import QtMultimedia 5.0 as Media
import harbour.laufhelden 1.0
ApplicationWindow
{
id: appWindow
onApplicationActiveChanged:
{
console.log("onApplicationActiveChanged: " + applicationActive);
}
//Define global variables
//*** HRM Start ***
property bool bHRMConnected: false //the connection state to the HRM device
property bool bHRMConnecting: false
property bool bReconnectHRMDevice: false //HRM device has lost connection, reconnect
property bool bRecordDialogRequestHRM: false
property string sHeartRate: ""
property string sBatteryLevel: ""
property string sHRMAddress: ""
property string sHRMDeviceName: ""
property string sHeartRateHexString: ""
//*** HRM End ***
property var vMainPageObject //this is used for back jumps (pop) to the MainPage
property bool bLoadHistoryData: true //this is set on record page after a workout, to have mainpage load GPX files
property int iVibrationCounter: 0 //this is used for the vibration function
property bool bPlayerWasPlaying: false //this is used if playing music needs to be resumed after audio output
//*** Pebble Start ***
property bool bPebbleConnected: false
property bool bPebbleSportAppRequired: false
property string sPebblePath: ""
property string sPebbleNameAddress: ""
//*** Pebble End ***
property real rLastAccuracy: -1
property bool bPlayingSound: false
property int iPlayLoop: 0
property variant arrayPlaySounds: 0
//property bool bApplicationIsActive: fal
//Init C++ classes, libraries
HistoryModel{ id: id_HistoryModel }
Device{ id: id_Device }
LogWriter{ id: id_LogWriter }
Settings{ id: settings }
PlotWidget{ id: id_PlotWidget }
Light{ id: id_Light }
PebbleManagerComm{ id: id_PebbleManagerComm }
PebbleWatchComm{ id: id_PebbleWatchComm }
TrackRecorder
{
id: recorder
updateInterval: settings.updateInterval
onPauseChanged:
{
if (!settings.voicePauseContinueWorkout)
return;
var sVoiceLanguage = "_en_male.wav";
//check voice language and generate last part of audio filename
if (settings.voiceLanguage === 0) //english male
sVoiceLanguage = "_en_male.wav";
else if (settings.voiceLanguage === 1) //german male
sVoiceLanguage = "_de_male.wav";
if (pause)
fncPlaySound("audio/break_workout" + sVoiceLanguage);
else
fncPlaySound("audio/continue_workout" + sVoiceLanguage);
}
onRunningChanged:
{
if (!settings.voiceStartEndWorkout)
return;
var sVoiceLanguage = "_en_male.wav";
//check voice language and generate last part of audio filename
if (settings.voiceLanguage === 0) //english male
sVoiceLanguage = "_en_male.wav";
else if (settings.voiceLanguage === 1) //german male
sVoiceLanguage = "_de_male.wav";
if (running)
fncPlaySound("audio/start_workout" + sVoiceLanguage);
else
fncPlaySound("audio/end_workout" + sVoiceLanguage);
}
onAccuracyChanged:
{
if (!settings.voiceGPSConnectLost)
return;
var sVoiceLanguage = "_en_male.wav";
//check voice language and generate last part of audio filename
if (settings.voiceLanguage === 0) //english male
sVoiceLanguage = "_en_male.wav";
else if (settings.voiceLanguage === 1) //german male
sVoiceLanguage = "_de_male.wav";
if (accuracy < 30 && accuracy !== -1 && (rLastAccuracy >= 30 || rLastAccuracy === -1))
{
fncPlaySound("audio/gps_connected" + sVoiceLanguage);
}
if ((accuracy >= 30 || accuracy === -1) && rLastAccuracy < 30 && rLastAccuracy !== -1)
{
fncPlaySound("audio/gps_disconnected" + sVoiceLanguage);
}
rLastAccuracy = accuracy;
}
}
//These are connections to c++ events
Connections
{
target: id_Device
onSigReadDataReady: //This is called from C++ if there is data via bluetooth
{
fncCheckHeartrate(sData);
}
onSigHRMDataReady: // This gets HRM data from BTLE devices
{
fncCheckHeartrateBTLE(sData)
}
onSigBATDataReady: // This gets battery data from BTLE devices
{
fncCheckBatteryLevelBTLE(sData)
}
onSigConnected:
{
fncShowMessage(2,"HRM Connected", 4000);
bHRMConnected = true;
bHRMConnecting = false;
}
onSigDisconnected:
{
fncShowMessage(1,"HRM Disconnected", 4000);
sHeartRate = "";
sBatteryLevel = "";
bHRMConnected = false;
bHRMConnecting = false;
recorder.vSetCurrentHeartRate(-1);
//if record dialog is opened, try to reconnect to HRM device
if (bRecordDialogRequestHRM)
bReconnectHRMDevice = true;
}
onSigConnecting:
{
fncShowMessage(2,qsTr("Connecting to HR device..."),2000);
bHRMConnecting = true;
bHRMConnected = false;
}
onSigError:
{
fncShowMessage(3,qsTr("HR error: ") + sError, 5000);
bHRMConnected = false;
bHRMConnecting = false;
}
}
function fncCheckHeartrate(sData)
{
var sHeartRateTemp = 0;
var sBatteryLevelTemp = 0;
var iPacketLength = 0;
//Save received data to packet string. This must be done because a packet is not always consistent.
sHeartRateHexString = sHeartRateHexString + sData.toLowerCase();
//console.log("sHeartRateHexString: " + sHeartRateHexString);
//Check for minimal length
if (sHeartRateHexString.length < 8)
{
//console.log("Packet is too small!");
return;
}
//Search for vaid telegrams
//Check for Zephyr control characters start and enddelimiter
//console.log("Header: " + sHeartRateHexString.substr(0,4));
//console.log("Enddelimiter: " + sHeartRateHexString.substr(sHeartRateHexString.length - 2));
if (sHeartRateHexString.substr(0,4).indexOf("0226") !== -1 && sHeartRateHexString.substr(sHeartRateHexString.length-2).indexOf("03") !== -1)
{
//This should be a Zyphyr packet
//console.log("Valid Zepyhr HxM data packet found!");
//Extract length
iPacketLength = parseInt(sHeartRateHexString.substr(4,2),16);
//console.log("Length: " + iPacketLength.toString());
//Extract CRC
var sCRC = sHeartRateHexString.substr(-4);
sCRC = sCRC.substr(0,2);
//console.log("CRC: " + sCRC);
//Extract heart rate data
sHeartRateHexString = sHeartRateHexString.substring(6,sHeartRateHexString.length - 4);
//console.log("HR data: " + sHeartRateHexString);
//console.log("HR data length: " + sHeartRateHexString.length);
//Check if length match is given
if (sHeartRateHexString.length !== (iPacketLength*2))
{
//console.log("Length does not match, scrap packet!");
sHeartRateHexString = "";
return;
}
//Check if data is valid by CRC
//Man the example is in C )-: do it later...
//Extract battery level
sBatteryLevelTemp = (parseInt(sHeartRateHexString.substr(16,2),16)).toString();
//console.log("Battery level: " + sBatteryLevelTemp);
//Extract heart rate at byte 12
sHeartRateTemp = (parseInt(sHeartRateHexString.substr(18,2),16)).toString();
//console.log("Heartrate: " + sHeartRateTemp);
//If we found a valid packet, delete the packet memory string
sHeartRateHexString = "";
}
else if (sHeartRateHexString.substr(0,2).indexOf("fe") !== -1)
{
//This should be a POLAR packet
//Check if packet is at correct length
iPacketLength = parseInt(sHeartRateHexString.substr(2,2), 16);
//console.log("iPacketLength: " + iPacketLength);
if (sHeartRateHexString.length < (iPacketLength * 2))
{
sHeartRateHexString = "";
return; //Packet is not big enough
}
//Check check byte, 255 - packet length
var iCheckByte = parseInt(sHeartRateHexString.substr(4,2), 16);
//console.log("iCheckByte: " + iCheckByte);
if (iCheckByte !== (255 - iPacketLength))
{
sHeartRateHexString = "";
//console.log("Check byte is not valid!");
return; //Check byte is not valid
}
//Check sequence valid
var iSequenceValid = parseInt(sHeartRateHexString.substr(6,2), 16);
//console.log("iSequenceValid: " + iSequenceValid);
if (iSequenceValid >= 16)
{
sHeartRateHexString = "";
return; //Sequence valid byte is not valid
}
//Check status byte
var iStatus = parseInt(sHeartRateHexString.substr(8,2), 16);
//console.log("iStatus: " + iStatus);
//Check battery state
sBatteryLevelTemp = parseInt(sHeartRateHexString.substr(8,1), 16);
//console.log("iBattery: " + sBatteryLevelTemp);
//Extract heart rate
sHeartRateTemp = (parseInt(sHeartRateHexString.substr(10,2), 16)).toString();
//console.log("HeartRateTemp: " + sHeartRateTemp);
var sTemp = ((100/15) * sBatteryLevelTemp).toString();
if (sTemp.indexOf(".") != -1)
sTemp = sTemp.substring(0, sTemp.indexOf("."));
sBatteryLevelTemp = sTemp;
//Extraction was successful here. Reset message text var.
//Only kill the bytes for this packet. There might be more bytes after this packet.
if (sHeartRateHexString.length > (iPacketLength * 2))
{
sHeartRateHexString = sHeartRateHexString.substring((iPacketLength * 2));
//console.log("Found additional data: " + sHeartRateHexString);
fncCheckHeartrate("");
}
else
sHeartRateHexString = "";
}
else
{
//We have a strange start delimiter. Kill data...
//console.log("Strange data found. Kill data.");
sHeartRateHexString = "";
}
//Send heart rate to trackrecorderiHeartRate so that it can be included into the gpx file.
recorder.vSetCurrentHeartRate(parseInt(sHeartRateTemp));
sHeartRate = sHeartRateTemp;
sBatteryLevel = sBatteryLevelTemp;
}
function fncCheckHeartrateBTLE(sData)
{
//Send heart rate to trackrecorderiHeartRate so that it can be included into the gpx file.
recorder.vSetCurrentHeartRate(parseInt(sData));
sHeartRate = sData;
}
function fncCheckBatteryLevelBTLE(sData)
{
sBatteryLevel = sData;
}
function fncShowMessage(iType ,sMessage, iTime)
{
messagebox.showMessage(iType, sMessage, iTime);
}
Sensors.OrientationSensor
{
id: rotationSensor
active: true
property int angle: reading.orientation ? _getOrientation(reading.orientation) : 0
function _getOrientation(value)
{
switch (value)
{
case 2:
return 180
case 3:
return -90
case 4:
return 90
default:
return 0
}
}
}
Messagebox
{
id: messagebox
rotation: rotationSensor.angle
width: Math.abs(rotationSensor.angle) == 90 ? parent.height : parent.width
Behavior on rotation { SmoothedAnimation { duration: 500 } }
Behavior on width { SmoothedAnimation { duration: 500 } }
}
Timer
{
//This is called if the connection to the HRM device is broken
id: timReconnectHRMdevice
interval: 2000
running: bReconnectHRMDevice
repeat: false
onTriggered:
{
//console.log("Timer for HRM reconnection is running.");
id_Device.scanServices(sHRMAddress);
bReconnectHRMDevice = false;
}
}
Timer
{
//This is called if the connection to the HRM device is broken
id: timVibrationTimer
interval: 1000
running: iVibrationCounter > 0
repeat: iVibrationCounter > 0
onTriggered:
{
vibrateEffect.start();
iVibrationCounter--;
}
}
//iVibrationCount: amount of vibrations in series
//iVibrationSpeed: duration of vibrations and pause between the vibrations
function fncVibrate(iVibrationCount, iVibrationDuration)
{
//if there is a vibration process running, return
if (iVibrationCounter !== 0 || iVibrationCount === 0 || iVibrationDuration === 0)
return;
vibrateEffect.duration = iVibrationDuration;
timVibrationTimer.interval = (iVibrationDuration * 2);
//Start first vibration
vibrateEffect.start();
iVibrationCount--;
if (iVibrationCount === 0)
return;
//Start timer because we need some more vibrations
iVibrationCounter = iVibrationCount;
}
HapticsEffect
{
id: vibrateEffect
intensity: 1
duration: 200
}
function fncEnableScreenBlank(bEnableScreenBlank)
{
screenblank.enabled = bEnableScreenBlank;
}
ScreenBlank
{
id: screenblank
}
MediaPlayerControl
{
id: mediaPlayerControl
}
PebbleComm
{
id: pebbleComm
}
Media.Audio
{
id: playSoundEffect
source: "audio/hr_toohigh_de_male.wav"
onPlaybackStateChanged:
{
//console.log("onPlaybackStateChanged: " + playbackState.toString());
//Check if playing a sound is done.
if (playbackState == 0)
{
//Set index to next sound in array
iPlayLoop++;
//console.log("iPlayLoop: " + iPlayLoop.toString());
//Check if we are ready with playing sounds, all sounds in the array were played.
if (arrayPlaySounds.length === 0 || iPlayLoop >= arrayPlaySounds.length)
{
//Check if the system audio player was playing before
if (bPlayerWasPlaying)
{
//Resume system audio player
mediaPlayerControl.resume();
}
//We are done now with playing sounds. Mark that.
bPlayingSound = false;
//console.log("onPlayingChanged, the END!");
}
else
{
//There is still something to play in the array. Restart play timer.
timerPlaySoundArray.start();
//console.log("onPlayingChanged, starting timer!");
}
}
}
}
//This is not used because the onPlayingChanged often times is not fired after an audio file is played
Media.SoundEffect
{
id: playSoundEffect2
source: "audio/hr_toohigh_de_male.wav"
volume: 1.0; //Full 1.0
onPlayingChanged:
{
//console.log("onPlayingChanged: " + playing.toString());
//Check if playing a sound is done.
if (playing == false)
{
//Set index to next sound in array
iPlayLoop++;
//console.log("iPlayLoop: " + iPlayLoop.toString());
//Check if we are ready with playing sounds, all sounds in the array were played.
if (arrayPlaySounds.length === 0 || iPlayLoop >= arrayPlaySounds.length)
{
//Check if the system audio player was playing before
if (bPlayerWasPlaying)
{
//Resume system audio player
mediaPlayerControl.resume();
}
//We are done now with playing sounds. Mark that.
bPlayingSound = false;
//console.log("onPlayingChanged, the END!");
}
else
{
//There is still something to play in the array. Restart play timer.
timerPlaySoundArray.start();
//console.log("onPlayingChanged, starting timer!");
}
}
}
}
function fncPlaySoundArray(arraySoundArray)
{
//Check if a sound is already playing. If so, return!
if (bPlayingSound)
return;
else
bPlayingSound = true;
//detect if SFOS music player is currently playing
if (settings.voicePauseMusic && mediaPlayerControl.getPlayerStatus() === "Playing")
{
bPlayerWasPlaying = true;
mediaPlayerControl.pause();
}
else
bPlayerWasPlaying = false;
arrayPlaySounds = arraySoundArray;
iPlayLoop = 0;
playSoundEffect.source = arrayPlaySounds[iPlayLoop];
playSoundEffect.play();
}
Timer
{
id: timerPlaySoundArray
running: false
repeat: false
interval: 25
onTriggered:
{
//console.log("timerPlaySoundArray: " + iPlayLoop.toString());
playSoundEffect.source = arrayPlaySounds[iPlayLoop];
playSoundEffect.play();
}
}
function fncPlaySound(sFile)
{
//Check if a sound is already playing. If so, return!
if (bPlayingSound)
return;
else
bPlayingSound = true;
sFile = "usr/share/harbour-laufhelden/voicefiles/" + sFile;
console.log(sFile);
var arTemp = [];
arrayPlaySounds = arTemp;
iPlayLoop = 0;
//detect if SFOS music player is currently playing
if (settings.voicePauseMusic && mediaPlayerControl.getPlayerStatus() === "Playing")
{
bPlayerWasPlaying = true;
mediaPlayerControl.pause();
}
else
bPlayerWasPlaying = false;
playSoundEffect.source = sFile;
playSoundEffect.play();
}
initialPage: Component { MainPage { } }
cover: Qt.resolvedUrl("cover/CoverPage.qml")
allowedOrientations: Orientation.All
_defaultPageOrientations: Orientation.All
}