08-30-2020 15:15
08-30-2020 15:15
I've managed to put together a watch face (showing date, time, battery, heart rate, and steps) that works except for one thing - it won't update the steps unless I push the left button and swipe up to 'today'. Everything else updates without swiping up. I copied all this code from other threads and I'm really happy to get this far. Does anyone know what I'm missing? I can't tell what's different in the code for the other stats that lets them update.
import clock from "clock";
import document from "document";
import { preferences } from "user-settings";
import * as util from "../common/utils";
// Update the clock every minute
clock.granularity = "minutes";
// Get a handle on the <text> element
const myLabel = document.getElementById("myLabel");
const myMonth = document.getElementById("myMonth");
const myDay = document.getElementById("myDay");
// Update the <text> element every tick with the current time
clock.ontick = (evt) => {
let today = evt.date;
let hours = today.getHours();
let today = evt.date;
let hours = today.getHours();
let monthnum = today.getMonth();
let day = today.getDate();
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
let monthname = month[monthnum];
if (preferences.clockDisplay === "12h") {
// 12h format
hours = hours % 12 || 12;
} else {
// 24h format
hours = util.zeroPad(hours);
}
let mins = util.zeroPad(today.getMinutes());
myLabel.text = `${hours}:${mins}`;
myMonth.text = `${monthname}`;
myDay.text = `${day}`;
}
const myHRM = document.getElementById("myHRM");
import { HeartRateSensor } from "heart-rate";
if (HeartRateSensor) {
const hrm = new HeartRateSensor({ frequency: 1 });
hrm.addEventListener("reading", () => {
myHRM.text=`${hrm.heartRate}`;
});
hrm.start();
}
import { BodyPresenceSensor } from "body-presence";
if (BodyPresenceSensor) {
const body = new BodyPresenceSensor();
body.addEventListener("reading", () => {
if (!body.present) {
myHRM.text="--"
} else {
}
});
body.start();
}
const mySteps = document.getElementById("mySteps");
const myCalories = document.getElementById("myCalories");
import { me as appbit } from "appbit";
import { today } from "user-activity";
if (appbit.permissions.granted("access_activity")) {
console.log(`${today.adjusted.steps} Steps`);
mySteps.text=`${today.adjusted.steps}`;
}
const myBattery = document.getElementById("myBattery");
import { battery } from "power";
myBattery.text=(Math.floor(battery.chargeLevel) + "%");
08-30-2020 15:43
08-30-2020 15:43
Study your code, and ask yourself:
Tip: use console.log('some text') in strategic places to help you to see which bits of your code are running when.
08-30-2020 18:04
08-30-2020 18:04
It looks like the clock updates every minute. The heart rate monitor and the battery both have event listener and the steps doesn't so I'm guessing it has something to do with that bit? The steps look like they only update every day, so maybe it will only turn over to zero on it's own. I'm not sure how to tell the steps to update like the other things do.
08-30-2020 19:11
08-30-2020 19:11
You're right. A common approach is to update the step count within the ontick() function, because you know that's going to be called every minute.
If that's not fast enough for you, you can increase the granularity to 'seconds'.
Running that code every second means that you should try to make it as efficient as possible; ie, don't do stuff every second that doesn't need to be done every second. An example is populating month[].
08-31-2020 14:22
08-31-2020 14:22
You can use setInterval too, like this
function refresh_myActivity() {
statSteps.text = today.adjusted.steps
statCalories.text = today.adjusted.calories
statActiveMinutes.text = today.adjusted.activeMinutes
}
setInterval(refresh_myActivity, 700 /* refresh rate in milliseconds. change to whatever you'd like */);
08-31-2020 17:17
08-31-2020 17:17
Thanks for that! I'm going to mess with it and see what happens.
08-31-2020 17:29
08-31-2020 17:29