4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "ClimbAverageCalculator.hpp"
27 ClimbAverageCalculator::Reset()
30 for (int i
= 0; i
< MAX_HISTORY
; i
++)
35 ClimbAverageCalculator::GetAverage(fixed time
, fixed altitude
, fixed average_time
)
37 assert(average_time
<= fixed(MAX_HISTORY
));
41 // Don't update newestValIndex if the time didn't move forward
42 if (newestValIndex
< 0 ||
43 !history
[newestValIndex
].IsDefined() ||
44 time
> history
[newestValIndex
].time
)
45 newestValIndex
= newestValIndex
< MAX_HISTORY
- 1 ? newestValIndex
+ 1 : 0;
48 history
[newestValIndex
] = HistoryItem(time
, altitude
);
50 // initially bestHistory is the current...
51 bestHistory
= newestValIndex
;
53 // now run through the history and find the best sample
54 // for average period within the average time period
55 for (int i
= 0; i
< MAX_HISTORY
; i
++) {
56 if (!history
[i
].IsDefined())
59 // outside the period -> skip value
60 if (history
[i
].time
+ average_time
< time
)
63 // is the sample older (and therefore better) than the current found ?
64 if (history
[i
].time
< history
[bestHistory
].time
)
68 // calculate the average !
69 if (bestHistory
!= newestValIndex
)
70 return (altitude
- history
[bestHistory
].altitude
) /
71 (time
- history
[bestHistory
].time
);
77 ClimbAverageCalculator::Expired(fixed now
, fixed max_age
) const
79 if (newestValIndex
< 0)
82 auto item
= history
[newestValIndex
];
83 if (!item
.IsDefined())
86 return now
< item
.time
|| now
> item
.time
+ max_age
;