Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mega2560 example: rgb led #543

Merged
merged 3 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/arduino-leonardo/src/bin/leonardo-i2cdetect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
6 changes: 4 additions & 2 deletions examples/arduino-mega1280/src/bin/mega1280-i2cdetect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
6 changes: 4 additions & 2 deletions examples/arduino-mega2560/src/bin/mega2560-i2cdetect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
61 changes: 61 additions & 0 deletions examples/arduino-mega2560/src/bin/mega2560-rgb-led.rs
Original file line number Diff line number Diff line change
@@ -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]
Rahix marked this conversation as resolved.
Show resolved Hide resolved
Rahix marked this conversation as resolved.
Show resolved Hide resolved
#![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);
}
}
}
3 changes: 2 additions & 1 deletion examples/arduino-uno/src/bin/uno-16chan-servo-driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions examples/arduino-uno/src/bin/uno-74hc595.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/arduino-uno/src/bin/uno-eeprom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {}
}
8 changes: 4 additions & 4 deletions examples/arduino-uno/src/bin/uno-ext-interrupt.rs
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) -> ! {
Expand Down
2 changes: 1 addition & 1 deletion examples/atmega2560/src/bin/atmega2560-blink.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/atmega2560/src/bin/atmega2560-spi-feedback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _;
Expand Down
6 changes: 4 additions & 2 deletions examples/nano168/src/bin/nano168-i2cdetect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
1 change: 0 additions & 1 deletion examples/nano168/src/bin/nano168-millis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,3 @@ fn main() -> ! {
ufmt::uwriteln!(&mut serial, "Got {} after {} ms!\r", b, time).unwrap_infallible();
}
}

6 changes: 4 additions & 2 deletions examples/sparkfun-promicro/src/bin/promicro-i2cdetect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
16 changes: 8 additions & 8 deletions mcu/attiny-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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")]
Expand Down
2 changes: 1 addition & 1 deletion mcu/attiny-hal/src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down