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

rp2040 zero rainbow using palette #82

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions boards/waveshare-rp2040-zero/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repository = "https://github.com/rp-rs/rp-hal-boards.git"

[dependencies]
cortex-m-rt = { workspace = true, optional = true }
palette = { version = "0.7.6", default-features = false, features = ["libm"] }
rp2040-boot2 = { workspace = true, optional = true }
rp2040-hal.workspace = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

use core::iter::once;
use embedded_hal::delay::DelayNs;
use palette::rgb::Rgb;
use palette::{FromColor, Hsl};
// use palette::Hsl;
use panic_halt as _;
use smart_leds::{brightness, SmartLedsWrite, RGB8};
use waveshare_rp2040_zero::entry;
Expand Down Expand Up @@ -71,31 +74,26 @@ fn main() -> ! {
);

// Infinite colour wheel loop
let mut n: u8 = 128;
let mut hue: f64 = 0.0;
let mut timer = timer; // rebind to force a copy of the timer
loop {
ws.write(brightness(once(wheel(n)), 32)).unwrap();
n = n.wrapping_add(1);

timer.delay_ms(25);
}
}

/// Convert a number from `0..=255` to an RGB color triplet.
///
/// The colours are a transition from red, to green, to blue and back to red.
fn wheel(mut wheel_pos: u8) -> RGB8 {
wheel_pos = 255 - wheel_pos;
if wheel_pos < 85 {
// No green in this sector - red and blue only
(255 - (wheel_pos * 3), 0, wheel_pos * 3).into()
} else if wheel_pos < 170 {
// No red in this sector - green and blue only
wheel_pos -= 85;
(0, wheel_pos * 3, 255 - (wheel_pos * 3)).into()
} else {
// No blue in this sector - red and green only
wheel_pos -= 170;
(wheel_pos * 3, 255 - (wheel_pos * 3), 0).into()
ws.write(brightness(
once({
let hsl = Hsl::<Rgb, _>::new(hue as f64, 1.0, 0.5);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hue is already an f64 from its declaration. No need to cast it here.

let rgb = Rgb::from_color(hsl).into_format();
RGB8::new(rgb.red, rgb.green, rgb.blue)
}),
{
// 0 is off and 1 is max brightness
let brightness = 0.05 as f64;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let brightness = 0.05 as f64;
let brightness = 0.05f64;

(brightness * u8::MAX as f64) as u8
Comment on lines +87 to +89
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous code uses 32 as brightness which is 12.5% not 5%.
Why not leaving the value 32 and only adding a comment like /* 12.5% */

},
))
.unwrap();
hue += 360.0 / 6.0 / u8::MAX as f64;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these values are constants. What’s the intent in that math?

The current code turns the wheel by (1/255)rad (~1.41°) per iteration.
We don’t need this to match strictly. This should be enough:

Suggested change
hue += 360.0 / 6.0 / u8::MAX as f64;
hue += 1.0; // advance by 1°

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent is that there are 256 steps of changes in a value of either r, g, b in every 1/6 of 360 degrees, so adding the hue by 360/6/256 makes sure that there will actually be a change in pixels every iteration of the loop. This can be changed to 1 if that's what's prefered by this project.

if hue >= 360.0 {
hue -= 360.0;
}
timer.delay_ms(1)
}
}