diff --git a/final/src/bin/main.rs b/final/src/bin/main.rs index 8be0879..7e4b2df 100644 --- a/final/src/bin/main.rs +++ b/final/src/bin/main.rs @@ -104,7 +104,6 @@ async fn main(spawner: Spawner) -> ! { mqtt_publish("esp32/topic", b"hello from ESP32 (init)", QualityOfService::QoS1, false).await; info!("Sent initial MQTT message"); - // Subscribe to your topics (can be done anytime; command is queued) mqtt_subscribe("esp32/topic").await; // Get a receiver for incoming MQTT messages diff --git a/final/src/mqtt/client.rs b/final/src/mqtt/client.rs index c1cf552..93940a5 100644 --- a/final/src/mqtt/client.rs +++ b/final/src/mqtt/client.rs @@ -35,7 +35,7 @@ static TCP_TX_BUFFER: ConstStaticCell<[u8; 2048]> = ConstStaticCell::new([0; 204 static MQTT_TX_BUF: ConstStaticCell<[u8; 1024]> = ConstStaticCell::new([0; 1024]); static MQTT_RX_BUF: ConstStaticCell<[u8; 1024]> = ConstStaticCell::new([0; 1024]); -// Tie TcpSocket lifetime to session, MQTT buffers to 'a (fixes E0521). +// Tie TcpSocket lifetime to session type Client<'a, 'net> = MqttClient<'a, TcpSocket<'net>, 8, CountingRng>; #[derive(Clone)] diff --git a/test_LED/src/bin/main.rs b/test_LED/src/bin/main.rs index 166d33e..49df249 100644 --- a/test_LED/src/bin/main.rs +++ b/test_LED/src/bin/main.rs @@ -37,8 +37,49 @@ async fn main(spawner: Spawner) { info!("Embassy initialized!"); + + // LEDC initialization + let mut ledc = Ledc::new(peripherals.LEDC); + ledc.set_global_slow_clock(LSGlobalClkSource::APBClk); + + let mut pwm_timer = ledc.timer::(timer::Number::Timer0); + pwm_timer + .configure(timer::config::Config { + duty: timer::config::Duty::Duty14Bit, + frequency: Rate::from_hz(1000), + clock_source: timer::LSClockSource::APBClk, + }) + .unwrap(); + + let mut pwm_channel = + ledc.channel::(channel::Number::Channel0, peripherals.GPIO1); + + pwm_channel + .configure(channel::config::Config { + timer: &pwm_timer, + duty_pct: 0, + pin_config: channel::config::PinConfig::PushPull, + }) + .unwrap(); + + info!("LEDC configured successfully"); + let mut brightness: i32 = 0; + let mut direction: i32 = 5; + loop { - Timer::after(Duration::from_millis(50)).await; + // Set current brightness level (0–100 %) + pwm_channel.set_duty(brightness as u8).ok(); + + brightness += direction; + if brightness >= 100 { + brightness = 100; + direction = -5; + } else if brightness <= 0 { + brightness = 0; + direction = 5; + } + + Timer::after(Duration::from_millis(30)).await; } // for inspiration have a look at the examples at https://github.com/esp-rs/esp-hal/tree/esp-hal-v1.0.0-rc.0/examples/src/bin