android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / src / Computer / ClimbAverageCalculator.cpp
blobd724ebc7e6247b6d4b6c7f6e94e5391698c6c9cc
1 /*
2 Copyright_License {
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"
26 void
27 ClimbAverageCalculator::Reset()
29 newestValIndex = -1;
30 for (int i = 0; i < MAX_HISTORY; i++)
31 history[i].Reset();
34 fixed
35 ClimbAverageCalculator::GetAverage(fixed time, fixed altitude, fixed average_time)
37 assert(average_time <= fixed(MAX_HISTORY));
39 int bestHistory;
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;
47 // add the new sample
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())
57 continue;
59 // outside the period -> skip value
60 if (history[i].time + average_time < time)
61 continue;
63 // is the sample older (and therefore better) than the current found ?
64 if (history[i].time < history[bestHistory].time)
65 bestHistory = i;
68 // calculate the average !
69 if (bestHistory != newestValIndex)
70 return (altitude - history[bestHistory].altitude) /
71 (time - history[bestHistory].time);
73 return fixed(0);
76 bool
77 ClimbAverageCalculator::Expired(fixed now, fixed max_age) const
79 if (newestValIndex < 0)
80 return true;
82 auto item = history[newestValIndex];
83 if (!item.IsDefined())
84 return true;
86 return now < item.time || now > item.time + max_age;