Non-coding layman failed in putting steps in absolute numbers on plain watch face

Former Member
Former Member
Hi,

I've no experience in coding but I have been attempting to design my own watch face. So far, I have gotten the date and time the way I like it based on some stuff I found via google. But I have not had any success on steps. I have googled around and check various sources (including asking for tips from one of the senior members here) but couldn't figure out how to do it. The sample found in the sample folder of the SDK does it in terms of a bar and I can't for the life of me figure out how to change that to integers. So I was wondering if someone can see where I went wrong. I got the watch face to show, but the steps counter doesn't. And when I simulated the steps data, nothing changes until I get an error message:

"Failed invoking <symbol>
Invalid Font Specified
in onUpdate (C:\Users\....\workspace\bare_bones\source\bare_bonesView.mc:43)



using Toybox.WatchUi as Ui;
using Toybox.Graphics as Gfx;
using Toybox.System as Sys;
using Toybox.Lang as Lang;
using Toybox.Time as Time;
using Toybox.Time.Gregorian as Calendar;
using Toybox.ActivityMonitor as Act;

class bare_bonesView extends Ui.WatchFace {

function initialize() {
WatchFace.initialize();
}

// Load your resources here
function onLayout(dc) {
setLayout(Rez.Layouts.WatchFace(dc));
}

// Called when this View is brought to the foreground. Restore
// the state of this View and prepare it to be shown. This includes
// loading resources into memory.
function onShow() {
}

// Update the view
function onUpdate(dc) {
// Get and show the current day
var dateInfo = Calendar.info( Time.now(), Calendar.FORMAT_MEDIUM );
var dateString = Lang.format("$1$ $2$ $3$", [ dateInfo.day_of_week, dateInfo.day, dateInfo.month ]);
var date = View.findDrawableById("DateLabel"); date.setText(dateString);

// Get and show the current time
var clockTime = Sys.getClockTime();
var timeString = Lang.format("$1$:$2$", [clockTime.hour.format("%02d"), clockTime.min.format("%02d")]);
var view = View.findDrawableById("TimeLabel");
view.setText(timeString);

// Get and show the steps (this part's screwed)
var activityInfo = Act.getInfo();
var stepsString = activityInfo.steps;
dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK);
dc.drawText(50, 100, stepsString, Gfx.FONT_SYSTEM_LARGE, Gfx.TEXT_JUSTIFY_RIGHT);
// end of screwed part


// Call the parent onUpdate function to redraw the layout
View.onUpdate(dc);
}

// Called when this View is removed from the screen. Save the
// state of this View here. This includes freeing resources from
// memory.
function onHide() {
}

// The user has just looked at their watch. Timers and animations may be started here.
function onExitSleep() {
}

// Terminate any active timers and prepare for slow updates.
function onEnterSleep() {
}

}
  • There was a bug a while ago that each label in the layout got its own copy of the font data. If you used the same custom font for multiple things it was quite costly. The workaround was to set the font for each of the drawables in onLayout() so they shared the data. I'm curious if that is affecting you somehow.


    OK, I did a little bit of experimentation, and here's the interesting thing&#8230;

    In [FONT=Microsoft Sans Serif]layouts.xml[/FONT], I have:
    <layout id="WatchFace">
    <label id="HourNum" text="00" x="102" y="23" font="@Fonts.bold" justification="Gfx.TEXT_JUSTIFY_RIGHT" />
    <label id="MinuteNum" text="44" x="106" y="23" font="@Fonts.regular" justification="Gfx.TEXT_JUSTIFY_LEFT" />
    <label id="ActMonStats" text="E 12345kJ" x="106" y="152" font="@Fonts.clutter" justification="Gfx.TEXT_JUSTIFY_CENTER" />
    <label id="BatteryLevel" text="b" x="201" y="78" font="@Fonts.clutter" justification="Gfx.TEXT_JUSTIFY_LEFT" />
    <label id="NotifIcon" text="Q" x="51" y="0" font="@Fonts.clutter" justification="Gfx.TEXT_JUSTIFY_CENTER" />
    <label id="NotifCount1" text="24" x="51" y="1" font="Gfx.FONT_TINY" justification="Gfx.TEXT_JUSTIFY_CENTER" />
    <label id="AlarmIcon" text="R" x="164" y="0" font="@Fonts.clutter" justification="Gfx.TEXT_JUSTIFY_CENTER" />
    <label id="DateBanner" text="Mon 24 Aug" x="108" y="-1" font="Gfx.FONT_MEDIUM" justification="Gfx.TEXT_JUSTIFY_CENTER" />
    </layout>


    Upon running my watch face code in its basic form in the simulator (as a FR630), memory usage was 54.8kB (peaking at 55.9kB).

    I then added a call to [FONT=Courier New]setFont()[/FONT] in the body of the [FONT=Courier New]onLayout()[/FONT] function, without removing [FONT=Courier New]font="@Fonts.clutter"[/FONT] bit from [FONT=Microsoft Sans Serif]layout.xml[/FONT]:
    function onLayout(dc) {
    setLayout(Rez.Layouts.WatchFace(dc));
    View.findDrawableById("ActMonStats").setFont(iconsFont);
    }
    and memory usage then became 51.4kB (peaking at 55.0kB).

    Adding a few more calls to [FONT=Courier New]setFont()[/FONT]:
    function onLayout(dc) {
    setLayout(Rez.Layouts.WatchFace(dc));
    View.findDrawableById("ActMonStats").setFont(iconsFont);
    View.findDrawableById("AlarmIcon").setFont(iconsFont);
    View.findDrawableById("BatteryLevel").setFont(iconsFont);
    View.findDrawableById("NotifIcon").setFont(iconsFont);

    }
    and memory usage then became 41.3kB (peaking at 55.1kB).

    I then did a global find-and-replace and removed all mention of [FONT=Courier New]font="@Fonts.clutter"[/FONT] from [FONT=Microsoft Sans Serif]layout.xml[/FONT], and memory usage became 41.3kB (peaking at 42.3kB).

    What a difference that made!
  • That's great news! Glad Travis could assist you with some tips!
    Great work!
  • Thank you very much!

    Travis, Jim and Hermo, thank you very much for all your help. I look forward to learning more!

    (Right now I'm struggling a little to understand the inconsistencies and errors in Time::Gregorian::Info, particularly in the context of relying on it to translating dates into languages I don't personally know, but I'll post in the existing old threads on those issues once I get a handle on them.)
  • (Right now I'm struggling a little to understand the inconsistencies and errors in Time::Gregorian::Info, particularly in the context of relying on it to translating dates into languages I don't personally know, but I'll post in the existing old threads on those issues once I get a handle on them.)

    No problem!

    You might find the three of there posting too! :)
  • OK, I did a little bit of experimentation, ... what a difference that made!

    I filed a bug on this issue a long time ago, but it doesn't appear it has been fixed. The solution is pretty simple... The code generated by the resource compiler for the layout definition seeds a cache of the fonts that are loaded.

    I posted some sample code in the report here.

    Travis, Jim and Hermo, thank you very much for all your help.

    You're welcome.