From 2ba39945763c2ce72df5632f9e37bfd393e2f8e7 Mon Sep 17 00:00:00 2001 From: Jonathan McDowell Date: Sat, 26 Apr 2025 19:49:59 +0100 Subject: [PATCH] Add support for sending button images over MQTT Hook up a base64/image decoder combo so we can send an entire image over MQTT to display on the buttons. --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 6882f12..f8ea229 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index 13d5bd9..0b99c34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/src/main.rs b/src/main.rs index 978a77b..88a72a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ // // Copyright 2023 Jonathan McDowell +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 { -- 2.39.5