From 0e8ab0cf503147c2134940ae42863f32d92a9bd0 Mon Sep 17 00:00:00 2001 From: whowechina Date: Wed, 7 Sep 2022 21:28:32 +0800 Subject: [PATCH] RGB rainbow effect working --- FIRMWARE/src/buttons.c | 3 ++ FIRMWARE/src/rgb.c | 64 ++++++++++++++++++++++++++++++++++++++++++ FIRMWARE/src/rgb.h | 1 + 3 files changed, 68 insertions(+) diff --git a/FIRMWARE/src/buttons.c b/FIRMWARE/src/buttons.c index ee8381e..eefe16a 100644 --- a/FIRMWARE/src/buttons.c +++ b/FIRMWARE/src/buttons.c @@ -13,6 +13,8 @@ #include "hardware/gpio.h" #include "hardware/timer.h" +#include "rgb.h" + #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) static const struct button { @@ -75,6 +77,7 @@ uint16_t button_read() if (sw_pressed != sw_val[i]) { sw_val[i] = sw_pressed; sw_freeze_time[i] = now + DEBOUNCE_FREEZE_TIME_US; + rgb_speed_up(); } } diff --git a/FIRMWARE/src/rgb.c b/FIRMWARE/src/rgb.c index eb137f1..73303d7 100644 --- a/FIRMWARE/src/rgb.c +++ b/FIRMWARE/src/rgb.c @@ -12,6 +12,7 @@ #include "pico/multicore.h" #include "hardware/pio.h" +#include "hardware/timer.h" #include "ws2812.pio.h" @@ -27,6 +28,65 @@ static inline uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) return ((uint32_t)r << 8) | ((uint32_t)g << 16) | (uint32_t)b; } + +/* 6 segment regular hsv color wheel, better color cycle + * https://www.arnevogel.com/rgb-rainbow/ + * https://www.instructables.com/How-to-Make-Proper-Rainbow-and-Random-Colors-With-/ + */ +uint32_t color_wheel(int index) +{ + uint32_t section = index / 256 % 6; + uint8_t incr = index % 256; + uint8_t decr = 255 - incr; + if (section == 0) { + return urgb_u32(incr, 0, 255); + } else if (section == 1) { + return urgb_u32(255, 0, decr); + } else if (section == 2) { + return urgb_u32(255, incr, 0); + } else if (section == 3) { + return urgb_u32(decr, 255, 0); + } else if (section == 4) { + return urgb_u32(0, 255, incr); + } else { + return urgb_u32(0, decr, 255); + } +} + +uint32_t color[10]; +uint32_t rotator = 0; +uint32_t speed = 100; +uint32_t pitch = 151; + +void rotate() +{ + rotator += speed; + + for (int i = 0; i < sizeof(color); i++) { + put_pixel(color_wheel(rotator + pitch * i)); + } +} + +void rgb_speed_up() +{ + if (speed < 100) { + speed++; + } +} + +#define SPEED_DOWN_INTERVAL 200000ULL +void rgb_speed_down() +{ + static uint64_t next_change_time = 0; + uint64_t now = time_us_64(); + if (now >= next_change_time) { + next_change_time = now + SPEED_DOWN_INTERVAL; + if (speed > 5) { + speed = speed * 95 / 100; + } + } +} + static uint32_t logo_color = 0; void rgb_update_logo(uint8_t r, uint8_t g, uint8_t b) @@ -37,9 +97,13 @@ void rgb_update_logo(uint8_t r, uint8_t g, uint8_t b) void rgb_entry() { while (1) { +/* for (int i = 0; i < 10; i++) { put_pixel(logo_color); } +*/ + rotate(); + rgb_speed_down(); sleep_ms(10); } } diff --git a/FIRMWARE/src/rgb.h b/FIRMWARE/src/rgb.h index e7b42ba..5ca2d97 100644 --- a/FIRMWARE/src/rgb.h +++ b/FIRMWARE/src/rgb.h @@ -10,5 +10,6 @@ void rgb_init(); void rgb_update_logo(uint8_t r, uint8_t g, uint8_t b); +void rgb_speed_up(); #endif