pokeys_thread/
lib.rs

1#![allow(clippy::uninlined_format_args)]
2#![doc(test(attr(ignore)))]
3
4//! # PoKeys Threading Architecture
5//!
6//! This crate provides a threading architecture for PoKeys devices,
7//! allowing each device to operate in its own thread.
8//!
9//! ## Overview
10//!
11//! The PoKeys threading architecture is designed to solve the problem of
12//! communicating with multiple PoKeys devices from a single application.
13//! Each device operates in its own thread, allowing for concurrent
14//! communication and state updates without blocking the main application thread.
15//!
16//! ## Key Components
17//!
18//! - **ThreadController**: Manages device threads and provides a high-level interface
19//!   for device operations.
20//! - **DeviceWorker**: Runs in its own thread and handles device communication.
21//! - **SharedDeviceState**: Provides thread-safe access to device state.
22//! - **StateObserver**: Allows monitoring state changes.
23//! - **DeviceOperations**: Provides a high-level interface for device operations.
24//! - **DeviceSync**: Handles data synchronization between device and shared state.
25//! - **Logger**: Provides configurable logging for threads and controllers.
26//!
27//! ## Usage Example
28//!
29//! ```ignore
30//! use pokeys_thread::{ThreadControllerBuilder, ThreadController, DeviceOperations};
31//! use std::time::Duration;
32//!
33//! // Create a thread controller
34//! let mut controller = ThreadControllerBuilder::new()
35//!     .default_refresh_interval(100)
36//!     .build();
37//!
38//! // Discover USB devices
39//! let devices = controller.discover_usb_devices().unwrap();
40//!
41//! if !devices.is_empty() {
42//!     // Start a thread for the first device
43//!     let thread_id = controller.start_usb_device_thread(devices[0]).unwrap();
44//!
45//!     // Perform device operations
46//!     controller.set_digital_output(thread_id, 1, true).unwrap();
47//!
48//!     // Wait a bit
49//!     std::thread::sleep(Duration::from_millis(100));
50//!
51//!     // Read input
52//!     let input_value = controller.get_digital_input(thread_id, 2).unwrap();
53//!     println!("Digital input 2: {}", input_value);
54//! }
55//! ```
56//!
57//! ## Features
58//!
59//! - Thread-safe communication between main thread and device threads
60//! - Device state sharing with thread-safe access
61//! - Command pattern for thread control
62//! - Observer pattern for state change notifications
63//! - Comprehensive error handling
64//! - Configurable logging system
65//! - Support for USB and network devices
66
67pub mod builder;
68pub mod commands;
69pub mod controller;
70pub mod controller_builder;
71pub mod error;
72pub mod logging;
73pub mod observer;
74pub mod operations;
75pub mod state;
76pub mod sync;
77pub mod worker;
78
79#[cfg(test)]
80mod tests;
81
82// Re-export main types
83pub use builder::ThreadWorkerBuilder;
84pub use commands::DeviceCommand;
85pub use controller::{ThreadController, ThreadControllerImpl};
86pub use controller_builder::ThreadControllerBuilder;
87pub use error::{Result, ThreadError};
88pub use logging::{Logger, SimpleLogger, ThreadLogger};
89pub use observer::StateObserver;
90pub use operations::DeviceOperations;
91pub use state::{DeviceState, SharedDeviceState, StateChangeType, ThreadStatus};
92pub use sync::DeviceSync;
93pub use worker::{DeviceWorker, DeviceWorkerImpl};