Compare commits
4 Commits
b6ad376a64
...
c3fea7441d
Author | SHA1 | Date |
---|---|---|
Luca | c3fea7441d | |
Luca | ffc970b03e | |
Luca | 0b9c5db605 | |
Luca | 4c242c2983 |
29
src/main.rs
29
src/main.rs
|
@ -1,13 +1,13 @@
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
use core::cell::Cell;
|
use core::cell::{Cell, RefCell};
|
||||||
|
|
||||||
use critical_section::Mutex;
|
use critical_section::Mutex;
|
||||||
|
|
||||||
use esp_hal::clock::ClockControl;
|
use esp_hal::clock::ClockControl;
|
||||||
use esp_hal::delay::Delay;
|
use esp_hal::delay::Delay;
|
||||||
use esp_hal::gpio::{Event, IO};
|
use esp_hal::gpio::{Event, GpioPin, Input, PullUp, IO};
|
||||||
use esp_hal::ledc::{channel, timer, LSGlobalClkSource, LEDC};
|
use esp_hal::ledc::{channel, timer, LSGlobalClkSource, LEDC};
|
||||||
use esp_hal::peripherals::Peripherals;
|
use esp_hal::peripherals::Peripherals;
|
||||||
use esp_hal::prelude::*;
|
use esp_hal::prelude::*;
|
||||||
|
@ -15,17 +15,20 @@ use esp_hal::systimer::SystemTimer;
|
||||||
|
|
||||||
use panic_halt as _;
|
use panic_halt as _;
|
||||||
|
|
||||||
const BUTTON_TIMEOUT: u64 = SystemTimer::TICKS_PER_SECOND / 10;
|
const BUTTON_TIMEOUT: u64 = SystemTimer::TICKS_PER_SECOND / 5;
|
||||||
|
|
||||||
const COLORS: [[u8; 3]; 6] = [
|
const COLORS: [[u8; 3]; 6] = [
|
||||||
|
// G B R
|
||||||
|
[0, 0, 1],
|
||||||
|
[1, 0, 1],
|
||||||
[1, 0, 0],
|
[1, 0, 0],
|
||||||
[1, 1, 0],
|
[1, 1, 0],
|
||||||
[0, 1, 0],
|
[0, 1, 0],
|
||||||
[0, 1, 1],
|
[0, 1, 1],
|
||||||
[0, 0, 1],
|
|
||||||
[1, 0, 1],
|
|
||||||
];
|
];
|
||||||
|
|
||||||
|
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 LAST_TRIGGERED: Mutex<Cell<u64>> = Mutex::new(Cell::new(0));
|
||||||
|
|
||||||
static STOP_CHASE: Mutex<Cell<bool>> = Mutex::new(Cell::new(false));
|
static STOP_CHASE: Mutex<Cell<bool>> = Mutex::new(Cell::new(false));
|
||||||
|
@ -84,7 +87,9 @@ fn main() -> ! {
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
io.pins.gpio9.into_pull_up_input().listen(Event::RisingEdge);
|
let mut btn = io.pins.gpio9.into_pull_up_input();
|
||||||
|
btn.listen(Event::RisingEdge);
|
||||||
|
critical_section::with(|cs| BUTTON.borrow(cs).replace(Some(btn)));
|
||||||
|
|
||||||
let delay = Delay::new(&clocks);
|
let delay = Delay::new(&clocks);
|
||||||
|
|
||||||
|
@ -96,7 +101,7 @@ fn main() -> ! {
|
||||||
let _ = b.set_duty(*b_duty);
|
let _ = b.set_duty(*b_duty);
|
||||||
}
|
}
|
||||||
|
|
||||||
delay.delay_millis(200u32);
|
delay.delay_millis(100u32);
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {}
|
loop {}
|
||||||
|
@ -106,13 +111,15 @@ fn main() -> ! {
|
||||||
fn gpio_handler() {
|
fn gpio_handler() {
|
||||||
critical_section::with(|cs| {
|
critical_section::with(|cs| {
|
||||||
let last_triggered = LAST_TRIGGERED.borrow(cs);
|
let last_triggered = LAST_TRIGGERED.borrow(cs);
|
||||||
if last_triggered.get() + BUTTON_TIMEOUT > SystemTimer::now() {
|
if last_triggered.get() + BUTTON_TIMEOUT < SystemTimer::now() {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let stop_chase = STOP_CHASE.borrow(cs);
|
let stop_chase = STOP_CHASE.borrow(cs);
|
||||||
stop_chase.set(!stop_chase.get());
|
stop_chase.set(!stop_chase.get());
|
||||||
|
|
||||||
last_triggered.set(SystemTimer::now());
|
last_triggered.set(SystemTimer::now());
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(button) = BUTTON.borrow(cs).borrow_mut().as_mut() {
|
||||||
|
button.clear_interrupt();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue