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 LedCommandWidget : Gtk.HBox {
20 private const double CYCLE_TIME_MS = 1000.0 / 32768.0;
22 LedPatternRX51 pattern;
23 unowned List<LedCommandRX51> engine;
24 LedCommandRX51 command;
26 public LedCommandWidget (LedPatternRX51 _pattern, List<LedCommandRX51> _engine,
27 LedCommandRX51 _command) {
33 var button = new Hildon.Button (Hildon.SizeType.FINGER_HEIGHT,
34 Hildon.ButtonArrangement.VERTICAL);
35 button.set_style (Hildon.ButtonStyle.PICKER);
36 button.set_alignment (0, 0.5f, 0, 0.5f);
38 switch (command.type) {
39 case CommandType.UNKNOWN:
40 button.set_title ("???");
41 button.set_value ("0x%04x".printf (command.code));
42 button.set_sensitive (false);
44 case CommandType.RAMP_WAIT:
45 button.set_title ((command.steps != 0) ? "Ramp" : "Wait");
46 button.set_value ((command.steps != 0) ?
47 "%+d steps, %.2f ms each".printf (command.steps,
49 "%.2f ms".printf (command.duration));
50 button.clicked.connect (on_ramp_wait_clicked);
52 case CommandType.SET_PWM:
53 button.set_title ("Set PWM");
54 button.set_value ("Level = %d".printf (command.level));
55 button.clicked.connect (on_set_pwm_clicked);
57 case CommandType.RESET_MUX:
58 button.set_title ("Reset Mux");
60 case CommandType.GO_TO_START:
61 button.set_title ("Go To Start");
62 button.set_value ("");
63 button.clicked.connect (on_end_or_go_to_start_clicked);
65 case CommandType.BRANCH:
66 button.set_title ("Branch");
67 button.set_value ("0x%04x".printf (command.code));
68 button.set_sensitive (false);
71 button.set_title ("End");
72 button.set_value ((command.steps == -255) ? "Reset" : "Hold");
73 button.clicked.connect (on_end_or_go_to_start_clicked);
75 case CommandType.TRIGGER:
76 button.set_title ("Trigger");
78 if (0x0100 in command.code)
80 if (0x0004 in command.code)
82 if (0x0080 in command.code)
84 if (0x0002 in command.code)
86 if ((command.code & ~0xe186) != 0) {
87 text = "Unsupported: 0x%04x".printf (command.code);
88 button.set_sensitive (false);
90 button.set_value (text);
91 button.clicked.connect (on_trigger_clicked);
94 pack_start (button, true, true, 0);
97 private void on_ramp_wait_clicked (Gtk.Button source) {
98 double old_step_time = command.step_time;
99 int old_steps = command.steps;
100 var dialog = new Gtk.Dialog ();
101 dialog.set_title ("Ramp / Wait");
103 var content = (Gtk.VBox) dialog.get_content_area ();
105 var lpv = new LedPatternView (pattern);
106 lpv.set_size_request (-1, 70);
107 content.pack_start (lpv, true, true, 0);
109 var scale1 = new Gtk.HScale.with_range (1, 62, 1);
110 scale1.set_increments (1, 1);
111 var v = (command.step_time / 16 / CYCLE_TIME_MS);
114 scale1.set_value (v);
115 scale1.format_value.connect ((v) => {
117 return "%.2f ms".printf (v * 16 * CYCLE_TIME_MS);
119 return "%.1f ms".printf ((v - 31) * 512 * CYCLE_TIME_MS);
121 scale1.value_changed.connect ((s) => {
122 int val = (int) s.get_value ();
124 command.ramp_wait (val * 16 * CYCLE_TIME_MS, command.steps);
126 command.ramp_wait ((val - 31) * 512 * CYCLE_TIME_MS, command.steps);
128 content.pack_start (scale1, true, true, 0);
130 var hbox = new Gtk.HBox (false, 0);
131 var hbox2 = new Gtk.HBox (true, 0);
132 var radio_inc = (Gtk.RadioButton) Hildon.gtk_radio_button_new (Hildon.SizeType.FINGER_HEIGHT, null);
133 radio_inc.set_mode (false);
134 radio_inc.set_label ("+");
135 hbox2.pack_start (radio_inc, false, false, 0);
136 var radio_dec = (Gtk.RadioButton) Hildon.gtk_radio_button_new_from_widget (Hildon.SizeType.FINGER_HEIGHT, radio_inc);
137 radio_dec.set_mode (false);
138 radio_dec.set_label ("-");
139 bool sign = command.steps < 0;
140 radio_dec.set_active (sign);
141 radio_dec.toggled.connect ((s) => {
142 sign = s.get_active ();
143 if (sign != (command.steps < 0))
144 command.ramp_wait (command.step_time, -command.steps);
146 hbox2.pack_start (radio_dec, false, false, 0);
147 hbox.pack_start (hbox2, false, false, 0);
148 var scale2 = new Gtk.HScale.with_range (0, 255, 1);
149 scale2.set_increments (1, 1);
150 scale2.set_value ((command.steps < 0) ? -command.steps : command.steps);
151 scale2.set_value_pos (Gtk.PositionType.RIGHT);
152 scale2.format_value.connect ((v) => {
153 return "%+d".printf (sign ? -(int) v : (int) v);
155 scale2.value_changed.connect ((s) => {
157 command.ramp_wait (command.step_time, -(int) scale2.get_value ());
159 command.ramp_wait (command.step_time, (int) scale2.get_value ());
161 hbox.pack_start (scale2, true, true, 0);
162 content.pack_start (hbox, false, false, 0);
165 dialog.add_button ("Delete", 1);
166 dialog.add_button ("Done", Gtk.ResponseType.OK);
168 int response = dialog.run ();
169 if (response == Gtk.ResponseType.OK) {
170 var button = source as Hildon.Button;
171 button.set_title ((command.steps != 0) ? "Ramp" : "Wait");
172 button.set_value ((command.steps != 0) ?
173 "%+d steps, %.2f ms each".printf (command.steps,
175 "%.2f ms".printf (command.duration));
176 } else if (response == 1) {
177 engine.remove (command);
178 engine.first ().data.changed ();
181 command.ramp_wait (old_step_time, old_steps);
186 private void on_set_pwm_clicked (Gtk.Button source) {
187 int old_level = command.level;
188 var dialog = new Gtk.Dialog ();
189 dialog.set_title ("Set PWM");
191 var content = (Gtk.VBox) dialog.get_content_area ();
193 var lpv = new LedPatternView (pattern);
194 lpv.set_size_request (-1, 70);
195 content.pack_start (lpv, true, true, 0);
197 var scale = new Gtk.HScale.with_range (0, 255, 1);
198 scale.set_increments (1, 1);
199 scale.set_value (command.level);
200 scale.value_changed.connect ((s) => {
201 command.set_pwm ((int) s.get_value ());
203 content.pack_start (scale, true, true, 0);
206 dialog.add_button ("Delete", 1);
207 dialog.add_button ("Done", Gtk.ResponseType.OK);
209 int response = dialog.run ();
210 if (response == Gtk.ResponseType.OK) {
211 command.set_pwm ((int) scale.get_value ());
213 var button = source as Hildon.Button;
214 button.set_value ("Level = %d".printf (command.level));
215 } else if (response == 1) {
216 engine.remove (command);
217 engine.first ().data.changed ();
220 command.set_pwm (old_level);
225 private void on_end_or_go_to_start_clicked (Gtk.Button source) {
226 CommandType old_type = command.type;
227 int old_steps = command.steps;
228 uint16 old_code = command.code;
229 var dialog = new Gtk.Dialog ();
230 if (command.type == CommandType.END)
231 dialog.set_title ("End");
233 dialog.set_title ("Go To Start");
235 var content = (Gtk.VBox) dialog.get_content_area ();
237 var lpv = new LedPatternView (pattern);
238 lpv.set_size_request (-1, 70);
239 content.pack_start (lpv, true, true, 0);
241 var hbox = new Gtk.HBox (true, 0);
242 var radio_end = (Gtk.RadioButton) Hildon.gtk_radio_button_new (Hildon.SizeType.FINGER_HEIGHT, null);
243 radio_end.set_mode (false);
244 radio_end.set_label ("End");
245 hbox.pack_start (radio_end, true, true, 0);
246 var radio_go_to_start = (Gtk.RadioButton) Hildon.gtk_radio_button_new_from_widget (Hildon.SizeType.FINGER_HEIGHT, radio_end);
247 radio_go_to_start.set_mode (false);
248 radio_go_to_start.set_label ("Go To Start");
249 hbox.pack_start (radio_go_to_start, true, true, 0);
250 content.pack_start (hbox, true, true, 0);
252 hbox = new Gtk.HBox (true, 0);
253 var radio_hold = (Gtk.RadioButton) Hildon.gtk_radio_button_new (Hildon.SizeType.FINGER_HEIGHT, null);
254 radio_hold.set_mode (false);
255 radio_hold.set_label ("Hold");
256 hbox.pack_start (radio_hold, true, true, 0);
257 var radio_reset = (Gtk.RadioButton) Hildon.gtk_radio_button_new_from_widget (Hildon.SizeType.FINGER_HEIGHT, radio_hold);
258 radio_reset.set_mode (false);
259 radio_reset.set_label ("Reset");
260 hbox.pack_start (radio_reset, true, true, 0);
261 content.pack_start (hbox, true, true, 0);
263 if (command.type == CommandType.END)
264 radio_end.set_active (true);
266 radio_go_to_start.set_active (true);
267 radio_hold.set_sensitive (command.type == CommandType.END);
268 radio_reset.set_sensitive (command.type == CommandType.END);
269 radio_reset.set_active (command.steps == -255);
271 radio_end.toggled.connect ((s) => {
272 if (s.get_active ()) {
273 command.end (radio_reset.get_active ());
274 radio_hold.set_sensitive (true);
275 radio_reset.set_sensitive (true);
276 dialog.set_title ("End");
278 command.go_to_start ();
279 radio_hold.set_sensitive (false);
280 radio_reset.set_sensitive (false);
281 dialog.set_title ("Go To Start");
285 radio_reset.toggled.connect ((s) => {
286 if (command.type == CommandType.END) {
287 command.end (s.get_active ());
292 dialog.add_button ("Delete", 1);
293 dialog.add_button ("Done", Gtk.ResponseType.OK);
295 int response = dialog.run ();
296 if (response == Gtk.ResponseType.OK) {
297 var button = source as Hildon.Button;
298 button.set_value ((command.type == CommandType.END) ?
299 "End" : "Go To Start");
300 button.set_value ((command.type == CommandType.END) ?
301 ((command.steps == -255) ? "Reset" : "Hold") : "");
302 } else if (response == 1) {
303 engine.remove (command);
304 engine.first ().data.changed ();
307 command.type = old_type;
308 command.steps = old_steps;
309 command.code = old_code;
315 private void on_trigger_clicked (Gtk.Button source) {
316 uint16 old_code = command.code;
317 var dialog = new Gtk.Dialog ();
318 dialog.set_title ("Trigger");
320 var content = (Gtk.VBox) dialog.get_content_area ();
322 var lpv = new LedPatternView (pattern);
323 lpv.set_size_request (-1, 70);
324 content.pack_start (lpv, true, true, 0);
326 var check = new Hildon.CheckButton (Hildon.SizeType.FINGER_HEIGHT);
327 check.set_label ("Set trigger");
328 if ((command.code & 0x0006) != 0)
329 check.set_active (true);
330 check.toggled.connect ((s) => {
331 int bit = (engine == pattern.engine1) ? 0x0004 : 0x0002;
335 command.code &= ~bit;
338 content.pack_start (check, true, true, 0);
340 check = new Hildon.CheckButton (Hildon.SizeType.FINGER_HEIGHT);
341 check.set_label ("Wait for trigger");
342 if ((command.code & 0x0180) != 0)
343 check.set_active (true);
344 check.toggled.connect ((s) => {
345 int bit = (engine == pattern.engine1) ? 0x0100 : 0x0080;
349 command.code &= ~bit;
352 content.pack_start (check, true, true, 0);
355 dialog.add_button ("Delete", 1);
356 dialog.add_button ("Done", Gtk.ResponseType.OK);
358 int response = dialog.run ();
359 if (response == Gtk.ResponseType.OK) {
360 var button = source as Hildon.Button;
362 if (0x0100 in command.code)
364 if (0x0004 in command.code)
366 if (0x0080 in command.code)
368 if (0x0002 in command.code)
370 if ((command.code & ~0xe186) != 0) {
371 text = "Unsupported: 0x%04x".printf (command.code);
372 button.set_sensitive (false);
374 button.set_value (text);
375 } else if (response == 1) {
376 engine.remove (command);
377 engine.first ().data.changed ();
380 command.code = old_code;