| Crates.io | pokeys-lib |
| lib.rs | pokeys-lib |
| version | 0.19.1 |
| created_at | 2025-08-18 07:36:11.897861+00 |
| updated_at | 2025-09-25 07:38:55.9759+00 |
| description | Pure Rust core library for PoKeys device control - USB/Network connectivity, I/O, PWM, encoders, SPI/I2C protocols |
| homepage | |
| repository | https://github.com/pokeys-toolkit/core |
| max_upload_size | |
| id | 1800050 |
| size | 1,552,914 |
A pure Rust implementation of the PoKeysLib for controlling PoKeys devices. This is the core library that provides all fundamental device communication and control functionality for the PoKeys ecosystem.
PWM Control: 6 hardware PWM channels (pins 17-22) with 25MHz clock precision for servo control
Pulse Engine v2: Stepper motor control with advanced pulse generation
Encoder Support: Quadrature encoder reading with 4x/2x sampling modes, position and velocity tracking
Matrix Operations: Matrix keyboard scanning and LED matrix control
Matrix Keyboard: 4x4 to 16x8 matrix keyboard support with real-time key detection
Add this to your Cargo.toml:
[dependencies]
pokeys-lib = "0.3.0"
Or install from git:
[dependencies]
pokeys-lib = { git = "https://github.com/pokeys-toolkit/core" }
use pokeys_lib::*;
fn main() -> Result<()> {
// Enumerate and connect to first device
let device_count = enumerate_usb_devices()?;
if device_count > 0 {
let mut device = connect_to_device(0)?;
// Configure pin 1 as digital output
device.set_pin_function(1, PinFunction::DigitalOutput)?;
// Turn on pin 1
device.set_digital_output(1, true)?;
println!("Pin 1 is now HIGH");
}
Ok(())
}
use pokeys_lib::*;
fn main() -> Result<()> {
let mut device = connect_to_device(0)?;
// Configure encoder on pins 10-11
let options = EncoderOptions::with_4x_sampling();
device.configure_encoder(0, 10, 11, options)?;
loop {
let position = device.get_encoder_value(0)?;
println!("Encoder position: {}", position);
std::thread::sleep(std::time::Duration::from_millis(100));
}
}
use pokeys_lib::*;
fn main() -> Result<()> {
let mut device = connect_to_device(0)?;
// Configure PWM for servo control on pin 22 (PWM1)
// PoKeys PWM operates at 25MHz clock frequency
device.set_pwm_period(500000)?; // 20ms period (0.020 × 25,000,000)
device.enable_pwm_for_pin(22, true)?;
// Servo positions in clock cycles
let positions = [
(60000, "0°"), // Custom calibrated 0° position
(36000, "90°"), // Custom calibrated 90° position
(12000, "180°"), // Custom calibrated 180° position
];
for (duty_cycles, angle) in positions.iter() {
println!("Moving to {}", angle);
device.set_pwm_duty_cycle_for_pin(22, *duty_cycles)?;
std::thread::sleep(std::time::Duration::from_secs(1));
}
// Disable PWM
device.enable_pwm_for_pin(22, false)?;
Ok(())
}
use pokeys_lib::*;
fn main() -> Result<()> {
let mut device = connect_to_device(0)?;
// 180-degree position servo
let servo_180 = ServoConfig::one_eighty(22, 60000, 12000);
device.configure_servo(servo_180.clone())?;
device.set_servo_angle(&servo_180, 90.0)?; // Move to 90°
// 360-degree speed servo (continuous rotation)
// Datasheet: Counterclockwise 1-1.5ms, Stop 1.5ms, Clockwise 1.5-2ms
let servo_speed = ServoConfig::three_sixty_speed(21, 37500, 50000, 25000);
device.configure_servo(servo_speed.clone())?;
device.set_servo_speed(&servo_speed, 50.0)?; // 50% clockwise
device.stop_servo(&servo_speed)?; // Stop rotation
Ok(())
}
use pokeys_lib::*;
fn main() -> Result<()> {
let mut device = connect_to_device(0)?;
// Configure 4x4 matrix keyboard
let column_pins = [5, 6, 7, 8]; // Column pins
let row_pins = [1, 2, 3, 4]; // Row pins
device.configure_matrix_keyboard(4, 4, &column_pins, &row_pins)?;
// Monitor for key presses
let mut previous_states = vec![vec![false; 4]; 4];
loop {
device.read_matrix_keyboard()?;
for row in 0..4 {
for col in 0..4 {
let current_state = device.matrix_keyboard.get_key_state(row, col);
if current_state != previous_states[row][col] {
if current_state {
println!("Key PRESSED at ({}, {})", row, col);
}
previous_states[row][col] = current_state;
}
}
}
std::thread::sleep(std::time::Duration::from_millis(50));
}
}
use pokeys_lib::*;
fn main() -> Result<()> {
let mut device = connect_to_device(0)?;
// Configure I2C with enhanced features
device.i2c_init()?;
// Configure retry behavior
let retry_config = RetryConfig {
max_attempts: 3,
base_delay_ms: 100,
backoff_multiplier: 2.0,
jitter: true,
..Default::default()
};
// Write large data with automatic fragmentation
let large_data = vec![0x42; 100]; // 100 bytes
device.i2c_write_fragmented(0x50, &large_data)?;
// Write with intelligent retry on failure
let data = vec![0x01, 0x02, 0x03];
device.i2c_write_with_retry(0x50, &data, &retry_config)?;
// Monitor device health
let health = device.health_check();
println!("I2C Success Rate: {:.1}%", health.performance.success_rate * 100.0);
Ok(())
}
The examples/ directory contains focused examples demonstrating core library features:
# Basic device operations and configuration
cargo run --example physical_device_config_example
cargo run --example config_loader_example
cargo run --example step_by_step_config
# Communication protocols
cargo run --example spi_example
cargo run --example i2c_simple_test
cargo run --example i2c_comprehensive_test
cargo run --example i2c_common_devices
cargo run --example i2c_enhanced_features # NEW: Enhanced I2C features
# Matrix keyboard support
cargo run --example matrix_keyboard_simple # NEW: Simple matrix keyboard
cargo run --example keyboard_matrix_example # NEW: Full matrix keyboard
# Network device support
cargo run --example network_device_test
This core library provides the foundation for the PoKeys ecosystem:
We welcome contributions! Please ensure all tests pass:
cargo test
cargo test --features hardware-tests # Requires actual hardware
This project is licensed under the LGPL-2.1 License - see the LICENSE file for details.
Note: This is a pure Rust implementation and does not require the original PoKeysLib C library. All functionality is implemented natively in Rust for better performance, safety, and cross-platform compatibility.