1
0
mirror of https://github.com/whowechina/popn_pico.git synced 2024-11-28 00:20:53 +01:00

RGB rainbow effect working

This commit is contained in:
whowechina 2022-09-07 21:28:32 +08:00
parent 5a7828c496
commit 0e8ab0cf50
3 changed files with 68 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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