2 * gpx_trace - zavai GPX trace functions
4 * Copyright (C) 2009 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 class GPSOn(gtk.ToggleAction):
29 states = [_("GPS always on"), _("GPS on when needed")]
31 def __init__(self, registry, **kw):
33 super(GPSOn, self).__init__("menu.main.gps.alwayson", self.states[self.state], None, None)
35 self.registry = registry
36 self.set_active(False)
38 self.connect("toggled", self.on_toggle)
40 def on_toggle(self, *args):
41 self.state = (self.state + 1) % len(self.states)
42 self.set_property("label", self.states[self.state])
49 self.registry.resource("gps").connect("gps", self)
52 self.registry.resource("gps").disconnect("gps", self)
54 class GPXTracer(gtk.ToggleAction):
55 states = [_("Start GPX trace"), _("Stop GPX trace")]
57 def __init__(self, registry, **kw):
59 super(GPXTracer, self).__init__("menu.main.gps.gpx", self.states[self.state], None, None)
61 self.registry = registry
62 self.set_active(False)
64 self.connect("toggled", self.on_toggle)
66 def on_toggle(self, *args):
67 self.state = (self.state + 1) % len(self.states)
68 self.set_property("label", self.states[self.state])
75 zavai.info("GPX trace started")
76 self.registry.resource("gpx").connect("gpx", self)
79 zavai.info("GPX trace ended")
80 self.registry.resource("gpx").disconnect("gpx", self)
83 public class Waypoint : BigButton
87 set_label("Take waypoint");
88 zavai.gps.gpx.tracking_changed += on_gpx_tracking_changed;
89 clicked += on_clicked;
90 set_sensitive(zavai.gps.gpx.tracking);
93 protected void on_gpx_tracking_changed(zavai.gps.GPX gpx, bool new_state)
95 stderr.printf("Toggled %d\n", (int)new_state);
96 set_sensitive(new_state);
99 protected void on_clicked()
101 stderr.printf("Activate\n");
102 zavai.gps.gpx.waypoint();
107 class GPXAudioTracer(gtk.ToggleAction):
108 states = [_("Start GPX and audio trace"), _("Stop GPX and audio trace")]
110 def __init__(self, registry, **kw):
112 super(GPXAudioTracer, self).__init__("menu.main.gps.gpxaudio", self.states[self.state], None, None)
114 self.registry = registry
115 self.recorder = zavai.Recorder(registry)
116 self.set_active(False)
118 self.connect("toggled", self.on_toggle)
122 super(GPXAudioTracer, self).shutdown()
124 def on_toggle(self, *args):
125 self.state = (self.state + 1) % len(self.states)
126 self.set_property("label", self.states[self.state])
127 if self.get_active():
133 zavai.info("GPX trace started")
134 gpx = self.registry.resource("gpx")
135 gpx.connect("gpx", self)
136 gpx.add_activity_monitor(self.on_gpx_activity_changed)
139 zavai.info("GPX trace ended")
140 gpx = self.registry.resource("gpx")
141 gpx.disconnect("gpx", self)
143 gpx.del_activity_monitor(self.on_gpx_activity_changed)
145 def on_gpx_activity_changed(self, gpx, state):
147 self.recorder.start(gpx.basename + ".wav")
152 public class GPSRequestLink : Gtk.ToggleButton
154 protected string service_name;
155 protected string label_start;
156 protected string label_stop;
157 protected Gtk.StatusIcon status_icon;
158 protected int fix_status = 0;
160 public GPSRequestLink()
162 service_name = "gps";
163 label_start = "Keep GPS on";
164 label_stop = "Stop keeping GPS on";
165 set_size_request(0, zavai.config.min_button_height);
166 toggled += on_toggled;
168 set_label(get_active() ? label_stop : label_start);
170 //tooltip = "GPS status";
172 fix_status = zavai.gps.gps.device.GetFixStatus();
176 zavai.gps.gps.device.FixStatusChanged += on_fix_status_changed;
179 status_icon = new Gtk.StatusIcon();
180 status_icon.set_visible(true);
181 status_icon.activate += on_status_activate;
185 protected void update_icon()
188 if (fix_status == 2 || fix_status == 3)
189 name = zavai.config.icondir + "/" + (get_active() ? "gps_fix_on.png" : "gps_fix_off.png");
191 name = zavai.config.icondir + "/" + (get_active() ? "gps_nofix_on.png" : "gps_nofix_off.png");
192 stderr.printf("load icon from %s\n", name);
193 status_icon.set_from_file(name);
196 private void on_fix_status_changed(dynamic DBus.Object pos, int fix_status)
198 this.fix_status = fix_status;
202 private void on_toggled(Gtk.Button src)
204 Service s = zavai.registry.gets(service_name);
206 s.request("servicerequestlink");
208 s.release("servicerequestlink");
209 set_label(get_active() ? label_stop : label_start);
213 private void on_status_activate()
215 set_active(!get_active());
219 static string[] SAT_QI_NAMES;
221 public class SatelliteMonitor : Applet
226 public SatelliteMonitor()
228 _label = "Satellite monitor";
230 store = new Gtk.ListStore(11, typeof(string), typeof(string), typeof(string),
231 typeof(string), typeof(string), typeof(string), typeof(string),
232 typeof(string), typeof(string), typeof(string), typeof(string));
233 view = new Gtk.TreeView();
234 view.set_model(store);
236 var renderer = new Gtk.CellRendererText();
237 view.insert_column_with_attributes(-1, "CH", renderer, "text", 0, null);
238 view.insert_column_with_attributes(-1, "ID", renderer, "text", 1, null);
239 view.insert_column_with_attributes(-1, "SN", renderer, "text", 2, null);
240 view.insert_column_with_attributes(-1, "ELE", renderer, "text", 3, null);
241 view.insert_column_with_attributes(-1, "AZI", renderer, "text", 4, null);
242 view.insert_column_with_attributes(-1, "Used", renderer, "text", 5, null);
243 view.insert_column_with_attributes(-1, "Diff", renderer, "text", 6, null);
244 view.insert_column_with_attributes(-1, "Alm", renderer, "text", 7, null);
245 view.insert_column_with_attributes(-1, "Eph", renderer, "text", 8, null);
246 view.insert_column_with_attributes(-1, "Bad", renderer, "text", 9, null);
247 view.insert_column_with_attributes(-1, "Status", renderer, "text", 10, null);
248 pack_start(view, true, true, 0);
251 public override void start()
253 zavai.gps.monitor.sat_info += on_sat_info;
254 zavai.gps.monitor.request("app.satmonitor");
257 public override void stop()
259 zavai.gps.monitor.release("app.satmonitor");
260 zavai.gps.monitor.sat_info -= on_sat_info;
263 public void on_sat_info(PtrArray< HashTable<string, Value?> > data)
266 stderr.printf("CH ID SN ELE AZI Used Diff Alm Eph Bad Status\n");
267 for (int i = 0; i < data.len; ++i)
269 HashTable<string, Value?> sv = (HashTable<string, Value?>)data.pdata[i];
271 Value v = sv.lookup("Flags");
274 v = sv.lookup("chn");
277 v = sv.lookup("SVID");
280 v = sv.lookup("CNO");
283 v = sv.lookup("Elev");
286 v = sv.lookup("Azim");
289 bool used = (flags & 0x01) != 0;
290 bool diff = (flags & 0x02) != 0;
291 bool almoreph = (flags & 0x04) != 0;
292 bool eph = (flags & 0x08) != 0;
293 bool bad = (flags & 0x10) != 0;
295 string sqi = "Unknown";
298 if (qi < SAT_QI_NAMES.length)
299 sqi = "%i: %s".printf(qi, SAT_QI_NAMES[qi]);
302 store.append(out iter);
303 store.set(iter, "%2d".printf(chn),
310 almoreph ? "alm" : "",
312 bad ? "bad" : "", sqi);
313 stderr.printf("%2d %2d %2d %3d %3d %d %d %d %d %d %s\n", chn, svid, cno, elev, azim, (int)used, (int)diff, (int)almoreph, (int)eph, (int)bad, sqi);
316 def on_ubxdebug_packet(self, clid, length, data):
317 # In zhone it is cbUBXDebugPacket
318 #if clid == "NAV-STATUS" and data:
319 # i = ["%s: %d" % (k, data[0][k]) for k in sorted(data[0].keys())]
320 # zavai.info("Status:", " ".join(i))
321 ## if data[0]['TTFF']:
322 ## zavai.info("TTFF: %f", data[0]['TTFF']/1000.0)
323 if clid == "NAV-SVINFO":
324 self.handle_ubx_sat_data(data[1:])
326 # zavai.info("gps got ubxdebug packet", clid)
327 # zavai.info("DATA:", data)
336 SAT_QI_NAMES = new string[] {
348 registry.register(GPXAudioTracer(registry))
349 registry.register(GPXWaypoint(registry))
353 var window_list = new SatelliteMonitor();
354 zavai.registry.register_applet("ui.gps.monitor", window_list);
356 var menu_waypoint = new Waypoint();
357 var menu_gpsrequest = new GPSRequestLink();
358 //label_on = "Stop GPX trace";
359 //label_off = "Start GPX trace";
360 //label_on = "Stop GPS monitor";
361 //label_off = "Start GPS monitor";
362 //label_on = "Stop GPS position tracking";
363 //label_off = "Start GPS position tracking";
364 //label_on = "Stop keeping GPS on";
365 //label_off = "Keep GPS on";
368 var menu_gps = new zavai.Menu("GPS");
369 //menu_gps.add_applet("app.debug.useless");
370 menu_gps.add_service_toggle("gps.gpx", "Start GPX trace", "Stop GPX trace");
371 menu_gps.add_applet("ui.gps.monitor");
372 menu_gps.add_widget(menu_waypoint);
373 menu_gps.add_widget(menu_gpsrequest);
375 zavai.registry.register_menu("menu.gps", menu_gps);
376 zavai.registry.getmenu("menu.main").add_applet("menu.gps");