#![no_std]
#![no_main]
use esp_backtrace as _;
use esp_hal::{clock::CpuClock, delay::Delay, gpio::*};
use log::info;
const DEBOUNCE_DELAY: u32 = 50;
#[esp_hal::main]
fn main() -> ! {
esp_println::logger::init_logger_from_env();
let delay = Delay::new();
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
let mut leds = [
Output::new(peripherals.GPIO2, Level::Low), // B - bit 0
Output::new(peripherals.GPIO4, Level::Low), // G - bit 1
Output::new(peripherals.GPIO5, Level::Low), // R - bit 2
];
let button = Input::new(peripherals.GPIO15, Pull::Up);
let max_count = 1u8 << leds.len();
let mut counter = 0;
let mut button_pressed = false;
info!("Binary counter starting... Press the button to increment!");
info!("Maximum count: {max_count}");
loop {
if !button_pressed && button.is_low() {
counter = (counter + 1) % max_count;
leds.iter_mut().enumerate().for_each(|(i, led)| {
let bit_mask = 1 << i;
if counter & bit_mask != 0 {
led.set_high();
} else {
led.set_low();
}
});
info!("Counter: {counter} (binary: {counter:03b})");
delay.delay_millis(DEBOUNCE_DELAY);
button_pressed = true;
} else if button_pressed && button.is_high() {
delay.delay_millis(DEBOUNCE_DELAY);
button_pressed = false;
}
}
}