class XDataField extends Ui.SimpleDataField
{
hidden var _M_samples;
function initialize(seconds) {
SimpleDataField.initialize();
_M_samples = new [seconds];
_M_index = 0;
_M_count = 0;
}
function compute(info) {
if (info.currentPower == null) {
return null;
}
_M_samples[_M_index] = info.currentPower;
_M_index = (_M_index + 1) % _M_samples.size();
if (_M_samples.size() != _M_count) {
_M_count += 1;
}
var sum = 0.0;
for (var i = 0; i < _M_count; ++i) {
sum += _M_samples;
}
return (sum / _M_count).toNumber();
}
}
[/code]
I think the following is another reasonable way to implement it, but it is a filtered value (it won't change as quickly as the one above).
class XDataField extends Ui.SimpleDataField
{
hidden var _M_seconds;
hidden var _M_average;
function initialize(seconds) {
SimpleDataField.initialize();
_M_seconds = seconds;
}
function compute(info) {
if (info.currentPower == null) {
return null;
}
if (_M_average == null) {
_M_average = info.currentPower;
}
else {
_M_average = ((_M_average * (_M_seconds - 1)) + info.currentPower) / (_M_seconds * 1.0);
}
return _M_average.toNumber();
}
}
You may have to implement the timer callback functions (onTimerStop(), onTimerPause(), onTimerReset()) to finish this, but the above should be a good start.
Travis
If you just want three second average power, you can store the last three power values and average them. I'm just throwing this code out there, it may not be perfect..
class XDataField extends Ui.SimpleDataField
{
hidden var _M_samples;
function initialize(seconds) {
SimpleDataField.initialize();
_M_samples = new [seconds];
_M_index = 0;
_M_count = 0;
}
function compute(info) {
if (info.currentPower == null) {
return null;
}
_M_samples[_M_index] = info.currentPower;
_M_index = (_M_index + 1) % _M_samples.size();
if (_M_samples.size() != _M_count) {
_M_count += 1;
}
var sum = 0.0;
for (var i = 0; i < _M_count; ++i) {
sum += _M_samples;
}
return (sum / _M_count).toNumber();
}
}
[/code]
I think the following is another reasonable way to implement it, but it is a filtered value (it won't change as quickly as the one above).
class XDataField extends Ui.SimpleDataField
{
hidden var _M_seconds;
hidden var _M_average;
function initialize(seconds) {
SimpleDataField.initialize();
_M_seconds = seconds;
}
function compute(info) {
if (info.currentPower == null) {
return null;
}
if (_M_average == null) {
_M_average = info.currentPower;
}
else {
_M_average = (_M_average + _M_average + info.currentPower) / 3.0;
}
return _M_average.toNumber();
}
}
Travis
This is really helpful, thank you very much!