Renderer, ...: use PixelRect::GetCenter()
[xcsoar.git] / src / Computer / AutoQNH.cpp
blob8bdbf74ba2527a536a687c3139b38aa8a738982f
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 "AutoQNH.hpp"
25 #include "NMEA/Info.hpp"
26 #include "NMEA/Derived.hpp"
27 #include "Settings.hpp"
28 #include "Engine/Waypoint/Waypoints.hpp"
30 void
31 AutoQNH::Process(const NMEAInfo &basic, DerivedInfo &calculated,
32 const ComputerSettings &settings_computer, const Waypoints &way_points)
34 if (!calculated.flight.on_ground // must be on ground
35 || IsFinished() // only do it once
36 || !basic.location_available // Reject if no valid GPS fix
37 || !basic.static_pressure_available // Reject if no pressure
38 || settings_computer.pressure_available // Reject if QNH already known
39 ) {
40 if (!IsFinished())
41 Reset(); // restart if havent performed
43 return;
46 if (!IsFinished())
47 countdown_autoqnh--;
49 if (!countdown_autoqnh) {
50 if (CalculateQNH(basic, calculated, way_points))
51 countdown_autoqnh = UINT_MAX; // disable after performing once
52 else
53 Reset();
57 void
58 AutoQNH::Reset()
60 countdown_autoqnh = QNH_TIME;
63 inline bool
64 AutoQNH::CalculateQNH(const NMEAInfo &basic, DerivedInfo &calculated,
65 const Waypoints &way_points)
67 const Waypoint *next_wp;
68 next_wp = way_points.LookupLocation(basic.location, fixed(1000));
70 if (next_wp && next_wp->IsAirport())
71 CalculateQNH(basic, calculated, next_wp->elevation);
72 else if (calculated.terrain_valid)
73 CalculateQNH(basic, calculated, calculated.terrain_altitude);
74 else
75 return false;
77 return true;
80 inline void
81 AutoQNH::CalculateQNH(const NMEAInfo &basic, DerivedInfo &calculated,
82 fixed altitude)
84 calculated.pressure = AtmosphericPressure::FindQNHFromPressure(basic.static_pressure, altitude);
85 calculated.pressure_available.Update(basic.clock);