]> git.gag.com Git - fw/altos/blob - ao-tools/ao-view/aoview_monitor.c
altos: Compute desired frequency when upgrading from pre-1.1
[fw/altos] / ao-tools / ao-view / aoview_monitor.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "aoview.h"
19
20 static struct aoview_serial *monitor_serial;
21
22 #define MONITOR_LEN     1024
23
24 static char     monitor_line[MONITOR_LEN + 1];
25 static int      monitor_pos;
26
27 void
28 aoview_monitor_disconnect(void)
29 {
30         if (monitor_serial) {
31                 aoview_serial_close(monitor_serial);
32                 monitor_serial = NULL;
33         }
34         aoview_log_new();
35 }
36
37 gboolean
38 aoview_monitor_parse(const char *input_line)
39 {
40         struct cc_telem telem;
41
42         if (!cc_telem_parse(input_line, &telem))
43                 return FALSE;
44         aoview_state_notify(&telem);
45         return TRUE;
46 }
47
48 static void
49 aoview_monitor_callback(gpointer user_data,
50                         struct aoview_serial *serial,
51                         gint revents)
52 {
53         int     c;
54
55         if (revents & (G_IO_HUP|G_IO_ERR)) {
56                 aoview_monitor_disconnect();
57                 return;
58         }
59         if (revents & G_IO_IN) {
60                 for (;;) {
61                         c = aoview_serial_getc(serial);
62                         if (c == -1)
63                                 break;
64                         if (c == '\r')
65                                 continue;
66                         if (c == '\n') {
67                                 monitor_line[monitor_pos] = '\0';
68                                 if (monitor_pos) {
69                                         if (aoview_monitor_parse(monitor_line)) {
70                                                 aoview_log_set_serial(aostate.data.serial);
71                                                 aoview_log_set_flight(aostate.data.flight);
72                                                 if (aoview_log_get_serial())
73                                                         aoview_log_printf ("%s\n", monitor_line);
74                                         }
75                                 }
76                                 monitor_pos = 0;
77                         } else if (monitor_pos < MONITOR_LEN)
78                                 monitor_line[monitor_pos++] = c;
79                 }
80         }
81 }
82
83 void
84 aoview_monitor_set_channel(int channel)
85 {
86         if (monitor_serial) {
87                 aoview_serial_printf(monitor_serial, "m 0\n");
88                 aoview_serial_printf(monitor_serial, "c r %d\n", channel);
89                 aoview_serial_printf(monitor_serial, "m 1\n");
90         }
91 }
92
93 gboolean
94 aoview_monitor_connect(char *tty)
95 {
96         int     channel;
97         aoview_monitor_disconnect();
98         monitor_serial = aoview_serial_open(tty);
99         if (!monitor_serial)
100                 return FALSE;
101         aoview_table_clear();
102         aoview_state_reset();
103         channel = aoview_channel_current();
104         aoview_monitor_set_channel(channel);
105         aoview_serial_set_callback(monitor_serial,
106                                    aoview_monitor_callback);
107         return TRUE;
108 }