]> mj.ucw.cz Git - ursary.git/blobdiff - ursaryd.c
Better IR control of lights
[ursary.git] / ursaryd.c
index 85cde2bfdd8e06ef013076f1eab0cad89d4520ed..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
@@ -43,7 +43,7 @@
  *     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,47 @@ 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;
@@ -809,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/";
@@ -857,6 +935,9 @@ void notify_mqtt(const char *topic, const char *val)
            }
        }
     }
+
+  if (!strcmp(topic, "burrow/control/catarium-ir"))
+    notify_ir(val);
 }
 
 /*** Main entry point ***/