diff options
| author | Curly <[email protected]> | 2025-02-23 07:33:58 -0800 |
|---|---|---|
| committer | Curly <[email protected]> | 2025-02-23 07:33:58 -0800 |
| commit | 3932835998802fc3abf7cce4f736e072858ebfd1 (patch) | |
| tree | 5dd714b99bc74a03556c58809237c88691c293bb /examples/rp235x/src/bin/pio_rotary_encoder.rs | |
| parent | c3c67db93e627a4fafe5e1a1123e5cbb4abafe47 (diff) | |
rename `rp23` (?) folder to `rp235x`; fix `ci.sh` to use `rp235x` folder
Diffstat (limited to 'examples/rp235x/src/bin/pio_rotary_encoder.rs')
| -rw-r--r-- | examples/rp235x/src/bin/pio_rotary_encoder.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/examples/rp235x/src/bin/pio_rotary_encoder.rs b/examples/rp235x/src/bin/pio_rotary_encoder.rs new file mode 100644 index 000000000..2750f61ae --- /dev/null +++ b/examples/rp235x/src/bin/pio_rotary_encoder.rs | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | //! This example shows how to use the PIO module in the RP2040 to read a quadrature rotary encoder. | ||
| 2 | |||
| 3 | #![no_std] | ||
| 4 | #![no_main] | ||
| 5 | |||
| 6 | use defmt::info; | ||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_rp::bind_interrupts; | ||
| 9 | use embassy_rp::peripherals::PIO0; | ||
| 10 | use embassy_rp::pio::{InterruptHandler, Pio}; | ||
| 11 | use embassy_rp::pio_programs::rotary_encoder::{Direction, PioEncoder, PioEncoderProgram}; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | bind_interrupts!(struct Irqs { | ||
| 15 | PIO0_IRQ_0 => InterruptHandler<PIO0>; | ||
| 16 | }); | ||
| 17 | |||
| 18 | #[embassy_executor::task] | ||
| 19 | async fn encoder_0(mut encoder: PioEncoder<'static, PIO0, 0>) { | ||
| 20 | let mut count = 0; | ||
| 21 | loop { | ||
| 22 | info!("Count: {}", count); | ||
| 23 | count += match encoder.read().await { | ||
| 24 | Direction::Clockwise => 1, | ||
| 25 | Direction::CounterClockwise => -1, | ||
| 26 | }; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | #[embassy_executor::task] | ||
| 31 | async fn encoder_1(mut encoder: PioEncoder<'static, PIO0, 1>) { | ||
| 32 | let mut count = 0; | ||
| 33 | loop { | ||
| 34 | info!("Count: {}", count); | ||
| 35 | count += match encoder.read().await { | ||
| 36 | Direction::Clockwise => 1, | ||
| 37 | Direction::CounterClockwise => -1, | ||
| 38 | }; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | #[embassy_executor::main] | ||
| 43 | async fn main(spawner: Spawner) { | ||
| 44 | let p = embassy_rp::init(Default::default()); | ||
| 45 | let Pio { | ||
| 46 | mut common, sm0, sm1, .. | ||
| 47 | } = Pio::new(p.PIO0, Irqs); | ||
| 48 | |||
| 49 | let prg = PioEncoderProgram::new(&mut common); | ||
| 50 | let encoder0 = PioEncoder::new(&mut common, sm0, p.PIN_4, p.PIN_5, &prg); | ||
| 51 | let encoder1 = PioEncoder::new(&mut common, sm1, p.PIN_6, p.PIN_7, &prg); | ||
| 52 | |||
| 53 | spawner.must_spawn(encoder_0(encoder0)); | ||
| 54 | spawner.must_spawn(encoder_1(encoder1)); | ||
| 55 | } | ||
