mirror of https://github.com/espruino/BangleApps
btadv: fix menus, avoid magic numbers
parent
f8ef58b48b
commit
eaed0f8215
|
@ -1,5 +1,19 @@
|
|||
"use strict";
|
||||
var UPDATE_MILLISECONDS = 30 * 1000;
|
||||
var __assign = (this && this.__assign) || function () {
|
||||
__assign = Object.assign || function(t) {
|
||||
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||||
s = arguments[i];
|
||||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
||||
t[p] = s[p];
|
||||
}
|
||||
return t;
|
||||
};
|
||||
return __assign.apply(this, arguments);
|
||||
};
|
||||
var _a, _b;
|
||||
Bangle.loadWidgets();
|
||||
Bangle.drawWidgets();
|
||||
var ADVERT_MS = 30 * 1000;
|
||||
var acc;
|
||||
var bar;
|
||||
var gps;
|
||||
|
@ -23,8 +37,7 @@ var showMainMenu = function () {
|
|||
curMenu = cur;
|
||||
}; };
|
||||
mainMenu[""] = {
|
||||
"title": "-- btadv --",
|
||||
back: showMainMenu,
|
||||
"title": "BLE Advert",
|
||||
};
|
||||
mainMenu[""].scroll = mainMenuScroll;
|
||||
mainMenu["Acceleration"] = showMenu(accMenu, 0, "acc");
|
||||
|
@ -36,15 +49,18 @@ var showMainMenu = function () {
|
|||
E.showMenu(mainMenu);
|
||||
curMenu = "main";
|
||||
};
|
||||
var optionsCommon = {
|
||||
back: showMainMenu,
|
||||
};
|
||||
var accMenu = {
|
||||
"": { "title": "- Acceleration -" },
|
||||
"": __assign({ "title": "Acceleration -" }, optionsCommon),
|
||||
"Active": { value: "true (fixed)" },
|
||||
"x": { value: "" },
|
||||
"y": { value: "" },
|
||||
"z": { value: "" },
|
||||
};
|
||||
var barMenu = {
|
||||
"": { "title": "- Barometer -" },
|
||||
"": __assign({ "title": "Barometer" }, optionsCommon),
|
||||
"Active": {
|
||||
value: settings.barEnabled,
|
||||
onchange: function (v) { return updateSetting('barEnabled', v); },
|
||||
|
@ -54,7 +70,7 @@ var barMenu = {
|
|||
"Temp": { value: "" },
|
||||
};
|
||||
var gpsMenu = {
|
||||
"": { "title": "- GPS -" },
|
||||
"": __assign({ "title": "GPS" }, optionsCommon),
|
||||
"Active": {
|
||||
value: settings.gpsEnabled,
|
||||
onchange: function (v) { return updateSetting('gpsEnabled', v); },
|
||||
|
@ -66,7 +82,7 @@ var gpsMenu = {
|
|||
"HDOP": { value: "" },
|
||||
};
|
||||
var hrmMenu = {
|
||||
"": { "title": "- Heart Rate -" },
|
||||
"": __assign({ "title": "- Heart Rate -" }, optionsCommon),
|
||||
"Active": {
|
||||
value: settings.hrmEnabled,
|
||||
onchange: function (v) { return updateSetting('hrmEnabled', v); },
|
||||
|
@ -75,7 +91,7 @@ var hrmMenu = {
|
|||
"Confidence": { value: "" },
|
||||
};
|
||||
var magMenu = {
|
||||
"": { "title": "- Magnetometer -" },
|
||||
"": __assign({ "title": "Magnetometer" }, optionsCommon),
|
||||
"Active": {
|
||||
value: settings.magEnabled,
|
||||
onchange: function (v) { return updateSetting('magEnabled', v); },
|
||||
|
@ -132,26 +148,26 @@ var updateMenu = function () {
|
|||
}
|
||||
};
|
||||
var updateBleAdvert = function () {
|
||||
var _a, _b;
|
||||
var bleAdvert;
|
||||
if (!(bleAdvert = Bangle.bleAdvert))
|
||||
bleAdvert = Bangle.bleAdvert = {};
|
||||
if (hrm) {
|
||||
bleAdvert[0x180d] = undefined;
|
||||
bleAdvert["0x180d"] = undefined;
|
||||
if (NRF.getSecurityStatus().connected) {
|
||||
NRF.updateServices({
|
||||
0x180d: {
|
||||
0x2a37: {
|
||||
NRF.updateServices((_a = {},
|
||||
_a["0x180d"] = (_b = {},
|
||||
_b["0x2a37"] = {
|
||||
value: [0, hrm.bpm],
|
||||
notify: true,
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
_b),
|
||||
_a));
|
||||
return;
|
||||
}
|
||||
}
|
||||
var interval = UPDATE_MILLISECONDS;
|
||||
NRF.setAdvertising(Bangle.bleAdvert, {
|
||||
interval: interval,
|
||||
interval: ADVERT_MS,
|
||||
});
|
||||
};
|
||||
var encodeHrm = function () { return [0, hrm ? hrm.bpm : 0]; };
|
||||
|
@ -207,28 +223,30 @@ var updateSetting = function (name, value) {
|
|||
settings[name] = value;
|
||||
enableSensors();
|
||||
};
|
||||
NRF.setServices({
|
||||
0x180d: {
|
||||
0x2a37: {
|
||||
NRF.setServices((_a = {},
|
||||
_a["0x180d"] = (_b = {},
|
||||
_b["0x2a37"] = {
|
||||
value: encodeHrm(),
|
||||
readable: true,
|
||||
notify: true,
|
||||
},
|
||||
},
|
||||
}, {
|
||||
_b),
|
||||
_a), {
|
||||
advertise: [
|
||||
'180d',
|
||||
]
|
||||
});
|
||||
var updateServices = function () {
|
||||
NRF.updateServices({
|
||||
0x180d: {
|
||||
0x2a37: {
|
||||
var _a, _b;
|
||||
NRF.updateServices((_a = {},
|
||||
_a["0x180d"] = (_b = {},
|
||||
_b["0x2a37"] = {
|
||||
value: encodeHrm(),
|
||||
notify: true,
|
||||
}
|
||||
}
|
||||
});
|
||||
dog: 1,
|
||||
},
|
||||
_b),
|
||||
_a));
|
||||
};
|
||||
Bangle.on('accel', function (newAcc) { return acc = newAcc; });
|
||||
Bangle.on('pressure', function (newBar) { return bar = newBar; });
|
||||
|
|
|
@ -1,4 +1,11 @@
|
|||
const UPDATE_MILLISECONDS = 30 * 1000;
|
||||
// TODO: emit other data beside HRM (via set/updateServices)
|
||||
|
||||
const enum Intervals {
|
||||
BLE_ADVERT = 30 * 1000,
|
||||
BLE = 1000,
|
||||
MENU_WAKE = 1000,
|
||||
MENU_SLEEP = 30 * 1000,
|
||||
}
|
||||
|
||||
let acc: undefined | AccelData;
|
||||
let bar: undefined | PressureData;
|
||||
|
@ -8,7 +15,6 @@ let mag: undefined | CompassData;
|
|||
|
||||
type BtAdvMenu = "acc" | "bar" | "gps" | "hrm" | "mag" | "main";
|
||||
let curMenu: BtAdvMenu = "main";
|
||||
let updateInterval: undefined | number;
|
||||
let mainMenuScroll = 0;
|
||||
const settings = {
|
||||
barEnabled: false,
|
||||
|
@ -28,8 +34,7 @@ const showMainMenu = () => {
|
|||
};
|
||||
|
||||
mainMenu[""] = {
|
||||
"title": "-- btadv --",
|
||||
back: showMainMenu,
|
||||
"title": "BLE Advert",
|
||||
};
|
||||
(mainMenu[""] as any).scroll = mainMenuScroll; // typehack
|
||||
|
||||
|
@ -44,8 +49,12 @@ const showMainMenu = () => {
|
|||
curMenu = "main";
|
||||
};
|
||||
|
||||
const optionsCommon = {
|
||||
back: showMainMenu,
|
||||
};
|
||||
|
||||
const accMenu = {
|
||||
"": { "title" : "- Acceleration -" },
|
||||
"": { "title" : "Acceleration -", ...optionsCommon },
|
||||
"Active": { value: "true (fixed)" },
|
||||
"x": { value: "" },
|
||||
"y": { value: "" },
|
||||
|
@ -53,7 +62,7 @@ const accMenu = {
|
|||
};
|
||||
|
||||
const barMenu = {
|
||||
"": { "title" : "- Barometer -" },
|
||||
"": { "title" : "Barometer", ...optionsCommon },
|
||||
"Active": {
|
||||
value: settings.barEnabled,
|
||||
onchange: (v: boolean) => updateSetting('barEnabled', v),
|
||||
|
@ -64,7 +73,7 @@ const barMenu = {
|
|||
};
|
||||
|
||||
const gpsMenu = {
|
||||
"": { "title" : "- GPS -" },
|
||||
"": { "title" : "GPS", ...optionsCommon },
|
||||
"Active": {
|
||||
value: settings.gpsEnabled,
|
||||
onchange: (v: boolean) => updateSetting('gpsEnabled', v),
|
||||
|
@ -77,7 +86,7 @@ const gpsMenu = {
|
|||
};
|
||||
|
||||
const hrmMenu = {
|
||||
"": { "title" : "- Heart Rate -" },
|
||||
"": { "title" : "- Heart Rate -", ...optionsCommon },
|
||||
"Active": {
|
||||
value: settings.hrmEnabled,
|
||||
onchange: (v: boolean) => updateSetting('hrmEnabled', v),
|
||||
|
@ -87,7 +96,7 @@ const hrmMenu = {
|
|||
};
|
||||
|
||||
const magMenu = {
|
||||
"": { "title" : "- Magnetometer -" },
|
||||
"": { "title" : "Magnetometer", ...optionsCommon },
|
||||
"Active": {
|
||||
value: settings.magEnabled,
|
||||
onchange: (v: boolean) => updateSetting('magEnabled', v),
|
||||
|
@ -149,8 +158,15 @@ const updateMenu = () => {
|
|||
}
|
||||
};
|
||||
|
||||
const enum BleServ {
|
||||
HRM = "0x180d",
|
||||
}
|
||||
const enum BleChar {
|
||||
HRM = "0x2a37",
|
||||
}
|
||||
|
||||
const updateBleAdvert = () => {
|
||||
let bleAdvert: { [key: number]: undefined };
|
||||
let bleAdvert: { [key: string]: undefined };
|
||||
|
||||
if (!(bleAdvert = (Bangle as any).bleAdvert))
|
||||
bleAdvert = (Bangle as any).bleAdvert = {};
|
||||
|
@ -168,13 +184,13 @@ const updateBleAdvert = () => {
|
|||
// }
|
||||
|
||||
if (hrm) {
|
||||
bleAdvert[0x180d] = undefined; // Advertise HRM
|
||||
bleAdvert[BleServ.HRM] = undefined; // Advertise HRM
|
||||
|
||||
// hack
|
||||
if (NRF.getSecurityStatus().connected) {
|
||||
NRF.updateServices({
|
||||
0x180d: {
|
||||
0x2a37: {
|
||||
[BleServ.HRM]: {
|
||||
[BleChar.HRM]: {
|
||||
value: [0, hrm.bpm],
|
||||
notify: true,
|
||||
}
|
||||
|
@ -192,14 +208,7 @@ const updateBleAdvert = () => {
|
|||
// mag = undefined;
|
||||
// }
|
||||
|
||||
const interval = UPDATE_MILLISECONDS; // / data.length;
|
||||
|
||||
NRF.setAdvertising(
|
||||
(Bangle as any).bleAdvert,
|
||||
{
|
||||
interval,
|
||||
},
|
||||
);
|
||||
NRF.setAdvertising((Bangle as any).bleAdvert);
|
||||
};
|
||||
|
||||
// {
|
||||
|
@ -288,8 +297,8 @@ const updateSetting = (
|
|||
|
||||
NRF.setServices(
|
||||
{
|
||||
0x180d: {
|
||||
0x2a37: {
|
||||
[BleServ.HRM]: {
|
||||
[BleChar.HRM]: {
|
||||
value: encodeHrm(),
|
||||
readable: true,
|
||||
notify: true,
|
||||
|
@ -306,8 +315,8 @@ NRF.setServices(
|
|||
|
||||
const updateServices = () => {
|
||||
NRF.updateServices({
|
||||
0x180d: {
|
||||
0x2a37: {
|
||||
[BleServ.HRM]: {
|
||||
[BleChar.HRM]: {
|
||||
value: encodeHrm(),
|
||||
notify: true,
|
||||
}
|
||||
|
@ -321,22 +330,27 @@ Bangle.on('GPS', newGps => gps = newGps);
|
|||
Bangle.on('HRM', newHrm => hrm = newHrm);
|
||||
Bangle.on('mag', newMag => mag = newMag);
|
||||
|
||||
enableSensors();
|
||||
// show menu first to have it reserve space for widgets (appRect)
|
||||
Bangle.loadWidgets();
|
||||
Bangle.drawWidgets();
|
||||
showMainMenu();
|
||||
enableSensors();
|
||||
|
||||
// setInterval(updateAdvert, 10000);
|
||||
setInterval(updateMenu, 1000);
|
||||
setInterval(updateBleAdvert, Intervals.BLE_ADVERT);
|
||||
|
||||
const menuInterval = setInterval(updateMenu, Intervals.MENU_WAKE);
|
||||
Bangle.on("lock", locked => {
|
||||
changeInterval(
|
||||
menuInterval,
|
||||
locked ? Intervals.MENU_SLEEP : Intervals.MENU_WAKE,
|
||||
);
|
||||
});
|
||||
|
||||
let serviceInterval: undefined | number;
|
||||
NRF.on("connect", () => {
|
||||
updateInterval = setInterval(updateServices, 1000);
|
||||
serviceInterval = setInterval(updateServices, Intervals.BLE);
|
||||
});
|
||||
NRF.on("disconnect", () => {
|
||||
clearInterval(updateInterval);
|
||||
updateInterval = undefined;
|
||||
clearInterval(serviceInterval);
|
||||
serviceInterval = undefined;
|
||||
});
|
||||
|
||||
// TODO debounce
|
||||
// TODO: emit other data beside HRM (via set/updateServices)
|
||||
// FIXME: ui overlap
|
||||
// Bangle.loadWidgets();
|
||||
// Bangle.drawWidgets();
|
||||
|
|
Loading…
Reference in New Issue