Hi,
I'm writing a data field, but am running into problems. My data field seems to run well, but sometimes I get this error screen:
What does it mean and how can I fix it?
Thanks in advance!
Lorenzo
With the circ buffer, it can be sped up once you have a total for all 10 elements. (lets say the total is 2500)
when you are going to replace, say, the 3rd element, subtract it's value from the 2500 (say 35), and add the value of the replacement (say 45). You'd have the new total (2510) without adding everything up again.
When you are building the initial 10 your total would start at 0, and as you add an element, you add it to the current total. So you never have to run down the array and add everything up. You keep a running total of what's in the array. With 10 items it might not be a big issue, but if you used 1000 samples, you'd save a boatload of processor!
using Toybox.WatchUi as Ui;
using Toybox.Math as Math;
using Toybox.Graphics as Gfx;
using Toybox.System as Sys;
class easyAvgView extends Ui.View {
var timer;
//the values to be averaged
var values=[0,0,0,0,0,0,0,0,0,0];
var valuesMax=10; //10 values
var valuesUsed=0; //how many of valuesMax are used
var valuesNext=0; //where to but next value;
var valuesTotal=0; //running total of values
var valuesAvg=0; //current avg of values
function initialize() {
timer = new Timer.Timer();
//have time run to provide values
timer.start( method(:onTimer), 1000*10, true );
}
function onTimer() {
//randowm number between 0 and 100
var num = Math.rand()%100;
addValue(num);
}
//! Update the view
function onUpdate(dc) {
dc.setColor(Gfx.COLOR_BLACK,Gfx.COLOR_BLACK);
dc.clear();
dc.setColor(Gfx.COLOR_WHITE,Gfx.COLOR_TRANSPARENT);
dc.drawText(dc.getWidth()/2,10,Gfx.FONT_LARGE,"avg="+valuesAvg.format("%.2f")+"\nwith "+valuesUsed+" values",Gfx.TEXT_JUSTIFY_CENTER);
}
function addValue(num) {
//
//update the running total
// this is how you avoid adding everything up for an average
//
//subtract value being replaced and add in new value
valuesTotal=valuesTotal-values[valuesNext]+num;
//store new one
values[valuesNext]=num;
//
// manage the circural buffer
//
//inc index for next location
valuesNext++;
//circle back to beginning if needed
if(valuesNext==valuesMax) {
valuesNext=0;
}
//
// values used will max out at 10 in this example, but will be less
// than that as the values are first being populated
//
if(valuesUsed<valuesMax) {
//was less than max
valuesUsed++;
}
valuesAvg=(valuesTotal*1.0)/valuesUsed;
Sys.println("used="+valuesUsed.format("%2d")+" next="+valuesNext.format("%2d")+" added="+num.format("%3d")+" total="+valuesTotal.format("%4d")+" avg="+valuesAvg.format("%.2f"));
//
// just so it's shown in the simulator
//
Ui.requestUpdate();
}
}