]> ToastFreeware Gitweb - gregoa/zavai.git/blobdiff - src/leds.vala
Merge branch 'master' into gregoa
[gregoa/zavai.git] / src / leds.vala
diff --git a/src/leds.vala b/src/leds.vala
new file mode 100644 (file)
index 0000000..013e1fa
--- /dev/null
@@ -0,0 +1,173 @@
+/*
+ * leds - Leds support
+ *
+ * Copyright (C) 2010  Enrico Zini <enrico@enricozini.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+using GLib;
+
+namespace zavai {
+namespace led {
+
+public class LedState
+{
+    public string name;
+    public int brightness;
+    public string trigger;
+    public int delay_on;
+    public int delay_off;
+
+    public LedState(string name)
+    {
+        this.name = name;
+        set_constant(0);
+    }
+
+    public void set_constant(int power)
+    {
+        brightness = power;
+        trigger = "none";
+        delay_on = 0;
+        delay_off = 0;
+    }
+
+    public void set_blink(int power, int delay_on=200, int delay_off=300)
+    {
+        brightness = power;
+        trigger = "timer";
+        this.delay_on = delay_on;
+        this.delay_off = delay_off;
+    }
+
+    public void to_omhacks(ref Omhacks.Led led)
+    {
+        led.brightness = brightness;
+        Memory.copy(led.trigger, trigger, trigger.size());
+        led.delay_on = 200;
+        led.delay_off = 300;
+    }
+}
+
+public class Led : zavai.Resource, Object
+{
+    protected Omhacks.Led led;
+
+    protected List<LedState> states;
+
+    public Led(string name) throws FileError
+    {
+        if (led.init(name) != 0)
+            throw new FileError.NOENT("led " + name + " not found");
+        states = new List<LedState>();
+    }
+
+    public void turn_off()
+    {
+        led.brightness = 0;
+        Memory.copy(led.trigger, "none", 5);
+        led.set();
+    }
+
+    public void push_state(LedState state)
+    {
+        states.prepend(state);
+        state.to_omhacks(ref led);
+        led.set();
+    }
+
+    public void pop_state(string name)
+    {
+        // Handle empty list
+        if (states == null) return;
+
+        // Track if the list head changed
+        weak List<LedState> old_top = states;
+
+        // Remove state "name" from the stack
+        for (weak List<LedState> i = states; i != null; i = i.next)
+            if (i.data.name == name)
+            {
+                states.delete_link(i);
+                break;
+            }
+
+        // If the list head changed, put into action the new top state
+        if (states != old_top)
+            if (states == null)
+                turn_off();
+            else {
+                // Activate the new top
+                states.data.to_omhacks(ref led);
+                led.set();
+            }
+    }
+
+    public void shutdown()
+    {
+        while (states != null)
+            states.delete_link(states);
+        turn_off();
+    }
+}
+
+public class AUXButton: zavai.Service
+{
+    public signal bool event(ulong time, bool pressed);
+
+    public AUXButton()
+    {
+        // Listen to the button via X
+        input.hotkeys.hotkey += on_auxbutton;
+        input.hotkeys.grab(zavai.config.aux_button_keycode, 0, false);
+        input.hotkeys.request("auxbutton");
+    }
+
+    protected bool on_auxbutton(uint keycode, ulong time, bool pressed)
+    {
+        if (keycode == zavai.config.aux_button_keycode)
+            return event(time, pressed);
+        return false;
+    }
+}
+
+public Led aux = null;
+public Led vibrator = null;
+public AUXButton auxbutton = null;
+
+public void init()
+{
+    auxbutton = new AUXButton();
+
+    try {
+        aux = new Led("gta02-aux:red");
+        zavai.registry.register(aux);
+    } catch (Error e) {
+        zavai.log.info("No aux led found");
+        aux = null;
+    }
+
+    try {
+        vibrator = new Led("neo1973:vibrator");
+        zavai.registry.register(vibrator);
+    } catch (Error e) {
+        zavai.log.info("No vibrator found");
+        vibrator = null;
+    }
+}
+
+}
+}