Skip to content

Commit

Permalink
Add hello_rgb example to the esp-hal-smartled package
Browse files Browse the repository at this point in the history
  • Loading branch information
jessebraham committed Sep 16, 2024
1 parent c02f5ee commit edb742a
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/actions/check-package/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ runs:
shell: bash
run: |
cd ${{ inputs.package }}
cargo check --features=${{ inputs.soc }} --target=${{ inputs.target }}
cargo check --features=${{ inputs.soc }} --target=${{ inputs.target }} --examples
# Build the package (Xtensa):
- if: ${{ startsWith(inputs.target, 'xtensa') }}
shell: bash
run: |
cd ${{ inputs.package }}
cargo check -Zbuild-std=core --features=${{ inputs.soc }} --target=${{ inputs.target }}
cargo check -Zbuild-std=core --features=${{ inputs.soc }} --target=${{ inputs.target }} --examples
16 changes: 16 additions & 0 deletions esp-hal-smartled/.cargo/config.toml
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",
]
28 changes: 20 additions & 8 deletions esp-hal-smartled/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,36 @@ esp-hal = "0.20.0"
fugit = "0.3.7"
smart-leds-trait = "0.3.0"

[dev-dependencies]
cfg-if = "1.0.0"
esp-backtrace = { version = "0.14.1", features = [
"exception-handler",
"panic-handler",
"println",
] }
esp-println = "0.11.0"
smart-leds = "0.4.0"

[features]
## Implement `defmt::Format` on certain types.
defmt = ["dep:defmt", "esp-hal/defmt"]

#! ### Chip Support Feature Flags
## Target the ESP32.
esp32 = ["esp-hal/esp32"]
esp32 = ["esp-backtrace/esp32", "esp-hal/esp32", "esp-println/esp32"]
## Target the ESP32-C3.
esp32c3 = ["esp-hal/esp32c3"]
esp32c3 = ["esp-backtrace/esp32c3", "esp-hal/esp32c3", "esp-println/esp32c3"]
## Target the ESP32-C6.
esp32c6 = ["esp-hal/esp32c6"]
esp32c6 = ["esp-backtrace/esp32c6", "esp-hal/esp32c6", "esp-println/esp32c6"]
## Target the ESP32-H2.
esp32h2 = ["esp-hal/esp32h2"]
esp32h2 = ["esp-backtrace/esp32h2", "esp-hal/esp32h2", "esp-println/esp32h2"]
## Target the ESP32-S2.
esp32s2 = ["esp-hal/esp32s2"]
esp32s2 = ["esp-backtrace/esp32s2", "esp-hal/esp32s2", "esp-println/esp32s2"]
## Target the ESP32-S3.
esp32s3 = ["esp-hal/esp32s3"]
esp32s3 = ["esp-backtrace/esp32s3", "esp-hal/esp32s3", "esp-println/esp32s3"]

# TODO: Remove patch prior to next release
# TODO: Remove patches prior to next release
[patch.crates-io]
esp-hal = { git = "https://github.com/esp-rs/esp-hal", rev = "d44affc" }
esp-backtrace = { git = "https://github.com/esp-rs/esp-hal", rev = "d44affc" }
esp-hal = { git = "https://github.com/esp-rs/esp-hal", rev = "d44affc" }
esp-println = { git = "https://github.com/esp-rs/esp-hal", rev = "d44affc" }
96 changes: 96 additions & 0 deletions esp-hal-smartled/examples/hello_rgb.rs
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);
}
}
}

0 comments on commit edb742a

Please sign in to comment.