forked from esp-rs/esp-hal-community
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
hello_rgb
example to the esp-hal-smartled
package
- Loading branch information
1 parent
c02f5ee
commit edb742a
Showing
4 changed files
with
134 additions
and
10 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,16 @@ | ||
[target.'cfg(target_arch = "riscv32")'] | ||
rustflags = [ | ||
"-C", "link-arg=-Tlinkall.x", | ||
"-C", "force-frame-pointers", | ||
] | ||
|
||
[target.'cfg(target_arch = "xtensa")'] | ||
rustflags = [ | ||
# GNU LD | ||
"-C", "link-arg=-Wl,-Tlinkall.x", | ||
"-C", "link-arg=-nostartfiles", | ||
|
||
# LLD | ||
# "-C", "link-arg=-Tlinkall.x", | ||
# "-C", "linker=rust-lld", | ||
] |
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,96 @@ | ||
//! RGB LED Demo | ||
//! | ||
//! This example drives an SK68XX RGB LED, which is connected to a pin on the | ||
//! official DevKits. | ||
//! | ||
//! The demo will leverage the [`smart_leds`](https://crates.io/crates/smart-leds) | ||
//! crate functionality to circle through the HSV hue color space (with | ||
//! saturation and value both at 255). Additionally, we apply a gamma correction | ||
//! and limit the brightness to 10 (out of 255). | ||
//! | ||
//! The following wiring is assumed for ESP32: | ||
//! - LED => GPIO33 | ||
//! The following wiring is assumed for ESP32C3: | ||
//! - LED => GPIO8 | ||
//! The following wiring is assumed for ESP32C6, ESP32H2: | ||
//! - LED => GPIO8 | ||
//! The following wiring is assumed for ESP32S2: | ||
//! - LED => GPIO18 | ||
//! The following wiring is assumed for ESP32S3: | ||
//! - LED => GPIO48 | ||
|
||
#![no_std] | ||
#![no_main] | ||
|
||
use esp_backtrace as _; | ||
use esp_hal::{delay::Delay, gpio::Io, prelude::*, rmt::Rmt}; | ||
use esp_hal_smartled::{smartLedBuffer, SmartLedsAdapter}; | ||
use smart_leds::{ | ||
brightness, gamma, | ||
hsv::{hsv2rgb, Hsv}, | ||
SmartLedsWrite, | ||
}; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let peripherals = esp_hal::init(esp_hal::Config::default()); | ||
|
||
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); | ||
|
||
// Each devkit uses a unique GPIO for the RGB LED, so in order to support | ||
// all chips we must unfortunately use `#[cfg]`s: | ||
cfg_if::cfg_if! { | ||
if #[cfg(feature = "esp32")] { | ||
let led_pin = io.pins.gpio33; | ||
} else if #[cfg(feature = "esp32c3")] { | ||
let led_pin = io.pins.gpio8; | ||
} else if #[cfg(any(feature = "esp32c6", feature = "esp32h2"))] { | ||
let led_pin = io.pins.gpio8; | ||
} else if #[cfg(feature = "esp32s2")] { | ||
let led_pin = io.pins.gpio18; | ||
} else if #[cfg(feature = "esp32s3")] { | ||
let led_pin = io.pins.gpio48; | ||
} | ||
} | ||
|
||
// Configure RMT peripheral globally | ||
cfg_if::cfg_if! { | ||
if #[cfg(feature = "esp32h2")] { | ||
let freq = 32.MHz(); | ||
} else { | ||
let freq = 80.MHz(); | ||
} | ||
} | ||
|
||
let rmt = Rmt::new(peripherals.RMT, freq).unwrap(); | ||
|
||
// We use one of the RMT channels to instantiate a `SmartLedsAdapter` which can | ||
// be used directly with all `smart_led` implementations | ||
let rmt_buffer = smartLedBuffer!(1); | ||
let mut led = SmartLedsAdapter::new(rmt.channel0, led_pin, rmt_buffer); | ||
|
||
let delay = Delay::new(); | ||
|
||
let mut color = Hsv { | ||
hue: 0, | ||
sat: 255, | ||
val: 255, | ||
}; | ||
let mut data; | ||
|
||
loop { | ||
// Iterate over the rainbow! | ||
for hue in 0..=255 { | ||
color.hue = hue; | ||
// Convert from the HSV color space (where we can easily transition from one | ||
// color to the other) to the RGB color space that we can then send to the LED | ||
data = [hsv2rgb(color)]; | ||
// When sending to the LED, we do a gamma correction first (see smart_leds | ||
// documentation for details) and then limit the brightness to 10 out of 255 so | ||
// that the output it's not too bright. | ||
led.write(brightness(gamma(data.iter().cloned()), 10)) | ||
.unwrap(); | ||
delay.delay_millis(20); | ||
} | ||
} | ||
} |