#![allow(clippy::uninlined_format_args)]
#![allow(clippy::needless_range_loop)]
use pokeys_lib::*;
use std::thread;
use std::time::Duration;
fn main() -> Result<()> {
println!("đš PoKeys Matrix Keyboard Example");
println!("=================================");
let mut device = match connect_to_first_available_device() {
Ok(device) => device,
Err(e) => {
println!("â No PoKeys device found: {e}");
println!("đĄ Please connect a PoKeys device to run this example");
return Ok(());
}
};
println!(
"â
Connected to device (Serial: {})",
device.device_data.serial_number
);
let width = 4;
let height = 4;
let column_pins = [5, 6, 7, 8];
let row_pins = [1, 2, 3, 4];
println!("\nđ§ Configuring {width}x{height} matrix keyboard...");
println!(" Column pins: {:?}", &column_pins[..width as usize]);
println!(" Row pins: {:?}", &row_pins[..height as usize]);
device.configure_matrix_keyboard(
width,
height,
&column_pins[..width as usize],
&row_pins[..height as usize],
)?;
println!("â
Matrix keyboard configured successfully!");
display_keyboard_layout(width, height);
println!("\nđ Monitoring keyboard (press Ctrl+C to exit)...");
println!("Press keys on the matrix keyboard to see their state changes.");
let mut previous_states = vec![vec![false; width as usize]; height as usize];
let mut key_press_count = 0;
loop {
device.read_matrix_keyboard()?;
let mut state_changed = false;
for (row, row_states) in previous_states.iter_mut().enumerate().take(height as usize) {
for col in 0..width as usize {
let current_state = device.matrix_keyboard.get_key_state(row, col);
let previous_state = row_states[col];
if current_state != previous_state {
state_changed = true;
if current_state {
key_press_count += 1;
println!("đ´ Key PRESSED at ({row}, {col}) - Key #{key_press_count}");
} else {
println!("đĩ Key RELEASED at ({row}, {col})");
}
row_states[col] = current_state;
}
}
}
if state_changed {
display_current_state(&device, width, height);
}
thread::sleep(Duration::from_millis(50));
}
}
fn connect_to_first_available_device() -> Result<PoKeysDevice> {
println!("đ Searching for network devices...");
match enumerate_network_devices(4000) {
Ok(devices) if !devices.is_empty() => {
println!("â
Found {} network device(s)", devices.len());
return connect_to_network_device(&devices[0]);
}
Ok(_) => println!("âšī¸ No network devices found"),
Err(e) => println!("â ī¸ Network enumeration failed: {e}"),
}
println!("đ Searching for USB devices...");
match enumerate_usb_devices() {
Ok(count) if count > 0 => {
println!("â
Found {count} USB device(s)");
connect_to_device(0)
}
Ok(_) => Err(PoKeysError::DeviceNotFound),
Err(e) => Err(e),
}
}
fn display_keyboard_layout(width: u8, height: u8) {
println!("\nđ Keyboard Layout ({width}x{height}):");
println!(" âââââââŦââââââŦââââââŦââââââ");
for row in 0..height {
print!(" â");
for col in 0..width {
print!(" {row:>2},{col} â");
}
println!();
if row < height - 1 {
println!(" âââââââŧââââââŧââââââŧââââââ¤");
}
}
println!(" âââââââ´ââââââ´ââââââ´ââââââ");
println!(" Format: (row,col) - e.g., (0,0) is top-left");
}
fn display_current_state(device: &PoKeysDevice, width: u8, height: u8) {
println!("\nđ Current Keyboard State:");
println!(" âââââââŦââââââŦââââââŦââââââ");
for row in 0..height {
print!(" â");
for col in 0..width {
let pressed = device
.matrix_keyboard
.get_key_state(row as usize, col as usize);
let symbol = if pressed { " âââ " } else { " " };
print!("{symbol}â");
}
println!();
if row < height - 1 {
println!(" âââââââŧââââââŧââââââŧââââââ¤");
}
}
println!(" âââââââ´ââââââ´ââââââ´ââââââ");
println!(" âââ = pressed, blank = released");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_example_functions() {
display_keyboard_layout(4, 4);
}
}