Add GenericPrinter::set_fan_speed() across all five printer kinds

Same override pattern as connect(): required, no default body, five
completely different bodies.

- BambuGenericPrinter::set_fan_speed_impl() is shared by BambuV1Printer and
  BambuV2Printer (composition reuse, same as connect_impl) - both speak
  the same MQTT gcode-injection mechanism.
- PrusaLinkPrinter and PrusaSerialPrinter both send the same M106 gcode
  but over different transports (HTTP command injection vs. raw serial
  bytes) - they share the percent->PWM conversion despite having unrelated
  connect() implementations.
- KlipperPrinter uses a hypothetical Moonraker-native endpoint that takes
  a percentage directly - no PWM conversion at all, since that math only
  applies to the gcode-speaking vendors.

percent_to_pwm() lives in mod.rs as a private free function rather than a
trait default: it's genuine shared logic, but only for the subset of
vendors that need it, which is exactly the case a trait default can't
express cleanly. Visible to bambu.rs/prusa.rs via super:: because Rust's
module privacy reaches into child modules.

Verified with cargo check --all-targets and a full run of
printer_polymorphism: 50% converges on the same PWM value (127) across all
three gcode-based printers, Klipper's native path takes 0.50 directly.
This commit is contained in:
2026-08-28 21:40:11 +00:00
parent aa471ff906
commit 6e18215af5
5 changed files with 80 additions and 6 deletions
+19
View File
@@ -252,6 +252,17 @@ impl BambuGenericPrinter {
}
}
/// Shared by both `BambuV1Printer` and `BambuV2Printer` — same MQTT
/// gcode-injection mechanism regardless of protocol generation. Real
/// version would publish
/// `{"print":{"command":"gcode_line","param":"M106 P1 S<pwm>"}}` to
/// `device/{sn}/request` over the MQTTS session.
async fn set_fan_speed_impl(&mut self, percent: u8) -> Result<(), PrinterError> {
let pwm = super::percent_to_pwm(percent);
println!("[{}] sending gcode: M106 P1 S{pwm}", self.base.name);
Ok(())
}
/// Shared "connect" logic both V1 and V2 delegate to — the real
/// version would open the MQTTS session here. Still a stub, but no
/// longer duplicated across two structs.
@@ -302,6 +313,10 @@ impl GenericPrinter for BambuV1Printer {
async fn connect(&mut self) -> Result<(), PrinterError> {
self.generic.connect_impl().await
}
async fn set_fan_speed(&mut self, percent: u8) -> Result<(), PrinterError> {
self.generic.set_fan_speed_impl(percent).await
}
}
/// Current-generation Bambu MQTT report schema (X1 Carbon/X1E, H2 series).
@@ -341,4 +356,8 @@ impl GenericPrinter for BambuV2Printer {
async fn connect(&mut self) -> Result<(), PrinterError> {
self.generic.connect_impl().await
}
async fn set_fan_speed(&mut self, percent: u8) -> Result<(), PrinterError> {
self.generic.set_fan_speed_impl(percent).await
}
}