]> mj.ucw.cz Git - ursary.git/blobdiff - ursaryd.c
Lights: Long press = full power
[ursary.git] / ursaryd.c
index ddc896f8062b64fe58c878c8420aecb010312134..e28cebfa4cd2060c8764c053e45c6e53be3d1a62 100644 (file)
--- a/ursaryd.c
+++ b/ursaryd.c
@@ -1,10 +1,10 @@
 /*
  *     The Ursary Audio Controls
  *
- *     (c) 2014--2018 Martin Mares <mj@ucw.cz>
+ *     (c) 2014--2020 Martin Mares <mj@ucw.cz>
  */
 
-#undef LOCAL_DEBUG
+#define LOCAL_DEBUG
 
 #include <ucw/lib.h>
 #include <ucw/clists.h>
@@ -14,6 +14,7 @@
 #include <ucw/opt.h>
 #include <ucw/stkstring.h>
 
+#include <math.h>
 #include <signal.h>
 #include <stdio.h>
 #include <string.h>
 #include <syslog.h>
 
 #include "ursaryd.h"
+#include "usb.h"
 
 /*
  *     Map of all controls
  *
  *             rotary          red button      green button
- *     0       sink Brum       mute            -
+ *             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *     0       sink PCH        mute            use headphones
  *     1       -               -               -
  *     2       -               -               -
- *     3       -               -               -
- *     4       MPD             mute            play/pause/stop
- *     5       Albireo MPV     mute            -
- *     6       Albireo other   mute            -
- *     7       other machines  mute            -
+ *     3       desk brightness -               desk lights on
+ *     4       MPD             mute            MPD play/pause
+ *     5       Albireo MPV     mute            MPD stop
+ *     6       Albireo other   mute            MPD prev
+ *     7       other machines  mute            MPD next
  *
  *     center  -
- *     slider  -
+ *     slider  light color temperature
  */
 
+#define PCH_SINK "alsa_output.pci-0000_00_1f.3.analog-stereo"
+
 /*** Sink controls ***/
 
 static double volume_from_pa(pa_volume_t vol)
@@ -288,6 +293,8 @@ static void update_group_from_button(int i, int on)
     }
 }
 
+#if 0  // Not used at the moment
+
 static int find_touched_client(void)
 {
   int touched = -1;
@@ -302,8 +309,12 @@ static int find_touched_client(void)
   return touched;
 }
 
+#endif
+
 /*** Default sink controls ***/
 
+#if 0  // Not mapped to any button at the moment
+
 static const char *get_client_sink(int i)
 {
   const char *sink = NULL;
@@ -414,6 +425,8 @@ static void update_default_sink_from_button(int button, int on)
     }
 }
 
+#endif
+
 /*** MPD controls ***/
 
 static bool mpd_flash_state;
@@ -498,6 +511,91 @@ static void update_mpd_from_button(int button UNUSED, int on)
     }
 }
 
