esp32c3-playground/src/main.rs

126 lines
3.3 KiB
Rust
Raw Permalink Normal View History

2024-04-20 00:53:31 +02:00
#![no_std]
#![no_main]
2024-04-20 19:48:20 +02:00
use core::cell::{Cell, RefCell};
use critical_section::Mutex;
2024-04-20 00:53:31 +02:00
use esp_hal::clock::ClockControl;
use esp_hal::delay::Delay;
2024-04-20 19:48:20 +02:00
use esp_hal::gpio::{Event, GpioPin, Input, PullUp, IO};
2024-04-20 01:25:11 +02:00
use esp_hal::ledc::{channel, timer, LSGlobalClkSource, LEDC};
2024-04-20 00:53:31 +02:00
use esp_hal::peripherals::Peripherals;
use esp_hal::prelude::*;
use esp_hal::systimer::SystemTimer;
2024-04-20 00:53:31 +02:00
use panic_halt as _;
2024-04-20 19:49:11 +02:00
const BUTTON_TIMEOUT: u64 = SystemTimer::TICKS_PER_SECOND / 5;
const COLORS: [[u8; 3]; 6] = [
2024-04-20 19:48:50 +02:00
// G B R
[0, 0, 1],
[1, 0, 1],
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0, 1, 1],
];
2024-04-20 19:48:20 +02:00
static BUTTON: Mutex<RefCell<Option<GpioPin<Input<PullUp>, 9>>>> = Mutex::new(RefCell::new(None));
static LAST_TRIGGERED: Mutex<Cell<u64>> = Mutex::new(Cell::new(0));
static STOP_CHASE: Mutex<Cell<bool>> = Mutex::new(Cell::new(false));
2024-04-20 00:53:31 +02:00
#[entry]
fn main() -> ! {
let p = Peripherals::take();
let sys = p.SYSTEM.split();
let clocks = ClockControl::boot_defaults(sys.clock_control).freeze();
2024-04-20 01:25:11 +02:00
let mut ledc = LEDC::new(p.LEDC, &clocks);
ledc.set_global_slow_clock(LSGlobalClkSource::APBClk);
let mut timer = ledc.get_timer(timer::Number::Timer0);
timer
.configure(timer::config::Config {
clock_source: timer::LSClockSource::APBClk,
duty: timer::config::Duty::Duty8Bit,
frequency: 24.kHz(),
})
.unwrap();
let mut io = IO::new(p.GPIO, p.IO_MUX);
io.set_interrupt_handler(gpio_handler);
2024-04-20 00:53:31 +02:00
2024-04-20 01:25:11 +02:00
let mut r = ledc.get_channel(
channel::Number::Channel0,
io.pins.gpio5.into_push_pull_output(),
);
let mut g = ledc.get_channel(
channel::Number::Channel1,
io.pins.gpio6.into_push_pull_output(),
);
let mut b = ledc.get_channel(
channel::Number::Channel2,
io.pins.gpio7.into_push_pull_output(),
);
r.configure(channel::config::Config {
duty_pct: 0,
pin_config: channel::config::PinConfig::PushPull,
timer: &timer,
})
.unwrap();
g.configure(channel::config::Config {
duty_pct: 0,
pin_config: channel::config::PinConfig::PushPull,
timer: &timer,
})
.unwrap();
b.configure(channel::config::Config {
duty_pct: 0,
pin_config: channel::config::PinConfig::PushPull,
timer: &timer,
})
.unwrap();
2024-04-20 00:53:31 +02:00
2024-04-20 19:48:20 +02:00
let mut btn = io.pins.gpio9.into_pull_up_input();
btn.listen(Event::RisingEdge);
critical_section::with(|cs| BUTTON.borrow(cs).replace(Some(btn)));
2024-04-20 00:53:31 +02:00
let delay = Delay::new(&clocks);
for [r_duty, g_duty, b_duty] in COLORS.iter().cycle() {
let stop_chase = critical_section::with(|cs| STOP_CHASE.borrow(cs).get());
if !stop_chase {
let _ = r.set_duty(*r_duty);
let _ = g.set_duty(*g_duty);
let _ = b.set_duty(*b_duty);
}
2024-04-20 00:53:31 +02:00
2024-04-20 19:49:27 +02:00
delay.delay_millis(100u32);
2024-04-20 00:53:31 +02:00
}
loop {}
}
#[handler]
fn gpio_handler() {
critical_section::with(|cs| {
let last_triggered = LAST_TRIGGERED.borrow(cs);
2024-04-20 19:48:20 +02:00
if last_triggered.get() + BUTTON_TIMEOUT < SystemTimer::now() {
let stop_chase = STOP_CHASE.borrow(cs);
stop_chase.set(!stop_chase.get());
2024-04-20 19:48:20 +02:00
last_triggered.set(SystemTimer::now());
}
2024-04-20 19:48:20 +02:00
if let Some(button) = BUTTON.borrow(cs).borrow_mut().as_mut() {
button.clear_interrupt();
}
});
}