Skip to content

Commit edb742a

Browse files
committed
Add hello_rgb example to the esp-hal-smartled package
1 parent c02f5ee commit edb742a

File tree

4 files changed

+134
-10
lines changed

4 files changed

+134
-10
lines changed

.github/actions/check-package/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ runs:
2020
shell: bash
2121
run: |
2222
cd ${{ inputs.package }}
23-
cargo check --features=${{ inputs.soc }} --target=${{ inputs.target }}
23+
cargo check --features=${{ inputs.soc }} --target=${{ inputs.target }} --examples
2424
2525
# Build the package (Xtensa):
2626
- if: ${{ startsWith(inputs.target, 'xtensa') }}
2727
shell: bash
2828
run: |
2929
cd ${{ inputs.package }}
30-
cargo check -Zbuild-std=core --features=${{ inputs.soc }} --target=${{ inputs.target }}
30+
cargo check -Zbuild-std=core --features=${{ inputs.soc }} --target=${{ inputs.target }} --examples
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[target.'cfg(target_arch = "riscv32")']
2+
rustflags = [
3+
"-C", "link-arg=-Tlinkall.x",
4+
"-C", "force-frame-pointers",
5+
]
6+
7+
[target.'cfg(target_arch = "xtensa")']
8+
rustflags = [
9+
# GNU LD
10+
"-C", "link-arg=-Wl,-Tlinkall.x",
11+
"-C", "link-arg=-nostartfiles",
12+
13+
# LLD
14+
# "-C", "link-arg=-Tlinkall.x",
15+
# "-C", "linker=rust-lld",
16+
]

esp-hal-smartled/Cargo.toml

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,36 @@ esp-hal = "0.20.0"
1818
fugit = "0.3.7"
1919
smart-leds-trait = "0.3.0"
2020

21+
[dev-dependencies]
22+
cfg-if = "1.0.0"
23+
esp-backtrace = { version = "0.14.1", features = [
24+
"exception-handler",
25+
"panic-handler",
26+
"println",
27+
] }
28+
esp-println = "0.11.0"
29+
smart-leds = "0.4.0"
30+
2131
[features]
2232
## Implement `defmt::Format` on certain types.
2333
defmt = ["dep:defmt", "esp-hal/defmt"]
2434

2535
#! ### Chip Support Feature Flags
2636
## Target the ESP32.
27-
esp32 = ["esp-hal/esp32"]
37+
esp32 = ["esp-backtrace/esp32", "esp-hal/esp32", "esp-println/esp32"]
2838
## Target the ESP32-C3.
29-
esp32c3 = ["esp-hal/esp32c3"]
39+
esp32c3 = ["esp-backtrace/esp32c3", "esp-hal/esp32c3", "esp-println/esp32c3"]
3040
## Target the ESP32-C6.
31-
esp32c6 = ["esp-hal/esp32c6"]
41+
esp32c6 = ["esp-backtrace/esp32c6", "esp-hal/esp32c6", "esp-println/esp32c6"]
3242
## Target the ESP32-H2.
33-
esp32h2 = ["esp-hal/esp32h2"]
43+
esp32h2 = ["esp-backtrace/esp32h2", "esp-hal/esp32h2", "esp-println/esp32h2"]
3444
## Target the ESP32-S2.
35-
esp32s2 = ["esp-hal/esp32s2"]
45+
esp32s2 = ["esp-backtrace/esp32s2", "esp-hal/esp32s2", "esp-println/esp32s2"]
3646
## Target the ESP32-S3.
37-
esp32s3 = ["esp-hal/esp32s3"]
47+
esp32s3 = ["esp-backtrace/esp32s3", "esp-hal/esp32s3", "esp-println/esp32s3"]
3848