+/*** Lights ***/
+
+static bool lights_on[2];
+static double lights_brightness[2];
+static double lights_temperature[2];
+
+static void update_lights(void)
+{
+  if (!dmx_is_ready())
+    {
+      noct_set_ring(3, RING_MODE_SINGLE_ON, 0x7f);
+      noct_set_button(11, 0);
+      return;
+    }
+
+  for (uint i=0; i<1; i++)
+    {
+      uint warm, cold;
+      if (lights_on[i])
+       {
+         noct_set_ring(3, RING_MODE_LEFT, lights_brightness[i] * 127);
+         noct_set_button(11, 1);
+         double r = 2;
+         double x = (exp(r*lights_brightness[i]) - 1) / (exp(r) - 1);
+         double t = lights_temperature[i];
+         double w = 2*x*(1-t);
+         double c = 2*x*t;
+         warm = CLAMP((int)(255*w), 0, 255);
+         cold = CLAMP((int)(255*c), 0, 255);
+       }
+      else
+       {
+         noct_set_ring(3, RING_MODE_LEFT, 0);
+         noct_set_button(11, 0);
+         warm = cold = 0;
+       }
+      DBG("Lights[%d]: on=%d bri=%.3f temp=%.3f -> warm=%d cold=%d", i, lights_on[i], lights_brightness[i], lights_temperature[i], warm, cold);
+      dmx_set_pwm(2*i, warm);
+      dmx_set_pwm(2*i+1, cold);
+    }
+}
+
+static void update_lights_from_rotary(int ch, int delta)
+{
+  if (lights_on[ch])
+    lights_brightness[ch] = CLAMP(lights_brightness[ch] + 0.015*delta*abs(delta), 0., 1.);
+  update_lights();
+}
+
+static void update_lights_from_slider(int value)
+{
+  lights_temperature[0] = value / 127.;
+  update_lights();
+}
+
+static void lights_button_timeout(struct main_timer *t)
+{
+  int ch = (uintptr_t) t->data;
+  DBG("Lights[%d]: Full throttle!", ch);
+  timer_del(t);
+  lights_on[ch] = 1;
+  lights_brightness[ch] = 1;
+  update_lights();
+}
+
+static void update_lights_from_button(int ch, int on)
+{
+  static struct main_timer lights_button_timer[2] = {{
+    .handler = lights_button_timeout,
+    .data = (void *)(uintptr_t) 0,
+  },{
+    .handler = lights_button_timeout,
+    .data = (void *)(uintptr_t) 1,
+  }};
+
+  if (on)
+    {
+      lights_on[ch] = !lights_on[ch];
+      update_lights();
+      timer_add_rel(&lights_button_timer[ch], 1000);
+    }
+  else
+    timer_del(&lights_button_timer[ch]);
+}
+
 /*** Main update routines ***/
 
 static struct main_timer update_timer;
@@ -596,13 +694,14 @@ static void do_update(struct main_timer *t)
     }
 
   // Everything normal
-  update_ring_from_sink(0, "alsa_output.brum.analog-stereo");
-  update_button_from_port(8, "alsa_output.brum.analog-stereo", "analog-output-headphones");
+  update_ring_from_sink(0, PCH_SINK);
+  update_button_from_port(8, PCH_SINK, "analog-output-headphones");
   update_groups();
 #if 0
   update_default_sink();
 #endif
   update_mpd();
+  update_lights();
 }
 
 void schedule_update(void)
@@ -636,7 +735,13 @@ void notify_rotary(int rotary, int delta)
   switch (rotary)
     {
     case 0:
-      update_sink_from_rotary(delta, "alsa_output.brum.analog-stereo");
+      update_sink_from_rotary(delta, PCH_SINK);
+      break;
+    case 3:
+      update_lights_from_rotary(0, delta);
+      break;
+    case 9:
+      update_lights_from_slider(delta);
       break;
     default:
       update_group_from_rotary(rotary, delta);
@@ -651,10 +756,10 @@ void notify_button(int button, int on)
   switch (button)
     {
     case 0:
-      update_sink_mute_from_button(on, "alsa_output.brum.analog-stereo");
+      update_sink_mute_from_button(on, PCH_SINK);
       break;
     case 8:
-      update_port_from_button(on, "alsa_output.brum.analog-stereo", "analog-output-lineout", "analog-output-headphones");
+      update_port_from_button(on, PCH_SINK, "analog-output-lineout", "analog-output-headphones");
       break;
 #if 0
     case 9:
@@ -662,6 +767,9 @@ void notify_button(int button, int on)
       update_default_sink_from_button(button, on);
       break;
 #endif
+    case 11:
+      update_lights_from_button(0, on);
+      break;
     case 12:
       update_mpd_from_button(button, on);
       break;
@@ -682,14 +790,16 @@ void notify_button(int button, int on)
     }
 }
 
-void notify_touch(int rotary, int on UNUSED)
+void notify_touch(int rotary UNUSED, int on UNUSED)
 {
   if (!prepare_notify())
     return;
 
+#if 0
   // Rotary touches switch meaning of LEDs, this is handled inside display updates
   if (rotary >= 4 && rotary < 8)
     schedule_update();
+#endif
 }
 
 /*** Main entry point ***/
@@ -719,7 +829,9 @@ static void daemon_body(struct daemon_params *dp)
   main_init();
   update_timer.handler = do_update;
 
+  usb_init();
   noct_init();
+  dmx_init();
   pulse_init();
   mpd_init();