feat: Add support for brightness control (#3)

This commit is contained in:
Valentin Weber 2025-02-22 11:29:22 +01:00 committed by GitHub
parent 3724f16edf
commit f13e4c215b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 43 additions and 14 deletions

2
Cargo.lock generated
View file

@ -112,7 +112,7 @@ dependencies = [
[[package]]
name = "badgemagic"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"anyhow",
"base64",

View file

@ -1,6 +1,6 @@
[package]
name = "badgemagic"
version = "0.1.0"
version = "0.2.0"
authors = ["Martin Michaelis <code@mgjm.de>", "Valentin Weber <weva+code@kabelsalat.ch>"]
edition = "2021"
description = "Badge Magic with LEDs - Library and CLI"

View file

@ -5,13 +5,13 @@ use badgemagic::{
embedded_graphics::{
geometry::Point, mono_font::MonoTextStyle, pixelcolor::BinaryColor, text::Text,
},
protocol::{Mode, PayloadBuffer, Style},
protocol::{Brightness, Mode, PayloadBuffer, Style},
usb_hid::Device,
util::DrawableLayoutExt,
};
fn main() -> Result<()> {
let mut payload = PayloadBuffer::new();
let mut payload = PayloadBuffer::new(Brightness::default());
payload.add_message_drawable(
Style::default().mode(Mode::Center),

View file

@ -5,7 +5,7 @@ use std::{fs, path::PathBuf};
use anyhow::{Context, Result};
use badgemagic::{
ble::Device as BleDevice,
protocol::{Mode, PayloadBuffer, Speed, Style},
protocol::{Brightness, Mode, PayloadBuffer, Speed, Style},
usb_hid::Device as UsbDevice,
};
use base64::Engine;
@ -43,6 +43,10 @@ struct Args {
#[clap(long)]
transport: TransportProtocol,
/// Brightness of the panel
#[clap(long)]
brightness: Option<Brightness>,
/// List all devices visible to a transport and exit
#[clap(long)]
list_devices: bool,
@ -146,7 +150,7 @@ fn gnerate_payload(args: &mut Args) -> Result<PayloadBuffer> {
}
};
let mut payload = PayloadBuffer::new();
let mut payload = PayloadBuffer::new(args.brightness.unwrap_or_default());
for message in config.messages {
let mut style = Style::default();

View file

@ -1,7 +1,5 @@
//! Protocol used to update the badge
use std::num::TryFromIntError;
#[cfg(feature = "embedded-graphics")]
use embedded_graphics::{
draw_target::DrawTarget,
@ -11,6 +9,7 @@ use embedded_graphics::{
primitives::Rectangle,
Drawable,
};
use std::num::TryFromIntError;
use time::OffsetDateTime;
use zerocopy::{BigEndian, FromBytes, Immutable, IntoBytes, KnownLayout, U16};
@ -161,7 +160,7 @@ impl TryFrom<u8> for Speed {
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum Mode {
/// Scroll thorugh the message from left to right
/// Scroll through the message from left to right
#[default]
Left,
@ -193,14 +192,39 @@ pub enum Mode {
Laser,
}
/// Display Brightness
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "PascalCase"))]
pub enum Brightness {
#[default]
Full,
ThreeQuarters,
Half,
OneQuarter,
}
impl From<Brightness> for u8 {
fn from(value: Brightness) -> Self {
match value {
Brightness::Full => 0x00,
Brightness::ThreeQuarters => 0x10,
Brightness::Half => 0x20,
Brightness::OneQuarter => 0x30,
}
}
}
const MSG_PADDING_ALIGN: usize = 64;
const MAGIC: [u8; 6] = *b"wang\0\0";
const MAGIC: [u8; 5] = *b"wang\0";
#[derive(FromBytes, IntoBytes, Immutable, KnownLayout)]
#[repr(C)]
struct Header {
magic: [u8; 6],
magic: [u8; 5],
brightness: u8,
blink: u8,
border: u8,
speed_and_mode: [u8; 8],
@ -252,7 +276,7 @@ impl Timestamp {
/// primitives::{PrimitiveStyle, Rectangle, Styled},
/// };
///
/// let mut buffer = PayloadBuffer::new();
/// let mut buffer = PayloadBuffer::default();
/// buffer.add_message_drawable(
/// Style::default(),
/// &Styled::new(
@ -271,18 +295,19 @@ pub struct PayloadBuffer {
impl Default for PayloadBuffer {
fn default() -> Self {
Self::new()
Self::new(Brightness::Full)
}
}
impl PayloadBuffer {
/// Create a new empty buffer
#[must_use]
pub fn new() -> Self {
pub fn new(brightness: Brightness) -> Self {
Self {
num_messages: 0,
data: Header {
magic: MAGIC,
brightness: brightness.into(),
blink: 0,
border: 0,
speed_and_mode: [0; 8],