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
|
import nipplejs from 'nipplejs';
import hoverControlModule from './hoverControlModule';
import uBitBLE from "./uBit";
import { notif_alert, notif_warn, notif_info, notif_success } from './notification';
let sw = "service-worker.js";
if (navigator.serviceWorker) {
navigator.serviceWorker.register(
sw, {scope: '/hoverbit-ble/'}
).then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) { return; }
installingWorker.onstatechange = () => {
if (installingWorker.state === "installed") {
if (navigator.serviceWorker.controller) {
notif_info("New content is available, relaunch the app to install it.");
} else {
notif_success("Content is cached for offline use.");
}
}
};
};
registration.update();
}).catch(error => {
notif_alert("Could not install service worker...");
console.error("Error during service worker registration:", error);
});
}
document.getElementById("btn_ignore_landscape_warning").addEventListener("click", () => {
document.body.classList.add("ignore-landscape-warning");
});
if (!navigator.bluetooth) {
alert("Bluetooth not enabled in your browser, this won't work...");
}
/* Define and initialize things */
let ubit = new uBitBLE();
let hoverControl = new hoverControlModule();
window.ubit = ubit;
window.hoverControl = hoverControl;
let joystickLeft = nipplejs.create({
zone: document.querySelector(".joystick-left"),
size: 200,
position: {left: '50%', bottom: '50%'},
mode: "static",
lockX: true
});
let joystickRight = nipplejs.create({
zone: document.querySelector(".joystick-right"),
size: 200,
position: {left: '50%', bottom: '50%'},
mode: "static",
lockY: true
});
/* Setup event_listeners */
joystickLeft.on("move", (evt, data) => {
let rudder = ((data.distance * 90) / 100);
if (data.angle.degree > 90) { rudder = rudder * -1; }
hoverControl.setRudder(Math.round(rudder));
});
joystickLeft.on("end", (evt, data) => {
hoverControl.setRudder(0);
});
joystickRight.on("move", (evt, data) => {
let throttle = data.distance;
if (data.angle.degree > 90) {
throttle = 0;
if (data.distance >= 100) {
hoverControl.reset();
if (hoverControl.getArm()) {
notif_alert("Detected panicy reducing of throttle, dearming...");
}
}
}
hoverControl.setThrottle(Math.round(throttle));
});
joystickRight.on("end", (evt, data) => {
hoverControl.setThrottle(0);
});
document.getElementById("btn_arm").addEventListener("click", () => {
hoverControl.setArm(true);
});
document.getElementById("btn_disarm").addEventListener("click", () => {
hoverControl.setArm(false);
});
document.querySelector("#btn_disconnect").addEventListener("click", () => {
hoverControl.reset();
ubit.disconnect();
});
document.getElementById("btn_connect").addEventListener("click", () => {
ubit.searchDevice();
});
ubit.onConnect(() => {
document.body.classList.add("connected");
});
ubit.onDisconnect(() => {
document.body.classList.remove("connected");
document.body.classList.remove("armed");
});
ubit.onUartTx((text) => {
/* Just make the ping symbol reappear. */
var elm = document.querySelector(".ping i");
var newone = elm.cloneNode(true);
elm.parentNode.replaceChild(newone, elm);
/* Actually handle received text. */
// console.log(`Received: ${text}`);
if ((text).indexOf(":") != -1) {
let parts = (text).split(":");
if (parts[0] == "B") {
document.querySelector(".battery-status").innerHTML = parts[1] + "mV";
} else if (parts[0] == "ACC") {
hoverControl.acc(parts[1]);
} else {
console.log(parts);
}
} else {
notif_warn("Received weird data from MICRO:BIT...");
console.log(`Received unknown: ${text}`);
}
});
let intervalSendCommands = setInterval(async() => {
if (ubit.isConnected()) {
let command =
"T" + hoverControl.getThrottle().toString() +
"R" + hoverControl.getRudder().toString() +
"A" + (hoverControl.getArm() ? "1" : "0") +
"S0" +
":";
await ubit.sendUart(command);
// console.log(`Sent: ${command}`);
}
}, 100);
|