]> err.no Git - mapper/commitdiff
Add a simple gps simulator type. Move some code into functions.
authorKaj-Michael Lang <milang@onion.tal.org>
Thu, 24 Jan 2008 16:25:51 +0000 (18:25 +0200)
committerKaj-Michael Lang <milang@onion.tal.org>
Thu, 24 Jan 2008 16:25:51 +0000 (18:25 +0200)
src/gps.c

index 54e156ebb357b489c3436502f77edc47c57c0144..61cdc4e026f4febde7c5350a941831ce9ec01b92 100644 (file)
--- a/src/gps.c
+++ b/src/gps.c
@@ -293,6 +293,51 @@ if (gps->io.type==GPS_IO_BLUEZ_DBUS && gps->io.rfcomm_req_proxy) {
 
 }
 
+static gboolean
+gps_simulate(Gps *gps)
+{
+g_assert(gps);
+
+gps->data.lat+=g_random_double_range(-0.0002,0.0002);
+gps->data.lon+=g_random_double_range(-0.0002,0.0002);
+BOUND(gps->data.lat, -80.0, 80.0);
+BOUND(gps->data.lon, -80.0, 80.0);
+gps->data.speed=1+g_random_double_range(0.0,5.0);
+gps->data.heading+=g_random_double_range(-2.0,2.0);
+gps->data.time=time(NULL);
+gps_data_integerize(&gps->data);
+track_add(gps, FALSE);
+gps->data.lheading=gps->data.heading;
+map_refresh_mark();
+
+return TRUE;
+}
+
+/**
+ * Reset GPS read buffer 
+ */
+static void
+gps_read_buffer_prepare(Gps *gps)
+{
+gps->io.curr=gps->io.buffer;
+*gps->io.curr = '\0';
+gps->io.last=gps->io.buffer+GPS_READ_BUF_SIZE-1;
+}
+
+/**
+ * Connect file descriptor to channel and add watches. 
+ */
+static void
+gps_read_channel_connect(Gps *gps)
+{
+gps->io.channel=g_io_channel_unix_new(gps->io.fd);
+g_io_channel_set_encoding(gps->io.channel, NULL, NULL);
+g_io_channel_set_flags(gps->io.channel, G_IO_FLAG_NONBLOCK, NULL);
+gps->io.error_sid=g_io_add_watch_full(gps->io.channel, G_PRIORITY_HIGH_IDLE, G_IO_ERR | G_IO_HUP, gps_channel_cb_error, NULL, NULL);
+gps->io.connect_sid=g_io_add_watch_full(gps->io.channel, G_PRIORITY_HIGH_IDLE, G_IO_OUT, gps_channel_cb_connect, NULL, NULL);
+gps->io.clater_sid=0;
+}
+
 /**
  * Connect to the receiver.
  * This method assumes that fd is -1 and _channel is NULL.  If unsure, call
@@ -306,6 +351,7 @@ gboolean
 gps_connect_now(Gps *gps)
 {
 g_assert(gps);
+g_debug("GPS: Connecting GPS type %d to %s:%d\n", gps->io.type, gps->io.address, gps->io.port);
 
 switch (gps->io.type) {
        case GPS_IO_FILE:
@@ -382,23 +428,23 @@ switch (gps->io.type) {
 #endif
                return FALSE;
        break;
+       case GPS_IO_SIMULATION:
+               /* Set a periodic cb to generate random movement */
+               gps->data.lat=64.22;
+               gps->data.lat=22.22;
+               gps->io.conn=RCVR_FIXED;
+               gps->data.fix=FIX_2D;
+               gps_data_integerize(&gps->data);
+               gps->io.input_sid=g_timeout_add(1000, (GSourceFunc)gps_simulate, gps);
+               return FALSE;
+       break;
        default:
                return FALSE;
        break;
 }
 
-/* Reset GPS read buffer */
-gps->io.curr=gps->io.buffer;
-*gps->io.curr = '\0';
-gps->io.last=gps->io.buffer+GPS_READ_BUF_SIZE-1;
-
-/* Create channel and add watches. */
-gps->io.channel=g_io_channel_unix_new(gps->io.fd);
-g_io_channel_set_encoding(gps->io.channel, NULL, NULL);
-g_io_channel_set_flags(gps->io.channel, G_IO_FLAG_NONBLOCK, NULL);
-gps->io.error_sid=g_io_add_watch_full(gps->io.channel, G_PRIORITY_HIGH_IDLE, G_IO_ERR | G_IO_HUP, gps_channel_cb_error, NULL, NULL);
-gps->io.connect_sid=g_io_add_watch_full(gps->io.channel, G_PRIORITY_HIGH_IDLE, G_IO_OUT, gps_channel_cb_connect, NULL, NULL);
-gps->io.clater_sid=0;
+gps_read_buffer_prepare(gps);
+gps_read_channel_connect(gps);
 gps_connect(gps);
 return FALSE;
 }