From a0548e42cd89cf18fe7094c2e2fc15a59e8be84f Mon Sep 17 00:00:00 2001 From: Rhovian Date: Sun, 5 May 2024 23:21:46 +0200 Subject: [PATCH 1/3] examples: Apply formatting changes --- examples/arduino-leonardo/src/bin/leonardo-i2cdetect.rs | 6 ++++-- examples/arduino-mega1280/src/bin/mega1280-i2cdetect.rs | 6 ++++-- examples/arduino-mega2560/src/bin/mega2560-i2cdetect.rs | 6 ++++-- examples/arduino-uno/src/bin/uno-16chan-servo-driver.rs | 3 ++- examples/arduino-uno/src/bin/uno-74hc595.rs | 8 ++++---- examples/arduino-uno/src/bin/uno-eeprom.rs | 4 ++-- examples/arduino-uno/src/bin/uno-ext-interrupt.rs | 8 ++++---- .../arduino-uno/src/bin/uno-simple-pwm-embedded-hal.rs | 2 +- examples/atmega2560/src/bin/atmega2560-blink.rs | 2 +- examples/atmega2560/src/bin/atmega2560-spi-feedback.rs | 2 +- examples/nano168/src/bin/nano168-i2cdetect.rs | 6 ++++-- examples/nano168/src/bin/nano168-millis.rs | 1 - examples/sparkfun-promicro/src/bin/promicro-i2cdetect.rs | 6 ++++-- 13 files changed, 35 insertions(+), 25 deletions(-) diff --git a/examples/arduino-leonardo/src/bin/leonardo-i2cdetect.rs b/examples/arduino-leonardo/src/bin/leonardo-i2cdetect.rs index 8a3496b472..e6af7f4f4b 100644 --- a/examples/arduino-leonardo/src/bin/leonardo-i2cdetect.rs +++ b/examples/arduino-leonardo/src/bin/leonardo-i2cdetect.rs @@ -18,9 +18,11 @@ fn main() -> ! { ); ufmt::uwriteln!(&mut serial, "Write direction test:\r").unwrap_infallible(); - i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Write).unwrap_infallible(); + i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Write) + .unwrap_infallible(); ufmt::uwriteln!(&mut serial, "\r\nRead direction test:\r").unwrap_infallible(); - i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Read).unwrap_infallible(); + i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Read) + .unwrap_infallible(); loop {} } diff --git a/examples/arduino-mega1280/src/bin/mega1280-i2cdetect.rs b/examples/arduino-mega1280/src/bin/mega1280-i2cdetect.rs index 676266c322..59946ede22 100644 --- a/examples/arduino-mega1280/src/bin/mega1280-i2cdetect.rs +++ b/examples/arduino-mega1280/src/bin/mega1280-i2cdetect.rs @@ -18,9 +18,11 @@ fn main() -> ! { ); ufmt::uwriteln!(&mut serial, "Write direction test:\r").unwrap_infallible(); - i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Write).unwrap_infallible(); + i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Write) + .unwrap_infallible(); ufmt::uwriteln!(&mut serial, "\r\nRead direction test:\r").unwrap_infallible(); - i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Read).unwrap_infallible(); + i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Read) + .unwrap_infallible(); loop {} } diff --git a/examples/arduino-mega2560/src/bin/mega2560-i2cdetect.rs b/examples/arduino-mega2560/src/bin/mega2560-i2cdetect.rs index 676266c322..59946ede22 100644 --- a/examples/arduino-mega2560/src/bin/mega2560-i2cdetect.rs +++ b/examples/arduino-mega2560/src/bin/mega2560-i2cdetect.rs @@ -18,9 +18,11 @@ fn main() -> ! { ); ufmt::uwriteln!(&mut serial, "Write direction test:\r").unwrap_infallible(); - i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Write).unwrap_infallible(); + i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Write) + .unwrap_infallible(); ufmt::uwriteln!(&mut serial, "\r\nRead direction test:\r").unwrap_infallible(); - i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Read).unwrap_infallible(); + i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Read) + .unwrap_infallible(); loop {} } diff --git a/examples/arduino-uno/src/bin/uno-16chan-servo-driver.rs b/examples/arduino-uno/src/bin/uno-16chan-servo-driver.rs index e44a7d921c..b30ebc7c73 100644 --- a/examples/arduino-uno/src/bin/uno-16chan-servo-driver.rs +++ b/examples/arduino-uno/src/bin/uno-16chan-servo-driver.rs @@ -57,7 +57,8 @@ fn main() -> ! { arduino_hal::delay_ms(500); pwm.set_channel_off(Channel::C0, current).unwrap(); - pwm.set_channel_off(Channel::C1, servo_min + (servo_max - current)).unwrap(); + pwm.set_channel_off(Channel::C1, servo_min + (servo_max - current)) + .unwrap(); if current == servo_max { factor -= 1; diff --git a/examples/arduino-uno/src/bin/uno-74hc595.rs b/examples/arduino-uno/src/bin/uno-74hc595.rs index 6cf09708b8..97a64c3d9d 100644 --- a/examples/arduino-uno/src/bin/uno-74hc595.rs +++ b/examples/arduino-uno/src/bin/uno-74hc595.rs @@ -1,11 +1,11 @@ /*! - * + * * Code sample to work with 74HC595 shift register, * wired as in https://lastminuteengineers.com/74hc595-shift-register-arduino-tutorial/ - * + * * Connections * ----------- - * + * * - D4: SER (14) * - D5: RCLK (12) * - D6: SRCLK (11) @@ -66,7 +66,7 @@ fn main() -> ! { for i in 0..8 { data |= 1 << i; - + update_shift_register(&mut data_pin, &mut latch_pin, &mut clock_pin, &data); arduino_hal::delay_ms(500); } diff --git a/examples/arduino-uno/src/bin/uno-eeprom.rs b/examples/arduino-uno/src/bin/uno-eeprom.rs index 2ddb2024dc..5f0e04960f 100644 --- a/examples/arduino-uno/src/bin/uno-eeprom.rs +++ b/examples/arduino-uno/src/bin/uno-eeprom.rs @@ -20,7 +20,7 @@ fn main() -> ! { // KNOWN ISSUE: Avoid to read entire eeprom capacity at once // See: https://github.com/Rahix/avr-hal/issues/410 let mut data = [0_u8; 10]; - + let _start_address: u16 = 0; if ep.read(0, &mut data).is_err() { @@ -33,7 +33,7 @@ fn main() -> ! { ufmt::uwriteln!(&mut serial, "{}", i).unwrap_infallible(); } - let _=ep.erase(0, arduino_hal::Eeprom::CAPACITY); + let _ = ep.erase(0, arduino_hal::Eeprom::CAPACITY); loop {} } diff --git a/examples/arduino-uno/src/bin/uno-ext-interrupt.rs b/examples/arduino-uno/src/bin/uno-ext-interrupt.rs index 84fddb5f6b..2279841dfa 100644 --- a/examples/arduino-uno/src/bin/uno-ext-interrupt.rs +++ b/examples/arduino-uno/src/bin/uno-ext-interrupt.rs @@ -1,18 +1,18 @@ /*! * Blinks a 4 leds in sequence on pins D3 - D6. When an external interrupt on D2/INT0 comes in * the sequence is reversed. - * - * Note: The use of the either crate requires the deactivation of std to use it in core. See the Cargo.toml + * + * Note: The use of the either crate requires the deactivation of std to use it in core. See the Cargo.toml * in this directory for details. */ #![no_std] #![no_main] #![feature(abi_avr_interrupt)] -use panic_halt as _; -use core::sync::atomic::{AtomicBool, Ordering}; use arduino_hal::port::{mode, Pin}; +use core::sync::atomic::{AtomicBool, Ordering}; use either::*; +use panic_halt as _; static REVERSED: AtomicBool = AtomicBool::new(false); 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 index 991ea358d4..e0e0e2a786 100644 --- a/examples/arduino-uno/src/bin/uno-simple-pwm-embedded-hal.rs +++ b/examples/arduino-uno/src/bin/uno-simple-pwm-embedded-hal.rs @@ -5,9 +5,9 @@ #![no_std] #![no_main] +use arduino_hal::simple_pwm::*; 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) -> ! { diff --git a/examples/atmega2560/src/bin/atmega2560-blink.rs b/examples/atmega2560/src/bin/atmega2560-blink.rs index bd10bd71e2..e0bd3301f1 100644 --- a/examples/atmega2560/src/bin/atmega2560-blink.rs +++ b/examples/atmega2560/src/bin/atmega2560-blink.rs @@ -1,8 +1,8 @@ #![no_std] #![no_main] -use panic_halt as _; use embedded_hal::delay::DelayNs; +use panic_halt as _; // Define core clock. This can be used in the rest of the project. type CoreClock = atmega_hal::clock::MHz16; diff --git a/examples/atmega2560/src/bin/atmega2560-spi-feedback.rs b/examples/atmega2560/src/bin/atmega2560-spi-feedback.rs index 37dfc6213a..b4c657eb2c 100644 --- a/examples/atmega2560/src/bin/atmega2560-spi-feedback.rs +++ b/examples/atmega2560/src/bin/atmega2560-spi-feedback.rs @@ -9,9 +9,9 @@ #![no_std] #![no_main] +use atmega_hal::delay::Delay; use atmega_hal::spi; use atmega_hal::usart::{Baudrate, Usart}; -use atmega_hal::delay::Delay; use embedded_hal::delay::DelayNs; use embedded_hal::spi::SpiBus; use panic_halt as _; diff --git a/examples/nano168/src/bin/nano168-i2cdetect.rs b/examples/nano168/src/bin/nano168-i2cdetect.rs index dc69ef4cf6..ae2bf7ef18 100644 --- a/examples/nano168/src/bin/nano168-i2cdetect.rs +++ b/examples/nano168/src/bin/nano168-i2cdetect.rs @@ -18,9 +18,11 @@ fn main() -> ! { ); ufmt::uwriteln!(&mut serial, "Write direction test:\r").unwrap_infallible(); - i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Write).unwrap_infallible(); + i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Write) + .unwrap_infallible(); ufmt::uwriteln!(&mut serial, "\r\nRead direction test:\r").unwrap_infallible(); - i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Read).unwrap_infallible(); + i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Read) + .unwrap_infallible(); loop {} } diff --git a/examples/nano168/src/bin/nano168-millis.rs b/examples/nano168/src/bin/nano168-millis.rs index a7b390d7c9..e957e62614 100644 --- a/examples/nano168/src/bin/nano168-millis.rs +++ b/examples/nano168/src/bin/nano168-millis.rs @@ -89,4 +89,3 @@ fn main() -> ! { ufmt::uwriteln!(&mut serial, "Got {} after {} ms!\r", b, time).unwrap_infallible(); } } - diff --git a/examples/sparkfun-promicro/src/bin/promicro-i2cdetect.rs b/examples/sparkfun-promicro/src/bin/promicro-i2cdetect.rs index 8a3496b472..e6af7f4f4b 100644 --- a/examples/sparkfun-promicro/src/bin/promicro-i2cdetect.rs +++ b/examples/sparkfun-promicro/src/bin/promicro-i2cdetect.rs @@ -18,9 +18,11 @@ fn main() -> ! { ); ufmt::uwriteln!(&mut serial, "Write direction test:\r").unwrap_infallible(); - i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Write).unwrap_infallible(); + i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Write) + .unwrap_infallible(); ufmt::uwriteln!(&mut serial, "\r\nRead direction test:\r").unwrap_infallible(); - i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Read).unwrap_infallible(); + i2c.i2cdetect(&mut serial, arduino_hal::i2c::Direction::Read) + .unwrap_infallible(); loop {} } From 77d7cc9a529c89894d04a043cfc2f4beaa3725d0 Mon Sep 17 00:00:00 2001 From: Rhovian Date: Sun, 5 May 2024 23:22:40 +0200 Subject: [PATCH 2/3] attiny-hal: Apply formatting --- mcu/attiny-hal/src/lib.rs | 16 ++++++++-------- mcu/attiny-hal/src/spi.rs | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/mcu/attiny-hal/src/lib.rs b/mcu/attiny-hal/src/lib.rs index ebf3d0750d..a8bf1541e0 100644 --- a/mcu/attiny-hal/src/lib.rs +++ b/mcu/attiny-hal/src/lib.rs @@ -32,6 +32,14 @@ compile_error!( " ); +/// Reexport of `attiny167` from `avr-device` +/// +#[cfg(feature = "attiny167")] +pub use avr_device::attiny167 as pac; +/// Reexport of `attiny2313` from `avr-device` +/// +#[cfg(feature = "attiny2313")] +pub use avr_device::attiny2313 as pac; /// Reexport of `attiny84` from `avr-device` /// #[cfg(feature = "attiny84")] @@ -44,14 +52,6 @@ pub use avr_device::attiny85 as pac; /// #[cfg(feature = "attiny88")] pub use avr_device::attiny88 as pac; -/// Reexport of `attiny167` from `avr-device` -/// -#[cfg(feature = "attiny167")] -pub use avr_device::attiny167 as pac; -/// Reexport of `attiny2313` from `avr-device` -/// -#[cfg(feature = "attiny2313")] -pub use avr_device::attiny2313 as pac; /// See [`avr_device::entry`](https://docs.rs/avr-device/latest/avr_device/attr.entry.html). #[cfg(feature = "rt")] diff --git a/mcu/attiny-hal/src/spi.rs b/mcu/attiny-hal/src/spi.rs index 0b279f5605..efd78464b9 100644 --- a/mcu/attiny-hal/src/spi.rs +++ b/mcu/attiny-hal/src/spi.rs @@ -29,7 +29,7 @@ pub type Spi = avr_hal_generic::spi::Spi< port::PA4, port::PA2, port::PA6, - >; +>; #[cfg(feature = "attiny167")] avr_hal_generic::impl_spi! { hal: crate::Attiny, From 64e16996e70f6ddce3045f0ce12c37fdf4123420 Mon Sep 17 00:00:00 2001 From: Rhovian Date: Sun, 5 May 2024 23:23:04 +0200 Subject: [PATCH 3/3] examples: mega2560: Add RGB-LED example --- .../src/bin/mega2560-rgb-led.rs | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 examples/arduino-mega2560/src/bin/mega2560-rgb-led.rs diff --git a/examples/arduino-mega2560/src/bin/mega2560-rgb-led.rs b/examples/arduino-mega2560/src/bin/mega2560-rgb-led.rs new file mode 100644 index 0000000000..6fd5e80756 --- /dev/null +++ b/examples/arduino-mega2560/src/bin/mega2560-rgb-led.rs @@ -0,0 +1,61 @@ +//! This example demonstrates how to fade an RGB LED connected to an Arduino board. +//! +//! Wiring: +//! - Connect the common cathode of the RGB LED to GND. +//! - Connect the red LED anode to pin D6 through a current-limiting resistor. +//! - Connect the green LED anode to pin D5 through a current-limiting resistor. +//! - Connect the blue LED anode to pin D3 through a current-limiting resistor. +//! +//! Note: The current-limiting resistor values depend on the specific RGB LED and the desired brightness. +//! Typically, a resistor value between 220Ω and 1kΩ is suitable. + +#![no_std] +#![no_main] + +use panic_halt as _; +use arduino_hal::simple_pwm::IntoPwmPin; +use arduino_hal::simple_pwm::Prescaler; +use arduino_hal::simple_pwm::{Timer3Pwm, Timer4Pwm}; + +#[arduino_hal::entry] +fn main() -> ! { + let dp = arduino_hal::Peripherals::take().unwrap(); + let pins = arduino_hal::pins!(dp); + + let timer0 = Timer4Pwm::new(dp.TC4, Prescaler::Prescale64); + let timer1 = Timer3Pwm::new(dp.TC3, Prescaler::Prescale64); + + let mut d6 = pins.d6.into_output().into_pwm(&timer0); + let mut d5 = pins.d5.into_output().into_pwm(&timer1); + let mut d3 = pins.d3.into_output().into_pwm(&timer1); + + let max_duty_d6 = d6.get_max_duty(); + let max_duty_d5 = d5.get_max_duty(); + let max_duty_d3 = d3.get_max_duty(); + + let delay_time = 10; + + d6.enable(); + d5.enable(); + d3.enable(); + + loop { + // Fade in/out red + for i in (0..=max_duty_d6).chain((0..=max_duty_d6 - 1).rev()) { + d6.set_duty(i); + arduino_hal::delay_ms(delay_time); + } + + // Fade in/out green + for i in (0..=max_duty_d5).chain((0..=max_duty_d5 - 1).rev()) { + d5.set_duty(i); + arduino_hal::delay_ms(delay_time); + } + + // Fade in/out blue + for i in (0..=max_duty_d3).chain((0..=max_duty_d3 - 1).rev()) { + d3.set_duty(i); + arduino_hal::delay_ms(delay_time); + } + } +} \ No newline at end of file