android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / test / src / FlightTable.cpp
bloba5f448bb2a12a6dc13b9b1db14fa4d334a4e1085
1 /* Copyright_License {
3 XCSoar Glide Computer - http://www.xcsoar.org/
4 Copyright (C) 2000-2013 The XCSoar Project
5 A detailed list of copyright holders can be found in the file "AUTHORS".
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "IGC/IGCParser.hpp"
24 #include "IGC/IGCFix.hpp"
25 #include "IO/FileLineReader.hpp"
26 #include "OS/FileUtil.hpp"
27 #include "Util/StaticString.hpp"
28 #include "Compiler.h"
30 #include <cstdio>
32 class FlightCheck {
33 StaticString<64> name;
35 unsigned year, month, day;
37 IGCFix previous, slow, fast, takeoff, landing;
38 bool previous_valid, takeoff_valid, landing_valid;
39 unsigned slow_count, fast_count;
41 public:
42 FlightCheck(const TCHAR *_name)
43 :name(_name),
44 year(0), month(0), day(0),
45 previous_valid(false), takeoff_valid(false),
46 slow_count(0), fast_count(0) {}
48 void date(unsigned _year, unsigned _month, unsigned _day) {
49 year = _year;
50 month = _month;
51 day = _day;
54 void print_flight() {
55 _tprintf(_T("%s,%04u-%02u-%02u,%02u:%02u,%02u:%02u\n"), name.c_str(),
56 year, month, day,
57 takeoff.time.hour, takeoff.time.minute,
58 landing.time.hour, landing.time.minute);
61 void fix(const IGCFix &fix);
62 void finish();
65 void
66 FlightCheck::fix(const IGCFix &fix)
68 if (!fix.gps_valid)
69 return;
71 if (previous_valid && fix.time > previous.time) {
72 fixed distance = fix.location.Distance(previous.location);
73 fixed speed = distance / (fix.time.GetSecondOfDay() - previous.time.GetSecondOfDay());
74 if (speed > fixed(15)) {
75 if (fast_count == 0)
76 fast = fix;
78 ++fast_count;
79 } else
80 fast_count = 0;
82 if (speed < fixed(5)) {
83 if (slow_count == 0)
84 slow = fix;
85 ++slow_count;
86 } else
87 slow_count = 0;
89 if (takeoff_valid) {
90 if (slow_count > 10) {
91 landing = slow;
92 landing_valid = true;
94 print_flight();
95 takeoff_valid = landing_valid = false;
97 } else {
98 if (fast_count > 10) {
99 takeoff = fast;
100 takeoff_valid = true;
105 previous = fix;
106 previous_valid = true;
109 void
110 FlightCheck::finish()
112 if (takeoff_valid) {
113 landing = previous;
114 landing_valid = true;
116 print_flight();
120 class IGCFileVisitor : public File::Visitor {
121 virtual void Visit(const TCHAR *path, const TCHAR *filename);
124 void
125 IGCFileVisitor::Visit(const TCHAR *path, const TCHAR *filename)
127 FileLineReaderA reader(path);
128 if (reader.error()) {
129 _ftprintf(stderr, _T("Failed to open %s\n"), path);
130 return;
133 FlightCheck flight(filename);
134 char *line;
135 while ((line = reader.ReadLine()) != NULL) {
136 unsigned day, month, year;
138 IGCFix fix;
139 if (IGCParseFix(line, fix))
140 flight.fix(fix);
141 else if (sscanf(line, "HFDTE%02u%02u%02u", &day, &month, &year)) {
142 /* damn you, Y2K bug! */
143 if (year > 80)
144 year += 1900;
145 else
146 year += 2000;
148 flight.date(year, month, day);
152 flight.finish();
155 int main(gcc_unused int argc, gcc_unused char **argv)
157 IGCFileVisitor visitor;
158 Directory::VisitSpecificFiles(_T("."), _T("*.igc"), visitor);
159 return 0;