Hello,
I've built a beta app only tested on my Edge 520Plus device.
It's working well except that, every 5 to 6 seconds, my app isn't refreshed on my device after starting an activity.
I noticed this because every 5 or 6 seconds, a second (timer.Time) isn't displayed.
I was wondering if my counter (compteur) was the cause, so I tested it and noticed that it faced the same issue simultaneously with the timer.
So what ? Am I asking for too many calculation to my device ? Did you faced similar issue ?
Just as an information, every 5mn (counter = 299) the counter is reset to zero because from counter = 294 to 299, averages values are displayed instead of live values.
Here's the code of the View. Thanks in advance for your hints !
import Toybox.Activity;
import Toybox.Graphics;
import Toybox.Lang;
import Toybox.WatchUi;
import Toybox.Time;
import Toybox.Math;
class projView extends WatchUi.DataField {
hidden var mHR as Numeric;
hidden var mHRMoy as Numeric;
hidden var mDistance as Numeric;
hidden var mDistKM as Numeric;
hidden var mSpeed as Numeric;
hidden var mCad as Numeric;
hidden var mCadMoy as Numeric;
hidden var mPW as Numeric;
hidden var mPW1 as Numeric;
hidden var mPW2 as Numeric;
hidden var mPW3 as Numeric;
hidden var mPW4 as Numeric;
var mClimb as Numeric;
hidden var mAlt1 as Numeric;
hidden var mAlt2 as Numeric;
hidden var mAltInt1 as Numeric;
hidden var mAltInt2 as Numeric;
hidden var mSlop as Numeric;
hidden var mTime as Numeric;
hidden var mTimeSec as Numeric;
hidden var mDuree;
hidden var horloge;
hidden var horloge_text;
hidden var batt;
hidden var batt_stat;
hidden var batt_text;
var compteur as Numeric;
hidden var reset_valeur as Numeric;
hidden var previous_time as Numeric;
hidden var vitesseMoyenne as Numeric;
hidden var timeArrondi as Numeric;
function initialize() {
DataField.initialize();
mHR= 0d;
mHRMoy = 0d;
mDistance = 0d;
mDistKM = 0.00f;
mSpeed = 0.0f;
mCad =0d;
mCadMoy=0d;
mClimb = 0d;
mAlt1 = -999d;
mAlt2 = 0d;
mAltInt1 = 0d;
mAltInt2 = 0d;
batt = 0d;
mSlop = 0f;
mTime = 0d;
mTimeSec = 0d;
previous_time = 0d;
mPW = 0d;
mPW1 = 0d;
mPW2 = 0d;
mPW3 = 0d;
mPW4 = 0d;
compteur = 0d;
vitesseMoyenne = 0.0f;
reset_valeur = 299;
timeArrondi = 0.00f;
mDuree="00:00:00";
}
// Set your layout here. Anytime the size of obscurity of
// the draw context is changed this will be called.
function onLayout(dc as Dc) as Void {
// var obscurityFlags = DataField.getObscurityFlags();
View.setLayout(Rez.Layouts.MainLayout(dc));
var hrlbl = View.findDrawableById("hr_lbl") as Text;
hrlbl.locY = 7 ;
hrlbl.locX = 40 ;
var hrval= View.findDrawableById("hr_val") as Text;
hrval.locY = hrlbl.locY + 15 ;
hrval.locX = 60;
var pwlbl = View.findDrawableById("pw_lbl") as Text;
pwlbl.locY = hrlbl.locY ;
pwlbl.locX = 140;
var pwval= View.findDrawableById("pw_val") as Text;
pwval.locY = hrval.locY ;
pwval.locX = 175;
var vitlbl= View.findDrawableById("vit_lbl") as Text;
vitlbl.locY = hrlbl.locY + 55;
vitlbl.locX = 100;
var vitval = View.findDrawableById("vit_val") as Text;
vitval.locY = vitlbl.locY + 20;
vitval.locX = 95;
var distlbl = View.findDrawableById("dist_lbl") as Text;
distlbl.locY = vitlbl.locY +70;
distlbl.locX = 55;
var distval= View.findDrawableById("dist_val") as Text;
distval.locY = distlbl.locY + 15;
distval.locX = 55 ;
var cadlbl = View.findDrawableById("cad_lbl") as Text;
cadlbl.locY = distlbl.locY ;
cadlbl.locX = 155;
var cadval= View.findDrawableById("cad_val") as Text;
cadval.locY = distval.locY ;
cadval.locX = 180;
var timelbl = View.findDrawableById("time_lbl") as Text;
timelbl.locY = distval.locY +40;
timelbl.locX = 55 ;
var timeval= View.findDrawableById("time_val") as Text;
timeval.locY = timelbl.locY + 15;
timeval.locX = 55 ;
var climblbl = View.findDrawableById("climb_lbl") as Text;
climblbl.locY = timelbl.locY;
climblbl.locX = 155;
var climbval= View.findDrawableById("climb_val") as Text;
climbval.locY = timeval.locY ;
climbval.locX = 185 ;
var horval = View.findDrawableById("horloge_val") as Text;
horval.locY = climblbl.locY + 55;
horval.locX = 55 ;
var battval = View.findDrawableById("battery_val") as Text;
battval.locY = horval.locY;
battval.locX = 190 ;
var compteurval = View.findDrawableById("compteur_val") as Text;
compteurval.locY = horval.locY;
compteurval.locX = 120 ;
(View.findDrawableById("hr_lbl") as Text).setText(Rez.Strings.hr_lbl);
(View.findDrawableById("cad_lbl") as Text).setText(Rez.Strings.cad_lbl);
(View.findDrawableById("pw_lbl") as Text).setText(Rez.Strings.pw_lbl);
(View.findDrawableById("vit_lbl") as Text).setText(Rez.Strings.vit_lbl);
(View.findDrawableById("dist_lbl") as Text).setText(Rez.Strings.dist_lbl);
(View.findDrawableById("climb_lbl") as Text).setText(Rez.Strings.climb_lbl);
(View.findDrawableById("time_lbl") as Text).setText(Rez.Strings.time_lbl);
}
// The given info object contains all the current workout information.
// Calculate a value and save it locally in this method.
// Note that compute() and onUpdate() are asynchronous, and there is no
// guarantee that compute() will be called before onUpdate().
function compute(info as Activity.Info) as Void {
// See Activity.Info in the documentation for available information.
// le compteur va permettre de faire varier les info affichées selon les valeurs
//il s'incrémente de 1 toutes les secondes.
mSpeed = info.currentSpeed*3.6 as Float;
if(info.timerTime != null and info.timerTime !=previous_time ){
previous_time = mTime;
mTime = info.timerTime;
compteur = compteur+1;
if(mTime<=5000){ // initialisation de l'altitude au démarrage
mAlt1 = info.altitude as Number;
mAlt2 = info.altitude as Number;
mAltInt1 = mAlt1.toNumber();
mAltInt2 = mAlt2.toNumber();
} else {
mAltInt1 = mAltInt2;
mAlt2 = info.altitude as Number;
mAltInt2 = mAlt2.toNumber();
}
//calcul du D+
if(mAltInt2>mAltInt1){ // si l'altitude actuelle est supérieure à la précédente, le D+ augmente
mClimb = mClimb + (mAltInt2 - mAltInt1) ;
}
else {
mClimb = mClimb;
}
} else {
// mClimb = 0d;
//mTime = 0d;
//compteur = 0d;
}
// parce que parfois le timer est dans la même seconde 2 fois d'affilée, j'arrondis au dessus.
timeArrondi= (mTime/1000.0)+.499;
//conversion du temps en secondes et formatage pour l'affichage.
mTimeSec = timeArrondi.toNumber();
var hour = (mTimeSec / 3600).toLong();
var minute = (mTimeSec / 60).toLong() - (hour * 60);
var second = mTimeSec - (minute * 60) - (hour * 3600);
if (mTimeSec < 3600) {
mDuree = Lang.format("$1$:$2$", ([minute.format("%02d"), second.format("%02d")]));
} else {
mDuree = Lang.format("$1$:$2$:$3$", ([hour.format("%d"), minute.format("%02d"), second.format("%02d")]));
}
if(info.currentHeartRate != null){
mHR = info.currentHeartRate as Number;
mHRMoy = info.averageHeartRate as Number;
} else {
mHR= 0d;
mHRMoy = 0d;
}
if(info.elapsedDistance != null){
mDistance = info.elapsedDistance as Number;
mDistKM = mDistance / 1000 as Float;
vitesseMoyenne = 3600* mDistance / (mTime);
} else {
mDistance = 0d;
mDistKM = 0.0f;
vitesseMoyenne = 0;
}
if(info.currentCadence != null){
mCad = info.currentCadence as Number;
mCadMoy = info.averageCadence as Number;
} else {
mCad = 0d;
mCadMoy = 0d;
}
if(info.currentPower != null){
mPW1 = mPW2;
mPW2 = mPW3;
mPW3 = info.currentPower as Number;
mPW = (mPW1 + mPW2 + mPW3)/3;
} else {
mPW1 = mPW2;
mPW2 = mPW3;
mPW3 = 0d;
mPW = (mPW1 + mPW2 + mPW3)/3;
}
horloge = System.getClockTime(); // ClockTime object
horloge_text =(horloge.hour.format("%02d") + ":" +horloge.min.format("%02d") );
batt_stat= System.getSystemStats();
batt = batt_stat.battery;
// every 5mn, counter is reset to zero
if (compteur>reset_valeur){
compteur=0;
}else{
compteur=compteur;
}
}
// Display the value you computed here. This will be called
// once a second when the data field is visible.
function onUpdate(dc as Dc) as Void {
// Set the background color
(View.findDrawableById("Background") as Text).setColor(getBackgroundColor());
var pw = View.findDrawableById("pw_val") as Text;
if (getBackgroundColor() == Graphics.COLOR_BLACK) {
pw.setColor(Graphics.COLOR_WHITE);
} else {
pw.setColor(Graphics.COLOR_BLACK);
}
pw.setText(mPW.format("%d")+"w");
if (compteur< reset_valeur-5) {
// la vitesse moyenne, cadence moy et freq cardiaque moy st affichées pendant 5" après 4mn55
var vitesse = View.findDrawableById("vit_val") as Text;
if (getBackgroundColor() == Graphics.COLOR_BLACK) {
vitesse.setColor(Graphics.COLOR_WHITE);
} else {
vitesse.setColor(Graphics.COLOR_BLACK);
}
vitesse.setText(mSpeed.format("%.1f"));
var cad = View.findDrawableById("cad_val") as Text;
if (getBackgroundColor() == Graphics.COLOR_BLACK) {
cad.setColor(Graphics.COLOR_WHITE);
} else {
cad.setColor(Graphics.COLOR_BLACK);
}
cad.setText(mCad.format("%d"));
var HR = View.findDrawableById("hr_val") as Text;
if (getBackgroundColor() == Graphics.COLOR_BLACK) {
HR.setColor(Graphics.COLOR_WHITE);
} else {
HR.setColor(Graphics.COLOR_BLACK);
}
HR.setText(mHR.format("%d"));
} else{
var vitesse = View.findDrawableById("vit_val") as Text;
if (getBackgroundColor() == Graphics.COLOR_BLACK) {
vitesse.setColor(Graphics.COLOR_BLUE);
} else {
vitesse.setColor(Graphics.COLOR_DK_BLUE);
}
vitesse.setText(vitesseMoyenne.format("%.1f"));
var cad = View.findDrawableById("cad_val") as Text;
if (getBackgroundColor() == Graphics.COLOR_BLACK) {
cad.setColor(Graphics.COLOR_BLUE);
} else {
cad.setColor(Graphics.COLOR_DK_BLUE);
}
cad.setText(mCadMoy.format("%d"));
var HR = View.findDrawableById("hr_val") as Text;
if (getBackgroundColor() == Graphics.COLOR_BLACK) {
HR.setColor(Graphics.COLOR_BLUE);
} else {
HR.setColor(Graphics.COLOR_DK_BLUE);
}
HR.setText(mHRMoy.format("%d"));
}
//mon ajout
var distanceParcourue = View.findDrawableById("dist_val") as Text;
if (getBackgroundColor() == Graphics.COLOR_BLACK) {
distanceParcourue.setColor(Graphics.COLOR_WHITE);
} else {
distanceParcourue.setColor(Graphics.COLOR_BLACK);
}
distanceParcourue.setText(mDistKM.format("%.2f"));
var duree = View.findDrawableById("time_val") as Text;
if (getBackgroundColor() == Graphics.COLOR_BLACK) {
duree.setColor(Graphics.COLOR_WHITE);
} else {
duree.setColor(Graphics.COLOR_BLACK);
}
duree.setText(mDuree);
var denivele = View.findDrawableById("climb_val") as Text;
if (getBackgroundColor() == Graphics.COLOR_BLACK) {
denivele.setColor(Graphics.COLOR_WHITE);
} else {
denivele.setColor(Graphics.COLOR_BLACK);
}
denivele.setText(mClimb.format("%d"));
var heure_actuelle = View.findDrawableById("horloge_val") as Text;
if (getBackgroundColor() == Graphics.COLOR_BLACK) {
heure_actuelle.setColor(Graphics.COLOR_WHITE);
} else {
heure_actuelle.setColor(Graphics.COLOR_BLACK);
}
heure_actuelle.setText(horloge_text);
var battery_charge = View.findDrawableById("battery_val") as Text;
if (getBackgroundColor() == Graphics.COLOR_BLACK) {
battery_charge.setColor(Graphics.COLOR_WHITE);
} else {
battery_charge.setColor(Graphics.COLOR_BLACK);
}
battery_charge.setText("Batt " + batt.format("%d")+"%");
// Call parent's onUpdate(dc) to redraw the layout
View.onUpdate(dc);
// la mise en forme des lignes doit s'effectuer après la commande View.onUpdate(dc);
if (getBackgroundColor() == Graphics.COLOR_BLACK) {
dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_WHITE);
} else {
dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_BLACK);}
dc.setPenWidth(1);
dc.drawLine(0, 57, 200, 57);
dc.drawLine(0, 130, 200, 130);
dc.drawLine(0, 183, 200, 183);
dc.drawLine(0, 240, 200, 240);
dc.drawLine(85, 0, 85, 57);
dc.drawLine(110, 130, 110, 240);
}
}