Skip to content

Commit ee8cbd1

Browse files
Get temperature setting kinda working
1 parent a6e9360 commit ee8cbd1

11 files changed

Lines changed: 248 additions & 9 deletions

File tree

moonraker-rs/src/cache.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{
33
printer_objects::*,
44
};
55

6+
// TODO: Fill cache with configuration for min/max temp
67
#[derive(Debug, Default)]
78
pub struct Cache {
89
pub webhooks: Webhooks,
@@ -16,7 +17,7 @@ pub struct Cache {
1617
pub virtual_sdcard: VirtualSdcard,
1718
pub print_stats: PrintStats,
1819
pub display_status: DisplayStatus,
19-
pub temperature_sensors: Vec<NamedTemperatureSensor>,
20+
pub temperature_sensors: Vec<NamedTemperatureSensor>,
2021
pub temperature_fans: Vec<NamedTemperatureFan>,
2122
pub filament_switch_sensors: Vec<NamedFilamentSwitchSensor>,
2223
pub output_pins: Vec<NamedOutputPin>,

moonraker-rs/src/printer_objects/extruder.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use optional_struct::*;
22
use serde::Deserialize;
33

4+
use crate::printer_objects::TempControl;
5+
46
#[optional_struct]
5-
#[derive(Debug, Deserialize, Default, Clone)]
7+
#[derive(Debug, Deserialize, Clone)]
68
pub struct Extruder {
79
pub temperature: f32,
810
pub target: f32,
@@ -11,6 +13,22 @@ pub struct Extruder {
1113
pub pressure_advance: f32,
1214
pub smooth_time: f32,
1315
pub motion_queue: Option<String>,
16+
pub temp_control: TempControl
17+
}
18+
19+
impl Default for Extruder {
20+
fn default() -> Self {
21+
Self {
22+
temperature: 0.0,
23+
target: 0.0,
24+
power: 0.0,
25+
can_extrude: false,
26+
pressure_advance: 0.0,
27+
smooth_time: 0.0,
28+
motion_queue: None,
29+
temp_control: TempControl::default_hotend(),
30+
}
31+
}
1432
}
1533

1634
impl Extruder {
@@ -36,5 +54,8 @@ impl Extruder {
3654
if let Some(motion_queue) = extruder.motion_queue {
3755
self.motion_queue = Some(motion_queue);
3856
}
57+
if let Some(temp_control) = extruder.temp_control {
58+
self.temp_control = temp_control;
59+
}
3960
}
4061
}

moonraker-rs/src/printer_objects/heater_bed.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
use optional_struct::*;
22
use serde::Deserialize;
33

4+
use crate::printer_objects::TempControl;
5+
46
#[optional_struct]
5-
#[derive(Debug, Deserialize, Default, Clone)]
7+
#[derive(Debug, Deserialize, Clone)]
68
pub struct HeaterBed {
79
pub temperature: f32,
810
pub target: f32,
911
pub power: f32,
12+
pub temp_control: TempControl,
13+
}
14+
15+
impl Default for HeaterBed {
16+
fn default() -> Self {
17+
Self {
18+
temperature: 0.0,
19+
target: 0.0,
20+
power: 0.0,
21+
temp_control: TempControl::default_bed(),
22+
}
23+
}
1024
}
1125

1226
impl HeaterBed {
@@ -20,5 +34,8 @@ impl HeaterBed {
2034
if let Some(power) = heater_bed.power {
2135
self.power = power;
2236
}
37+
if let Some(temp_control) = heater_bed.temp_control {
38+
self.temp_control = temp_control;
39+
}
2340
}
2441
}

moonraker-rs/src/printer_objects/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub mod temperature_sensor;
1515
pub mod toolhead;
1616
pub mod virtual_sdcard;
1717
pub mod webhooks;
18+
pub mod temp_control;
1819

1920
pub use display_status::*;
2021
pub use exclude_object::*;
@@ -33,3 +34,4 @@ pub use temperature_sensor::*;
3334
pub use toolhead::*;
3435
pub use virtual_sdcard::*;
3536
pub use webhooks::*;
37+
pub use temp_control::*;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use serde::Deserialize;
2+
3+
#[derive(Debug, Deserialize, Clone, PartialEq)]
4+
pub struct TempControl
5+
{
6+
pub min_temp: i32,
7+
pub max_temp: i32,
8+
pub step_temp: i32,
9+
}
10+
11+
impl TempControl {
12+
pub fn default_hotend() -> Self {
13+
Self {
14+
min_temp: 180,
15+
max_temp: 300,
16+
step_temp: 5,
17+
}
18+
}
19+
pub fn default_bed() -> Self {
20+
Self {
21+
min_temp: 30,
22+
max_temp: 120,
23+
step_temp: 5,
24+
}
25+
}
26+
pub fn default_fan() -> Self {
27+
Self {
28+
min_temp: 30,
29+
max_temp: 60,
30+
step_temp: 5,
31+
}
32+
}
33+
}

moonraker-rs/src/printer_objects/temperature_fan.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
use optional_struct::*;
22
use serde::Deserialize;
33

4+
use crate::printer_objects::TempControl;
5+
46
#[optional_struct]
5-
#[derive(Debug, Deserialize, Default, Clone)]
7+
#[derive(Debug, Deserialize, Clone)]
68
pub struct TemperatureFan {
79
pub speed: f32,
810
pub rpm: Option<f32>,
911
pub temperature: f32,
1012
pub target: f32,
13+
pub temp_control: TempControl
14+
}
15+
16+
impl Default for TemperatureFan {
17+
fn default() -> Self {
18+
Self {
19+
speed: 0.0,
20+
rpm: None,
21+
temperature: 0.0,
22+
target: 0.0,
23+
temp_control: TempControl::default_fan(),
24+
}
25+
}
1126
}
1227

1328
#[derive(Debug, Deserialize, Default, Clone)]
@@ -36,5 +51,8 @@ impl TemperatureFan {
3651
if let Some(target) = fan.target {
3752
self.target = target;
3853
}
54+
if let Some(temp_control) = fan.temp_control {
55+
self.temp_control = temp_control;
56+
}
3957
}
4058
}

moonraker-rs/src/requests/printer_administration.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub trait PrinterAdministrationRequestHandler {
1212
&self,
1313
objects: Vec<String>,
1414
) -> Result<PrinterObjectsSubscribeResult, Error>;
15+
async fn run_gcode_script(&self, script: &str) -> Result<String, Error>;
1516
}
1617

1718
impl PrinterAdministrationRequestHandler for MoonrakerConnection {
@@ -35,6 +36,11 @@ impl PrinterAdministrationRequestHandler for MoonrakerConnection {
3536
self.send_request("printer.objects.subscribe", Some(args))
3637
.await
3738
}
39+
40+
async fn run_gcode_script(&self, script: &str) -> Result<String, Error> {
41+
let args = serde_json::json!({ "script": script });
42+
self.send_request("printer.gcode.script", Some(args)).await
43+
}
3844
}
3945

4046
#[derive(Debug, Deserialize)]

src/event_loop/temperature_devices.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ impl TemperatureDevicesHandler for EventLoop {
2424
name: SharedString::from("extruder"),
2525
target: extruder_event.target as i32,
2626
temperature: extruder_event.temperature as i32,
27+
min_temp: extruder_event.temp_control.min_temp,
28+
max_temp: extruder_event.temp_control.max_temp,
29+
step: extruder_event.temp_control.step_temp,
2730
};
2831

2932
self.ui_weak
@@ -34,9 +37,12 @@ impl TemperatureDevicesHandler for EventLoop {
3437

3538
if let PrinterEvent::HeaterBed(heater_bed_event) = printer_event {
3639
let bed = Heater {
37-
name: SharedString::from("heated_bed"),
40+
name: SharedString::from("heater_bed"),
3841
target: heater_bed_event.target as i32,
3942
temperature: heater_bed_event.temperature as i32,
43+
min_temp: heater_bed_event.temp_control.min_temp,
44+
max_temp: heater_bed_event.temp_control.max_temp,
45+
step: heater_bed_event.temp_control.step_temp,
4046
};
4147

4248
self.ui_weak.upgrade_in_event_loop(move |ui: AppWindow| {
@@ -83,6 +89,9 @@ impl TemperatureDevicesHandler for EventLoop {
8389
name: SharedString::from(&temperature_fan_event.name),
8490
temperature: temperature_fan_event.fan.temperature as i32,
8591
target: temperature_fan_event.fan.target as i32,
92+
min_temp: temperature_fan_event.fan.temp_control.min_temp,
93+
max_temp: temperature_fan_event.fan.temp_control.max_temp,
94+
step: temperature_fan_event.fan.temp_control.step_temp,
8695
},
8796
speed: temperature_fan_event.fan.speed,
8897
};

src/main.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use clap::Parser;
77
use moonraker_rs::{
88
connector::websocket_read::{MoonrakerEvent, PrinterEvent},
99
moonraker_connection::WebsocketEvent,
10-
requests::FileManagementRequestHandler,
10+
requests::{FileManagementRequestHandler, PrinterAdministrationRequestHandler},
1111
};
1212
use slint::{Image, Model, ModelRc, Rgba8Pixel, SharedPixelBuffer, SharedString, VecModel};
1313
use tokio::sync::Mutex;
@@ -49,6 +49,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
4949

5050
let moonraker_connection_clone = moonraker_connection.clone();
5151
let moonraker_connection_clone_2 = moonraker_connection.clone();
52+
let moonraker_connection_clone_3 = moonraker_connection.clone();
5253

5354
let mut receiver = moonraker_connection.get_listener();
5455

@@ -193,6 +194,19 @@ async fn main() -> Result<(), Box<dyn Error>> {
193194
SharedString::from(format!("{:.2} {}", value, units[idx]))
194195
});
195196

197+
ui.global::<TemperatureSensors>().on_set_new_target_temperature(move |heater_name, target| {
198+
println!("Set new target temperature for {}: {}", heater_name, target);
199+
let moonraker_connection = moonraker_connection_clone_3.clone();
200+
slint::spawn_local(async move {
201+
let heater_name = heater_name.to_string();
202+
// TODO: Double check if this works with temperature_fan's
203+
let moonraker_connection = moonraker_connection.clone();
204+
let command = format!("SET_HEATER_TEMPERATURE HEATER={} TARGET={}", heater_name, target);
205+
moonraker_connection.run_gcode_script(&command).await.expect("Failed to set heater temperature");
206+
})
207+
.unwrap();
208+
});
209+
196210
tokio::task::block_in_place(|| {
197211
ui.run().unwrap();
198212
});

0 commit comments

Comments
 (0)