Battery Saving Technique - But Issue with onUpdate

For many of my data fields, I don't need to recompute the underlying metrics every second. For some, every 5 seconds is more than fine. So I'm trying this technique.

class DeviceView extends Toybox.WatchUi.DataField {
	const computeDelay = 5; // run compute logic every 5 seconds
	var computeCount = 1; // track compute iterations to reduce compute execution to save battery and compute load

	function compute(info) {
		if (computeCount <= 1) { computeCount = computeDelay; } else { computeCount--; return; }

    	// compute logic

    	return;
    }
}

Any potential issues with this? Seems to work fine.

I'm also thinking of doing the same with the code in onUpdate(). No need to repaint the screen every second either, right? Unless onShow() is called. Then you need to update the screen immediately. But when I do this, the screen blanks out for the duration of the delay, then paints for one second, then blanks out. I don't call dc.clear(). Any idea why the screen goes blank if onUpdate() is skipped?

class DeviceView extends Toybox.WatchUi.DataField {
	const updateDelay = 5; // run compute logic every 5 seconds
	var updateCount = 1; // track compute iterations to reduce compute execution to save battery and compute load

	function onShow() {
		updateCount = 1; // force onUpdate to run when DF shown
    	return;
    }

    function onUpdate(dc) {
		if (updateCount <= 1) { updateCount = updateDelay; } else { updateCount--; return; }

    	// screen update logic

    	return;
    }
}

  • You don't want to do it in onUpdate()  Try a simple test where you toggle a Boolean each time onUpdate is called, and if true, just do a return

    var skip=false;
    function onUpdate(dc) {
      skip=!skip;
      if(skip) {return;}
      //real code
    }

    You get a flashing DF.  The screen is cleared before onUpdate is called.

    the cycle is, screen cleared, separators (if needed) are drawn, then data for all the DFs is drawn

    It's something I'd use if I wanted a specific value in the DF to flash.

  • Wow - yep exactly. I'm surprised then that we don't see very fast a screen flash every second.... screen cleared, separators drawn, and then it takes a few milliseconds to execute the repaint logic. But I guess it happens so fast it isn't obvious. Thanks!