From d2431aff8e8ff406beb47fb8db5ea528e95c0c93 Mon Sep 17 00:00:00 2001 From: JP Stringham Date: Sat, 10 Jan 2026 18:39:54 -0500 Subject: [PATCH] Addition of vague failsafes and speaker beep when probe goes out of range --- .cargo/config.toml | 2 +- src/main.rs | 58 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 71c48a4..9efbea9 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -32,4 +32,4 @@ rustflags = [ # This runner will find a supported SWD debug probe and flash your RP2040 over # SWD: -runner = "probe-rs run --chip RP2040" \ No newline at end of file +runner = "probe-rs run --chip RP2040 --protocol swd" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1f46f0d..30977eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ mod rotary; use defmt::println; +use embedded_hal::pwm::SetDutyCycle; // Ensure we halt the program on panic (if we don't mention this crate it won't // be linked) use panic_probe as _; @@ -131,7 +132,7 @@ fn main() -> ! { let mut tick = 0u32; let mut last_gfx_push = 0u32; - let mut target_temp = 220; + let mut target_temp = 250; let mut gate_pin = pins.gpio15.into_push_pull_output_in_state(rp2040_hal::gpio::PinState::Low); @@ -141,6 +142,17 @@ fn main() -> ! { let mut clk_pin = pins.gpio16.into_pull_up_input(); let mut dt_pin = pins.gpio17.into_pull_up_input(); + let mut pwm_slices = hal::pwm::Slices::new(pac.PWM, &mut pac.RESETS); + let pwm = &mut pwm_slices.pwm1; + pwm.set_ph_correct(); + pwm.set_div_int(3); + pwm.enable(); + + let beep_channel = &mut pwm.channel_a; + beep_channel.set_duty_cycle(5000); + beep_channel.output_to(pins.gpio2); + let mut emergency = 0u32; + let mut last_rotary = 0u32; const CYCLES_PER_FRAME:u32 = 100_000; @@ -150,6 +162,17 @@ fn main() -> ! { loop { + match emergency { + 0 => beep_channel.set_enabled(false), + e => { + beep_channel.set_enabled((e % 5_000_000) < 70000); + emergency += 1; + if emergency == 0 { + emergency = 1; + } + } + } + if tick - last_rotary > CYCLES_PER_ROTARY { rotary_input_allowed = true; } @@ -161,13 +184,13 @@ fn main() -> ! { match rotary.update(clk_now, dt_now) { RotaryResponse::UP => { - target_temp += 1; + target_temp += 5; rotary_input_allowed = false; last_rotary = tick; // println!("Rotary up"); } RotaryResponse::DOWN => { - target_temp -= 1; + target_temp -= 5; rotary_input_allowed = false; last_rotary = tick; // println!("Rotary dn"); @@ -224,13 +247,11 @@ fn main() -> ! { adc_avg += *v as u32; }); - if accepted_samps < 10 { - println!("Not enough accepted samples, skipping"); - continue; - } - - let adc_val = (adc_avg / accepted_samps) as u16; - + + let adc_val = match accepted_samps >= 10 { + true => (adc_avg / accepted_samps) as u16, + false => 0u16 + }; // println!("ADC {=u16}, samps {}", adc_val, accepted_samps); let deg = match adc_val { @@ -251,7 +272,16 @@ fn main() -> ! { let probe_temp = (deg * 10.) as u32; let mut itoa_result = itoa(probe_temp); - let deg_str = itoa_result.as_degrees_decicentigrade_str(); + let deg_str =match probe_temp { + 0 | 900.. => { + emergency |= 1; + "ERR" + } + _ => { + emergency = 0; + itoa_result.as_degrees_decicentigrade_str() + } + }; // println!("tenths of a deg {}, deg {}", probe_temp, deg_str); @@ -259,9 +289,9 @@ fn main() -> ! { gfx_buf.draw_string(10, START_Y+30, "Target Temp"); - gate_pin.set_state(match target_temp > probe_temp { - true => rp2040_hal::gpio::PinState::High, - false => rp2040_hal::gpio::PinState::Low + gate_pin.set_state(match (probe_temp > 0, target_temp > probe_temp) { + (true, true) => rp2040_hal::gpio::PinState::High, + _ => rp2040_hal::gpio::PinState::Low }).unwrap(); // dancing sprite just for heartbeat visibility