Hello
I'm creating a new watchface for EPIX2.
I tried to draw a HR chart using history data thanks to ActivityMonitor.getHeartRateHistory()
I was inspired by the example availlable at https://developer.garmin.com/connect-iq/api-docs/Toybox/ActivityMonitor.html but I had to fix the "Watchdog Tripped Error - Code Executed Too Long" by replacing the while(true) { }
Here I am now:
var hrDuration = 240; //! x last minutes of HR
var hrIterator = ActivityMonitor.getHeartRateHistory(hrDuration, true); //! get last minutes of HR
var hrMax = (220-(info.year-profile.birthYear)*1.1); //! 10% margin added to the basic calculus rule
var hrMin = profile.restingHeartRate; //! data store in the user profile
var previous = hrIterator.next(); //! get the previous HR
var lastSampleTime = null; //! get the last
var hChart = 44;
var xStartHr = 280;
var yStartHr = 150;
var genericZoneInfo = Us.getHeartRateZones(Us.HR_ZONE_SPORT_GENERIC);
var hrOrd;
//! Draw the cHR chart
for (var i=0;i<=hrDuration;i+=1) {
var sample = hrIterator.next();
if (null!=sample) { //! null check
if (sample.heartRate != ActivityMonitor.INVALID_HR_SAMPLE && previous.heartRate != ActivityMonitor.INVALID_HR_SAMPLE) { //! check for invalid samples
if (sample.heartRate != null && sample.heartRate != 0) {
hrOrd = ((sample.heartRate-hrMin)*(hChart-2)/(hrMax-hrMin)); //! Height in pixel of current HR in chart / -2 to substract 2px of the frame line
dc.setColor(Gfx.COLOR_BLUE,Gfx.COLOR_TRANSPARENT);
if (sample.heartRate >= genericZoneInfo[1]) {dc.setColor(Gfx.COLOR_GREEN, Gfx.COLOR_TRANSPARENT);}
if (sample.heartRate >= genericZoneInfo[2]) {dc.setColor(Gfx.COLOR_YELLOW,Gfx.COLOR_TRANSPARENT);}
if (sample.heartRate >= genericZoneInfo[3]) {dc.setColor(Gfx.COLOR_ORANGE,Gfx.COLOR_TRANSPARENT);}
if (sample.heartRate >= genericZoneInfo[4]) {dc.setColor(Gfx.COLOR_RED, Gfx.COLOR_TRANSPARENT);}
if (sample.heartRate >= genericZoneInfo[5]) {dc.setColor(Gfx.COLOR_PURPLE,Gfx.COLOR_TRANSPARENT);}
dc.drawLine(xStartHr-i,yStartHr-hrOrd,xStartHr-i,yStartHr); //! Draw chart if with have a HR value
} else { //! Draw a blank if we have no value
dc.setColor(Gfx.COLOR_DK_GRAY,Gfx.COLOR_TRANSPARENT);
dc.drawLine(xStartHr-i,yStartHr-hChart,xStartHr-i,yStartHr);
}
}
}
}
It seems to work on the simulator. But I got instant unhandled exception with the device.
Have you ever experienced this kind of trouble ?
Thank you for your help
Gautier
