1 /* This file is part of LED Pattern Editor.
3 * Copyright (C) 2010 Philipp Zabel
5 * LED Pattern Editor 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.
10 * LED Pattern Editor 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.
15 * You should have received a copy of the GNU General Public License
16 * along with LED Pattern Editor. If not, see <http://www.gnu.org/licenses/>.
19 class LedProgramDialog : Gtk.Dialog {
21 LedPatternRX51 pattern;
23 LedColorButton led_color1;
24 LedColorButton led_color2;
28 public LedProgramDialog (LedPatternRX51 _pattern) {
30 set_title ("LED pattern editor - " +
31 (pattern.name.has_prefix ("Pattern") ?
32 pattern.name.offset (7) : pattern.name) + " pattern");
34 var content = (Gtk.VBox) get_content_area ();
35 content.set_size_request (-1, 5*70);
37 lpv = new LedPatternView (pattern.copy ());
38 lpv.set_size_request (-1, 70);
39 content.pack_start (lpv, false, false, 0);
41 var hbox = new Gtk.HBox (true, 0);
42 list1 = led_command_list (1);
43 hbox.pack_start (list1, true, true, 0);
44 list2 = led_command_list (2);
45 hbox.pack_start (list2, true, true, 0);
46 content.pack_start (hbox, true, true, 0);
51 hbox = new Gtk.HBox (true, 0);
52 led_color1 = new LedColorButton.with_color (lpv.pattern.color1);
53 led_color1.clicked.connect (on_color1_clicked);
54 hbox.pack_start (led_color1, true, true, 0);
55 led_color2 = new LedColorButton.with_color (lpv.pattern.color2);
56 led_color2.clicked.connect (on_color2_clicked);
57 hbox.pack_start (led_color2, true, true, 0);
58 add_action_widget (hbox, 2);
59 action_area.set_child_secondary (hbox, true);
61 hbox = new Gtk.HBox (true, 0);
62 var radio = (Gtk.RadioButton) Hildon.gtk_radio_button_new (Hildon.SizeType.FINGER_HEIGHT, null);
63 radio.set_mode (false);
64 radio.set_label ("1");
65 radio.set_active (true);
66 radio.toggled.connect (on_engine1_toggled);
67 hbox.pack_start (radio, true, true, 0);
68 radio = (Gtk.RadioButton) Hildon.gtk_radio_button_new_from_widget (Hildon.SizeType.FINGER_HEIGHT, radio);
69 radio.set_mode (false);
70 radio.set_label ("2");
71 radio.toggled.connect (on_engine2_toggled);
72 hbox.pack_start (radio, true, true, 0);
73 add_action_widget (hbox, 2);
74 action_area.set_child_secondary (hbox, true);
76 action_area.show_all ();
78 add_button ("Test", 1);
79 add_button ("Done", Gtk.ResponseType.ACCEPT);
81 response.connect (on_response);
84 private Gtk.Widget led_command_list (int engine) {
85 var pannable = new Hildon.PannableArea ();
86 var vbox = new Gtk.VBox (false, 0);
88 var label = new Gtk.Label ("Engine %d:".printf (engine));
89 label.set_alignment (0, 0.5f);
90 vbox.pack_start (label, false, false, 0);
92 unowned List<LedCommandRX51> list = (engine == 1) ?
93 lpv.pattern.engine1 : lpv.pattern.engine2;
94 foreach (LedCommandRX51 command in list) {
95 if (command.type == CommandType.RESET_MUX)
97 var command_widget = new LedCommandWidget (lpv.pattern, list,
100 vbox.pack_start (command_widget, false, false, 0);
103 var button = new Gtk.Button.with_label ("New command");
104 Hildon.gtk_widget_set_theme_size (button, Hildon.SizeType.FINGER_HEIGHT);
105 button.clicked.connect (on_new_command_clicked);
106 vbox.pack_end (button, false, false, 0);
108 pannable.add_with_viewport (vbox);
112 void on_response (int response) {
114 Timeout.add (200, delayed_spawn);
117 if (response == Gtk.ResponseType.ACCEPT) {
118 if (pattern.dump () != lpv.pattern.dump ()) {
119 pattern.replace_with (lpv.pattern);
124 bool delayed_spawn () {
128 var command = "sudo /usr/bin/led-pattern-helper test \"" +
129 lpv.pattern.dump () + "\"";
130 Process.spawn_command_line_sync (command, null, out error, out exit_status);
131 if (exit_status != 0) {
132 var information = "Exit status: %d\n%s".printf (exit_status, error);
133 Hildon.Banner.show_information (null, null, information);
135 } catch (SpawnError e) {
136 Hildon.Banner.show_information (null, null, e.message);
142 void on_color1_clicked (Gtk.Button button) {
143 var dialog = new LedColorDialog ();
144 int response = dialog.run ();
146 LedColor color = (LedColor) response;
147 led_color1.set_color (color);
148 lpv.pattern.color1 = color;
149 color = led_color2.get_color () & ~response;
150 led_color2.set_color (color);
151 lpv.pattern.color2 = color;
152 lpv.pattern.changed ();
157 void on_color2_clicked (Gtk.Button button) {
158 var dialog = new LedColorDialog ();
159 int response = dialog.run ();
161 LedColor color = (LedColor) response;
162 led_color2.set_color (color);
163 lpv.pattern.color2 = color;
164 color = led_color1.get_color () & ~response;
165 led_color1.set_color (color);
166 lpv.pattern.color1 = color;
167 lpv.pattern.changed ();
172 void on_engine1_toggled (Gtk.ToggleButton source) {
173 if (source.get_active ())
179 void on_engine2_toggled (Gtk.ToggleButton source) {
180 if (source.get_active ())
186 void on_new_command_clicked (Gtk.Button button) {
187 var widget = button.parent.parent.parent;
188 unowned List<LedCommandRX51> engine;
189 if (widget == list1) {
190 engine = lpv.pattern.engine1;
191 } else if (widget == list2) {
192 engine = lpv.pattern.engine2;
197 var dialog = new Hildon.PickerDialog (this);
198 dialog.set_title ("New command");
200 var touch_selector = new Hildon.TouchSelector.text ();
201 touch_selector.append_text ("Set PWM");
202 touch_selector.append_text ("Ramp");
203 touch_selector.append_text ("Wait");
204 touch_selector.append_text ("Trigger");
205 touch_selector.append_text ("Go To Start");
206 touch_selector.append_text ("End");
207 dialog.set_selector (touch_selector);
209 int response = dialog.run ();
210 if (response == Gtk.ResponseType.OK) {
211 var command = new LedCommandRX51 ();
212 LedCommandRX51 last_command = engine.last ().data;
213 engine.append (command);
214 command.changed.connect (lpv.pattern.on_changed);
216 switch (touch_selector.get_active (0)) {
218 command.set_pwm (127);
221 if (last_command.type == CommandType.RAMP_WAIT) {
222 command.ramp_wait (last_command.step_time,
223 -last_command.steps);
224 } else if (last_command.level > 0) {
225 command.ramp_wait (125.0 / last_command.level,
226 -last_command.level);
228 command.ramp_wait (0.49, 255);
232 command.ramp_wait (100.0, 0);
235 command.type = CommandType.TRIGGER;
236 command.code = 0xe000;
240 command.go_to_start ();
246 var vbox = (Gtk.VBox) button.parent;
247 var command_widget = new LedCommandWidget (lpv.pattern, engine,
249 vbox.pack_start (command_widget, false, false, 0);
250 command_widget.show_all ();