12-31-2020 09:01
12-31-2020 09:01
I have had a few customers contact me about the HR stat not displaying at all.
From my troubleshooting, I have determined that
It almost seems as if the HRM is not starting, however I am unable to find anything in my code that could cause it. It seems to be happening on 4.2 and not 5.0 so I am unsure if it was a firmware change that could have caused it....
I would appreciate a quick review of my HR code below to see if I have missed anything or if the code has been updated which is why I am having issues.
import { display } from "display";
import { HeartRateSensor } from "heart-rate";
import { user } from "user-profile";
import { vibration } from "haptics";
import document from "document";
import * as simpleButtons from "./buttons";
let hrm, watchID, hrmCallback;
let lastReading = 0;
let heartRate;
var lowerHR = document.getElementById("lowerHR");
var upperHR = document.getElementById("upperHR");
export function initialize(callback) {
hrmCallback = callback;
hrm = new HeartRateSensor();
hrm.start();
lastReading = hrm.timestamp;
start();
}
function getReading() {
if (hrm.timestamp === lastReading) {
heartRate = "--";
} else {
heartRate = hrm.heartRate;
}
lastReading = hrm.timestamp;
hrmCallback({
bpm: heartRate,
zone: user.heartRateZone(hrm.heartRate || 0),
restingHeartRate: user.restingHeartRate
});
detectHR();
}
display.addEventListener("change", function() {
if (display.on) {
start();
} else {
stop();
}
})
export function start() {
if (!watchID) {
getReading();
watchID = setInterval(getReading, 1000);
}
}
export function stop() {
clearInterval(watchID);
watchID = null;
}
// ***************HR FUNCTIONS*************** \\
function detectHR() {
let hr = (hrm.heartRate);
// console.log("Detecting HR")
if (lowerHR.text == "--" || lowerHR.text.length == 0 || hr == null) {
return;
}
if (hr != null && hr <= lowerHR.text) {
console.log("Low HR Detected at " + lowerHR.text + " BPM")
console.log("Launching Pop-Up Screen")
vibration.start("nudge-max");
simpleButtons.hrDetect();
}
if (upperHR.text == "--" || upperHR.text.length == 0 || hr == null) {
return;
}
if (hr != null && hr >= upperHR.text){
console.log("High HR Detected at " + upperHR.text + " BPM")
console.log("Launching Pop-Up Screen")
simpleButtons.hrDetect();
}
}
01-02-2021 06:45
01-02-2021 06:45
I can't seem to spot anything out of a quick skim.
I did notice you never do hrm.stop(). This maybe causing an issue with the HRM never stopping and potentially breaking. It maybe a stretch but try putting hrm.stop() at the top of stop()