-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement
embedded-hal
v1 SetDutyCycle
this also ships with an example making use of it. note that the fading of the new example is faster than the existing `uno-simple-pwm.rs` example, since the latter fades with 255 steps while the new example uses 0 - 100% percent (and thus only sleeps 200 * 10ms instead of 512 * 10ms).
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
examples/arduino-uno/src/bin/uno-simple-pwm-embedded-hal.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/*! | ||
* Example of using simple_pwm to fade a LED in and out on pin d5, but with a twist: | ||
* the fade function is not aware of `avr-hal` and only uses `embedded-hal` traits. | ||
*/ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use embedded_hal::delay::DelayNs; | ||
use embedded_hal::pwm::SetDutyCycle; | ||
use arduino_hal::simple_pwm::*; | ||
use panic_halt as _; | ||
|
||
fn fade(led: &mut impl SetDutyCycle, delay: &mut impl DelayNs) -> ! { | ||
loop { | ||
for pct in (0..=100).chain((0..100).rev()) { | ||
led.set_duty_cycle_percent(pct).unwrap(); | ||
delay.delay_ms(10); | ||
} | ||
} | ||
} | ||
|
||
#[arduino_hal::entry] | ||
fn main() -> ! { | ||
let dp = arduino_hal::Peripherals::take().unwrap(); | ||
let pins = arduino_hal::pins!(dp); | ||
|
||
let timer0 = Timer0Pwm::new(dp.TC0, Prescaler::Prescale64); | ||
|
||
// Digital pin 5 is connected to a LED and a resistor in series | ||
let mut pwm_led = pins.d5.into_output().into_pwm(&timer0); | ||
pwm_led.enable(); | ||
|
||
let mut delay = arduino_hal::Delay::new(); | ||
|
||
fade(&mut pwm_led, &mut delay); | ||
} |