Revert "Device/Driver/LX: Add small delay after baud rate change"
[xcsoar.git] / test / src / FeedFlyNetData.cpp
blobd4a06350f987b1acffb1e312b8417137da774447
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 "Util/StaticString.hpp"
25 #include "Math/fixed.hpp"
26 #include "Time/PeriodClock.hpp"
27 #include "OS/SocketDescriptor.hpp"
28 #include "OS/SocketAddress.hpp"
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <stdlib.h>
37 int main(int argc, char **argv)
39 // Determine on which TCP port to connect to the server
40 const char *tcp_port;
41 if (argc < 2) {
42 fprintf(stderr, "This program opens a TCP connection to a server which is assumed ");
43 fprintf(stderr, "to be at 127.0.0.1, and sends artificial FlyNet vario data.\n\n");
44 fprintf(stderr, "Usage: %s PORT\n", argv[0]);
45 fprintf(stderr, "Defaulting to port 4353\n");
46 tcp_port = "4353";
47 } else {
48 tcp_port = argv[1];
51 // Convert IP address to binary form
52 SocketAddress server_address;
53 if (!server_address.Lookup("127.0.0.1", tcp_port, AF_INET)) {
54 fprintf(stderr, "Failed to look up address\n");
55 exit(EXIT_FAILURE);
58 // Create socket for the outgoing connection
59 SocketDescriptor sock;
60 if (!sock.CreateTCP()) {
61 perror("Socket");
62 exit(EXIT_FAILURE);
65 // Connect to the specified server
66 if (!sock.Connect(server_address))
68 perror("Connect");
69 exit(EXIT_FAILURE);
72 PeriodClock start_clock;
73 start_clock.Update();
75 PeriodClock pressure_clock;
76 PeriodClock battery_clock;
78 fixed pressure = fixed(101300);
79 unsigned battery_level = 11;
80 while (true) {
81 if (pressure_clock.CheckUpdate(48)) {
82 NarrowString<16> sentence;
84 int elapsed_ms = start_clock.Elapsed();
85 fixed elapsed = fixed(elapsed_ms) / 1000;
86 fixed vario = sin(elapsed / 3) * cos(elapsed / 10) *
87 cos(elapsed / 20 + fixed(2)) * 3;
89 fixed pressure_vario = -vario * fixed(12.5);
90 fixed delta_pressure = pressure_vario * 48 / 1000;
91 pressure += delta_pressure;
93 sentence = "_PRS ";
94 sentence.AppendFormat("%08X", uround(pressure));
95 sentence += "\n";
97 sock.Write(sentence.c_str(), sentence.length());
100 if (battery_clock.CheckUpdate(11000)) {
101 NarrowString<16> sentence;
103 sentence = "_BAT ";
104 if (battery_level <= 10)
105 sentence.AppendFormat("%X", battery_level);
106 else
107 sentence += "*";
109 sentence += "\n";
110 sock.Write(sentence.c_str(), sentence.length());
112 if (battery_level == 0)
113 battery_level = 11;
114 else
115 battery_level--;
119 return EXIT_SUCCESS;