//! Blinks an LED when button GPIO is pulled high
//!
//! This assumes that a LED is connected to the pin assigned to `led`. (GPIO4)
#![no_std]
#![no_main]
// use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
use esp_backtrace as _;
use esp_println::println;
use hal::{
clock::ClockControl,
gpio::IO,
peripherals::Peripherals,
prelude::*,
timer::TimerGroup, Delay,
Rtc,
};
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let mut system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
// Disable the RTC and TIMG watchdog timers
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let mut wdt0 = timer_group0.wdt;
let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks);
let mut wdt1 = timer_group1.wdt;
rtc.rwdt.disable();
wdt0.disable();
wdt1.disable();
println!("Hello world!");
// Get GPIO
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
// Set GPIO4 as an output
let mut led = io.pins.gpio4.into_push_pull_output();
// Set GPIO2 as an input
let mut btn = io.pins.gpio2.into_pull_down_input().degrade();
// Set LED low initially
led.set_low().unwrap();
// Initialize the Delay peripheral, and use it to toggle the LED state in a
// loop.
let mut delay = Delay::new(&clocks);
// TODO: Detect button press with interrupt instead.
// see: https://esp-rs.github.io/book/writing-your-own-application/no-std-applications/interrupt.html
loop {
if btn.is_high().unwrap() {
esp_println::println!("Button\r");
//led.toggle().unwrap();
led.set_high().unwrap();
} else {
led.set_low().unwrap();
}
delay.delay_ms(500u32);
}
}