Renderer, ...: use PixelRect::GetCenter()
[xcsoar.git] / src / Device / Driver / Vega / Parser.cpp
blobf23954b718ef4b17e253033f3edbe27c581b0ebc
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 "Internal.hpp"
25 #include "Message.hpp"
26 #include "NMEA/Info.hpp"
27 #include "NMEA/InputLine.hpp"
28 #include "Compiler.h"
29 #include "Util/Macros.hpp"
31 #include <tchar.h>
32 #include <stdio.h>
33 #include <algorithm>
35 #ifdef _UNICODE
36 #include <windows.h>
37 #endif
39 static bool
40 PDSWC(NMEAInputLine &line, NMEAInfo &info, Vega::VolatileData &volatile_data)
42 unsigned value;
43 if (line.ReadChecked(value) &&
44 info.settings.ProvideMacCready(fixed(value) / 10, info.clock))
45 volatile_data.mc = value;
47 auto &switches = info.switch_state;
48 auto &vs = switches.vega;
49 vs.inputs = line.ReadHex(0);
50 vs.outputs = line.ReadHex(0);
52 if (vs.GetFlapLanding())
53 switches.flap_position = SwitchState::FlapPosition::LANDING;
54 else if (vs.GetFlapZero())
55 switches.flap_position = SwitchState::FlapPosition::NEUTRAL;
56 else if (vs.GetFlapNegative())
57 switches.flap_position = SwitchState::FlapPosition::NEGATIVE;
58 else if (vs.GetFlapPositive())
59 switches.flap_position = SwitchState::FlapPosition::POSITIVE;
60 else
61 switches.flap_position = SwitchState::FlapPosition::UNKNOWN;
63 if (vs.GetUserSwitchMiddle())
64 switches.user_switch = SwitchState::UserSwitch::MIDDLE;
65 else if (vs.GetUserSwitchUp())
66 switches.user_switch = SwitchState::UserSwitch::UP;
67 else if (vs.GetUserSwitchDown())
68 switches.user_switch = SwitchState::UserSwitch::DOWN;
69 else
70 switches.user_switch = SwitchState::UserSwitch::UNKNOWN;
72 if (vs.GetAirbrakeLocked())
73 switches.airbrake_state = SwitchState::AirbrakeState::LOCKED;
74 else if (vs.GetAirbrakeNotLocked())
75 switches.airbrake_state = SwitchState::AirbrakeState::NOT_LOCKED;
76 else
77 switches.airbrake_state = SwitchState::AirbrakeState::UNKNOWN;
79 switches.flight_mode = vs.GetCircling()
80 ? SwitchState::FlightMode::CIRCLING
81 : SwitchState::FlightMode::CRUISE;
83 if (line.ReadChecked(value)) {
84 info.voltage = fixed(value) / 10;
85 info.voltage_available.Update(info.clock);
88 return true;
91 //#include "Audio/VarioSound.h"
93 static bool
94 PDAAV(NMEAInputLine &line, gcc_unused NMEAInfo &info)
96 gcc_unused unsigned short beepfrequency = line.Read(0);
97 gcc_unused unsigned short soundfrequency = line.Read(0);
98 gcc_unused unsigned char soundtype = line.Read(0);
100 // Temporarily commented out - function as yet undefined
101 // audio_setconfig(beepfrequency, soundfrequency, soundtype);
103 return true;
106 bool
107 VegaDevice::PDVSC(NMEAInputLine &line, gcc_unused NMEAInfo &info)
109 char responsetype[10];
110 line.Read(responsetype, 10);
112 char name[80];
113 line.Read(name, 80);
115 if (strcmp(name, "ERROR") == 0)
116 // ignore error responses...
117 return true;
119 int value = line.Read(0);
121 if (strcmp(name, "ToneDeadbandCruiseLow") == 0)
122 value = std::max(value, -value);
123 if (strcmp(name, "ToneDeadbandCirclingLow") == 0)
124 value = std::max(value, -value);
126 settings.Lock();
127 settings.Set(name, value);
128 settings.Unlock();
130 return true;
134 // $PDVDV,vario,ias,densityratio,altitude,staticpressure
136 static bool
137 PDVDV(NMEAInputLine &line, NMEAInfo &info)
139 int value;
141 if (line.ReadChecked(value))
142 info.ProvideTotalEnergyVario(fixed(value) / 10);
144 bool ias_available = line.ReadChecked(value);
145 int tas_ratio = line.Read(1024);
146 if (ias_available) {
147 const fixed ias = fixed(value) / 10;
148 info.ProvideBothAirspeeds(ias, ias * tas_ratio / 1024);
151 //hasVega = true;
153 if (line.ReadChecked(value))
154 info.ProvidePressureAltitude(fixed(value));
156 return true;
160 // $PDVDS,nx,nz,flap,stallratio,netto
161 static bool
162 PDVDS(NMEAInputLine &line, NMEAInfo &info)
164 const int accel_x = line.Read(0), accel_z = line.Read(0);
166 fixed mag = SmallHypot(fixed(accel_x), fixed(accel_z));
167 info.acceleration.ProvideGLoad(mag / 100, true);
170 double flap = line.Read(0.0);
172 line.Skip();
174 info.stall_ratio = line.Read(fixed(0));
175 info.stall_ratio_available.Update(info.clock);
177 int value;
178 if (line.ReadChecked(value))
179 info.ProvideNettoVario(fixed(value) / 10);
181 //hasVega = true;
183 return true;
186 static bool
187 PDVVT(NMEAInputLine &line, NMEAInfo &info)
189 int value;
190 info.temperature_available = line.ReadChecked(value);
191 if (info.temperature_available)
192 info.temperature = fixed(value) / 10;
194 info.humidity_available = line.ReadChecked(info.humidity);
196 return true;
199 // PDTSM,duration_ms,"free text"
200 static bool
201 PDTSM(NMEAInputLine &line, gcc_unused NMEAInfo &info)
204 int duration = (int)strtol(String, NULL, 10);
206 line.Skip();
208 const auto message = line.Rest();
210 StaticString<256> buffer;
211 buffer.SetASCII(message.begin(), message.end());
213 // todo duration handling
214 Message::AddMessage(_T("VEGA:"), buffer);
216 return true;
219 bool
220 VegaDevice::ParseNMEA(const char *String, NMEAInfo &info)
222 NMEAInputLine line(String);
223 char type[16];
224 line.Read(type, 16);
226 if (memcmp(type, "$PD", 3) == 0)
227 detected = true;
229 if (strcmp(type, "$PDSWC") == 0)
230 return PDSWC(line, info, volatile_data);
231 else if (strcmp(type, "$PDAAV") == 0)
232 return PDAAV(line, info);
233 else if (strcmp(type, "$PDVSC") == 0)
234 return PDVSC(line, info);
235 else if (strcmp(type, "$PDVDV") == 0)
236 return PDVDV(line, info);
237 else if (strcmp(type, "$PDVDS") == 0)
238 return PDVDS(line, info);
239 else if (strcmp(type, "$PDVVT") == 0)
240 return PDVVT(line, info);
241 else if (strcmp(type, "$PDVSD") == 0) {
242 const auto message = line.Rest();
243 StaticString<256> buffer;
244 buffer.SetASCII(message.begin(), message.end());
245 Message::AddMessage(buffer);
246 return true;
247 } else if (strcmp(type, "$PDTSM") == 0)
248 return PDTSM(line, info);
249 else
250 return false;