Guest User

garmin forums r bad

a guest
Apr 8th, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. import Toybox.Lang;
  2. import Toybox.WatchUi;
  3. import Toybox.Activity;
  4.  
  5.  
  6. // implements a rolling average for numerical values
  7. class RollingAverage {
  8. // circular "queue" for rolling average data
  9. // (it's actually a simplified queue which is sufficient for our purposes)
  10. private var _data as Array<Numeric?>;
  11. private var _startIndex as Number = 0; // current start index of the queue
  12. private var _maxSize as Number; // max size of queue
  13. private var _currentSize as Number = 0; // current size of queue
  14.  
  15. function initialize(windowSize as Number) {
  16. _maxSize = windowSize;
  17. _data = new Array<Numeric>[_maxSize];
  18. }
  19.  
  20. public function reset() {
  21. _data = new Array<Numeric>[_maxSize];
  22. _currentSize = 0;
  23. _startIndex = 0;
  24. }
  25.  
  26. public function addValue(value as Numeric) {
  27. var currentIndex = (_startIndex + _currentSize) % _maxSize;
  28. _data[currentIndex] = value;
  29.  
  30. if (_currentSize < _maxSize) {
  31. _currentSize++;
  32. } else {
  33. _startIndex = (_startIndex + 1) % _maxSize;
  34. }
  35. }
  36.  
  37. // Get the rolling average for the current data set.
  38. // If the data set is not full, return null
  39. public function getAverage() as Float? {
  40. if (_currentSize < _maxSize) {
  41. return null;
  42. }
  43.  
  44. var total = 0;
  45. for (var i = 0; i < _maxSize; i++) {
  46. total += _data[i];
  47. }
  48. return total.toFloat() / _maxSize;
  49. }
  50. }
  51.  
  52. class MyDataFieldView extends WatchUi.DataField {
  53. // ...
  54. var _power3s = new RollingAverage(3);
  55. // ...
  56.  
  57. function compute(info as Activity.Info) as Void {
  58. // ...
  59. if (info has :currentPower) {
  60. var power = info.currentPower;
  61. if (power == null) {
  62. _power3s.reset();
  63. } else {
  64. _power3s.addValue(power);
  65. }
  66.  
  67. System.println("3s power = " + _power3s.getAverage());
  68. }
  69. // ...
  70. }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment