+/*** Source mute controls ***/
+
+static void update_source_buttons(void)
+{
+ struct pulse_source *source = pulse_source_by_name(LOGI_SOURCE);
+ if (!source)
+ return;
+
+#if 0 // Disabled for now
+ // if (source->suspended || source->mute)
+ if (source->mute)
+ noct_set_button(2, 0);
+ else
+ noct_set_button(2, 1);
+#endif
+}
+
+static void update_source_mute_from_button(int on, const char *source_name)
+{
+ if (!on)
+ return;
+
+ struct pulse_source *s = pulse_source_by_name(source_name);
+ if (!s)
+ return;
+
+#if 0
+ DBG("## Setting mute of source %s to %d", s->name, !s->mute);
+ pulse_source_set_mute(s->idx, !s->mute);
+#endif
+}
+
+/*** MPD controls ***/
+
+static bool mpd_flash_state;
+
+static void mpd_flash_timeout(struct main_timer *t)
+{
+ mpd_flash_state ^= 1;
+ noct_set_button(12, mpd_flash_state);
+ timer_add_rel(t, 500);
+}
+
+static struct main_timer mpd_flash_timer = {
+ .handler = mpd_flash_timeout,
+};
+
+static void update_mpd(void)
+{
+ const char *state = mpd_get_player_state();
+ if (!strcmp(state, "play"))
+ {
+ noct_set_button(12, 1);
+ timer_del(&mpd_flash_timer);
+ }
+ else if (!strcmp(state, "pause"))
+ {
+ if (!timer_is_active(&mpd_flash_timer))
+ {
+ mpd_flash_state = 1;
+ mpd_flash_timeout(&mpd_flash_timer);
+ }
+ }
+ else
+ {
+ noct_set_button(12, 0);
+ timer_del(&mpd_flash_timer);
+ }
+}
+
+static void mpd_button_timeout(struct main_timer *t)
+{
+ DBG("MPD stop");
+ timer_del(t);
+ mpd_stop();
+}
+
+static void update_mpd_from_button(int button UNUSED, int on)
+{
+ static struct main_timer mpd_button_timer = {
+ .handler = mpd_button_timeout,
+ };
+
+ const char *state = mpd_get_player_state();
+
+ if (!on)
+ {
+ if (timer_is_active(&mpd_button_timer))
+ {
+ timer_del(&mpd_button_timer);
+ if (!strcmp(state, "play"))
+ {
+ DBG("MPD pause");
+ mpd_pause(1);
+ }
+ else if (!strcmp(state, "pause"))
+ {
+ DBG("MPD resume");
+ mpd_pause(0);
+ }
+ }
+ return;
+ }
+
+ if (!strcmp(state, "stop"))
+ {
+ DBG("MPD play");
+ mpd_play();
+ }
+ else
+ {
+ DBG("MPD starting button timer");
+ timer_add_rel(&mpd_button_timer, 1000);
+ }
+}
+
+/*** Lights ***/
+
+static bool lights_on[2];
+static double lights_brightness[2];
+static double lights_temperature[2];
+static timestamp_t lights_last_update[2];
+
+static void update_lights(void)
+{
+ for (uint ch=0; ch < 2; ch++)
+ {
+ if (lights_on[ch])
+ {
+ noct_set_ring(3-ch, RING_MODE_LEFT, lights_brightness[ch] * 127);
+ noct_set_button(11-ch, 1);
+ }
+ else
+ {
+ noct_set_ring(3-ch, RING_MODE_LEFT, 0);
+ noct_set_button(11-ch, 0);
+ }
+ }
+}
+
+static void send_lights(int ch)
+{
+ DBG("Lights[%d]: on=%d bri=%.3f temp=%.3f", ch, lights_on[ch], lights_brightness[ch], lights_temperature[ch]);
+ double b = lights_on[ch] ? lights_brightness[ch] : 0;
+ double t = lights_on[ch] ? lights_temperature[ch] : 0;
+ char topic[100], val[100];
+ snprintf(topic, sizeof(topic), "burrow/lights/catarium/%s", (ch ? "top" : "bottom"));
+ snprintf(val, sizeof(val), "%.3f %.3f", b, t);
+ mqtt_publish(topic, val);
+ lights_last_update[ch] = main_get_now();
+ update_lights();
+}
+
+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.);
+ send_lights(ch);
+}
+
+static void update_lights_from_slider(int value)
+{
+ lights_temperature[0] = value / 127.;
+ lights_temperature[1] = value / 127.;
+ send_lights(0);
+ send_lights(1);
+}
+
+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;
+ send_lights(ch);
+}
+
+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)
+ timer_add_rel(&lights_button_timer[ch], 500);
+ else if (timer_is_active(&lights_button_timer[ch]))
+ {
+ timer_del(&lights_button_timer[ch]);
+ lights_on[ch] = !lights_on[ch];
+ send_lights(ch);
+ }
+}
+
+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();
+}
+