elk_led_controller/lib.rs
1/*!
2 # elk-led-controller library
3
4 A Rust library for controlling ELK-BLEDOM and similar Bluetooth LED strips.
5 Supports multiple device types including ELK-BLE, LEDBLE, MELK, ELK-BULB, and ELK-LAMPL.
6
7 ## Features
8
9 * Power on/off control
10 * RGB color control
11 * Color temperature control
12 * Brightness adjustment
13 * Effect modes (fade, jump, blink)
14 * Effect speed control
15 * Scheduling
16 * Audio monitoring and visualization
17
18 ## Example
19
20 ```rust
21 use elk_led_controller::*;
22
23 #[tokio::main]
24 async fn main() -> Result<(), Error> {
25 // Initialize tracing for logs
26 tracing_subscriber::fmt::init();
27
28 // Initialize error handling
29 color_eyre::install()?;
30
31 // Initialize and connect to the device
32 let mut device = BleLedDevice::new_without_power().await?;
33
34 // Basic operations
35 device.power_on().await?;
36 device.set_color(255, 0, 0).await?; // Set to red
37 device.set_brightness(80).await?; // 80% brightness
38
39 Ok(())
40 }
41 ```
42*/
43
44use thiserror::Error;
45
46/// Custom error types for the ELK LED controller library
47#[derive(Error, Debug)]
48pub enum Error {
49 /// No Bluetooth adapters found
50 #[error("No Bluetooth adapters found")]
51 NoBluetoothAdapters,
52
53 /// No compatible LED device found
54 #[error("No compatible LED device found")]
55 NoCompatibleDevice,
56
57 /// Failed to find required BLE characteristic
58 #[error("Could not find required BLE characteristic: {0}")]
59 CharacteristicNotFound(String),
60
61 /// BLE communication error
62 #[error("BLE communication error: {0}")]
63 BleError(String),
64
65 /// Command timeout
66 #[error("Command timed out after {0} retries")]
67 CommandTimeout(u8),
68
69 /// Value out of range
70 #[error("Value {0} out of range ({1}..{2})")]
71 ValueOutOfRange(u32, u32, u32),
72
73 /// General error
74 #[error("Error: {0}")]
75 General(String),
76
77 /// Error from btleplug
78 #[error(transparent)]
79 BtlePlugError(#[from] btleplug::Error),
80
81 /// Audio capture error
82 #[error("Audio capture error: {0}")]
83 AudioCaptureError(String),
84
85 /// CPAL Stream build error
86 #[error("Audio stream build error: {0}")]
87 StreamBuildError(String),
88
89 /// CPAL Stream play error
90 #[error("Audio stream play error: {0}")]
91 StreamPlayError(String),
92
93 /// Other errors
94 #[error(transparent)]
95 Other(#[from] Box<dyn std::error::Error + Send + Sync>),
96}
97
98// Import needed for Result type extension
99pub type Result<T> = std::result::Result<T, Error>;
100
101// Re-export modules
102pub mod audio;
103pub mod device;
104pub mod effects;
105pub mod schedule;
106
107// Re-export key types
108pub use audio::{AudioMonitor, AudioVisualization, FrequencyRange, VisualizationMode};
109pub use device::{BleLedDevice, Days, DeviceConfig, DeviceType, Effects, EFFECTS, WEEK_DAYS};