]> the.earth.li Git - mqttdeck.git/commitdiff
Add support for sending button images over MQTT
authorJonathan McDowell <noodles@earth.li>
Sat, 26 Apr 2025 18:49:59 +0000 (19:49 +0100)
committerJonathan McDowell <noodles@earth.li>
Sat, 26 Apr 2025 18:49:59 +0000 (19:49 +0100)
Hook up a base64/image decoder combo so we can send an entire image over
MQTT to display on the buttons.

Cargo.lock
Cargo.toml
src/main.rs

index 6882f12f5e931456a4e1fe88f9b89fc0d3167033..f8ea2290622ea7f7ba5e216c748eaf1f818346fb 100644 (file)
@@ -47,6 +47,12 @@ version = "1.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
 
+[[package]]
+name = "base64"
+version = "0.22.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
+
 [[package]]
 name = "bit_field"
 version = "0.10.2"
@@ -547,6 +553,7 @@ dependencies = [
 name = "mqttdeck"
 version = "0.1.0"
 dependencies = [
+ "base64",
  "clap",
  "elgato-streamdeck",
  "hidapi",
index 13d5bd90b2af255bcec3b1ced782a98dcfb7aef8..0b99c3427055dc6950a9f8461c8aafa4056996b0 100644 (file)
@@ -4,6 +4,7 @@ version = "0.1.0"
 edition = "2021"
 
 [dependencies]
+base64 = "0.22.1"
 clap = { version = "4.0.32", features = ["derive"] }
 elgato-streamdeck = { version = "0.5.0", features = ["async"] }
 hidapi = { version = "2.6.3", default-features = false, features = ["linux-static-hidraw"] }
index 978a77b4b7e7620cc9909214201a2527966c37b9..88a72a0e0f4f3e397508a2d126e35a1344a60d93 100644 (file)
@@ -3,6 +3,7 @@
 //
 // Copyright 2023 Jonathan McDowell <noodles@earth.li>
 
+use base64::prelude::*;
 use clap::Parser;
 use elgato_streamdeck::{list_devices, new_hidapi, AsyncStreamDeck, StreamDeckInput};
 use image::{DynamicImage, ImageBuffer, RgbImage};
@@ -102,6 +103,43 @@ async fn handle_commands(
                     .unwrap();
                     send_command_response(&cli, &resp_topic, "OK").await;
                 }
+                Some("set-image") => {
+                    if command["button"].is_null() {
+                        send_command_response(&cli, &resp_topic, "no button supplied").await;
+                        continue;
+                    }
+                    if command["image"].is_null() {
+                        send_command_response(&cli, &resp_topic, "no image supplied").await;
+                        continue;
+                    }
+
+                    let image = match BASE64_STANDARD.decode(command["image"].as_str().unwrap()) {
+                        Ok(image) => image,
+                        Err(e) => {
+                            send_command_response(&cli, &resp_topic, e.to_string().as_str()).await;
+                            continue;
+                        }
+                    };
+
+                    let img = match image::load_from_memory(&image) {
+                        Ok(img) => img.rotate180(),
+                        Err(e) => {
+                            send_command_response(&cli, &resp_topic, e.to_string().as_str()).await;
+                            continue;
+                        }
+                    };
+
+                    match deck.set_button_image(
+                        command["button"].as_u8().unwrap(),
+                        img,
+                    )
+                    .await {
+                        Ok(_) => send_command_response(&cli, &resp_topic, "OK").await,
+                        Err(e) => send_command_response(&cli, &resp_topic, e.to_string().as_str()).await,
+                    }
+
+
+                },
                 _ => send_command_response(&cli, &resp_topic, "Unknown action").await,
             }
         } else {