#![allow(clippy::uninlined_format_args)]
use pokeys_lib::*;
fn main() -> Result<()> {
println!("๐ง Simple I2C Test (Network Connection)");
println!("=======================================");
let device_serial = 32223;
let timeout_ms = 3000;
println!("๐ Connecting to device {} over network...", device_serial);
let mut device = match connect_to_device_with_serial(device_serial, true, timeout_ms) {
Ok(device) => {
println!("โ
Successfully connected to device {}", device_serial);
device
}
Err(e) => {
println!("โ Failed to connect to device {}: {}", device_serial, e);
println!("๐ก Make sure:");
println!(
" - Device {} is powered on and connected to network",
device_serial
);
println!(" - Device is accessible from this computer");
println!(" - Serial number {} is correct", device_serial);
return Ok(());
}
};
device.get_device_data()?;
let device_name = String::from_utf8_lossy(&device.device_data.device_name);
println!(
"๐ Connected to: {} (Serial: {})",
device_name.trim_end_matches('\0'),
device.device_data.serial_number
);
println!("\n๐ Initializing I2C...");
match device.i2c_init() {
Ok(()) => println!("โ
I2C initialized successfully"),
Err(e) => {
println!("โ I2C initialization failed: {}", e);
return Ok(());
}
}
let test_address = 0x50; println!("\n๐ก Testing with EEPROM address 0x50");
println!(" Connect an I2C EEPROM (like 24LC256) to see full functionality");
test_basic_operations(&mut device, test_address)?;
println!("\n๐ Simple I2C Test Complete!");
println!("๐ก To test with different devices:");
println!(" - 0x50-0x57: EEPROM devices");
println!(" - 0x68: DS1307 Real-Time Clock");
println!(" - 0x48-0x4F: Temperature sensors");
Ok(())
}
fn test_basic_operations(device: &mut PoKeysDevice, address: u8) -> Result<()> {
println!(
"\n๐งช Testing basic operations with device at 0x{:02X}",
address
);
let test_data = vec![0x00, 0x01, 0x02];
println!("๐ค Writing test data: {:02X?}", test_data);
match device.i2c_write(address, &test_data) {
Ok(I2cStatus::Ok) => println!("โ
Write successful"),
Ok(status) => println!("โ ๏ธ Write status: {:?} (device may not be present)", status),
Err(e) => println!("โ Write failed: {} (device may not be present)", e),
}
std::thread::sleep(std::time::Duration::from_millis(10));
println!("๐ฅ Reading 3 bytes...");
match device.i2c_read(address, 3) {
Ok((I2cStatus::Ok, data)) => {
println!("โ
Read successful: {:02X?}", data);
}
Ok((status, data)) => {
println!(
"โ ๏ธ Read status: {:?}, data: {:02X?} (device may not be present)",
status, data
);
}
Err(e) => println!("โ Read failed: {} (device may not be present)", e),
}
if (0x50..=0x57).contains(&address) {
println!("๐ Testing EEPROM register operations...");
test_eeprom_operations(device, address)?;
}
Ok(())
}
fn test_eeprom_operations(device: &mut PoKeysDevice, address: u8) -> Result<()> {
let register = 0x00;
let test_data = b"Hi!";
println!(
"๐ Writing '{}' to EEPROM address 0x{:04X}",
String::from_utf8_lossy(test_data),
register
);
let mut write_data = Vec::new();
write_data.push(register);
write_data.extend_from_slice(test_data);
match device.i2c_write(address, &write_data) {
Ok(I2cStatus::Ok) => {
println!("โ
EEPROM write successful");
std::thread::sleep(std::time::Duration::from_millis(10));
match device.i2c_write(address, &[register]) {
Ok(I2cStatus::Ok) => {
match device.i2c_read(address, test_data.len() as u8) {
Ok((I2cStatus::Ok, data)) => {
let read_str = String::from_utf8_lossy(&data);
println!("๐ EEPROM read: '{}'", read_str);
if data == test_data {
println!("โ
EEPROM test PASSED!");
} else {
println!(
"โ ๏ธ Data mismatch - wrote: {:02X?}, read: {:02X?}",
test_data, data
);
println!(
" This could be normal if EEPROM is write-protected or different data was already stored"
);
}
}
Ok((status, data)) => {
println!("โ ๏ธ EEPROM read status: {:?}, data: {:02X?}", status, data)
}
Err(e) => println!("โ EEPROM read failed: {}", e),
}
}
Ok(status) => println!("โ ๏ธ EEPROM address set status: {:?}", status),
Err(e) => println!("โ EEPROM address set failed: {}", e),
}
}
Ok(status) => println!(
"โ ๏ธ EEPROM write status: {:?} (device may not be present)",
status
),
Err(e) => println!("โ EEPROM write failed: {} (device may not be present)", e),
}
Ok(())
}