Add conditional debug printing

This commit is contained in:
Lennard Kittner
2025-11-15 17:02:42 +01:00
parent 529a76560d
commit 39ddba7fdb
5 changed files with 45 additions and 15 deletions

View File

@@ -1,4 +1,7 @@
use crate::devices::{ChargingStatus, Device, DeviceError, DeviceEvent, DeviceState};
use crate::{
debug_println,
devices::{ChargingStatus, Device, DeviceError, DeviceEvent, DeviceState},
};
use std::time::Duration;
const HYPERX: u16 = 0x0951;
@@ -167,6 +170,7 @@ impl Device for CloudIIWireless {
}
fn get_event_from_device_response(&self, response: &[u8]) -> Option<Vec<DeviceEvent>> {
debug_println!("Read packet: {:?}", response);
if response.len() < 7 {
return None;
}
@@ -181,7 +185,7 @@ impl Device for CloudIIWireless {
let status = response[4];
let connected = status == 1 || status == 4;
if status == 2 {
println!("Pairing mode");
debug_println!("Pairing mode");
}
Some(vec![DeviceEvent::WirelessConnected(connected)])
}
@@ -199,9 +203,12 @@ impl Device for CloudIIWireless {
Some(vec![DeviceEvent::Muted(muted)])
}
FIRMWARE_VERSION_RESPONSE_ID => {
println!(
debug_println!(
"Firmware version: {}.{}.{}.{}",
response[4], response[5], response[6], response[7]
response[4],
response[5],
response[6],
response[7]
);
None
}
@@ -220,7 +227,7 @@ impl Device for CloudIIWireless {
4 => {
// Command 4: Charge limit or battery management
// This may be sent asynchronously when charging state changes
println!(
debug_println!(
"Charge limit/battery management response (cmd 4): data={:?}",
&response[4..8]
);
@@ -228,11 +235,11 @@ impl Device for CloudIIWireless {
}
9 | 29 => {
// Commands 9 and 29 are seen during initialization but purpose unclear
println!("Initialization response (cmd {})", response[3]);
debug_println!("Initialization response (cmd {})", response[3]);
None
}
_ => {
println!("Unknown command response: cmd_id={}", response[3]);
debug_println!("Unknown command response: cmd_id={}", response[3]);
None
}
}
@@ -244,7 +251,7 @@ impl Device for CloudIIWireless {
Some(vec![DeviceEvent::SurroundSound(surround_enabled)])
}
_ => {
println!("Unknown response format: report_id={}", response[0]);
debug_println!("Unknown response format: report_id={}", response[0]);
None
}
}

View File

@@ -1,4 +1,7 @@
use crate::devices::{ChargingStatus, Color, Device, DeviceError, DeviceEvent, DeviceState};
use crate::{
debug_println,
devices::{ChargingStatus, Color, Device, DeviceError, DeviceEvent, DeviceState},
};
use std::time::Duration;
const HP: u16 = 0x03F0;
@@ -184,6 +187,7 @@ impl Device for CloudIIWirelessDTS {
}
fn get_event_from_device_response(&self, response: &[u8]) -> Option<Vec<DeviceEvent>> {
debug_println!("Read packet: {:?}", response);
if response.len() < 7 {
return None;
}
@@ -225,7 +229,7 @@ impl Device for CloudIIWirelessDTS {
Some(vec![DeviceEvent::ProductColor(Color::from(status))])
}
_ => {
println!("Unknown device event: {:?}", response);
debug_println!("Unknown device event: {:?}", response);
None
}
}

View File

@@ -1,4 +1,7 @@
use crate::devices::{ChargingStatus, Color, Device, DeviceError, DeviceEvent, DeviceState};
use crate::{
debug_println,
devices::{ChargingStatus, Color, Device, DeviceError, DeviceEvent, DeviceState},
};
use std::{time::Duration, vec};
const HP: u16 = 0x03F0;
@@ -177,6 +180,7 @@ impl Device for CloudIIIWireless {
}
fn get_event_from_device_response(&self, response: &[u8]) -> Option<Vec<DeviceEvent>> {
debug_println!("Read packet: {response:?}");
if response[0] != 102 {
return None;
}
@@ -218,7 +222,10 @@ impl Device for CloudIIIWireless {
}
Some(vec![DeviceEvent::RequireSIRKReset(flag)])
}
_ => None,
_ => {
debug_println!("Unknown device event: {:?}", response);
None
}
}
}

View File

@@ -2,9 +2,12 @@ pub mod cloud_ii_wireless;
pub mod cloud_ii_wireless_dts;
pub mod cloud_iii_wireless;
use crate::devices::{
cloud_ii_wireless::CloudIIWireless, cloud_ii_wireless_dts::CloudIIWirelessDTS,
cloud_iii_wireless::CloudIIIWireless,
use crate::{
debug_println,
devices::{
cloud_ii_wireless::CloudIIWireless, cloud_ii_wireless_dts::CloudIIWirelessDTS,
cloud_iii_wireless::CloudIIIWireless,
},
};
use hidapi::{HidApi, HidDevice, HidError};
use std::{fmt::Display, time::Duration};
@@ -506,6 +509,7 @@ pub trait Device {
let mut responded = false;
for packet in packets.into_iter().flatten() {
self.prepare_write();
debug_println!("Write packet: {packet:?}");
self.get_device_state().hid_device.write(&packet)?;
std::thread::sleep(RESPONSE_DELAY);
if let Some(events) = self.wait_for_updates(Duration::from_secs(1)) {

View File

@@ -1 +1,9 @@
pub mod devices;
#[macro_export]
macro_rules! debug_println {
($($args:tt)*) => {
#[cfg(debug_assertions)]
println!($($args)*);
};
}