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; either version 2 of the License, or
7 * (at your option) any later version.
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.
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.
21 #define EEPROM_LEN 1024
23 static struct aoview_file *eeprom_file;
24 static char eeprom_line[EEPROM_LEN + 1];
25 static int eeprom_pos;
26 static GtkMessageDialog *eeprom_save_done;
27 static GtkWidget *eeprom_save_close;
28 static gboolean eeprom_save_shown;
31 aoview_eeprom_disconnect(struct aoview_serial *serial)
33 aoview_file_finish(eeprom_file);
37 aoview_eeprom_done(struct aoview_serial *serial)
39 gtk_window_set_title(GTK_WINDOW(eeprom_save_done),
41 gtk_message_dialog_set_markup(eeprom_save_done,
42 "<b>EEPROM data saved as</b>");
43 if (!eeprom_save_shown)
44 gtk_widget_show(GTK_WIDGET(eeprom_save_done));
45 eeprom_save_close = gtk_window_get_default_widget(GTK_WINDOW(eeprom_save_done));
46 if (eeprom_save_close)
47 gtk_widget_set_sensitive(eeprom_save_close, TRUE);
48 aoview_eeprom_disconnect(serial);
52 aoview_eeprom_parse(struct aoview_serial *serial,
63 if (!strcmp(line, "end")) {
64 aoview_eeprom_done(serial);
67 if (sscanf(line, "serial-number %u", &serial_number) == 1) {
68 aoview_file_set_serial(eeprom_file, serial_number);
69 } else if (sscanf(line, "%c %x %x %x", &cmd, &tick, &a, &b) == 4) {
71 aoview_file_set_flight(eeprom_file, b);
72 aoview_file_printf(eeprom_file, "%s\n", line);
73 if (cmd == 'S' && a == 8) {
74 aoview_eeprom_done(serial);
78 if (!eeprom_save_shown)
80 name = aoview_file_name(eeprom_file);
82 utf8_name = g_filename_to_utf8(name, -1, NULL, NULL, NULL);
84 utf8_name = (char *) name;
85 gtk_widget_set_sensitive(eeprom_save_close, FALSE);
86 gtk_window_set_title(GTK_WINDOW(eeprom_save_done),
87 "Saving EEPROM data");
88 gtk_message_dialog_set_markup(eeprom_save_done,
89 "<b>Saving EEPROM data as</b>");
90 gtk_message_dialog_format_secondary_text(eeprom_save_done, "%s",
92 if (utf8_name != name)
94 gtk_container_check_resize(GTK_CONTAINER(eeprom_save_done));
95 gtk_widget_show(GTK_WIDGET(eeprom_save_done));
96 eeprom_save_shown = TRUE;
97 eeprom_save_close = gtk_window_get_default_widget(GTK_WINDOW(eeprom_save_done));
98 if (eeprom_save_close)
99 gtk_widget_set_sensitive(eeprom_save_close, FALSE);
107 aoview_eeprom_callback(gpointer user_data,
108 struct aoview_serial *serial,
113 if (revents & (G_IO_HUP|G_IO_ERR)) {
114 aoview_eeprom_disconnect(serial);
117 if (revents & G_IO_IN) {
119 c = aoview_serial_getc(serial);
125 eeprom_line[eeprom_pos] = '\0';
127 if (!aoview_eeprom_parse(serial, eeprom_line))
130 } else if (eeprom_pos < EEPROM_LEN)
131 eeprom_line[eeprom_pos++] = c;
137 aoview_eeprom_save(const char *device)
139 struct aoview_serial *serial;
141 gtk_widget_hide(GTK_WIDGET(eeprom_save_done));
142 eeprom_save_shown = FALSE;
143 serial = aoview_serial_open(device);
146 aoview_serial_set_callback(serial, aoview_eeprom_callback);
147 aoview_serial_printf(serial, "v\nl\n");
152 aoview_eeprom_init(GladeXML *xml)
154 eeprom_file = aoview_file_new("eeprom");
157 eeprom_save_done = GTK_MESSAGE_DIALOG(glade_xml_get_widget(xml, "ao_save_done"));
158 assert(eeprom_save_done);