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