mirror of
https://github.com/whowechina/aic_pico.git
synced 2025-01-19 03:17:24 +01:00
RGB rainbow effect
This commit is contained in:
parent
2a08a99214
commit
bb06b22eca
@ -9,7 +9,7 @@
|
||||
|
||||
typedef void (*cmd_handler_t)(int argc, char *argv[]);
|
||||
|
||||
void cli_init();
|
||||
void cli_init(const char *prompt, const char *logo);
|
||||
void cli_register(const char *cmd, cmd_handler_t handler, const char *help);
|
||||
void cli_run();
|
||||
void cli_fps_count(int core);
|
||||
|
@ -46,6 +46,10 @@ void report_usb_hid()
|
||||
|
||||
uint64_t now = time_us_64();
|
||||
|
||||
if (memcmp(cardio.current, "\0\0\0\0\0\0\0\0\0", 9) != 0) {
|
||||
rgb_set_rainbow_speed(255);
|
||||
}
|
||||
|
||||
if ((memcmp(cardio.current, cardio.reported, 9) != 0) &&
|
||||
(now - cardio.report_time > 1000000)) {
|
||||
|
||||
@ -70,10 +74,6 @@ static void core1_loop()
|
||||
if (mutex_try_enter(&core1_io_lock, NULL)) {
|
||||
mutex_exit(&core1_io_lock);
|
||||
}
|
||||
rgb_set_color(0, 0x800000);
|
||||
rgb_set_color(1, 0x008000);
|
||||
rgb_set_color(2, 0x000080);
|
||||
rgb_set_color(3, 0x808080);
|
||||
rgb_update();
|
||||
cli_fps_count(1);
|
||||
sleep_ms(1);
|
||||
|
@ -59,14 +59,14 @@ static bool pn532_wait_ready()
|
||||
{
|
||||
uint8_t status = 0;
|
||||
|
||||
for (int retry = 0; retry < 50; retry++) {
|
||||
for (int retry = 0; retry < 30; retry++) {
|
||||
if (pn532_read(&status, 1) == 1 && status == 0x01) {
|
||||
return true;
|
||||
}
|
||||
if (wait_loop) {
|
||||
wait_loop();
|
||||
}
|
||||
sleep_ms(1);
|
||||
sleep_us(1000);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
@ -83,6 +83,80 @@ uint32_t rgb32_from_hsv(uint8_t h, uint8_t s, uint8_t v)
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint32_t apply_level(uint32_t color)
|
||||
{
|
||||
unsigned r = (color >> 16) & 0xff;
|
||||
unsigned g = (color >> 8) & 0xff;
|
||||
unsigned b = color & 0xff;
|
||||
|
||||
r = r * aic_cfg->led.level / 255;
|
||||
g = g * aic_cfg->led.level / 255;
|
||||
b = b * aic_cfg->led.level / 255;
|
||||
|
||||
return r << 16 | g << 8 | 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-/
|
||||
*/
|
||||
#define COLOR_WHEEL_SIZE 256
|
||||
static uint32_t color_wheel[COLOR_WHEEL_SIZE];
|
||||
static void generate_color_wheel()
|
||||
{
|
||||
static uint8_t old_level = 0;
|
||||
if (old_level == aic_cfg->led.level) {
|
||||
return;
|
||||
}
|
||||
old_level = aic_cfg->led.level;
|
||||
|
||||
for (int i = 0; i < COLOR_WHEEL_SIZE; i++) {
|
||||
color_wheel[i] = rgb32_from_hsv(i, 250, 255);
|
||||
color_wheel[i] = apply_level(color_wheel[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#define RAINBOW_PITCH 37
|
||||
#define RAINBOW_MIN_SPEED 1
|
||||
static uint32_t rainbow_speed = RAINBOW_MIN_SPEED;
|
||||
|
||||
static void rainbow_update()
|
||||
{
|
||||
static uint64_t last = 0;
|
||||
uint64_t now = time_us_64();
|
||||
if (now - last < 33333) { // no faster than 30Hz
|
||||
return;
|
||||
}
|
||||
last = now;
|
||||
|
||||
static uint32_t rotator = 0;
|
||||
rotator = (rotator + rainbow_speed) % COLOR_WHEEL_SIZE;
|
||||
|
||||
for (int i = 0; i < ARRAY_SIZE(rgb_buf); i++) {
|
||||
uint32_t index = (rotator + RAINBOW_PITCH * i) % COLOR_WHEEL_SIZE;
|
||||
rgb_buf[i] = color_wheel[index];
|
||||
}
|
||||
}
|
||||
|
||||
void rgb_set_rainbow_speed(uint8_t speed)
|
||||
{
|
||||
rainbow_speed = speed / 8;
|
||||
}
|
||||
|
||||
static void rainbow_speed_down()
|
||||
{
|
||||
static uint64_t last = 0;
|
||||
uint64_t now = time_us_64();
|
||||
if (now - last < 200000) {
|
||||
return;
|
||||
}
|
||||
last = now;
|
||||
|
||||
if (rainbow_speed > RAINBOW_MIN_SPEED) {
|
||||
rainbow_speed = rainbow_speed * 95 / 100;
|
||||
}
|
||||
}
|
||||
|
||||
static void drive_led()
|
||||
{
|
||||
static uint64_t last = 0;
|
||||
@ -97,19 +171,6 @@ static void drive_led()
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint32_t apply_level(uint32_t color)
|
||||
{
|
||||
unsigned r = (color >> 16) & 0xff;
|
||||
unsigned g = (color >> 8) & 0xff;
|
||||
unsigned b = color & 0xff;
|
||||
|
||||
r = r * aic_cfg->led.level / 255;
|
||||
g = g * aic_cfg->led.level / 255;
|
||||
b = b * aic_cfg->led.level / 255;
|
||||
|
||||
return r << 16 | g << 8 | b;
|
||||
}
|
||||
|
||||
void rgb_set_color(unsigned index, uint32_t color)
|
||||
{
|
||||
if (index >= ARRAY_SIZE(rgb_buf)) {
|
||||
@ -144,12 +205,17 @@ void rgb_set_brg(unsigned index, const uint8_t *brg_array, size_t num)
|
||||
void rgb_init()
|
||||
{
|
||||
uint pio0_offset = pio_add_program(pio0, &ws2812_program);
|
||||
|
||||
gpio_set_drive_strength(RGB_PIN, GPIO_DRIVE_STRENGTH_2MA);
|
||||
ws2812_program_init(pio0, 0, pio0_offset, RGB_PIN, 800000, false);
|
||||
|
||||
generate_color_wheel();
|
||||
}
|
||||
|
||||
void rgb_update()
|
||||
{
|
||||
generate_color_wheel();
|
||||
|
||||
rainbow_update();
|
||||
rainbow_speed_down();
|
||||
drive_led();
|
||||
}
|
||||
|
@ -20,5 +20,6 @@ uint32_t rgb32_from_hsv(uint8_t h, uint8_t s, uint8_t v);
|
||||
|
||||
void rgb_set_color(unsigned index, uint32_t color);
|
||||
void rgb_set_color_all(uint32_t color);
|
||||
void rgb_set_rainbow_speed(uint8_t speed);
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user