]> mj.ucw.cz Git - ursary.git/blobdiff - ursaryd.c
Better IR control of lights
[ursary.git] / ursaryd.c
index 748ddc3f3eac524d6f08b2319a56a94c954dffaa..1d0e5ff4ed56ea8881a1506f44f1a4908d5f048b 100644 (file)
--- a/ursaryd.c
+++ b/ursaryd.c
@@ -1,7 +1,7 @@
 /*
  *     The Ursary Control Panel
  *
- *     (c) 2014--2022 Martin Mares <mj@ucw.cz>
+ *     (c) 2014-2023 Martin Mares <mj@ucw.cz>
  */
 
 #undef LOCAL_DEBUG
  *     6       Albireo Zoom    mute            MPD prev
  *     7       eveyrhing else  mute            MPD next
  *
- *     center  -
+ *     center  rainbow brightness
  *     slider  light color temperature
  */
 
-#define PCH_SINK "alsa_output.pci-0000_00_1f.3.analog-stereo"
+#define PCH_SINK "alsa_output.pci-0000_07_00.6.analog-stereo"
 #define BT_SINK "bluez_sink.CC_98_8B_D0_8C_06.a2dp_sink"
 #define LOGI_SOURCE "alsa_input.usb-046d_Logitech_Webcam_C925e_EF163C5F-02.analog-stereo"
 
@@ -567,6 +567,72 @@ static void update_lights_from_button(int ch, int on)
     }
 }
 
+static void update_lights_from_ir(int ch, int dir)
+{
+  if (lights_on[ch])
+    lights_brightness[ch] = CLAMP(lights_brightness[ch] + 0.07*dir, 0., 1.);
+  else if (dir > 0)
+    {
+      lights_on[ch] = 1;
+      lights_brightness[ch] = 1;
+    }
+  else
+    {
+      lights_on[ch] = 1;
+      lights_brightness[ch] = 0.05;
+    }
+  send_lights(ch);
+}
+
+static void update_lights_on_off_ir(int ch)
+{
+  lights_on[ch] ^= 1;
+  send_lights(ch);
+}
+
+static void update_lights_temp_ir(void)
+{
+  if (!lights_on[0] && !lights_on[1])
+    return;
+
+  double t = (lights_temperature[0] + lights_temperature[1]) / 2;
+  if (t >= 0.66)
+    t = 0;
+  else if (t < 0.33)
+    t = 0.5;
+  else
+    t = 1;
+  lights_temperature[0] = lights_temperature[1] = t;
+
+  send_lights(0);
+  send_lights(1);
+}
+
+/*** Rainbow ***/
+
+static double rainbow_brightness;
+static timestamp_t rainbow_last_update;
+
+static void update_rainbow(void)
+{
+  noct_set_ring(8, RING_MODE_LEFT, rainbow_brightness * 127);
+}
+
+static void send_rainbow(void)
+{
+  char val[100];
+  snprintf(val, sizeof(val), "%.3f", rainbow_brightness);
+  mqtt_publish("burrow/lights/rainbow/brightness", val);
+  rainbow_last_update = main_get_now();
+  update_rainbow();
+}
+
+static void update_rainbow_from_rotary(int delta)
+{
+  rainbow_brightness = CLAMP(rainbow_brightness + 0.015*delta*abs(delta), 0., 1.);
+  send_rainbow();
+}
+
 /*** Main update routines ***/
 
 static struct main_timer update_timer;
@@ -672,6 +738,7 @@ static void do_update(struct main_timer *t)
   update_source_buttons();
   update_mpd();
   update_lights();
+  update_rainbow();
 }
 
 void schedule_update(void)
@@ -716,6 +783,9 @@ void notify_rotary(int rotary, int delta)
     case 3:
       update_lights_from_rotary(0, delta);
       break;
+    case 8:
+      update_rainbow_from_rotary(delta);
+      break;
     case 9:
       update_lights_from_slider(delta);
       break;
@@ -780,6 +850,43 @@ void notify_touch(int rotary UNUSED, int on UNUSED)
     schedule_update();
 }
 
+static void notify_ir(const char *key)
+{
+  DBG("Received IR key %s", key);
+
+  // Lights
+  if (!strcmp(key, "preset+"))
+    update_lights_from_ir(1, 1);
+  else if (!strcmp(key, "preset-"))
+    update_lights_from_ir(1, -1);
+  else if (!strcmp(key, "tuning-up"))
+    update_lights_from_ir(0, 1);
+  else if (!strcmp(key, "tuning-down"))
+    update_lights_from_ir(0, -1);
+  else if (!strcmp(key, "band"))
+    update_lights_on_off_ir(1);
+  else if (!strcmp(key, "fm-mode"))
+    update_lights_on_off_ir(0);
+  else if (!strcmp(key, "dimmer"))
+    update_lights_temp_ir();
+
+  // Player
+  else if (!strcmp(key, "play"))
+    mpd_play();
+  else if (!strcmp(key, "stop"))
+    mpd_stop();
+  else if (!strcmp(key, "pause"))
+    mpd_pause(1);
+  else if (!strcmp(key, "prev-song"))
+    mpd_prev();
+  else if (!strcmp(key, "next-song"))
+    mpd_next();
+  else if (!strcmp(key, "rewind"))
+    update_sink_from_rotary(-2, PCH_SINK);
+  else if (!strcmp(key, "ffwd"))
+    update_sink_from_rotary(2, PCH_SINK);
+}
+
 void notify_mqtt(const char *topic, const char *val)
 {
   const char blc[] = "burrow/lights/catarium/";
@@ -813,6 +920,24 @@ void notify_mqtt(const char *topic, const char *val)
          update_lights();
        }
     }
+
+  if (!strcmp(topic, "burrow/lights/rainbow/brightness"))
+    {
+      double b;
+      if (sscanf(val, "%lf", &b) == 1 && b >= 0 && b <= 1)
+       {
+         timestamp_t now = main_get_now();
+         if (!rainbow_last_update || rainbow_last_update + 1000 < now)
+           {
+             DBG("Received foreign rainbow settings");
+             rainbow_brightness = b;
+             update_rainbow();
+           }
+       }
+    }
+
+  if (!strcmp(topic, "burrow/control/catarium-ir"))
+    notify_ir(val);
 }
 
 /*** Main entry point ***/