diff --git a/arduino-hal/Cargo.toml b/arduino-hal/Cargo.toml index e352d6ac2b..6ba3f6df75 100644 --- a/arduino-hal/Cargo.toml +++ b/arduino-hal/Cargo.toml @@ -27,9 +27,13 @@ nano168 = ["mcu-atmega", "atmega-hal/atmega168", "atmega-hal/enable-extra-adc", [dependencies] cfg-if = "1" -embedded-hal = "0.2.3" +embedded-hal = "1.0.0-rc.3" ufmt = "0.2.0" +[dependencies.embedded-hal-v0] +version = "0.2.3" +package = "embedded-hal" + [dependencies.void] version = "1.0.2" default-features = false diff --git a/arduino-hal/src/delay.rs b/arduino-hal/src/delay.rs index 8a05ca01ac..f1b590724f 100644 --- a/arduino-hal/src/delay.rs +++ b/arduino-hal/src/delay.rs @@ -1,4 +1,4 @@ -use embedded_hal::blocking::delay::{DelayMs, DelayUs}; +use embedded_hal_v0::blocking::delay::{DelayMs, DelayUs}; /// Delay type for `embedded-hal` compatibility. /// diff --git a/avr-hal-generic/Cargo.toml b/avr-hal-generic/Cargo.toml index 784a349de9..cfbcab19d0 100644 --- a/avr-hal-generic/Cargo.toml +++ b/avr-hal-generic/Cargo.toml @@ -11,9 +11,11 @@ ufmt = "0.2.0" paste = "1.0.0" avr-device = "0.5.3" embedded-storage = "0.2" +embedded-hal = "1.0.0-rc.3" -[dependencies.embedded-hal] +[dependencies.embedded-hal-v0] version = "0.2.3" +package = "embedded-hal" features = ["unproven"] [dependencies.void] diff --git a/avr-hal-generic/src/delay.rs b/avr-hal-generic/src/delay.rs index 012ead742e..7a480ec384 100644 --- a/avr-hal-generic/src/delay.rs +++ b/avr-hal-generic/src/delay.rs @@ -1,7 +1,8 @@ //! Delay implementations use core::marker; -use hal::blocking::delay; +use hal::blocking::delay as delay_v0; +use embedded_hal::delay::DelayNs; #[cfg(all(target_arch = "avr", avr_hal_asm_macro))] use core::arch::asm; @@ -76,7 +77,7 @@ cfg_if::cfg_if! { } // Clock-Specific Delay Implementations ----------------------------------- {{{ -impl delay::DelayUs for Delay { +impl delay_v0::DelayUs for Delay { fn delay_us(&mut self, mut us: u16) { // for the 24 crate::clock::MHz clock for the aventurous ones, trying to overclock @@ -99,7 +100,7 @@ impl delay::DelayUs for Delay { } } -impl delay::DelayUs for Delay { +impl delay_v0::DelayUs for Delay { fn delay_us(&mut self, mut us: u16) { // for the 20 crate::clock::MHz clock on rare Arduino boards @@ -133,7 +134,7 @@ impl delay::DelayUs for Delay { } } -impl delay::DelayUs for Delay { +impl delay_v0::DelayUs for Delay { fn delay_us(&mut self, mut us: u16) { // for the 16 crate::clock::MHz clock on most Arduino boards @@ -157,7 +158,7 @@ impl delay::DelayUs for Delay { } } -impl delay::DelayUs for Delay { +impl delay_v0::DelayUs for Delay { fn delay_us(&mut self, mut us: u16) { // for the 12 crate::clock::MHz clock if somebody is working with USB @@ -181,7 +182,7 @@ impl delay::DelayUs for Delay { } } -impl delay::DelayUs for Delay { +impl delay_v0::DelayUs for Delay { fn delay_us(&mut self, mut us: u16) { // for the 8 crate::clock::MHz internal clock @@ -205,7 +206,7 @@ impl delay::DelayUs for Delay { } } -impl delay::DelayUs for Delay { +impl delay_v0::DelayUs for Delay { fn delay_us(&mut self, mut us: u16) { // for the 1 crate::clock::MHz internal clock (default settings for common Atmega microcontrollers) @@ -219,9 +220,9 @@ impl delay::DelayUs for Delay { // compensate for the time taken by the preceeding and next commands (about 22 cycles) us -= 22; // = 2 cycles - // the following loop takes 4 microseconds (4 cycles) - // per iteration, so execute it us/4 times - // us is at least 4, divided by 4 gives us 1 (no zero delay bug) + // the following loop takes 4 microseconds (4 cycles) + // per iteration, so execute it us/4 times + // us is at least 4, divided by 4 gives us 1 (no zero delay bug) us >>= 2; // us div 4, = 4 cycles busy_loop(us); @@ -230,18 +231,18 @@ impl delay::DelayUs for Delay { // ------------------------------------------------------------------------ }}} -impl delay::DelayUs for Delay -where - Delay: delay::DelayUs, +impl delay_v0::DelayUs for Delay + where + Delay: delay_v0::DelayUs, { fn delay_us(&mut self, us: u8) { - delay::DelayUs::::delay_us(self, us as u16); + delay_v0::DelayUs::::delay_us(self, us as u16); } } -impl delay::DelayUs for Delay -where - Delay: delay::DelayUs, +impl delay_v0::DelayUs for Delay + where + Delay: delay_v0::DelayUs, { fn delay_us(&mut self, us: u32) { // TODO: Somehow fix the overhead induced by this loop @@ -252,27 +253,51 @@ where let iters = us >> 12; let mut i = 0; while i < iters { - delay::DelayUs::::delay_us(self, 0xfff); + delay_v0::DelayUs::::delay_us(self, 0xfff); i += 1; } - delay::DelayUs::::delay_us(self, (us & 0xfff) as u16); + delay_v0::DelayUs::::delay_us(self, (us & 0xfff) as u16); } } -impl delay::DelayMs for Delay -where - Delay: delay::DelayUs, +impl delay_v0::DelayMs for Delay + where + Delay: delay_v0::DelayUs, { fn delay_ms(&mut self, ms: u16) { - delay::DelayUs::::delay_us(self, ms as u32 * 1000); + delay_v0::DelayUs::::delay_us(self, ms as u32 * 1000); } } -impl delay::DelayMs for Delay -where - Delay: delay::DelayMs, +impl delay_v0::DelayMs for Delay + where + Delay: delay_v0::DelayMs, { fn delay_ms(&mut self, ms: u8) { - delay::DelayMs::::delay_ms(self, ms as u16); + delay_v0::DelayMs::::delay_ms(self, ms as u16); + } +} + +impl DelayNs for Delay + where + Delay: delay_v0::DelayUs, { + fn delay_ns(&mut self, ns: u32) { + // quick-win to get an initial implementation. + // note that the trait does not guarantee nanosecond-accuracy. + + let mut us = ns / 1000; + + // ensure that we wait _at least_ as long as specified. + // (we truncate the integer, so if we don't do this we'd wait 1us instead of 2us when specifying 1999ns). + let diff = ns - (us * 1000); + if diff > 0 { + us += 1; + } + + delay_v0::DelayUs::::delay_us(self, us); + } + + fn delay_us(&mut self, us: u32) { + delay_v0::DelayUs::::delay_us(self, us); } } diff --git a/avr-hal-generic/src/lib.rs b/avr-hal-generic/src/lib.rs index 2a6644f700..751af60a18 100644 --- a/avr-hal-generic/src/lib.rs +++ b/avr-hal-generic/src/lib.rs @@ -2,7 +2,7 @@ #![cfg_attr(avr_hal_asm_macro, feature(asm_experimental_arch))] #![cfg_attr(not(avr_hal_asm_macro), feature(llvm_asm))] -pub extern crate embedded_hal as hal; +pub extern crate embedded_hal_v0 as hal; #[doc(hidden)] pub extern crate avr_device; diff --git a/avr-hal-generic/src/port.rs b/avr-hal-generic/src/port.rs index 1786703323..2de2083b02 100644 --- a/avr-hal-generic/src/port.rs +++ b/avr-hal-generic/src/port.rs @@ -3,7 +3,8 @@ //! Please take a look at the documentation for [`Pin`] for a detailed explanation. use core::marker::PhantomData; -use embedded_hal::digital::v2::{InputPin, OutputPin}; +use embedded_hal::digital::{ErrorType, InputPin, OutputPin, StatefulOutputPin}; +use embedded_hal_v0::digital::v2::{InputPin as InputPinV0, OutputPin as OutputPinV0}; pub trait PinMode: crate::Sealed {} /// GPIO pin modes @@ -318,8 +319,8 @@ impl Pin { } } -// Implements OutputPin from embedded-hal to make sure external libraries work -impl OutputPin for Pin { +// Implements OutputPinV0 from embedded-hal to make sure external libraries work +impl OutputPinV0 for Pin { type Error = core::convert::Infallible; fn set_high(&mut self) -> Result<(), Self::Error> { @@ -333,6 +334,30 @@ impl OutputPin for Pin { } } +impl ErrorType for Pin { type Error = core::convert::Infallible; } + +impl OutputPin for Pin { + fn set_low(&mut self) -> Result<(), Self::Error> { + self.set_low(); + Ok(()) + } + + fn set_high(&mut self) -> Result<(), Self::Error> { + self.set_high(); + Ok(()) + } +} + +impl StatefulOutputPin for Pin { + fn is_set_high(&mut self) -> Result { + Ok((*self).is_set_high()) + } + + fn is_set_low(&mut self) -> Result { + Ok((*self).is_set_low()) + } +} + /// # Digital Output Open Drain impl Pin { /// Set the pin high (Input without PullUp so it is floating) @@ -364,8 +389,8 @@ impl Pin { } } -// Implements OutputPin from embedded-hal to make sure external libraries work -impl OutputPin for Pin { +// Implements OutputPinV0 from embedded-hal to make sure external libraries work +impl OutputPinV0 for Pin { type Error = core::convert::Infallible; fn set_high(&mut self) -> Result<(), Self::Error> { @@ -379,8 +404,30 @@ impl OutputPin for Pin { } } -// Implements InputPin from embedded-hal to make sure external libraries work -impl InputPin for Pin { +impl OutputPin for Pin { + fn set_low(&mut self) -> Result<(), Self::Error> { + self.set_low(); + Ok(()) + } + + fn set_high(&mut self) -> Result<(), Self::Error> { + self.set_high(); + Ok(()) + } +} + +impl StatefulOutputPin for Pin { + fn is_set_high(&mut self) -> Result { + Ok((*self).is_high()) + } + + fn is_set_low(&mut self) -> Result { + Ok((*self).is_low()) + } +} + +// Implements InputPinV0 from embedded-hal to make sure external libraries work +impl InputPinV0 for Pin { type Error = core::convert::Infallible; fn is_high(&self) -> Result { @@ -392,8 +439,20 @@ impl InputPin for Pin { } } -// Implements InputPin from embedded-hal to make sure external libraries work -impl InputPin for Pin, PIN> { +impl ErrorType for Pin { type Error = core::convert::Infallible; } + +impl InputPin for Pin { + fn is_high(&mut self) -> Result { + Ok((*self).is_high()) + } + + fn is_low(&mut self) -> Result { + Ok((*self).is_low()) + } +} + +// Implements InputPinV0 from embedded-hal to make sure external libraries work +impl InputPinV0 for Pin, PIN> { type Error = core::convert::Infallible; fn is_high(&self) -> Result { @@ -405,6 +464,18 @@ impl InputPin for Pin, P } } +impl ErrorType for Pin, PIN> { type Error = core::convert::Infallible; } + +impl InputPin for Pin, PIN> { + fn is_high(&mut self) -> Result { + Ok((*self).is_high()) + } + + fn is_low(&mut self) -> Result { + Ok((*self).is_low()) + } +} + /// # Digital Input impl Pin, PIN> { /// Check whether the pin is driven high. diff --git a/avr-hal-generic/src/simple_pwm.rs b/avr-hal-generic/src/simple_pwm.rs index cc9df78d9a..3b6d7985e1 100644 --- a/avr-hal-generic/src/simple_pwm.rs +++ b/avr-hal-generic/src/simple_pwm.rs @@ -1,6 +1,8 @@ //! PWM Implementation use core::marker::PhantomData; +use embedded_hal::pwm; +use embedded_hal::pwm::{ErrorKind, ErrorType, SetDutyCycle}; use crate::port::mode; use crate::port::Pin; @@ -82,6 +84,35 @@ impl> Pin, PIN> { } } +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +pub enum PwmError { + /// `embedded-hal` supports duty cycles up to `u16`, however `avr` devices only support up to `u8`. + /// Passing a duty cycle larger than [`u8::MAX`] will result in this error. + DutyCycleTooLarge, +} + +impl pwm::Error for PwmError { + fn kind(&self) -> ErrorKind { + ErrorKind::Other + } +} + +impl> ErrorType for Pin, PIN> { type Error = PwmError; } + +impl> SetDutyCycle for Pin, PIN> { + fn max_duty_cycle(&self) -> u16 { + self.get_max_duty() as u16 + } + + fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> { + if duty > u8::MAX as u16 { + return Err(PwmError::DutyCycleTooLarge); + } + self.set_duty(duty as u8); + Ok(()) + } +} + #[macro_export] macro_rules! impl_simple_pwm { ( diff --git a/avr-hal-generic/src/spi.rs b/avr-hal-generic/src/spi.rs index 97c172d9a9..74a3ab2279 100644 --- a/avr-hal-generic/src/spi.rs +++ b/avr-hal-generic/src/spi.rs @@ -1,7 +1,7 @@ //! SPI Implementation use crate::port; use core::marker::PhantomData; -use embedded_hal::spi; +use embedded_hal_v0::spi; /// Oscillator Clock Frequency division options. /// diff --git a/avr-hal-generic/src/usart.rs b/avr-hal-generic/src/usart.rs index a62e5c7871..97f6c3926e 100644 --- a/avr-hal-generic/src/usart.rs +++ b/avr-hal-generic/src/usart.rs @@ -375,7 +375,7 @@ impl, RX, TX, CLOCK> hal::serial::Read /// Created by calling [`Usart::split`]. Splitting a peripheral into reader and writer allows /// concurrently receiving and transmitting data from different contexts. /// -/// The writer half most notably implements [`embedded_hal::serial::Write`] and [`ufmt::uWrite`] +/// The writer half most notably implements [`embedded_hal_v0::serial::Write`] and [`ufmt::uWrite`] /// for transmitting data. pub struct UsartWriter, RX, TX, CLOCK> { p: USART, @@ -390,7 +390,7 @@ pub struct UsartWriter, RX, TX, CLOCK> { /// Created by calling [`Usart::split`]. Splitting a peripheral into reader and writer allows /// concurrently receiving and transmitting data from different contexts. /// -/// The reader half most notably implements [`embedded_hal::serial::Read`] for receiving data. +/// The reader half most notably implements [`embedded_hal_v0::serial::Read`] for receiving data. pub struct UsartReader, RX, TX, CLOCK> { p: USART, rx: RX, diff --git a/examples/arduino-diecimila/Cargo.toml b/examples/arduino-diecimila/Cargo.toml index 2c75d4d38a..140cab04d6 100644 --- a/examples/arduino-diecimila/Cargo.toml +++ b/examples/arduino-diecimila/Cargo.toml @@ -9,7 +9,11 @@ publish = false panic-halt = "0.2.0" ufmt = "0.2.0" nb = "1.1.0" -embedded-hal = "0.2.3" +embedded-hal = "1.0.0-rc.3" + +[dependencies.embedded-hal-v0] +version = "0.2.3" +package = "embedded-hal" [dependencies.arduino-hal] path = "../../arduino-hal/" diff --git a/examples/arduino-diecimila/src/bin/diecimila-spi-feedback.rs b/examples/arduino-diecimila/src/bin/diecimila-spi-feedback.rs index 3355684c46..78bb5d6dbe 100644 --- a/examples/arduino-diecimila/src/bin/diecimila-spi-feedback.rs +++ b/examples/arduino-diecimila/src/bin/diecimila-spi-feedback.rs @@ -17,7 +17,7 @@ use arduino_hal::prelude::*; use arduino_hal::spi; -use embedded_hal::spi::FullDuplex; +use embedded_hal_v0::spi::FullDuplex; use panic_halt as _; #[arduino_hal::entry] diff --git a/examples/arduino-diecimila/src/bin/diecimila-usart.rs b/examples/arduino-diecimila/src/bin/diecimila-usart.rs index aecdd2b8a1..13f68a782f 100644 --- a/examples/arduino-diecimila/src/bin/diecimila-usart.rs +++ b/examples/arduino-diecimila/src/bin/diecimila-usart.rs @@ -4,7 +4,7 @@ use arduino_hal::prelude::*; use panic_halt as _; -use embedded_hal::serial::Read; +use embedded_hal_v0::serial::Read; #[arduino_hal::entry] fn main() -> ! { diff --git a/examples/arduino-leonardo/Cargo.toml b/examples/arduino-leonardo/Cargo.toml index 7b1413de9e..c69981b0b0 100644 --- a/examples/arduino-leonardo/Cargo.toml +++ b/examples/arduino-leonardo/Cargo.toml @@ -9,7 +9,11 @@ publish = false panic-halt = "0.2.0" ufmt = "0.2.0" nb = "1.1.0" -embedded-hal = "0.2.3" +embedded-hal = "1.0.0-rc.3" + +[dependencies.embedded-hal-v0] +version = "0.2.3" +package = "embedded-hal" [dependencies.arduino-hal] path = "../../arduino-hal/" diff --git a/examples/arduino-leonardo/src/bin/leonardo-spi-feedback.rs b/examples/arduino-leonardo/src/bin/leonardo-spi-feedback.rs index 19b332bdd0..96667833e9 100644 --- a/examples/arduino-leonardo/src/bin/leonardo-spi-feedback.rs +++ b/examples/arduino-leonardo/src/bin/leonardo-spi-feedback.rs @@ -17,7 +17,7 @@ use arduino_hal::prelude::*; use arduino_hal::spi; -use embedded_hal::spi::FullDuplex; +use embedded_hal_v0::spi::FullDuplex; use panic_halt as _; #[arduino_hal::entry] diff --git a/examples/arduino-leonardo/src/bin/leonardo-usart.rs b/examples/arduino-leonardo/src/bin/leonardo-usart.rs index 9add152c06..f99f02337f 100644 --- a/examples/arduino-leonardo/src/bin/leonardo-usart.rs +++ b/examples/arduino-leonardo/src/bin/leonardo-usart.rs @@ -4,7 +4,7 @@ use arduino_hal::prelude::*; use panic_halt as _; -use embedded_hal::serial::Read; +use embedded_hal_v0::serial::Read; #[arduino_hal::entry] fn main() -> ! { diff --git a/examples/arduino-mega1280/Cargo.toml b/examples/arduino-mega1280/Cargo.toml index accf5a3fa8..75064b1595 100644 --- a/examples/arduino-mega1280/Cargo.toml +++ b/examples/arduino-mega1280/Cargo.toml @@ -9,7 +9,11 @@ publish = false panic-halt = "0.2.0" ufmt = "0.2.0" nb = "1.1.0" -embedded-hal = "0.2.3" +embedded-hal = "1.0.0-rc.3" + +[dependencies.embedded-hal-v0] +version = "0.2.3" +package = "embedded-hal" [dependencies.arduino-hal] path = "../../arduino-hal/" diff --git a/examples/arduino-mega1280/src/bin/mega1280-usart.rs b/examples/arduino-mega1280/src/bin/mega1280-usart.rs index 9add152c06..f99f02337f 100644 --- a/examples/arduino-mega1280/src/bin/mega1280-usart.rs +++ b/examples/arduino-mega1280/src/bin/mega1280-usart.rs @@ -4,7 +4,7 @@ use arduino_hal::prelude::*; use panic_halt as _; -use embedded_hal::serial::Read; +use embedded_hal_v0::serial::Read; #[arduino_hal::entry] fn main() -> ! { diff --git a/examples/arduino-mega1280/src/bin/mega1280spi-feedback.rs b/examples/arduino-mega1280/src/bin/mega1280spi-feedback.rs index 4c22854e65..0f82a07857 100644 --- a/examples/arduino-mega1280/src/bin/mega1280spi-feedback.rs +++ b/examples/arduino-mega1280/src/bin/mega1280spi-feedback.rs @@ -17,7 +17,7 @@ use arduino_hal::prelude::*; use arduino_hal::spi; -use embedded_hal::spi::FullDuplex; +use embedded_hal_v0::spi::FullDuplex; use panic_halt as _; #[arduino_hal::entry] diff --git a/examples/arduino-mega2560/Cargo.toml b/examples/arduino-mega2560/Cargo.toml index c004a3e88b..223b769eb4 100644 --- a/examples/arduino-mega2560/Cargo.toml +++ b/examples/arduino-mega2560/Cargo.toml @@ -9,7 +9,11 @@ publish = false panic-halt = "0.2.0" ufmt = "0.2.0" nb = "1.1.0" -embedded-hal = "0.2.3" +embedded-hal = "1.0.0-rc.3" + +[dependencies.embedded-hal-v0] +version = "0.2.3" +package = "embedded-hal" [dependencies.arduino-hal] path = "../../arduino-hal/" diff --git a/examples/arduino-mega2560/src/bin/mega2560-spi-feedback.rs b/examples/arduino-mega2560/src/bin/mega2560-spi-feedback.rs index 197adab9cd..fe73ba714b 100644 --- a/examples/arduino-mega2560/src/bin/mega2560-spi-feedback.rs +++ b/examples/arduino-mega2560/src/bin/mega2560-spi-feedback.rs @@ -17,7 +17,7 @@ use arduino_hal::prelude::*; use arduino_hal::spi; -use embedded_hal::spi::FullDuplex; +use embedded_hal_v0::spi::FullDuplex; use panic_halt as _; #[arduino_hal::entry] diff --git a/examples/arduino-mega2560/src/bin/mega2560-usart.rs b/examples/arduino-mega2560/src/bin/mega2560-usart.rs index 9add152c06..f99f02337f 100644 --- a/examples/arduino-mega2560/src/bin/mega2560-usart.rs +++ b/examples/arduino-mega2560/src/bin/mega2560-usart.rs @@ -4,7 +4,7 @@ use arduino_hal::prelude::*; use panic_halt as _; -use embedded_hal::serial::Read; +use embedded_hal_v0::serial::Read; #[arduino_hal::entry] fn main() -> ! { diff --git a/examples/arduino-nano/Cargo.toml b/examples/arduino-nano/Cargo.toml index 73e1ea1d43..dbc11c404a 100644 --- a/examples/arduino-nano/Cargo.toml +++ b/examples/arduino-nano/Cargo.toml @@ -9,7 +9,11 @@ publish = false panic-halt = "0.2.0" ufmt = "0.2.0" nb = "1.1.0" -embedded-hal = "0.2.3" +embedded-hal = "1.0.0-rc.3" + +[dependencies.embedded-hal-v0] +version = "0.2.3" +package = "embedded-hal" [dependencies.arduino-hal] path = "../../arduino-hal/" diff --git a/examples/arduino-uno/Cargo.toml b/examples/arduino-uno/Cargo.toml index b1d899833f..ad435eee7f 100644 --- a/examples/arduino-uno/Cargo.toml +++ b/examples/arduino-uno/Cargo.toml @@ -9,11 +9,15 @@ publish = false panic-halt = "0.2.0" ufmt = "0.2.0" nb = "1.1.0" -embedded-hal = "0.2.3" +embedded-hal = "1.0.0-rc.3" pwm-pca9685 = "0.3.1" infrared = "0.14.1" embedded-storage = "0.2" +[dependencies.embedded-hal-v0] +version = "0.2.3" +package = "embedded-hal" + [dependencies.arduino-hal] path = "../../arduino-hal/" features = ["arduino-uno"] diff --git a/examples/arduino-uno/src/bin/uno-blink-embedded-hal.rs b/examples/arduino-uno/src/bin/uno-blink-embedded-hal.rs new file mode 100644 index 0000000000..61b1e8cf5a --- /dev/null +++ b/examples/arduino-uno/src/bin/uno-blink-embedded-hal.rs @@ -0,0 +1,39 @@ +/*! + * Blink the builtin LED - the "Hello World" of embedded programming, but with a twist: + * the blink 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::digital::OutputPin; + +use panic_halt as _; + +fn blink(led: &mut impl OutputPin, delay: &mut impl DelayNs) -> ! { + loop { + // TODO: once embedded-hal v1.0.0 is released switch to `StatefulOutputPin` & use `toggle` (not part of RC 3) + led.set_low().unwrap(); + delay.delay_ms(100); + led.set_high().unwrap(); + delay.delay_ms(100); + led.set_low().unwrap(); + delay.delay_ms(100); + led.set_high().unwrap(); + delay.delay_ms(800); + } +} + +#[arduino_hal::entry] +fn main() -> ! { + let dp = arduino_hal::Peripherals::take().unwrap(); + let pins = arduino_hal::pins!(dp); + + // Digital pin 13 is also connected to an onboard LED marked "L" + let mut led = pins.d13.into_output(); + led.set_high(); + + let mut delay = arduino_hal::Delay::new(); + + blink(&mut led, &mut delay); +} diff --git a/examples/arduino-uno/src/bin/uno-millis.rs b/examples/arduino-uno/src/bin/uno-millis.rs index 2a36c26f9a..a7be268f89 100644 --- a/examples/arduino-uno/src/bin/uno-millis.rs +++ b/examples/arduino-uno/src/bin/uno-millis.rs @@ -16,7 +16,7 @@ use arduino_hal::prelude::*; use core::cell; use panic_halt as _; -use embedded_hal::serial::Read; +use embedded_hal_v0::serial::Read; // Possible Values: // diff --git a/examples/arduino-uno/src/bin/uno-simple-pwm-embedded-hal.rs b/examples/arduino-uno/src/bin/uno-simple-pwm-embedded-hal.rs new file mode 100644 index 0000000000..991ea358d4 --- /dev/null +++ b/examples/arduino-uno/src/bin/uno-simple-pwm-embedded-hal.rs @@ -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); +} diff --git a/examples/arduino-uno/src/bin/uno-spi-feedback.rs b/examples/arduino-uno/src/bin/uno-spi-feedback.rs index f098c2e907..14c835741e 100644 --- a/examples/arduino-uno/src/bin/uno-spi-feedback.rs +++ b/examples/arduino-uno/src/bin/uno-spi-feedback.rs @@ -16,7 +16,7 @@ use arduino_hal::prelude::*; use arduino_hal::spi; -use embedded_hal::spi::FullDuplex; +use embedded_hal_v0::spi::FullDuplex; use panic_halt as _; #[arduino_hal::entry] diff --git a/examples/arduino-uno/src/bin/uno-usart.rs b/examples/arduino-uno/src/bin/uno-usart.rs index c29093c586..792148d1ab 100644 --- a/examples/arduino-uno/src/bin/uno-usart.rs +++ b/examples/arduino-uno/src/bin/uno-usart.rs @@ -7,7 +7,7 @@ use arduino_hal::prelude::*; use panic_halt as _; -use embedded_hal::serial::Read; +use embedded_hal_v0::serial::Read; #[arduino_hal::entry] fn main() -> ! { diff --git a/examples/nano168/Cargo.toml b/examples/nano168/Cargo.toml index 280f8f34fe..9b50fbf8f2 100644 --- a/examples/nano168/Cargo.toml +++ b/examples/nano168/Cargo.toml @@ -10,7 +10,11 @@ publish = false panic-halt = "0.2.0" ufmt = "0.2.0" nb = "1.1.0" -embedded-hal = "0.2.3" +embedded-hal = "1.0.0-rc.3" + +[dependencies.embedded-hal-v0] +version = "0.2.3" +package = "embedded-hal" [dependencies.arduino-hal] path = "../../arduino-hal/" diff --git a/examples/nano168/src/bin/nano168-millis.rs b/examples/nano168/src/bin/nano168-millis.rs index d65b1115be..20f2230084 100644 --- a/examples/nano168/src/bin/nano168-millis.rs +++ b/examples/nano168/src/bin/nano168-millis.rs @@ -14,7 +14,7 @@ use arduino_hal::prelude::*; use core::cell; use panic_halt as _; -use embedded_hal::serial::Read; +use embedded_hal_v0::serial::Read; // Possible Values: // diff --git a/examples/nano168/src/bin/nano168-usart.rs b/examples/nano168/src/bin/nano168-usart.rs index 9add152c06..f99f02337f 100644 --- a/examples/nano168/src/bin/nano168-usart.rs +++ b/examples/nano168/src/bin/nano168-usart.rs @@ -4,7 +4,7 @@ use arduino_hal::prelude::*; use panic_halt as _; -use embedded_hal::serial::Read; +use embedded_hal_v0::serial::Read; #[arduino_hal::entry] fn main() -> ! { diff --git a/examples/sparkfun-promicro/Cargo.toml b/examples/sparkfun-promicro/Cargo.toml index a0003d03a4..ec66bcc38b 100644 --- a/examples/sparkfun-promicro/Cargo.toml +++ b/examples/sparkfun-promicro/Cargo.toml @@ -9,7 +9,11 @@ publish = false panic-halt = "0.2.0" ufmt = "0.2.0" nb = "1.1.0" -embedded-hal = "0.2.3" +embedded-hal = "1.0.0-rc.3" + +[dependencies.embedded-hal-v0] +version = "0.2.3" +package = "embedded-hal" [dependencies.arduino-hal] path = "../../arduino-hal/" diff --git a/examples/sparkfun-promicro/src/bin/promicro-spi-feedback.rs b/examples/sparkfun-promicro/src/bin/promicro-spi-feedback.rs index 690e12fa73..fbc3b3ac58 100644 --- a/examples/sparkfun-promicro/src/bin/promicro-spi-feedback.rs +++ b/examples/sparkfun-promicro/src/bin/promicro-spi-feedback.rs @@ -17,7 +17,7 @@ use arduino_hal::prelude::*; use arduino_hal::spi; -use embedded_hal::spi::FullDuplex; +use embedded_hal_v0::spi::FullDuplex; use panic_halt as _; #[arduino_hal::entry] diff --git a/examples/sparkfun-promicro/src/bin/promicro-usart.rs b/examples/sparkfun-promicro/src/bin/promicro-usart.rs index 9add152c06..f99f02337f 100644 --- a/examples/sparkfun-promicro/src/bin/promicro-usart.rs +++ b/examples/sparkfun-promicro/src/bin/promicro-usart.rs @@ -4,7 +4,7 @@ use arduino_hal::prelude::*; use panic_halt as _; -use embedded_hal::serial::Read; +use embedded_hal_v0::serial::Read; #[arduino_hal::entry] fn main() -> ! { diff --git a/examples/sparkfun-promini-5v/Cargo.toml b/examples/sparkfun-promini-5v/Cargo.toml index ebaf453031..36909cef1e 100644 --- a/examples/sparkfun-promini-5v/Cargo.toml +++ b/examples/sparkfun-promini-5v/Cargo.toml @@ -9,7 +9,11 @@ publish = false panic-halt = "0.2.0" ufmt = "0.2.0" nb = "1.1.0" -embedded-hal = "0.2.3" +embedded-hal = "1.0.0-rc.3" + +[dependencies.embedded-hal-v0] +version = "0.2.3" +package = "embedded-hal" [dependencies.arduino-hal] path = "../../arduino-hal/" diff --git a/examples/trinket-pro/Cargo.toml b/examples/trinket-pro/Cargo.toml index 4c160904a5..7175198072 100644 --- a/examples/trinket-pro/Cargo.toml +++ b/examples/trinket-pro/Cargo.toml @@ -7,7 +7,11 @@ publish = false [dependencies] panic-halt = "0.2.0" -embedded-hal = "0.2.3" +embedded-hal = "1.0.0-rc.3" + +[dependencies.embedded-hal-v0] +version = "0.2.3" +package = "embedded-hal" [dependencies.arduino-hal] path = "../../arduino-hal/" diff --git a/examples/trinket/Cargo.toml b/examples/trinket/Cargo.toml index 53cfdaccfb..168a6efb93 100644 --- a/examples/trinket/Cargo.toml +++ b/examples/trinket/Cargo.toml @@ -7,7 +7,11 @@ publish = false [dependencies] panic-halt = "0.2.0" -embedded-hal = "0.2.3" +embedded-hal = "1.0.0-rc.3" + +[dependencies.embedded-hal-v0] +version = "0.2.3" +package = "embedded-hal" [dependencies.arduino-hal] path = "../../arduino-hal/"