source: src/i4ucpicstartpluspage.vala @ 6915425a0e86f2ba09bd797739dee20e186f3bb5

Revision 6915425a0e86f2ba09bd797739dee20e186f3bb5, 7.2 KB checked in by Matias De la Puente <mfpuente.ar@…>, 4 years ago (diff)

Put working directory in configuration file

  • Property mode set to 100644
Line 
1/* i4ucpicstartpluspage.vala
2 *
3 * Copyright (C) 2009  Matias De la Puente
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author:
19 *      Matias De la Puente <mfpuente.ar@gmail.com>
20 */
21using Gtk;
22using Gee;
23#if !ENABLE_WIN32
24using DBus;
25using Hal;
26#endif
27
28public class I4uc.PicstartPlusPage: Page
29{
30        private PicstartPlus _picstart_plus = new PicstartPlus ();
31        private LogsPanel _logs_panel;
32        private Log _log = new Log ();
33        private ComboBox _connection_port = new ComboBox.text ();
34        private Entry _pic_model = new Entry ();
35        private Entry _configuration_bits = new Entry ();
36        private FileChooserButton _hexfile = new FileChooserButton (_("Select Hexfile to write"), FileChooserAction.OPEN);
37        private RadioButton _programmer_version;
38        private RadioButton _erase_flash;
39        private RadioButton _write_program;
40        private RadioButton _read_program;
41        private RadioButton _write_configuration_bits;
42        private RadioButton _read_configuration_bits;
43       
44        public PicstartPlusPage (LogsPanel logs_panel)
45        {
46                _logs_panel = logs_panel;
47               
48                _log.title = "PICSTART Plus";
49                _log.tab_title = _("PICSTART Plus log");
50                _log.headers_visible = false;
51                _log.close_action.connect (() => {
52                        _logs_panel.remove_log (_log);
53                });
54                _picstart_plus.message_added.connect ((message) => {
55                        _log.add_line (message);
56                });
57               
58                foreach (var device in get_serial_devices ())
59                        _connection_port.append_text (device);
60                _connection_port.active = 0;
61               
62                var size_group = new SizeGroup (SizeGroupMode.HORIZONTAL);
63               
64                var vbox = new VBox (false, 0);
65               
66                var hbox = create_hbox (_("Connection port:"), size_group, vbox);
67                hbox.pack_start (_connection_port, true, true, 5);
68                hbox = create_hbox (_("Pic model:"), size_group, vbox);
69                hbox.pack_start (_pic_model, true, true, 5);
70               
71                _programmer_version = new RadioButton.with_label (null, _("Get programmer version"));
72                hbox = create_hbox_with_radio_button (_programmer_version, size_group, vbox);
73               
74                _erase_flash = new RadioButton.with_label_from_widget (_programmer_version, _("Erase flash"));
75                hbox = create_hbox_with_radio_button (_erase_flash, size_group, vbox);
76               
77                _write_program = new RadioButton.with_label_from_widget (_erase_flash, _("Write program"));
78                hbox = create_hbox_with_radio_button (_write_program, size_group, vbox);
79                hbox.pack_start (_hexfile, true, true, 5);
80                var filter = new FileFilter ();
81                filter.set_name (_(".hex files"));
82                filter.add_pattern ("*.hex");
83                _hexfile.add_filter (filter);
84                _hexfile.set_current_folder (Configuration.get_instance ().working_directory);
85                _hexfile.current_folder_changed.connect (() => {
86                        Configuration.get_instance ().working_directory = _hexfile.get_current_folder ();
87                });
88               
89                _read_program = new RadioButton.with_label_from_widget (_write_program, _("Read program"));
90                hbox = create_hbox_with_radio_button (_read_program, size_group, vbox);
91               
92                _write_configuration_bits = new RadioButton.with_label_from_widget (_read_program, _("Write configuration bits"));
93                hbox = create_hbox_with_radio_button (_write_configuration_bits, size_group, vbox);
94                hbox.pack_start (_configuration_bits, true, true, 5);
95               
96                _read_configuration_bits = new RadioButton.with_label_from_widget (_write_configuration_bits, _("Read configuration bits"));
97                hbox = create_hbox_with_radio_button (_read_configuration_bits, size_group, vbox);
98               
99                var run_command = new Button.with_label (_("Run command"));
100                run_command.clicked.connect (on_run_command);
101               
102                hbox = new HBox (false, 0);
103                hbox.pack_end (run_command, false, false, 5);
104               
105                vbox.pack_start (hbox, false, false, 5);
106               
107                var scrolled_window = new ScrolledWindow (null, null);
108                scrolled_window.add_with_viewport (vbox);
109                scrolled_window.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
110               
111                pack_start (scrolled_window, true, true, 5);
112                show_all ();
113        }
114       
115        private void on_run_command ()
116        {
117                _picstart_plus.connection_port = _connection_port.get_active_text ();
118                _picstart_plus.pic_model = _pic_model.text;
119                bool log_found = false;
120                foreach (var log in _logs_panel.list_logs ())
121                        if (log.title == "PICSTART Plus")
122                                log_found = true;
123                if (!log_found)
124                        _logs_panel.add_log (_log);
125                if (!_logs_panel.visible)
126                        _logs_panel.show ();
127                _logs_panel.show_log (_log);
128               
129                if (_programmer_version.active)
130                        _picstart_plus.get_programmer_version ();
131               
132                if (_erase_flash.active)
133                        _picstart_plus.erase_flash ();
134                       
135                if (_write_program.active)
136                        _picstart_plus.write_program (_hexfile.get_filename ());
137               
138                if (_read_program.active)
139                        _picstart_plus.write_program (Path.build_filename (Configuration.get_instance ().working_directory, "i4uc-read.hex"));
140               
141                if (_write_configuration_bits.active)
142                        _picstart_plus.write_configuration_bits (_configuration_bits.text);
143               
144                if (_read_configuration_bits.active)
145                        _picstart_plus.read_configuration_bits ();
146        }
147       
148        private HBox create_hbox (string title, SizeGroup size_group, VBox vbox)
149        {
150                var hbox = new HBox (false, 0);
151                var label = new Label (title);
152                label.xalign = 1;
153                size_group.add_widget (label);
154               
155                hbox.pack_start (label, false, false, 5);
156                vbox.pack_start (hbox, false, false, 5);
157
158                return hbox;
159        }
160       
161        private HBox create_hbox_with_radio_button (RadioButton title, SizeGroup size_group, VBox vbox)
162        {
163                var hbox = new HBox (false, 0);
164                size_group.add_widget (title);
165               
166                hbox.pack_start (title, false, false, 5);
167                vbox.pack_start (hbox, false, false, 5);
168
169                return hbox;
170        }
171       
172        private Gee.List<string> get_serial_devices ()
173        {
174                #if ENABLE_WIN32
175               
176                var device_list = new ArrayList<string> (str_equal);
177                device_list.add ("COM1");
178                device_list.add ("COM2");
179                device_list.add ("COM3");
180                device_list.add ("COM4");
181               
182                #else
183               
184                var dbus_error = DBus.RawError ();
185               
186                var hal = new Hal.Context ();
187
188                if (hal == null)
189                        print ("Error getting HAL context\n");
190
191                if (!hal.set_dbus_connection (RawBus.get (BusType.SYSTEM, ref dbus_error)))
192                        print ("Error setting dbus connection: %s\n".printf (dbus_error.message));
193               
194                if (!hal.init (ref dbus_error))
195                        print ("Cannot initialize connection to the hald\nNormally this means that the HAL deamon (hald) is not running or not ready\n");
196               
197                var udis = hal.find_device_by_capability ("serial", ref dbus_error);
198                if (dbus_error.is_set ())
199                        print ("Error finding serial devices: %s: %s\n".printf (dbus_error.name, dbus_error.message));
200               
201                var device_list = new ArrayList<string> (str_equal);
202                foreach (string udi in udis)
203                {
204                        string device;
205                        device = hal.device_get_property_string (udi, "serial.device", ref dbus_error);
206                        if (dbus_error.is_set ())
207                                print ("Error getting property string: %s: %s\n".printf (dbus_error.name, dbus_error.message));
208                        device_list.add (device);
209                }
210               
211                #endif
212               
213                return device_list;
214        }
215}
Note: See TracBrowser for help on using the repository browser.