/[zanavi_public1]/navit/navit/vehicle/demo/vehicle_demo.c
ZANavi

Contents of /navit/navit/vehicle/demo/vehicle_demo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (show annotations) (download)
Mon Apr 9 21:27:36 2012 UTC (10 years, 7 months ago) by zoff99
File MIME type: text/plain
File size: 6805 byte(s)
lots of new stuff, tranlsations, bug fixes ...
1 /**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2011-2012 Zoff <zoff@zoff.cc>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * Navit, a modular navigation system.
22 * Copyright (C) 2005-2008 Navit Team
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * version 2 as published by the Free Software Foundation.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the
35 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
36 * Boston, MA 02110-1301, USA.
37 */
38
39 #include <glib.h>
40 #include <string.h>
41 #include "config.h"
42 #include "debug.h"
43 #include "coord.h"
44 #include "item.h"
45 #include "navit.h"
46 #include "map.h"
47 #include "route.h"
48 #include "callback.h"
49 #include "transform.h"
50 #include "plugin.h"
51 #include "vehicle.h"
52 #include "event.h"
53 #include "util.h"
54
55 struct vehicle_priv
56 {
57 int interval;
58 int position_set;
59 struct callback_list *cbl;
60 struct navit *navit;
61 struct coord_geo geo;
62 struct coord last;
63 double config_speed;
64 double speed;
65 double direction;
66 struct callback *timer_callback;
67 struct event_timeout *timer;
68 char *timep;
69
70 };
71
72 static void vehicle_demo_destroy(struct vehicle_priv *priv)
73 {
74 g_free(priv->timep);
75 g_free(priv);
76 }
77
78 static int vehicle_demo_position_attr_get(struct vehicle_priv *priv, enum attr_type type, struct attr *attr)
79 {
80 switch (type)
81 {
82 case attr_position_speed:
83 attr->u.numd = &priv->speed;
84 break;
85 case attr_position_direction:
86 attr->u.numd = &priv->direction;
87 break;
88 case attr_position_coord_geo:
89 attr->u.coord_geo = &priv->geo;
90 break;
91 case attr_position_time_iso8601:
92 g_free(priv->timep);
93 priv->timep = current_to_iso8601();
94 attr->u.str = priv->timep;
95 break;
96 default:
97 return 0;
98 }
99 attr->type = type;
100 return 1;
101 }
102
103 static int vehicle_demo_set_attr(struct vehicle_priv *priv, struct attr *attr)
104 {
105 if (attr->type == attr_navit)
106 priv->navit = attr->u.navit;
107 return 1;
108 }
109
110 struct vehicle_methods vehicle_demo_methods =
111 { vehicle_demo_destroy, vehicle_demo_position_attr_get, vehicle_demo_set_attr, };
112
113 static void vehicle_demo_timer(struct vehicle_priv *priv)
114 {
115 struct coord c, c2, pos, ci;
116 int slen, len, dx, dy;
117 struct route *route = NULL;
118 struct map *route_map = NULL;
119 struct map_rect *mr = NULL;
120 struct item *item = NULL;
121
122 len = (priv->config_speed * priv->interval / 1000) / 3.6;
123 //DBG dbg(0, "###### Entering simulation loop\n");
124 if (priv->navit)
125 {
126 route = navit_get_route(priv->navit);
127 }
128 //DBG dbg(0,"rr 1\n");
129 if (route)
130 {
131 route_map = route_get_map(route);
132 }
133 //DBG dbg(0,"rr 2\n");
134 if (route_map)
135 {
136 mr = map_rect_new(route_map, NULL);
137 }
138 //DBG dbg(0,"rr 3\n");
139 if (mr)
140 {
141 item = map_rect_get_item(mr);
142 }
143 //DBG dbg(0,"rr 4\n");
144 if (item && item->type == type_route_start)
145 {
146 item = map_rect_get_item(mr);
147 }
148 //DBG dbg(0,"rr 5\n");
149 if (item && item_coord_get(item, &pos, 1))
150 {
151 priv->position_set = 0;
152 ////DBG dbg(0, "current pos=0x%x,0x%x\n", pos.x, pos.y);
153 ////DBG dbg(0, "last pos=0x%x,0x%x\n", priv->last.x, priv->last.y);
154 if (priv->last.x == pos.x && priv->last.y == pos.y)
155 {
156 //dbg(1, "endless loop\n");
157 }
158 priv->last = pos;
159 while (item && priv->config_speed)
160 {
161 if (!item_coord_get(item, &c, 1))
162 {
163 item = map_rect_get_item(mr);
164 continue;
165 }
166 ////DBG dbg(0, "next pos=0x%x,0x%x\n", c.x, c.y);
167 slen = transform_distance(projection_mg, &pos, &c);
168 ////DBG dbg(0, "len=%d slen=%d\n", len, slen);
169 if (slen < len)
170 {
171 len -= slen;
172 pos = c;
173 }
174 else
175 {
176 if (item_coord_get(item, &c2, 1) || map_rect_get_item(mr))
177 {
178 dx = c.x - pos.x;
179 dy = c.y - pos.y;
180 ci.x = pos.x + dx * len / slen;
181 ci.y = pos.y + dy * len / slen;
182 priv->direction = transform_get_angle_delta(&pos, &c, 0);
183 priv->speed = priv->config_speed;
184 }
185 else
186 {
187 ci.x = pos.x;
188 ci.y = pos.y;
189 priv->speed = 0;
190 ////DBG dbg(0, "destination reached\n");
191 }
192 //dbg(1, "ci=0x%x,0x%x\n", ci.x, ci.y);
193 transform_to_geo(projection_mg, &ci, &priv->geo);
194 callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
195 break;
196 }
197 }
198 }
199 else
200 {
201 if (priv->position_set)
202 callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
203 }
204 //DBG dbg(0,"rr 6\n");
205 if (mr)
206 {
207 map_rect_destroy(mr);
208 }
209 //DBG dbg(0,"rr F\n");
210 }
211
212 static struct vehicle_priv *
213 vehicle_demo_new(struct vehicle_methods *meth, struct callback_list *cbl, struct attr **attrs)
214 {
215 struct vehicle_priv *ret;
216 struct attr *interval, *speed, *position_coord_geo;
217
218 //DBG dbg(0, "enter\n");
219 ret = g_new0(struct vehicle_priv, 1);
220 ret->cbl = cbl;
221 ret->interval = 1200;
222 ret->config_speed = 41;
223
224 //dbg(0, "vd 3.1 %d %d\n", ret->interval, ret->config_speed);
225
226 //DBG dbg(0, "vd 1\n");
227 if ((speed = attr_search(attrs, NULL, attr_speed)))
228 {
229 ret->config_speed = speed->u.num;
230 }
231
232 //DBG dbg(0, "vd 2\n");
233 if ((interval = attr_search(attrs, NULL, attr_interval)))
234 {
235 ret->interval = interval->u.num;
236 }
237
238 // ret->geo.lat = 0;
239 // ret->geo.lng = 0;
240 // //DBG dbg(0, "position_default %f %f\n", ret->geo.lat, ret->geo.lng);
241
242 //dbg(0, "vd 3.2 %d %d\n", ret->interval, ret->config_speed);
243 if ((position_coord_geo = attr_search(attrs, NULL, attr_position_coord_geo)))
244 {
245 ret->geo = *(position_coord_geo->u.coord_geo);
246 ret->position_set = 1;
247 //DBG dbg(0, "position_set %f %f\n", ret->geo.lat, ret->geo.lng);
248 }
249
250 //DBG dbg(0, "vd 4\n");
251 *meth = vehicle_demo_methods;
252 ret->timer_callback = callback_new_1(callback_cast(vehicle_demo_timer), ret);
253 //DBG dbg(0, "vd 5\n");
254 dbg(0, "event_add_timeout %d,%d,%p", ret->interval, 1, ret->timer_callback);
255 ret->timer = event_add_timeout(ret->interval, 1, ret->timer_callback);
256 //DBG dbg(0, "leave\n");
257 return ret;
258 }
259
260 void plugin_init(void)
261 {
262 //DBG dbg(0, "enter\n");
263 plugin_register_vehicle_type("demo", vehicle_demo_new);
264 }

   
Visit the ZANavi Wiki