btadv: change info layout to plain text

pull/2587/head
Rob Pilling 2023-01-27 23:07:47 +00:00
parent c82e0f4e0a
commit bd3efc5f9f
2 changed files with 152 additions and 239 deletions

View File

@ -11,6 +11,8 @@ var __assign = (this && this.__assign) || function () {
return __assign.apply(this, arguments); return __assign.apply(this, arguments);
}; };
var Layout = require("Layout"); var Layout = require("Layout");
Bangle.loadWidgets();
Bangle.drawWidgets();
var services = ["0x180d", "0x181a", "0x1819"]; var services = ["0x180d", "0x181a", "0x1819"];
var acc; var acc;
var bar; var bar;
@ -32,6 +34,7 @@ var idToName = {
hrm: "HRM", hrm: "HRM",
mag: "Magnetometer", mag: "Magnetometer",
}; };
var infoFont = "6x8:2";
var colour = { var colour = {
on: "#0f0", on: "#0f0",
off: "#fff", off: "#fff",
@ -88,129 +91,71 @@ var btnLayout = new Layout({
}); });
var setBtnsShown = function (b) { var setBtnsShown = function (b) {
btnsShown = b; btnsShown = b;
g.clearRect(Bangle.appRect);
redraw(); redraw();
}; };
var infoFont = "6x8:2"; var drawInfo = function () {
var infoCommon = { var _a = Bangle.appRect, y = _a.y, x = _a.x, w = _a.w;
type: "txt", var mid = x + w / 2;
label: "", var drawn = false;
font: infoFont, g.reset()
pad: 5, .clearRect(Bangle.appRect)
}; .setFont(infoFont)
var infoLayout = new Layout({ .setFontAlign(0, -1);
type: "v",
c: [
{
type: "h",
c: [
__assign({ id: "bar_alti" }, infoCommon),
__assign({ id: "bar_pres" }, infoCommon),
__assign({ id: "bar_temp" }, infoCommon),
]
},
{
type: "h",
c: [
__assign({ id: "gps_lat" }, infoCommon),
__assign({ id: "gps_lon" }, infoCommon),
__assign({ id: "gps_altitude" }, infoCommon),
__assign({ id: "gps_satellites" }, infoCommon),
__assign({ id: "gps_hdop" }, infoCommon),
]
},
{
type: "h",
c: [
__assign({ id: "hrm_bpm" }, infoCommon),
__assign({ id: "hrm_confidence" }, infoCommon),
]
},
{
type: "h",
c: [
__assign({ id: "mag_x" }, infoCommon),
__assign({ id: "mag_y" }, infoCommon),
__assign({ id: "mag_z" }, infoCommon),
__assign({ id: "mag_heading" }, infoCommon),
]
},
__assign({ type: "btn", label: "Set", cb: function () {
setBtnsShown(true);
} }, btnStyle),
]
}, {
lazy: true,
});
var showElem = function (layout, s) {
layout.label = s;
delete layout.height;
};
var hideElem = function (layout) {
layout.height = 0;
};
var populateInfo = function () {
if (bar) { if (bar) {
showElem(infoLayout["bar_alti"], "".concat(bar.altitude.toFixed(1), "m")); g.drawString("".concat(bar.altitude.toFixed(1), "m"), mid, y);
showElem(infoLayout["bar_pres"], "".concat(bar.pressure.toFixed(1), "mbar")); y += g.getFontHeight();
showElem(infoLayout["bar_temp"], "".concat(bar.temperature.toFixed(1), "C")); g.drawString("".concat(bar.pressure.toFixed(1), "mbar"), mid, y);
} y += g.getFontHeight();
else { g.drawString("".concat(bar.temperature.toFixed(1), "C"), mid, y);
hideElem(infoLayout["bar_alti"]); y += g.getFontHeight();
hideElem(infoLayout["bar_pres"]); drawn = true;
hideElem(infoLayout["bar_temp"]);
} }
if (gps) { if (gps) {
showElem(infoLayout["gps_lat"], gps.lat.toFixed(4)); g.drawString("".concat(gps.lat.toFixed(4), " lat, ").concat(gps.lon.toFixed(4), " lon"), mid, y);
showElem(infoLayout["gps_lon"], gps.lon.toFixed(4)); y += g.getFontHeight();
showElem(infoLayout["gps_altitude"], "".concat(gps.alt, "m")); g.drawString("".concat(gps.alt, "m (").concat(gps.satellites, " sat)"), mid, y);
showElem(infoLayout["gps_satellites"], "".concat(gps.satellites)); y += g.getFontHeight();
showElem(infoLayout["gps_hdop"], "".concat((gps.hdop * 5).toFixed(1), "m")); drawn = true;
}
else {
hideElem(infoLayout["gps_lat"]);
hideElem(infoLayout["gps_lon"]);
hideElem(infoLayout["gps_altitude"]);
hideElem(infoLayout["gps_satellites"]);
hideElem(infoLayout["gps_hdop"]);
} }
if (hrm) { if (hrm) {
showElem(infoLayout["hrm_bpm"], "".concat(hrm.bpm)); g.drawString("".concat(hrm.bpm, " BPM (").concat(hrm.confidence, "%)"), mid, y);
showElem(infoLayout["hrm_confidence"], "".concat(hrm.confidence, "%")); y += g.getFontHeight();
} drawn = true;
else {
hideElem(infoLayout["hrm_bpm"]);
hideElem(infoLayout["hrm_confidence"]);
} }
if (mag) { if (mag) {
showElem(infoLayout["mag_x"], "".concat(mag.x)); g.drawString("".concat(mag.x, " ").concat(mag.y, " ").concat(mag.z), mid, y);
showElem(infoLayout["mag_y"], "".concat(mag.y)); y += g.getFontHeight();
showElem(infoLayout["mag_z"], "".concat(mag.z)); g.drawString("heading: ".concat(mag.heading.toFixed(1)), mid, y);
showElem(infoLayout["mag_heading"], mag.heading.toFixed(1)); y += g.getFontHeight();
drawn = true;
} }
else { if (!drawn) {
hideElem(infoLayout["mag_x"]); g.drawString("swipe to enable", mid, y);
hideElem(infoLayout["mag_y"]); y += g.getFontHeight();
hideElem(infoLayout["mag_z"]);
hideElem(infoLayout["mag_heading"]);
} }
}; };
var onTap = function (_) {
setBtnsShown(true);
};
var redraw = function () { var redraw = function () {
var layout;
if (btnsShown) { if (btnsShown) {
layout = btnLayout; if (!prevBtnsShown) {
prevBtnsShown = btnsShown;
Bangle.removeListener("swipe", onTap);
btnLayout.setUI();
btnLayout.forgetLazyState();
g.clearRect(Bangle.appRect);
}
btnLayout.render();
} }
else { else {
populateInfo(); if (prevBtnsShown) {
infoLayout.update();
layout = infoLayout;
}
if (btnsShown !== prevBtnsShown) {
prevBtnsShown = btnsShown; prevBtnsShown = btnsShown;
layout.forgetLazyState(); Bangle.setUI();
layout.setUI(); Bangle.on("swipe", onTap);
}
drawInfo();
} }
layout.render();
}; };
var encodeHrm = function (hrm) { var encodeHrm = function (hrm) {
return [0, hrm.bpm]; return [0, hrm.bpm];
@ -277,7 +222,7 @@ var enableSensors = function () {
mag = undefined; mag = undefined;
console.log("enableSensors():", settings); console.log("enableSensors():", settings);
}; };
var serviceActive = function (serv) { var haveServiceData = function (serv) {
switch (serv) { switch (serv) {
case "0x180d": return !!hrm; case "0x180d": return !!hrm;
case "0x181a": return !!(bar || mag); case "0x181a": return !!(bar || mag);
@ -297,6 +242,7 @@ var serviceToAdvert = function (serv, initial) {
}; };
if (hrm) { if (hrm) {
o.value = encodeHrm(hrm); o.value = encodeHrm(hrm);
hrm = undefined;
} }
return _a = {}, _a["0x2a37"] = o, _a; return _a = {}, _a["0x2a37"] = o, _a;
} }
@ -310,6 +256,7 @@ var serviceToAdvert = function (serv, initial) {
}; };
if (gps) { if (gps) {
o.value = encodeGps(gps); o.value = encodeGps(gps);
gps = undefined;
} }
return _b = {}, _b["0x2a67"] = o, _b; return _b = {}, _b["0x2a67"] = o, _b;
} }
@ -336,6 +283,7 @@ var serviceToAdvert = function (serv, initial) {
o["0x2a6c"].value = encodeElevation(bar); o["0x2a6c"].value = encodeElevation(bar);
o["0x2A1F"].value = encodeTemp(bar); o["0x2A1F"].value = encodeTemp(bar);
o["0x2a6d"].value = encodePressure(bar); o["0x2a6d"].value = encodePressure(bar);
bar = undefined;
} }
} }
if (mag || initial) { if (mag || initial) {
@ -346,6 +294,7 @@ var serviceToAdvert = function (serv, initial) {
}; };
if (mag) { if (mag) {
o["0x2aa1"].value = encodeMag(mag); o["0x2aa1"].value = encodeMag(mag);
mag = undefined;
} }
} }
return o; return o;
@ -357,7 +306,7 @@ var getBleAdvert = function (map, all) {
var advert = {}; var advert = {};
for (var _i = 0, services_1 = services; _i < services_1.length; _i++) { for (var _i = 0, services_1 = services; _i < services_1.length; _i++) {
var serv = services_1[_i]; var serv = services_1[_i];
if (all || serviceActive(serv)) { if (all || haveServiceData(serv)) {
advert[serv] = map(serv); advert[serv] = map(serv);
} }
} }
@ -385,12 +334,10 @@ Bangle.on('pressure', function (newBar) { return bar = newBar; });
Bangle.on('GPS', function (newGps) { return gps = newGps; }); Bangle.on('GPS', function (newGps) { return gps = newGps; });
Bangle.on('HRM', function (newHrm) { return hrm = newHrm; }); Bangle.on('HRM', function (newHrm) { return hrm = newHrm; });
Bangle.on('mag', function (newMag) { return mag = newMag; }); Bangle.on('mag', function (newMag) { return mag = newMag; });
Bangle.loadWidgets();
Bangle.drawWidgets();
setBtnsShown(true); setBtnsShown(true);
var redrawInterval = setInterval(redraw, 1000); var redrawInterval = setInterval(redraw, 2000);
Bangle.on("lock", function (locked) { Bangle.on("lock", function (locked) {
changeInterval(redrawInterval, locked ? 30000 : 1000); changeInterval(redrawInterval, locked ? 15000 : 2000);
}); });
enableSensors(); enableSensors();
{ {
@ -416,3 +363,10 @@ var setIntervals = function (connected) {
iv = undefined; iv = undefined;
} }
}; };
setIntervals(NRF.getSecurityStatus().connected);
NRF.on("connect", function () {
setIntervals(true);
});
NRF.on("disconnect", function () {
setIntervals(false);
});

