From 202e26714acc31bdf984d2eb6bd114f1b37ab83f Mon Sep 17 00:00:00 2001 From: andromeda Date: Mon, 6 Apr 2026 16:10:48 +0200 Subject: [PATCH] rgb --- Cargo.lock | 7 +++++ Cargo.toml | 3 +- src/bin/main.rs | 47 ------------------------------- src/lib.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 36 ++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 48 deletions(-) delete mode 100644 src/bin/main.rs create mode 100644 src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 10cd3a1..39e8ad4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -565,6 +565,7 @@ dependencies = [ "esp-hal-smartled", "esp-println", "log", + "micromath", "smart-leds", ] @@ -785,6 +786,12 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" +[[package]] +name = "micromath" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3c8dda44ff03a2f238717214da50f65d5a53b45cd213a7370424ffdb6fae815" + [[package]] name = "nb" version = "0.1.3" diff --git a/Cargo.toml b/Cargo.toml index 58996d3..60e9b52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ version = "0.1.0" [[bin]] name = "esp32c6-play" -path = "./src/bin/main.rs" +path = "./src/main.rs" [dependencies] esp-hal = { version = "~1.0", features = ["esp32c6", "log-04", "unstable"] } @@ -23,6 +23,7 @@ esp-backtrace = { version = "0.18.1", features = [ esp-println = { version = "0.16.1", features = ["esp32c6", "log-04"] } esp-hal-smartled = { version = "0.17.0", features = ["esp32c6"] } smart-leds = { version = "0.4.0", default-features = false } +micromath = { version = "2.1.0", default-features = false } [profile.dev] diff --git a/src/bin/main.rs b/src/bin/main.rs deleted file mode 100644 index 3567fbb..0000000 --- a/src/bin/main.rs +++ /dev/null @@ -1,47 +0,0 @@ -#![no_std] -#![no_main] - -use esp_backtrace as _; -use esp_hal::clock::CpuClock; -use esp_hal::main; -use esp_hal::rmt::Rmt; -use esp_hal::time::{Duration, Instant, Rate}; -use esp_hal_smartled::{SmartLedsAdapter, smart_led_buffer}; -use log::info; -use smart_leds::{ - SmartLedsWrite as _, brightness, - colors::{BLUE, GREEN, RED}, -}; - -// This creates a default app-descriptor required by the esp-idf bootloader. -// For more information see: -esp_bootloader_esp_idf::esp_app_desc!(); - -#[main] -fn main() -> ! { - // generator version: 1.2.0 - - esp_println::logger::init_logger_from_env(); - - let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max()); - let peripherals = esp_hal::init(config); - - let mut buffer = smart_led_buffer!(1); - let mut led = { - let frequency = Rate::from_mhz(80); - let rmt = Rmt::new(peripherals.RMT, frequency).expect("failed to initialize RMT0"); - SmartLedsAdapter::new(rmt.channel0, peripherals.GPIO8, &mut buffer) - }; - let level = 10; - - loop { - info!("Hello world!"); - let delay_start = Instant::now(); - led.write(brightness([RED].into_iter(), level)).unwrap(); - while delay_start.elapsed() < Duration::from_millis(500) {} - led.write(brightness([GREEN].into_iter(), level)).unwrap(); - while delay_start.elapsed() < Duration::from_millis(1000) {} - led.write(brightness([BLUE].into_iter(), level)).unwrap(); - while delay_start.elapsed() < Duration::from_millis(1500) {} - } -} diff --git a/src/lib.rs b/src/lib.rs index 0c9ac1a..a721c77 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,74 @@ #![no_std] + +use esp_hal::{ + clock::CpuClock, + time::{Duration, Instant}, +}; +use esp_hal_smartled::SmartLedsAdapter; +use smart_leds::{RGB, SmartLedsWrite as _, brightness}; + +// default brightness level of RGB led, used by write_rgb +const RGB_LED_LEVEL: u8 = u8::MAX; + +// writes color at RGB_LED_LEVEL brightness to rgb_led +pub fn write_rgb(rgb_led: &mut SmartLedsAdapter<25>, color: RGB) { + rgb_led + .write(brightness([color].into_iter(), RGB_LED_LEVEL)) + .unwrap(); +} + +// chugs for millis milliseconds +pub fn wait_ms(millis: u64) { + let delay_start = Instant::now(); + while delay_start.elapsed() < Duration::from_millis(millis) {} +} + +// initializes peripherals and logging +pub fn init() -> esp_hal::peripherals::Peripherals { + esp_println::logger::init_logger_from_env(); + + let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max()); + + let peripherals = esp_hal::init(config); + + log::info!("test info log"); + log::warn!("test warn log"); + log::error!("test error log"); + + peripherals +} + +// used the site https://deepbluembedded.com/sine-lookup-table-generator-calculator/ to generate +const SIN_LOOKUP_U8: [u8; 256] = [ + 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 162, 165, 167, 170, 173, 176, 179, 182, + 185, 188, 190, 193, 196, 198, 201, 203, 206, 208, 211, 213, 215, 218, 220, 222, 224, 226, 228, + 230, 232, 234, 235, 237, 238, 240, 241, 243, 244, 245, 246, 248, 249, 250, 250, 251, 252, 253, + 253, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 252, 251, 250, + 250, 249, 248, 246, 245, 244, 243, 241, 240, 238, 237, 235, 234, 232, 230, 228, 226, 224, 222, + 220, 218, 215, 213, 211, 208, 206, 203, 201, 198, 196, 193, 190, 188, 185, 182, 179, 176, 173, + 170, 167, 165, 162, 158, 155, 152, 149, 146, 143, 140, 137, 134, 131, 128, 124, 121, 118, 115, + 112, 109, 106, 103, 100, 97, 93, 90, 88, 85, 82, 79, 76, 73, 70, 67, 65, 62, 59, 57, 54, 52, + 49, 47, 44, 42, 40, 37, 35, 33, 31, 29, 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11, 10, 9, 7, + 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9, 10, 11, + 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35, 37, 40, 42, 44, 47, 49, 52, 54, 57, 59, + 62, 65, 67, 70, 73, 76, 79, 82, 85, 88, 90, 93, 97, 100, 103, 106, 109, 112, 115, 118, 121, + 124, +]; + +// looks up the sin of a value where the domain and range are both scaled to fit 0..255 +pub fn sin(i: u8) -> u8 { + SIN_LOOKUP_U8[usize::from(i)] +} + +// looks up the cos of a value where the domain and range are both scaled to fit 0..255 +pub fn cos(i: u8) -> u8 { + SIN_LOOKUP_U8[usize::from(i + 64)] +} + +// looks up the tan of a value where the domain and range are both scaled to fit 0..255, given that cos(i) /= 0 +pub fn tan(i: u8) -> Option { + match cos(i) { + 0 => None, + cosofi => Some(sin(i) / cosofi), + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..312fe20 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,36 @@ +#![no_std] +#![no_main] + +use esp_backtrace as _; // panic_handler +use esp_hal::{rmt::Rmt, time::Rate}; +use esp_hal_smartled::{SmartLedsAdapter, smart_led_buffer}; +use smart_leds::RGB; + +use esp32c6_play::{init, sin, wait_ms, write_rgb}; + +// This creates a default app-descriptor required by the esp-idf bootloader. +// For more information see: +esp_bootloader_esp_idf::esp_app_desc!(); + +#[esp_hal::main] +fn main() -> ! { + let peripherals = init(); + + let mut led_buffer = smart_led_buffer!(1); + let mut rgb_led = { + let frequency = Rate::from_mhz(80); + let rmt = Rmt::new(peripherals.RMT, frequency).expect("failed to initialize RMT0"); + SmartLedsAdapter::new(rmt.channel0, peripherals.GPIO8, &mut led_buffer) + }; + + loop { + for i in 0..255 { + let r: u8 = i; + let g: u8 = i + 255 / 3; // phase shifted a third + let b: u8 = i + 2 * (255 / 3); // phase shifted two thirds + write_rgb(&mut rgb_led, RGB::new(sin(r), sin(g), sin(b))); + + wait_ms(8); + } + } +}