| 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 | */ |
|---|
| 21 | using Gtk; |
|---|
| 22 | using Gee; |
|---|
| 23 | #if !ENABLE_WIN32 |
|---|
| 24 | using DBus; |
|---|
| 25 | using Hal; |
|---|
| 26 | #endif |
|---|
| 27 | |
|---|
| 28 | public 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 string _working_directory = Environment.get_home_dir (); |
|---|
| 38 | private RadioButton _programmer_version; |
|---|
| 39 | private RadioButton _erase_flash; |
|---|
| 40 | private RadioButton _write_program; |
|---|
| 41 | private RadioButton _read_program; |
|---|
| 42 | private RadioButton _write_configuration_bits; |
|---|
| 43 | private RadioButton _read_configuration_bits; |
|---|
| 44 | |
|---|
| 45 | public PicstartPlusPage (LogsPanel logs_panel) |
|---|
| 46 | { |
|---|
| 47 | _logs_panel = logs_panel; |
|---|
| 48 | |
|---|
| 49 | _log.title = "PICSTART Plus"; |
|---|
| 50 | _log.tab_title = _("PICSTART Plus log"); |
|---|
| 51 | _log.headers_visible = false; |
|---|
| 52 | _log.close_action.connect (() => { |
|---|
| 53 | _logs_panel.remove_log (_log); |
|---|
| 54 | }); |
|---|
| 55 | _picstart_plus.message_added.connect ((message) => { |
|---|
| 56 | _log.add_line (message); |
|---|
| 57 | }); |
|---|
| 58 | |
|---|
| 59 | foreach (var device in get_serial_devices ()) |
|---|
| 60 | _connection_port.append_text (device); |
|---|
| 61 | _connection_port.active = 0; |
|---|
| 62 | |
|---|
| 63 | var size_group = new SizeGroup (SizeGroupMode.HORIZONTAL); |
|---|
| 64 | |
|---|
| 65 | var vbox = new VBox (false, 0); |
|---|
| 66 | |
|---|
| 67 | var hbox = create_hbox (_("Connection port:"), size_group, vbox); |
|---|
| 68 | hbox.pack_start (_connection_port, true, true, 5); |
|---|
| 69 | hbox = create_hbox (_("Pic model:"), size_group, vbox); |
|---|
| 70 | hbox.pack_start (_pic_model, true, true, 5); |
|---|
| 71 | |
|---|
| 72 | _programmer_version = new RadioButton.with_label (null, _("Get programmer version")); |
|---|
| 73 | hbox = create_hbox_with_radio_button (_programmer_version, size_group, vbox); |
|---|
| 74 | |
|---|
| 75 | _erase_flash = new RadioButton.with_label_from_widget (_programmer_version, _("Erase flash")); |
|---|
| 76 | hbox = create_hbox_with_radio_button (_erase_flash, size_group, vbox); |
|---|
| 77 | |
|---|
| 78 | _write_program = new RadioButton.with_label_from_widget (_erase_flash, _("Write program")); |
|---|
| 79 | hbox = create_hbox_with_radio_button (_write_program, size_group, vbox); |
|---|
| 80 | hbox.pack_start (_hexfile, true, true, 5); |
|---|
| 81 | var filter = new FileFilter (); |
|---|
| 82 | filter.set_name (_(".hex files")); |
|---|
| 83 | filter.add_pattern ("*.hex"); |
|---|
| 84 | _hexfile.add_filter (filter); |
|---|
| 85 | _hexfile.set_current_folder (_working_directory); |
|---|
| 86 | _hexfile.current_folder_changed.connect (() => { |
|---|
| 87 | _working_directory = _hexfile.get_current_folder (); |
|---|
| 88 | }); |
|---|
| 89 | |
|---|
| 90 | _read_program = new RadioButton.with_label_from_widget (_write_program, _("Read program")); |
|---|
| 91 | hbox = create_hbox_with_radio_button (_read_program, size_group, vbox); |
|---|
| 92 | |
|---|
| 93 | _write_configuration_bits = new RadioButton.with_label_from_widget (_read_program, _("Write configuration bits")); |
|---|
| 94 | hbox = create_hbox_with_radio_button (_write_configuration_bits, size_group, vbox); |
|---|
| 95 | hbox.pack_start (_configuration_bits, true, true, 5); |
|---|
| 96 | |
|---|
| 97 | _read_configuration_bits = new RadioButton.with_label_from_widget (_write_configuration_bits, _("Read configuration bits")); |
|---|
| 98 | hbox = create_hbox_with_radio_button (_read_configuration_bits, size_group, vbox); |
|---|
| 99 | |
|---|
| 100 | var run_command = new Button.with_label (_("Run command")); |
|---|
| 101 | run_command.clicked.connect (on_run_command); |
|---|
| 102 | |
|---|
| 103 | hbox = new HBox (false, 0); |
|---|
| 104 | hbox.pack_end (run_command, false, false, 5); |
|---|
| 105 | |
|---|
| 106 | vbox.pack_start (hbox, false, false, 5); |
|---|
| 107 | |
|---|
| 108 | var scrolled_window = new ScrolledWindow (null, null); |
|---|
| 109 | scrolled_window.add_with_viewport (vbox); |
|---|
| 110 | scrolled_window.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC); |
|---|
| 111 | |
|---|
| 112 | pack_start (scrolled_window, true, true, 5); |
|---|
| 113 | show_all (); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | private void on_run_command () |
|---|
| 117 | { |
|---|
| 118 | _picstart_plus.connection_port = _connection_port.get_active_text (); |
|---|
| 119 | _picstart_plus.pic_model = _pic_model.text; |
|---|
| 120 | bool log_found = false; |
|---|
| 121 | foreach (var log in _logs_panel.list_logs ()) |
|---|
| 122 | if (log.title == "PICSTART Plus") |
|---|
| 123 | log_found = true; |
|---|
| 124 | if (!log_found) |
|---|
| 125 | _logs_panel.add_log (_log); |
|---|
| 126 | if (!_logs_panel.visible) |
|---|
| 127 | _logs_panel.show (); |
|---|
| 128 | _logs_panel.show_log (_log); |
|---|
| 129 | |
|---|
| 130 | if (_programmer_version.active) |
|---|
| 131 | _picstart_plus.get_programmer_version (); |
|---|
| 132 | |
|---|
| 133 | if (_erase_flash.active) |
|---|
| 134 | _picstart_plus.erase_flash (); |
|---|
| 135 | |
|---|
| 136 | if (_write_program.active) |
|---|
| 137 | _picstart_plus.write_program (_hexfile.get_filename ()); |
|---|
| 138 | |
|---|
| 139 | if (_read_program.active) |
|---|
| 140 | _picstart_plus.write_program (Path.build_filename (_working_directory, "i4uc-read.hex")); |
|---|
| 141 | |
|---|
| 142 | if (_write_configuration_bits.active) |
|---|
| 143 | _picstart_plus.write_configuration_bits (_configuration_bits.text); |
|---|
| 144 | |
|---|
| 145 | if (_read_configuration_bits.active) |
|---|
| 146 | _picstart_plus.read_configuration_bits (); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | private HBox create_hbox (string title, SizeGroup size_group, VBox vbox) |
|---|
| 150 | { |
|---|
| 151 | var hbox = new HBox (false, 0); |
|---|
| 152 | var label = new Label (title); |
|---|
| 153 | label.xalign = 1; |
|---|
| 154 | size_group.add_widget (label); |
|---|
| 155 | |
|---|
| 156 | hbox.pack_start (label, false, false, 5); |
|---|
| 157 | vbox.pack_start (hbox, false, false, 5); |
|---|
| 158 | |
|---|
| 159 | return hbox; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | private HBox create_hbox_with_radio_button (RadioButton title, SizeGroup size_group, VBox vbox) |
|---|
| 163 | { |
|---|
| 164 | var hbox = new HBox (false, 0); |
|---|
| 165 | size_group.add_widget (title); |
|---|
| 166 | |
|---|
| 167 | hbox.pack_start (title, false, false, 5); |
|---|
| 168 | vbox.pack_start (hbox, false, false, 5); |
|---|
| 169 | |
|---|
| 170 | return hbox; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | private Gee.List<string> get_serial_devices () |
|---|
| 174 | { |
|---|
| 175 | #if ENABLE_WIN32 |
|---|
| 176 | |
|---|
| 177 | var device_list = new ArrayList<string> (str_equal); |
|---|
| 178 | device_list.add ("COM1"); |
|---|
| 179 | device_list.add ("COM2"); |
|---|
| 180 | device_list.add ("COM3"); |
|---|
| 181 | device_list.add ("COM4"); |
|---|
| 182 | |
|---|
| 183 | #else |
|---|
| 184 | |
|---|
| 185 | var dbus_error = DBus.RawError (); |
|---|
| 186 | |
|---|
| 187 | var hal = new Hal.Context (); |
|---|
| 188 | |
|---|
| 189 | if (hal == null) |
|---|
| 190 | print ("Error getting HAL context\n"); |
|---|
| 191 | |
|---|
| 192 | if (!hal.set_dbus_connection (RawBus.get (BusType.SYSTEM, ref dbus_error))) |
|---|
| 193 | print ("Error setting dbus connection: %s\n".printf (dbus_error.message)); |
|---|
| 194 | |
|---|
| 195 | if (!hal.init (ref dbus_error)) |
|---|
| 196 | print ("Cannot initialize connection to the hald\nNormally this means that the HAL deamon (hald) is not running or not ready\n"); |
|---|
| 197 | |
|---|
| 198 | var udis = hal.find_device_by_capability ("serial", ref dbus_error); |
|---|
| 199 | if (dbus_error.is_set ()) |
|---|
| 200 | print ("Error finding serial devices: %s: %s\n".printf (dbus_error.name, dbus_error.message)); |
|---|
| 201 | |
|---|
| 202 | var device_list = new ArrayList<string> (str_equal); |
|---|
| 203 | foreach (string udi in udis) |
|---|
| 204 | { |
|---|
| 205 | string device; |
|---|
| 206 | device = hal.device_get_property_string (udi, "serial.device", ref dbus_error); |
|---|
| 207 | if (dbus_error.is_set ()) |
|---|
| 208 | print ("Error getting property string: %s: %s\n".printf (dbus_error.name, dbus_error.message)); |
|---|
| 209 | device_list.add (device); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | #endif |
|---|
| 213 | |
|---|
| 214 | return device_list; |
|---|
| 215 | } |
|---|
| 216 | } |
|---|