2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
22 #include <sys/types.h>
26 #include <semaphore.h>
27 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
28 #define AO_GPS_NUM_SAT_SHIFT (0)
30 #define AO_GPS_VALID (1 << 4)
37 int32_t latitude; /* degrees * 10⁷ */
38 int32_t longitude; /* degrees * 10⁷ */
39 int16_t altitude; /* m */
40 uint16_t ground_speed; /* cm/s */
41 uint8_t course; /* degrees / 2 */
42 uint8_t hdop; /* * 5 */
43 int16_t climb_rate; /* cm/s */
44 uint16_t h_error; /* m */
45 uint16_t v_error; /* m */
49 ao_mutex_get(uint8_t *mutex)
54 ao_mutex_put(uint8_t *mutex)
70 sprintf (line, "\\%02x", ((int) c) & 0xff);
72 sprintf (line, "%c", c);
74 write(1, line, strlen(line));
77 #define QUEUE_LEN 4096
79 static char input_queue[QUEUE_LEN];
80 int input_head, input_tail;
82 static sem_t input_semaphore;
85 ao_serial_getchar(void)
91 sem_getvalue(&input_semaphore, &value);
92 // printf ("ao_serial_getchar %d\n", value);
93 sem_wait(&input_semaphore);
94 c = input_queue[input_head];
95 input_head = (input_head + 1) % QUEUE_LEN;
96 // sprintf (line, "%02x\n", ((int) c) & 0xff);
97 // write(1, line, strlen(line));
102 ao_gps_input(void *arg)
107 printf("ao_gps_input\n");
109 i = read(ao_gps_fd, &c, 1);
113 input_queue[input_tail] = c;
114 input_tail = (input_tail + 1) % QUEUE_LEN;
115 sem_post(&input_semaphore);
116 sem_getvalue(&input_semaphore, &v);
117 // printf ("ao_gps_input %02x %d\n", ((int) c) & 0xff, v);
121 if (i < 0 && (errno == EINTR || errno == EAGAIN))
129 ao_serial_putchar(char c)
135 i = write(ao_gps_fd, &c, 1);
138 if (i < 0 && (errno == EINTR || errno == EAGAIN))
146 ao_serial_set_speed(uint8_t fast)
149 struct termios termios;
152 tcgetattr(fd, &termios);
153 cfsetspeed(&termios, fast ? B9600 : B4800);
154 tcsetattr(fd, TCSAFLUSH, &termios);
155 tcflush(fd, TCIFLUSH);
158 #include "ao_gps_print.c"
164 printf ("dump state\n");
168 ao_gps_open(const char *tty)
170 struct termios termios;
173 fd = open (tty, O_RDWR);
177 tcgetattr(fd, &termios);
179 cfsetspeed(&termios, B4800);
180 tcsetattr(fd, TCSAFLUSH, &termios);
183 tcflush(fd, TCIFLUSH);
187 pthread_t input_thread;
190 main (int argc, char **argv)
192 char *gps_file = "/dev/ttyUSB0";
194 ao_gps_fd = ao_gps_open(gps_file);
200 sem_init(&input_semaphore, 0, 0);
201 if (pthread_create(&input_thread, NULL, ao_gps_input, NULL) != 0)
202 perror("pthread_create");