-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a88d31
commit bec8c11
Showing
3 changed files
with
138 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
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,62 @@ | ||
//! Breath an led in and out without using the BSP split() method. | ||
#![no_std] | ||
#![no_main] | ||
|
||
use panic_halt as _; | ||
use pygamer as hal; | ||
|
||
use core::f32::consts::FRAC_PI_2; | ||
use hal::clock::GenericClockController; | ||
use hal::delay::Delay; | ||
use hal::entry; | ||
use hal::pac::{CorePeripherals, Peripherals}; | ||
use hal::prelude::*; | ||
use hal::pwm::{Pwm4, TC4Pinout}; | ||
use micromath::F32Ext; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let mut peripherals = Peripherals::take().unwrap(); | ||
let core = CorePeripherals::take().unwrap(); | ||
let mut clocks = GenericClockController::with_internal_32kosc( | ||
peripherals.GCLK, | ||
&mut peripherals.MCLK, | ||
&mut peripherals.OSC32KCTRL, | ||
&mut peripherals.OSCCTRL, | ||
&mut peripherals.NVMCTRL, | ||
); | ||
|
||
let mut delay = Delay::new(core.SYST, &mut clocks); | ||
delay.delay_ms(400u16); | ||
|
||
let mut pins = hal::Pins::new(peripherals.PORT); | ||
|
||
let gclk = clocks.gclk0(); | ||
|
||
let mut pwm0 = Pwm4::new( | ||
&clocks.tc4_tc5(&gclk).unwrap(), | ||
1.khz(), | ||
peripherals.TC4, | ||
TC4Pinout::Pa23(pins.d13.into_function_e(&mut pins.port)), | ||
&mut peripherals.MCLK, | ||
); | ||
let max_duty = pwm0.get_max_duty(); | ||
|
||
loop { | ||
for j in 0..255 { | ||
pwm0.set_duty(sine_ease_in(j as f32, 0.0, max_duty.into(), 255.0) as u16); | ||
delay.delay_ms(10u16); | ||
} | ||
for j in (0..255).rev() { | ||
pwm0.set_duty(sine_ease_in(j as f32, 0.0, max_duty.into(), 255.0) as u16); | ||
delay.delay_ms(10u16); | ||
} | ||
} | ||
} | ||
|
||
#[inline] | ||
// current step, where oputput starts, where output ends, last step | ||
fn sine_ease_in(t: f32, b: f32, c: f32, d: f32) -> f32 { | ||
-c * (t / d * FRAC_PI_2).cos() + c + b | ||
} |
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,68 @@ | ||
//! Blink an led without using the BSP split() method. | ||
#![no_std] | ||
#![no_main] | ||
|
||
use panic_halt as _; | ||
use pygamer as hal; | ||
|
||
use core::f32::consts::FRAC_PI_2; | ||
use hal::clock::GenericClockController; | ||
use hal::delay::Delay; | ||
use hal::entry; | ||
use hal::pac::{CorePeripherals, Peripherals}; | ||
use hal::prelude::*; | ||
use hal::pwm::{Channel, TCC0Pinout, Tcc0Pwm}; | ||
use micromath::F32Ext; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let mut peripherals = Peripherals::take().unwrap(); | ||
let core = CorePeripherals::take().unwrap(); | ||
let mut clocks = GenericClockController::with_internal_32kosc( | ||
peripherals.GCLK, | ||
&mut peripherals.MCLK, | ||
&mut peripherals.OSC32KCTRL, | ||
&mut peripherals.OSCCTRL, | ||
&mut peripherals.NVMCTRL, | ||
); | ||
|
||
let mut delay = Delay::new(core.SYST, &mut clocks); | ||
delay.delay_ms(400u16); | ||
|
||
let mut pins = hal::Pins::new(peripherals.PORT); | ||
|
||
let gclk = clocks.gclk0(); | ||
|
||
let mut pwm0 = Tcc0Pwm::new( | ||
&clocks.tcc0_tcc1(&gclk).unwrap(), | ||
1.khz(), | ||
peripherals.TCC0, | ||
TCC0Pinout::Pa23(pins.d13.into_function_g(&mut pins.port)), | ||
&mut peripherals.MCLK, | ||
); | ||
let max_duty = pwm0.get_max_duty(); | ||
|
||
loop { | ||
for j in 0..255 { | ||
pwm0.set_duty( | ||
Channel::_3, | ||
sine_ease_in(j as f32, 0.0, max_duty as f32, 255.0) as u32, | ||
); | ||
delay.delay_ms(10u16); | ||
} | ||
for j in (0..255).rev() { | ||
pwm0.set_duty( | ||
Channel::_3, | ||
sine_ease_in(j as f32, 0.0, max_duty as f32, 255.0) as u32, | ||
); | ||
delay.delay_ms(10u16); | ||
} | ||
} | ||
} | ||
|
||
#[inline] | ||
// current step, where oputput starts, where output ends, last step | ||
fn sine_ease_in(t: f32, b: f32, c: f32, d: f32) -> f32 { | ||
-c * (t / d * FRAC_PI_2).cos() + c + b | ||
} |