-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.js
executable file
·130 lines (115 loc) · 4.22 KB
/
application.js
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
(function() {
'use strict';
document.addEventListener('DOMContentLoaded', e => {
let connectButton = document.querySelector("#connect");
let statusDisplay = document.querySelector('#status');
let port;
$("#form1 :input").prop("disabled", true);
// check if WebUSB is supported
if ("usb" in navigator)
{ console.log("has WebUSB support"); }
else {
alert("WebUSB not supported: Please use Google Chrome");
}
// listen for form input changes and save them to the device
$('#form1 input').on('change', function() {
var orientation = $('input[name=orientation]:checked', '#form1').val();
var baro_calibration = $("input#seaPressureInput").val();
var min_batt_v = $("input#minBattInput").val();
var max_batt_v = $("input#maxBattInput").val();
var metric_temp = $("#units-temp").prop("checked");
var metric_alt = $("#units-alt").prop("checked");
var usb_json = {
"major_v" : 4,
"minor_v" : 1,
"screen_rot": orientation,
"sea_pressure": parseFloat(baro_calibration),
"metric_temp": metric_temp,
"metric_alt": metric_alt,
"min_batt_v": min_batt_v,
"max_batt_v": max_batt_v
}
console.log("sending", usb_json);
sendJSON(usb_json);
$("#saved-status").removeClass("blink");
$("#saved-status").width(); // trigger a DOM reflow
$("#saved-status").addClass("blink");
});
document.querySelector("button#bl").addEventListener('click', function(){
let bl_command_json = { "command": "rbl" };
console.log("sending", bl_command_json);
sendJSON(bl_command_json);
disconnect();
});
function connect() {
port.connect().then(() => {
statusDisplay.textContent = '';
connectButton.textContent = 'Disconnect';
$("#form1 :input").prop("disabled", false);
port.onReceive = data => {
let textDecoder = new TextDecoder();
var usb_input = textDecoder.decode(data);
if (usb_input.length < 5) { return };
var usb_parsed = JSON.parse(usb_input); // TODO figure out why empty data is sent
$("#armedTime").text(display(usb_parsed["armed_time"]));
$("#deviceId").text(usb_parsed["device_id"]);
$("#versionMajor").text(usb_parsed["major_v"]);
$("#versionMinor").text(usb_parsed["minor_v"]);
$("#orientation-lh").prop("checked", usb_parsed["screen_rot"] == 2);
$("#orientation-rh").prop("checked", usb_parsed["screen_rot"] == 0);
$("#units-temp").prop("checked", usb_parsed["metric_temp"]);
$("#units-alt").prop("checked", usb_parsed["metric_alt"]);
$("#seaPressureInput").val(usb_parsed["sea_pressure"]);
$("#minBattInput").val(usb_parsed["min_batt_v"]);
$("#maxBattInput").val(usb_parsed["max_batt_v"]);
console.log("received", usb_input);
};
port.onReceiveError = error => {
console.error(error);
};
}, error => {
displayError(error)
});
}
function displayError(error){
console.log(error);
statusDisplay.textContent = error.message;
}
function display (minutes) {
const format = val => `0${Math.floor(val)}`.slice(-2)
const hours = minutes / 60
return [hours, minutes % 60].map(format).join(':')
}
connectButton.addEventListener('click', function() {
if (port) {
disconnect();
} else {
serial.requestPort().then(selectedPort => {
port = selectedPort;
connect();
}).catch(error => {
displayError(error);
});
}
});
serial.getPorts().then(ports => {
if (ports.length === 0) {
statusDisplay.textContent = 'No device found.';
} else {
statusDisplay.textContent = 'Connecting...';
port = ports[0];
connect();
}
});
function disconnect(){
port.disconnect();
$("#form1 :input").prop("disabled", true);
connectButton.textContent = 'Connect';
statusDisplay.textContent = '';
port = null;
}
function sendJSON(usb_json){
port.send(new TextEncoder('utf-8').encode(JSON.stringify(usb_json)));
}
});
})();