Renderer, ...: use PixelRect::GetCenter()
[xcsoar.git] / src / Device / Driver / FLARM / Mode.cpp
blobabb64c8b20fd84282767becf65f3dd9a3912643e
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 "Device.hpp"
25 #include "Device/Port/Port.hpp"
26 #include "Operation/Operation.hpp"
28 bool
29 FlarmDevice::EnableNMEA(OperationEnvironment &env)
31 switch (mode) {
32 case Mode::UNKNOWN:
33 /* device could be in binary mode, we don't know, but this is the
34 best we can do: */
35 if (!BinaryReset(env, 500))
36 return false;
38 mode = Mode::NMEA;
40 /* request self-test results and version information from FLARM */
41 Send("PFLAE,R", env);
42 Send("PFLAV,R", env);
43 return true;
45 case Mode::NMEA:
46 return true;
48 case Mode::TEXT:
49 /* no real difference between NMEA and TEXT; in mode==TEXT, the
50 Port thread is stopped, but the caller is responsible for
51 restarting it, which means there's nothing to do for us */
52 mode = Mode::NMEA;
53 return true;
55 case Mode::BINARY:
56 if (!BinaryReset(env, 500)) {
57 mode = Mode::UNKNOWN;
58 return false;
61 mode = Mode::NMEA;
62 return true;
65 gcc_unreachable();
66 assert(false);
67 return false;
70 bool
71 FlarmDevice::BinaryMode(OperationEnvironment &env)
73 if (mode == Mode::BINARY)
74 return true;
76 port.StopRxThread();
78 // "Binary mode is engaged by sending the text command "$PFLAX"
79 // (including a newline character) to Flarm."
80 if (!Send("PFLAX", env))
81 return false;
83 // Remember that we should now be in binary mode (for further assert() calls)
84 mode = Mode::BINARY;
86 // "After switching, connection should again be checked by issuing a ping."
87 // Testing has revealed that switching the protocol takes a certain amount
88 // of time (around 1.5 sec). Due to that it is recommended to issue new pings
89 // for a certain time until the ping is ACKed properly or a timeout occurs.
90 for (unsigned i = 0; i < 10; ++i) {
91 if (env.IsCancelled()) {
92 BinaryReset(env, 200);
93 mode = Mode::UNKNOWN;
94 return false;
97 if (BinaryPing(env, 500))
98 // We are now in binary mode and have verified that with a binary ping
99 return true;
102 // Apparently the switch to binary mode didn't work
103 mode = Mode::UNKNOWN;
104 return false;