2 * leds - Leds/buttons support
4 * Copyright (C) 2010 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 public int brightness;
30 public string trigger;
34 public LedState(string name)
40 public void set_constant(int power)
48 public void set_blink(int power, int delay_on=200, int delay_off=300)
52 this.delay_on = delay_on;
53 this.delay_off = delay_off;
56 public void to_omhacks(ref Omhacks.Led led)
58 led.brightness = brightness;
59 Memory.copy(led.trigger, trigger, trigger.size());
65 public class Vibrator : zavai.Resource, Object
67 protected Omhacks.Led vibrator;
69 protected List<LedState> states;
71 public Vibrator() throws FileError
73 if (vibrator.init("neo1973:vibrator") != 0)
74 throw new FileError.NOENT("vibrator not found");
76 states = new List<LedState>();
79 public void turn_off()
81 vibrator.brightness = 0;
82 Memory.copy(vibrator.trigger, "none", 5);
86 public void push_state(LedState state)
88 states.prepend(state);
89 state.to_omhacks(ref vibrator);
93 public void pop_state(string name)
96 if (states == null) return;
98 // Track if the list head changed
99 weak List<LedState> old_top = states;
101 // Remove state "name" from the stack
102 for (weak List<LedState> i = states; i != null; i = i.next)
103 if (i.data.name == name)
105 states.delete_link(i);
109 // If the list head changed, put into action the new top state
110 if (states != old_top)
114 // Activate the new top
115 states.data.to_omhacks(ref vibrator);
120 public void shutdown()
122 while (states != null)
123 states.delete_link(states);
128 public class AUX: zavai.Service
130 protected Omhacks.Led auxled;
131 protected bool has_aux;
133 protected weak clock.AlarmTriggerInfo current_alarm = null;
137 Object(name: "ui.aux");
139 has_aux = (auxled.init("gta02-aux:red") == 0);
143 zavai.log.warning("aux: can notify alarm triggers");
144 clock.alarm_trigger_queue.triggered += on_alarm_trigger;
145 clock.alarm_trigger_queue.acked += on_alarm_done;
146 clock.alarm_trigger_queue.canceled += on_alarm_done;
148 zavai.log.warning("aux: no way to notify alarm triggers");
151 // Listen to the button via X
152 input.hotkeys.hotkey += on_auxbutton;
153 input.hotkeys.grab(zavai.config.aux_button_keycode, 0, false);
154 input.hotkeys.request("auxbutton");
157 protected bool on_auxbutton(uint keycode, ulong time, bool pressed)
159 if (keycode == zavai.config.aux_button_keycode)
163 zavai.log.debug("AUX button pressed");
164 if (current_alarm != null)
166 zavai.log.debug("HASCA");
167 clock.alarm_trigger_queue.ack(current_alarm);
171 zavai.log.debug("AUX button released");
177 public void on_alarm_trigger(clock.AlarmTriggerInfo info)
179 zavai.log.debug("Start blinking");
180 auxled.brightness = 256;
181 // FIXME: is there a better way? I hope there is a better way. Please
182 // tell me there is a better way.
184 for (int i = 0; ; ++i)
186 auxled.trigger[i] = (char)trig[i];
187 if (trig[i] == 0) break;
189 auxled.delay_on = 200;
190 auxled.delay_off = 300;
192 current_alarm = info;
195 public void on_alarm_done(clock.AlarmTriggerInfo info)
197 zavai.log.debug("Stop blinking");
199 for (int i = 0; ; ++i)
201 auxled.trigger[i] = (char)trig[i];
202 if (trig[i] == 0) break;
204 auxled.brightness = 0;
206 current_alarm = null;
210 public AUX aux = null;
211 public Vibrator vibrator = null;
217 vibrator = new Vibrator();
218 zavai.registry.register(vibrator);
220 zavai.log.info("No vibrator found");