Skip to content

Commit 1cb1562

Browse files
committed
feat: add support for brightness control
1 parent 4422284 commit 1cb1562

File tree

6 files changed

+49
-18
lines changed

6 files changed

+49
-18
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "badgemagic"
3-
version = "0.1.0"
4-
authors = ["Martin Michaelis <[email protected]>"]
3+
version = "0.2.0"
4+
authors = ["Martin Michaelis <[email protected]>", "Valentin Weber <[email protected]>"]
55
edition = "2021"
66
description = "Badge Magic with LEDs - Library and CLI"
77
homepage = "https://badgemagic.fossasia.org"

LICENSE-MIT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Copyright 2024 Martin Michaelis
2+
Copyright 2025 Valentin Weber
23

34
Permission is hereby granted, free of charge, to any person obtaining a copy
45
of this software and associated documentation files (the “Software”), to deal

examples/hello-world.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use badgemagic::{
55
embedded_graphics::{
66
geometry::Point, mono_font::MonoTextStyle, pixelcolor::BinaryColor, text::Text,
77
},
8-
protocol::{Mode, PayloadBuffer, Style},
8+
protocol::{Brightness, Mode, PayloadBuffer, Style},
99
usb_hid::Device,
1010
util::DrawableLayoutExt,
1111
};
1212

1313
fn main() -> Result<()> {
14-
let mut payload = PayloadBuffer::new();
14+
let mut payload = PayloadBuffer::new(Brightness::default());
1515

1616
payload.add_message_drawable(
1717
Style::default().mode(Mode::Center),

src/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{fs, path::PathBuf};
55
use anyhow::{Context, Result};
66
use badgemagic::{
77
ble::Device as BleDevice,
8-
protocol::{Mode, PayloadBuffer, Speed, Style},
8+
protocol::{Brightness, Mode, PayloadBuffer, Speed, Style},
99
usb_hid::Device as UsbDevice,
1010
};
1111
use base64::Engine;
@@ -43,6 +43,10 @@ struct Args {
4343
#[clap(long)]
4444
transport: TransportProtocol,
4545

46+
/// Brightness of the panel
47+
#[clap(long)]
48+
brightness: Option<Brightness>,
49+
4650
/// List all devices visible to a transport and exit
4751
#[clap(long)]
4852
list_devices: bool,
@@ -146,7 +150,7 @@ fn gnerate_payload(args: &mut Args) -> Result<PayloadBuffer> {
146150
}
147151
};
148152

149-
let mut payload = PayloadBuffer::new();
153+
let mut payload = PayloadBuffer::new(args.brightness.unwrap_or_default());
150154

151155
for message in config.messages {
152156
let mut style = Style::default();
@@ -164,6 +168,7 @@ fn gnerate_payload(args: &mut Args) -> Result<PayloadBuffer> {
164168
Point::new(0, 7),
165169
MonoTextStyle::new(&FONT_6X9, BinaryColor::On),
166170
);
171+
167172
payload.add_message_drawable(style, &text);
168173
}
169174
Content::Bitstring { bitstring } => {

src/protocol.rs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Protocol used to update the badge
22
3-
use std::num::TryFromIntError;
4-
53
#[cfg(feature = "embedded-graphics")]
64
use embedded_graphics::{
75
draw_target::DrawTarget,
@@ -11,6 +9,7 @@ use embedded_graphics::{
119
primitives::Rectangle,
1210
Drawable,
1311
};
12+
use std::num::TryFromIntError;
1413
use time::OffsetDateTime;
1514
use zerocopy::{BigEndian, FromBytes, Immutable, IntoBytes, KnownLayout, U16};
1615

@@ -54,7 +53,7 @@ impl Style {
5453
self
5554
}
5655

57-
/// Show a dotted border arround the display.
56+
/// Show a dotted border around the display.
5857
/// ```
5958
/// use badgemagic::protocol::Style;
6059
/// # (
@@ -161,7 +160,7 @@ impl TryFrom<u8> for Speed {
161160
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
162161
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
163162
pub enum Mode {
164-
/// Scroll thorugh the message from left to right
163+
/// Scroll through the message from left to right
165164
#[default]
166165
Left,
167166

@@ -193,14 +192,39 @@ pub enum Mode {
193192
Laser,
194193
}
195194

195+
/// Display Brightness
196+
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
197+
#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
198+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
199+
#[cfg_attr(feature = "serde", serde(rename_all = "PascalCase"))]
200+
pub enum Brightness {
201+
#[default]
202+
Full,
203+
ThreeQuarters,
204+
Half,
205+
OneQuarter,
206+
}
207+
208+
impl From<Brightness> for u8 {
209+
fn from(value: Brightness) -> Self {
210+
match value {
211+
Brightness::Full => 0x00,
212+
Brightness::ThreeQuarters => 0x10,
213+
Brightness::Half => 0x20,
214+
Brightness::OneQuarter => 0x30,
215+
}
216+
}
217+
}
218+
196219
const MSG_PADDING_ALIGN: usize = 64;
197220

198-
const MAGIC: [u8; 6] = *b"wang\0\0";
221+
const MAGIC: [u8; 5] = *b"wang\0";
199222

200223
#[derive(FromBytes, IntoBytes, Immutable, KnownLayout)]
201224
#[repr(C)]
202225
struct Header {
203-
magic: [u8; 6],
226+
magic: [u8; 5],
227+
brightness: u8,
204228
blink: u8,
205229
border: u8,
206230
speed_and_mode: [u8; 8],
@@ -241,7 +265,7 @@ impl Timestamp {
241265

242266
/// Buffer to create a payload
243267
///
244-
/// A payload consits of up to 8 messages
268+
/// A payload consists of up to 8 messages
245269
/// ```
246270
/// # #[cfg(feature = "embedded-graphics")]
247271
/// # fn main() {
@@ -252,7 +276,7 @@ impl Timestamp {
252276
/// primitives::{PrimitiveStyle, Rectangle, Styled},
253277
/// };
254278
///
255-
/// let mut buffer = PayloadBuffer::new();
279+
/// let mut buffer = PayloadBuffer::default();
256280
/// buffer.add_message_drawable(
257281
/// Style::default(),
258282
/// &Styled::new(
@@ -271,18 +295,19 @@ pub struct PayloadBuffer {
271295

272296
impl Default for PayloadBuffer {
273297
fn default() -> Self {
274-
Self::new()
298+
Self::new(Brightness::Full)
275299
}
276300
}
277301

278302
impl PayloadBuffer {
279303
/// Create a new empty buffer
280304
#[must_use]
281-
pub fn new() -> Self {
305+
pub fn new(brightness: Brightness) -> Self {
282306
Self {
283307
num_messages: 0,
284308
data: Header {
285309
magic: MAGIC,
310+
brightness: brightness.into(),
286311
blink: 0,
287312
border: 0,
288313
speed_and_mode: [0; 8],
@@ -368,7 +393,7 @@ impl PayloadBuffer {
368393
&self.data
369394
}
370395

371-
/// Convert the payload buffe into bytes (with padding)
396+
/// Convert the payload buffer into bytes (with padding)
372397
#[allow(clippy::missing_panics_doc)] // should never panic
373398
#[must_use]
374399
pub fn into_padded_bytes(self) -> impl AsRef<[u8]> {

0 commit comments

Comments
 (0)