| Crates.io | pokeys-thread |
| lib.rs | pokeys-thread |
| version | 0.5.1 |
| created_at | 2025-09-25 07:27:20.485101+00 |
| updated_at | 2025-09-25 07:43:58.127116+00 |
| description | Advanced threading architecture for multi-device PoKeys applications |
| homepage | |
| repository | https://github.com/pokeys-toolkit/thread |
| max_upload_size | |
| id | 1854300 |
| size | 434,315 |
Advanced threading architecture for multi-device PoKeys applications. This library provides thread-safe multi-device management with per-device threads, state synchronization, and concurrent operations.
Add this to your Cargo.toml:
[dependencies]
pokeys-thread = { git = "https://github.com/pokeys-toolkit/thread" }
pokeys-lib = { git = "https://github.com/pokeys-toolkit/core" }
use pokeys_thread::*;
fn main() -> Result<()> {
// Create thread controller
let mut controller = ThreadControllerBuilder::new()
.default_refresh_interval(100)
.build();
// Start device threads
let devices = controller.discover_usb_devices()?;
for device_id in devices {
controller.start_usb_device_thread(device_id)?;
}
// Control devices concurrently
// Each device operates in its own thread
Ok(())
}
use pokeys_thread::*;
fn main() -> Result<()> {
let mut controller = ThreadControllerBuilder::new().build();
// Start device thread
controller.start_usb_device_thread(12345678)?;
// Observe state changes
let observer = controller.create_state_observer(12345678)?;
loop {
if let Some(state) = observer.get_latest_state() {
println!("Device state updated: {:?}", state);
}
std::thread::sleep(std::time::Duration::from_millis(100));
}
}
use pokeys_thread::*;
fn main() -> Result<()> {
let mut controller = ThreadControllerBuilder::new().build();
controller.start_usb_device_thread(12345678)?;
// Non-blocking device operations
controller.set_digital_output(12345678, 1, true)?;
controller.set_pwm_duty_cycle(12345678, 0, 50.0)?;
// Read device state
let analog_value = controller.read_analog_input(12345678, 1)?;
println!("Analog input 1: {}", analog_value);
Ok(())
}
The ThreadController is the main entry point for managing device threads:
Each device runs in its own dedicated thread:
Thread-safe state management:
let controller = ThreadControllerBuilder::new()
.default_refresh_interval(50) // 50ms refresh rate
.max_retry_attempts(3) // Retry failed operations
.timeout_duration(5000) // 5 second timeout
.enable_logging(true) // Enable debug logging
.build();
controller.configure_device_thread(device_id, |config| {
config
.refresh_interval(25) // 25ms for high-speed device
.priority_mode(true) // High priority thread
.buffer_size(1024) // Larger communication buffer
});
The examples/ directory contains comprehensive examples:
# Simple controller setup
cargo run --example simple_controller
# State observation patterns
cargo run --example state_observer
# Device operation examples
cargo run --example device_operations
# Logging and debugging
cargo run --example logging_example
# Comprehensive multi-device example
cargo run --example comprehensive_example
Run the test suite:
# Unit tests
cargo test
# Integration tests (requires hardware)
cargo test --features hardware-tests
# Performance benchmarks
cargo test --release --features benchmarks
We welcome contributions! Please ensure:
This project is licensed under the LGPL-2.1 License - see the LICENSE file for details.
Perfect for: Industrial automation, multi-device control systems, responsive GUI applications, and any scenario requiring concurrent PoKeys device management.