Timer in a WatchUi.WatchFace view

Hi,

I am new at this!!!
I am trying to use Timer object in a WatchUi.WatchFace view, but I getting a runtime error in the line "timer1.start( method(:xcallback1), 5000, true );".
Details bellow.

Here is my source code of my view:

using Toybox.WatchUi as Ui;
using Toybox.Graphics as Gfx;
using Toybox.Timer as Timer;

var timer1;

class WatchFace1View extends Ui.WatchFace {

function xcallback1()
{
var x = 1;
//Ui.requestUpdate();
}

//! Load your resources here
function onLayout(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() {
var timer1 = new Timer.Timer();
timer1.start( method(:xcallback1), 5000, true );
}

//! Update the view
function onUpdate(dc) {

dc.setColor( Gfx.COLOR_BLACK, Gfx.COLOR_BLACK );
dc.clear();
dc.setColor( Gfx.COLOR_BLUE, Gfx.COLOR_TRANSPARENT );

dc.drawText( 0, 0, Gfx.FONT_NUMBER_THAI_HOT, "Test", Gfx.TEXT_JUSTIFY_LEFT );
}

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

//! 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() {

}

}

Here is the console output when runs
Found Transport: tcp
Connecting...
Connecting to device...
Device Version 0.1.0
Device id 1 name "A garmin device"
Shell Version 0.1.0
Copying file.... 3% complete
Copying file.... 6% complete
Copying file.... 10% complete
Copying file.... 13% complete
Copying file.... 17% complete
Copying file.... 20% complete
Copying file.... 23% complete
Copying file.... 27% complete
Copying file.... 30% complete
Copying file.... 34% complete
Copying file.... 37% complete
Copying file.... 41% complete
Copying file.... 44% complete
Copying file.... 47% complete
Copying file.... 51% complete
Copying file.... 54% complete
Copying file.... 58% complete
Copying file.... 61% complete
Copying file.... 65% complete
Copying file.... 68% complete
Copying file.... 71% complete
Copying file.... 75% complete
Copying file.... 78% complete
Copying file.... 82% complete
Copying file.... 85% complete
Copying file.... 89% complete
Copying file.... 92% complete
Copying file.... 95% complete
Copying file.... 99% complete
Copying file.... 100% complete
File pushed successfully
Connection Finished
Closing shell and port
Found Transport: tcp
Connecting...
Connecting to device...
Device Version 0.1.0
Device id 1 name "A garmin device"
Shell Version 0.1.0
Failed invoking <symbol>
Permission Required
in onShow (C:\_pjnr\Eclipse\workspace\WatchFace1\source\WatchFace1View.mc:24)
in onShow (C:\_pjnr\Eclipse\workspace\WatchFace1\source\WatchFace1View.mc:24)
Permission Required

Tried several variantions without luck.
Does anyone knows what this is about?


Best regards,
Paulo Reis
  • Former Member
    Former Member over 9 years ago
    Watchfaces are only allowed to use timers when they are out of sleep mode. Timers should be started in onExitSleep() and stopped in onEnterSleep(). If you use them outside of this context, you will get a permissions error like the one you have posted.
  • Thank you for your reply.
    Hum...so, how do I have seconds in a watchFace like "Digital" watch face?
  • Former Member
    Former Member over 9 years ago
    The native watch faces on the device do not run on ConnectIQ.

    I'll let you in on a little secret: The default watch face does not run on the main processor, but instead runs on a much lower power micro which doesn't have enough memory to run Connect IQ. It honestly doesn't have enough memory to hold the screen buffer; I work with some mad/crazy embedded hackers here at Garmin. All that for a second hand.

    We face similar challenges to other smart watches when it comes to balancing functionality and power usage. I think all smart watches have been figuring out how to offer the customize-ability of custom watch faces without killing the battery for the use cases the devices is intended before. We're starting with a conservative approach, but it could evolve over time.

    -Alpha Monkey
  • I am wearing a watch face that tics off the digital seconds when I

    I am wearing a watch face that tics off the digital seconds when I raise the device to look at it. So...how did the developer accomplish this?
  • That's by using onEnterSleep() and onExitSleep() which tell you when you enter and leave low power mode, like this:

    //! The user has just looked at their watch. Timers and animations may be started here.
    function onExitSleep() {
    inLowPower=false;
    Ui.requestUpdate();
    }

    //! Terminate any active timers and prepare for slow updates.
    function onEnterSleep() {
    inLowPower=true;
    Ui.requestUpdate();
    }


    what I do here is set a boolean to know what mode I'm in, and then request a screen update, and in onUpdate, act based on the boolean, and display seconds when not in low power mode.

    In a watchface, onUpdate() is called every minute when in low power mode, and every second when your not, so you don't really need your own timer at all.
  • Ahhh! Thank you so much. I didn't realize that the default behavior is to update 1/sec when exiting low power mode. I don't believe the SDK docs cover this (then again, I'm new to this so I may have missed it).

    That's by using onEnterSleep() and onExitSleep() which tell you when you enter and leave low power mode, like this:

    //! The user has just looked at their watch. Timers and animations may be started here.
    function onExitSleep() {
    inLowPower=false;
    Ui.requestUpdate();
    }

    //! Terminate any active timers and prepare for slow updates.
    function onEnterSleep() {
    inLowPower=true;
    Ui.requestUpdate();
    }


    what I do here is set a boolean to know what mode I'm in, and then request a screen update, and in onUpdate, act based on the boolean, and display seconds when not in low power mode.

    In a watchface, onUpdate() is called every minute when in low power mode, and every second when your not, so you don't really need your own timer at all.