-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeedo.html
354 lines (302 loc) · 8.61 KB
/
speedo.html
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
<!DOCTYPE html>
<html lang="en-AU">
<head>
<meta id="vp" name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
<meta name="author" content="Harris Hudson. [email protected]">
<title>Speedo by Harris Hudson</title>
<meta name="description" content="Speedometer (with HUD mode and limit warning) by Harris Hudson">
<meta id="application-name" name="application-name" content="Speedo by Harris Hudson - Released December 2022">
<meta charset="UTF-8">
<link rel="icon" href="favicon.svg">
<style>
body { padding: 0px; margin: 0px; background-color: lightgrey}
html, body { height: 100%; width: 100% }
</style>
</head>
<body id="bd">
<svg width="100%" height="100%" viewBox="0 0 30 20" pointer-events="none">
<text id="h" x="15" y="3.5" font-size="3" fill="black" opacity="0" text-anchor="middle" pointer-events="none">HUD Mode</text>
<text id="s" x="15" y="15" font-size="100%" fill="blue" stroke="none" text-anchor="middle" transform="" pointer-events="none">??</text>
</svg>
<script>
"use strict";
// Add manifest if not called in file context
if (window.location.protocol!='file:')
document.head.innerHTML+='<link rel="manifest" href="speedo.webmanifest" crossorigin="annonymous"/>';
// Setup for offline use
if ('serviceWorker' in navigator) {
try {
navigator.serviceWorker.register('speedo_service_worker.js').catch(function(e) {
console.info('Info Only - Failed to register service worker; speedo_service_worker.js') });
} catch(e) { console.info('Info Only - Failed to register service worker; speedo_service_worker.js') }
}
var gGEOLOCATION_THROTTLE=false
var gORIENTATION_THROTTLE=false
var gHUD_MODE=false
var gHUD_WARNING_TOGGLE=true
var gTHE_COLOR="blue"
var gCLICK_LATCH=0
var gALLOW_WARNING_VOICE=true
var gWARNING_LIMIT=null
var gWARNING_THROTTLE=false
var gGUESSED_UNITS='kmh'
function show_speed(the_speed) {
try {
the_speed = parseInt(the_speed)
} catch(e) {
the_speed = '??'
}
var s=document.querySelector("#s");
if (!gHUD_MODE)
s.setAttribute("fill",gTHE_COLOR);
s.innerHTML=the_speed;
}
var t=navigator.geolocation.watchPosition(
geoupdate,
geofailure,
{enableHighAccuracy: true})
function geoupdate(e) {
if (gGEOLOCATION_THROTTLE) return;
gGEOLOCATION_THROTTLE=true;
window.setTimeout(function(){gGEOLOCATION_THROTTLE=false;},200);
var s=e.coords.speed;
var the_speed;
if (e.coords.latitude < 0)
gGUESSED_UNITS='kmh'
else
gGUESSED_UNITS='mph'
var the_units = localStorage['UNITS'] || gGUESSED_UNITS
if (the_units == 'mph') {
gTHE_COLOR="darkgreen";
the_speed=parseInt(s*2.236936); //mph
}
else {
gTHE_COLOR="blue";
the_speed=parseInt(s*3.6); //kmh
}
show_speed(the_speed);
if ((gWARNING_LIMIT != null) && (the_speed > gWARNING_LIMIT))
announce_warning(the_speed)
}
function announce() {
var x=document.querySelector("#s");
var blurb=x.innerHTML
if (blurb=='??') blurb='Unknown'
gALLOW_WARNING_VOICE=false
speak(blurb, function() { gALLOW_WARNING_VOICE=true } );
}
function announce_warning(the_speed) {
if (!gALLOW_WARNING_VOICE) return
if (gWARNING_THROTTLE) return
gWARNING_THROTTLE=true
window.setTimeout(function(){ gWARNING_THROTTLE=false }, 10000)
speak('Warning '+the_speed.toString())
}
function geofailure(e) {
show_speed('??');
console.log('Geolocation error ...')
console.log(e)
}
function remove_hud_warning() {
var h=document.querySelector("#h");
h.setAttribute("opacity","0");
}
function show_hud_warning() {
var h=document.querySelector("#h");
h.setAttribute("opacity","1");
window.setTimeout(remove_hud_warning,600);
}
function handleOrientation(e) {
if (gORIENTATION_THROTTLE) return;
gORIENTATION_THROTTLE=true;
window.setTimeout(function(){gORIENTATION_THROTTLE=false;},200);
var b=Math.abs(event.beta);
var g=Math.abs(event.gamma);
if ((!b)&&(!g))
return;
var s=document.querySelector("#s");
var bd=document.querySelector("#bd");
if ((b < 18) && (g < 18)) {
// HUD mode
gHUD_MODE=true
s.setAttribute("transform","scale(1,-1) translate(0,-20)");
s.setAttribute("fill","black");
bd.style.backgroundColor="white";
//HUD warning
if (gHUD_WARNING_TOGGLE) {
gHUD_WARNING_TOGGLE=false;
show_hud_warning();
}
} else {
// Non HUD mode
gHUD_MODE=false
s.setAttribute("transform","");
//s.setAttribute("fill","blue");
s.setAttribute("fill",gTHE_COLOR);
bd.style.backgroundColor="lightgrey";
gHUD_WARNING_TOGGLE=true;
remove_hud_warning();
}
}
function check_latch() {
if (gCLICK_LATCH==1) {
console.log('Single Click')
announce();
}
if (gCLICK_LATCH==2) {
console.log('Double Click')
ask_for_limit();
}
if (gCLICK_LATCH==3) {
console.log('Triple Click')
ask_for_unit_change();
}
clear_latch()
}
function clear_latch() {
console.log('Clear Latch')
gCLICK_LATCH=0
}
function set_latch(e) {
gCLICK_LATCH++
e.preventDefault()
window.setTimeout(check_latch, 700)
window.setTimeout(clear_latch, 1400)
}
function ask_for_limit() {
gWARNING_LIMIT=null
gALLOW_WARNING_VOICE=false
ask('What speed limit warning value?', function (end, result) {
if (end) {
if (gWARNING_LIMIT != null) {
speak('Setting speed warning to '+gWARNING_LIMIT.toString(), function() {gALLOW_WARNING_VOICE=true});
console.log('Speed limit warning = '+gWARNING_LIMIT.toString())
} else
speak('Speed warning turned off.', function() {gALLOW_WARNING_VOICE=true});
}
if (result && result.transcript) {
try {
gWARNING_LIMIT=parseInt(result.transcript)
} catch(e) {
gWARNING_LIMIT=null
}
if (isNaN(gWARNING_LIMIT))
gWARNING_LIMIT=null
}
})
}
function ask_for_unit_change() {
var the_units=localStorage['UNITS']||gGUESSED_UNITS
let new_units=toggle_units(the_units)
gALLOW_WARNING_VOICE=false
let the_question='To change units to '+new_units.literal+', now say, change'
var units_response=null
ask(the_question, function (end, result) {
if (end) {
if (units_response == 'change') {
speak('Changing units to '+new_units.literal, function() {gALLOW_WARNING_VOICE=true})
console.log('Changing units to '+new_units.literal)
localStorage['UNITS']=new_units.units
} else {
speak('Leaving Units unchanged as '+toggle_units(toggle_units(the_units).units).literal, function() {gALLOW_WARNING_VOICE=true})
}
}
if (result && result.transcript) {
try {
units_response=result.transcript.split(' ')[0].toLowerCase(); //Get first word only lower case
} catch(e) {
units_response=null;
}
}
})
}
function ask(text, callback) {
speak(text, function () {
// get answer
var recognition;
try {
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
recognition = new SpeechRecognition();
} catch(e) { return; }
recognition.continuous = false;
recognition.interimResults = true;
//recognition.lang='en-US';
//recognition.lang='en-GB';
recognition.lang='en-AU';
recognition.onend = function (e) {
if (callback) {
callback('end');
}
}
recognition.onresult = function (e) {
// cancel onend handler
//recognition.onend = null;
if (callback) {
callback(null, {
transcript: e.results[0][0].transcript,
confidence: e.results[0][0].confidence
})
}
}
// start listening
recognition.start();
})
}
function speak(text, callback) {
try {
var u = new SpeechSynthesisUtterance();
u.text = text;
//u.lang = 'en-US';
//u.lang='en-GB';
u.lang='en-AU';
u.onend = function () {
if (callback) {
callback();
}
}
u.onerror = function (e) {
if (callback) {
callback(e);
}
}
speechSynthesis.speak(u);
} catch(e) {}
}
function toggle_units(the_units) {
if (the_units=='mph')
return {units:'kmh', literal:'kilometers per hour'}
else
return {units:'mph', literal:'miles per hour'}
}
document.body.addEventListener('click', set_latch)
window.addEventListener("deviceorientation", handleOrientation, true);
// The wake lock sentinel.
let wakeLock = null;
// Function that attempts to request a screen wake lock.
const requestWakeLock = async () => {
try {
wakeLock = await navigator.wakeLock.request();
wakeLock.addEventListener('release', () => {
console.log('Screen Wake Lock released:', wakeLock.released);
})
console.log('Screen Wake Lock released:', wakeLock.released);
} catch (err) {
console.error(`${err.name}, ${err.message}`);
}
}
// Request a screen wake lock…
requestWakeLock();
// Re-request after visibility change
document.addEventListener('visibilitychange', async () => {
if (wakeLock !== null && document.visibilityState === 'visible') {
requestWakeLock();
}
})
// and release it again after 5s.
//window.setTimeout(() => {
// wakeLock.release();
// wakeLock = null;
//}, 5000);
</script>
</body>
</html>