39-
# TODO: Remove patch prior to next release
49+
# TODO: Remove patches prior to next release
4050
[patch.crates-io]
41-
esp-hal = { git = "https://github.com/esp-rs/esp-hal", rev = "d44affc" }
51+
esp-backtrace = { git = "https://github.com/esp-rs/esp-hal", rev = "d44affc" }
52+
esp-hal = { git = "https://github.com/esp-rs/esp-hal", rev = "d44affc" }
53+
esp-println = { git = "https://github.com/esp-rs/esp-hal", rev = "d44affc" }
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//! RGB LED Demo
2+
//!
3+
//! This example drives an SK68XX RGB LED, which is connected to a pin on the
4+
//! official DevKits.
5+
//!
6+
//! The demo will leverage the [`smart_leds`](https://crates.io/crates/smart-leds)
7+
//! crate functionality to circle through the HSV hue color space (with
8+
//! saturation and value both at 255). Additionally, we apply a gamma correction
9+
//! and limit the brightness to 10 (out of 255).
10+
//!
11+
//! The following wiring is assumed for ESP32:
12+
//! - LED => GPIO33
13+
//! The following wiring is assumed for ESP32C3:
14+
//! - LED => GPIO8
15+
//! The following wiring is assumed for ESP32C6, ESP32H2:
16+
//! - LED => GPIO8
17+
//! The following wiring is assumed for ESP32S2:
18+
//! - LED => GPIO18
19+
//! The following wiring is assumed for ESP32S3:
20+
//! - LED => GPIO48
21+
22+
#![no_std]
23+
#![no_main]
24+
25+
use esp_backtrace as _;
26+
use esp_hal::{delay::Delay, gpio::Io, prelude::*, rmt::Rmt};
27+
use esp_hal_smartled::{smartLedBuffer, SmartLedsAdapter};
28+
use smart_leds::{
29+
brightness, gamma,
30+
hsv::{hsv2rgb, Hsv},
31+
SmartLedsWrite,
32+
};
33+
34+
#[entry]
35+
fn main() -> ! {
36+
let peripherals = esp_hal::init(esp_hal::Config::default());
37+
38+
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
39+
40+
// Each devkit uses a unique GPIO for the RGB LED, so in order to support
41+
// all chips we must unfortunately use `#[cfg]`s:
42+
cfg_if::cfg_if! {
43+
if #[cfg(feature = "esp32")] {
44+
let led_pin = io.pins.gpio33;
45+
} else if #[cfg(feature = "esp32c3")] {
46+
let led_pin = io.pins.gpio8;
47+
} else if #[cfg(any(feature = "esp32c6", feature = "esp32h2"))] {
48+
let led_pin = io.pins.gpio8;
49+
} else if #[cfg(feature = "esp32s2")] {
50+
let led_pin = io.pins.gpio18;
51+
} else if #[cfg(feature = "esp32s3")] {
52+
let led_pin = io.pins.gpio48;
53+
}
54+
}
55+
56+
// Configure RMT peripheral globally
57+
cfg_if::cfg_if! {
58+
if #[cfg(feature = "esp32h2")] {
59+
let freq = 32.MHz();
60+
} else {
61+
let freq = 80.MHz();
62+
}
63+
}
64+
65+
let rmt = Rmt::new(peripherals.RMT, freq).unwrap();
66+
67+
// We use one of the RMT channels to instantiate a `SmartLedsAdapter` which can
68+
// be used directly with all `smart_led` implementations
69+
let rmt_buffer = smartLedBuffer!(1);
70+
let mut led = SmartLedsAdapter::new(rmt.channel0, led_pin, rmt_buffer);
71+
72+
let delay = Delay::new();
73+
74+
let mut color = Hsv {
75+
hue: 0,
76+
sat: 255,
77+
val: 255,
78+
};
79+
let mut data;
80+
81+
loop {
82+
// Iterate over the rainbow!
83+
for hue in 0..=255 {
84+
color.hue = hue;
85+
// Convert from the HSV color space (where we can easily transition from one
86+
// color to the other) to the RGB color space that we can then send to the LED
87+
data = [hsv2rgb(color)];
88+
// When sending to the LED, we do a gamma correction first (see smart_leds
89+
// documentation for details) and then limit the brightness to 10 out of 255 so
90+
// that the output it's not too bright.
91+
led.write(brightness(gamma(data.iter().cloned()), 10))
92+
.unwrap();
93+
delay.delay_millis(20);
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)