View File

@ -1,10 +1,13 @@
const Layout = require("Layout") as Layout_.Layout; const Layout = require("Layout") as Layout_.Layout;
Bangle.loadWidgets();
Bangle.drawWidgets();
const enum Intervals { const enum Intervals {
BLE_ADVERT = 60 * 1000, // BLE_ADVERT = 60 * 1000,
BLE = 1000, BLE = 1000,
MENU_WAKE = 1000, MENU_WAKE = 2 * 1000,
MENU_SLEEP = 30 * 1000, MENU_SLEEP = 15 * 1000,
} }
type Hrm = { bpm: number, confidence: number }; type Hrm = { bpm: number, confidence: number };
@ -100,6 +103,8 @@ const idToName: BtAdvMap<string, true> = {
mag: "Magnetometer", mag: "Magnetometer",
}; };
const infoFont: FontNameWithScaleFactor = "6x8:2";
const colour = { const colour = {
on: "#0f0", on: "#0f0",
off: "#fff", off: "#fff",
@ -213,149 +218,107 @@ const btnLayout = new Layout(
const setBtnsShown = (b: boolean) => { const setBtnsShown = (b: boolean) => {
btnsShown = b; btnsShown = b;
g.clearRect(Bangle.appRect);
redraw(); redraw();
}; };
const infoFont: FontNameWithScaleFactor = "6x8:2"; const drawInfo = () => {
const infoCommon = { let { y, x, w } = Bangle.appRect;
type: "txt", const mid = x + w / 2
label: "", let drawn = false;
font: infoFont,
pad: 5,
} as const;
const infoLayout = new Layout( g.reset()
{ .clearRect(Bangle.appRect)
type: "v", .setFont(infoFont)
c: [ .setFontAlign(0, -1);
{
type: "h",
c: [
{ id: "bar_alti", ...infoCommon },
{ id: "bar_pres", ...infoCommon },
{ id: "bar_temp", ...infoCommon },
]
},
{
type: "h",
c: [
{ id: "gps_lat", ...infoCommon },
{ id: "gps_lon", ...infoCommon },
{ id: "gps_altitude", ...infoCommon },
{ id: "gps_satellites", ...infoCommon },
{ id: "gps_hdop", ...infoCommon },
]
},
{
type: "h",
c: [
{ id: "hrm_bpm", ...infoCommon },
{ id: "hrm_confidence", ...infoCommon },
]
},
{
type: "h",
c: [
{ id: "mag_x", ...infoCommon },
{ id: "mag_y", ...infoCommon },
{ id: "mag_z", ...infoCommon },
{ id: "mag_heading", ...infoCommon },
]
},
{
type: "btn",
label: "Set",
cb: () => {
setBtnsShown(true);
},
...btnStyle,
},
]
},
{
lazy: true,
// back: () => (load as any)(),
}
);
const showElem = (
layout: Layout_.Hierarchy & { type: "txt" },
s: string,
) => {
layout.label = s;
// delete layout.width; TODO?
delete layout.height;
};
const hideElem = (layout: Layout_.Hierarchy) => {
layout.height = 0;
};
const populateInfo = () => {
if (bar) { if (bar) {
showElem(infoLayout["bar_alti"]!, `${bar.altitude.toFixed(1)}m`); g.drawString(`${bar.altitude.toFixed(1)}m`, mid, y);
showElem(infoLayout["bar_pres"]!, `${bar.pressure.toFixed(1)}mbar`); y += g.getFontHeight();
showElem(infoLayout["bar_temp"]!, `${bar.temperature.toFixed(1)}C`);
} else { g.drawString(`${bar.pressure.toFixed(1)}mbar`, mid, y);
hideElem(infoLayout["bar_alti"]!); y += g.getFontHeight();
hideElem(infoLayout["bar_pres"]!);
hideElem(infoLayout["bar_temp"]!); g.drawString(`${bar.temperature.toFixed(1)}C`, mid, y);
y += g.getFontHeight();
drawn = true;
} }
if (gps) { if (gps) {
showElem(infoLayout["gps_lat"]!, gps.lat.toFixed(4)); g.drawString(
showElem(infoLayout["gps_lon"]!, gps.lon.toFixed(4)); `${gps.lat.toFixed(4)} lat, ${gps.lon.toFixed(4)} lon`,
showElem(infoLayout["gps_altitude"]!, `${gps.alt}m`); mid,
showElem(infoLayout["gps_satellites"]!, `${gps.satellites}`); y,
showElem(infoLayout["gps_hdop"]!, `${(gps.hdop * 5).toFixed(1)}m`); );
} else { y += g.getFontHeight();
hideElem(infoLayout["gps_lat"]!);
hideElem(infoLayout["gps_lon"]!); g.drawString(
hideElem(infoLayout["gps_altitude"]!); `${gps.alt}m (${gps.satellites} sat)`,
hideElem(infoLayout["gps_satellites"]!); mid,
hideElem(infoLayout["gps_hdop"]!); y,
);
y += g.getFontHeight();
drawn = true;
} }
if (hrm) { if (hrm) {
showElem(infoLayout["hrm_bpm"]!, `${hrm.bpm}`); g.drawString(`${hrm.bpm} BPM (${hrm.confidence}%)`, mid, y);
showElem(infoLayout["hrm_confidence"]!, `${hrm.confidence}%`); y += g.getFontHeight();
} else {
hideElem(infoLayout["hrm_bpm"]!); drawn = true;
hideElem(infoLayout["hrm_confidence"]!);
} }
if (mag) { if (mag) {
showElem(infoLayout["mag_x"]!, `${mag.x}`); g.drawString(
showElem(infoLayout["mag_y"]!, `${mag.y}`); `${mag.x} ${mag.y} ${mag.z}`,
showElem(infoLayout["mag_z"]!, `${mag.z}`); mid,
showElem(infoLayout["mag_heading"]!, mag.heading.toFixed(1)); y
} else { );
hideElem(infoLayout["mag_x"]!); y += g.getFontHeight();
hideElem(infoLayout["mag_y"]!);
hideElem(infoLayout["mag_z"]!); g.drawString(
hideElem(infoLayout["mag_heading"]!); `heading: ${mag.heading.toFixed(1)}`,
mid,
y
);
y += g.getFontHeight();
drawn = true;
}
if (!drawn) {
g.drawString(`swipe to enable`, mid, y);
y += g.getFontHeight();
} }
}; };
const onTap = (_: { /* ... */ }) => {
setBtnsShown(true);
};
const redraw = () => { const redraw = () => {
let layout;
if (btnsShown) { if (btnsShown) {
layout = btnLayout; if (!prevBtnsShown) {
} else {
populateInfo();
infoLayout.update();
layout = infoLayout;
}
if (btnsShown !== prevBtnsShown) {
prevBtnsShown = btnsShown; prevBtnsShown = btnsShown;
layout.forgetLazyState();
layout.setUI(); Bangle.removeListener("swipe", onTap);
btnLayout.setUI();
btnLayout.forgetLazyState();
g.clearRect(Bangle.appRect); // in case btnLayout isn't full screen
}
btnLayout.render();
} else {
if (prevBtnsShown) {
prevBtnsShown = btnsShown;
Bangle.setUI(); // remove all existing input handlers
Bangle.on("swipe", onTap);
}
drawInfo();
} }
layout.render();
}; };
const encodeHrm: LenFunc<Hrm> = (hrm: Hrm) => const encodeHrm: LenFunc<Hrm> = (hrm: Hrm) =>
@ -617,10 +580,6 @@ Bangle.on('GPS', newGps => gps = newGps);
Bangle.on('HRM', newHrm => hrm = newHrm); Bangle.on('HRM', newHrm => hrm = newHrm);
Bangle.on('mag', newMag => mag = newMag); Bangle.on('mag', newMag => mag = newMag);
// show UI
Bangle.loadWidgets();
Bangle.drawWidgets();
setBtnsShown(true); setBtnsShown(true);
const redrawInterval = setInterval(redraw, Intervals.MENU_WAKE); const redrawInterval = setInterval(redraw, Intervals.MENU_WAKE);