# HG changeset patch # User Ethan Zonca # Date 2024-08-16 12:32:51 # Node ID 91735168ee7552d924b38674bd67d618ffd969bc # Parent 8645339bec1e0c406801244217982e5d3426d471 Initial super rough WS2812 control with modified behavior over OSC diff --git a/main/ledstrip.c b/main/ledstrip.c --- a/main/ledstrip.c +++ b/main/ledstrip.c @@ -1,31 +1,172 @@ -#include -#include -#include -#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "led_strip.h" +#include "esp_log.h" +#include "esp_err.h" -#include "ledstrip.h" +#define LED_STRIP_BLINK_GPIO 11 +#define LED_STRIP_LED_NUMBERS 120 +#define LED_STRIP_RMT_RES_HZ (10 * 1000 * 1000) + +static const char *TAG = "ledstrip"; -#define BLINK_GPIO 0 +led_strip_handle_t configure_led(void) +{ + // LED strip general initialization, according to your led board design + led_strip_config_t strip_config = { + .strip_gpio_num = LED_STRIP_BLINK_GPIO, // The GPIO that connected to the LED strip's data line + .max_leds = LED_STRIP_LED_NUMBERS, // The number of LEDs in the strip, + .led_pixel_format = LED_PIXEL_FORMAT_GRB, // Pixel format of your LED strip + .led_model = LED_MODEL_WS2812, // LED strip model + .flags.invert_out = false, // whether to invert the output signal + }; + + // LED strip backend configuration: RMT + led_strip_rmt_config_t rmt_config = { +#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) + .rmt_channel = 0, +#else + .clk_src = RMT_CLK_SRC_DEFAULT, // different clock source can lead to different power consumption + .resolution_hz = LED_STRIP_RMT_RES_HZ, // RMT counter clock frequency + .flags.with_dma = false, // DMA feature is available on ESP target like ESP32-S3 +#endif + }; + + // LED Strip object handle + led_strip_handle_t led_strip; + ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip)); + ESP_LOGI(TAG, "Created LED strip object with RMT backend"); + return led_strip; +} + led_strip_handle_t led_strip; -/* LED strip initialization with the GPIO and pixels number*/ -led_strip_config_t strip_config = { - .strip_gpio_num = BLINK_GPIO, // The GPIO that connected to the LED strip's data line - .max_leds = 1, // The number of LEDs in the strip, - .led_pixel_format = LED_PIXEL_FORMAT_GRB, // Pixel format of your LED strip - .led_model = LED_MODEL_WS2812, // LED strip model - .flags.invert_out = false, // whether to invert the output signal (useful when your hardware has a level inverter) +void ledstrip_init(void) +{ + led_strip = configure_led(); + bool led_on_off = false; +} + +static float modifier = 0.0; +void ledstrip_set_modifier(float frac) +{ + modifier = frac; +} + +void ledstrip_set(uint32_t r, uint32_t g, uint32_t b) +{ + ESP_LOGI(TAG, "Led set!"); + + + for (int i = 0; i < LED_STRIP_LED_NUMBERS; i++) { + ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, i, r, g, b)); + } + led_strip_refresh(led_strip); +} + + + + + + +uint8_t effect = -1; +uint8_t effects = 120; +uint16_t effStep; +unsigned long effStart; + +void __reset(void) +{ + effStep = 0; + effect = (effect + 1) % effects; + effStart = xTaskGetTickCount() * portTICK_PERIOD_MS; +} + +typedef struct Loop +{ + uint8_t currentChild; + uint8_t childs; + bool timeBased; + uint16_t cycles; + uint16_t currentTime; + //Loop(uint8_t totchilds, bool timebased, uint16_t tottime) +// {currentTime=0;currentChild=0;childs=totchilds;timeBased=timebased;cycles=tottime;} +} loop_t; + +loop_t strip0loop0 = +{ + .currentTime = 0, + .currentChild = 0, + .childs = 1, + .timeBased = false, + .cycles = 1, }; -led_strip_rmt_config_t rmt_config = { - .clk_src = RMT_CLK_SRC_DEFAULT, // different clock source can lead to different power consumption - .resolution_hz = 10 * 1000 * 1000, // 10MHz - .flags.with_dma = false, // whether to enable the DMA feature -}; +//proto +uint8_t strip0_loop0func(); +uint8_t strip0_loop0_eff0(); + + +void ledstrip_refresh(void) +{ + if(strip0_loop0func() & 0x01) + led_strip_refresh(led_strip); +} + +uint8_t strip0_loop0func() { + uint8_t ret = 0x00; + switch(strip0loop0.currentChild) { + case 0: + ret = strip0_loop0_eff0();break; + } + if(ret & 0x02) { + ret &= 0xfd; + if(strip0loop0.currentChild + 1 >= strip0loop0.childs) { + strip0loop0.currentChild = 0; + if(++strip0loop0.currentTime >= strip0loop0.cycles) {strip0loop0.currentTime = 0; ret |= 0x02;} + } + else { + strip0loop0.currentChild++; + } + }; + return ret; +} -void ledstrip_init(void) -{ - ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip)); -} \ No newline at end of file +uint8_t strip0_loop0_eff0() { + // Strip ID: 0 - Effect: Rainbow - LEDS: 120 + // Steps: 60 - Delay: 20 + // Colors: 3 (255.0.0, 0.255.0, 0.0.255) + // Options: rainbowlen=60, toLeft=true, + if((xTaskGetTickCount() * portTICK_PERIOD_MS) - effStart < 20 * (effStep)) return 0x00; + float factor1, factor2; + uint16_t ind; + for(uint16_t j=0;j<120;j++) { + ind = effStep + j * 1; + switch((int)((ind % 60) / 20)) { + case 0: factor1 = 1.0 - ((float)(ind % 60 - 0 * 20) / 20); + factor2 = (float)((int)(ind - 0) % 60) / 20; + // strip_0.strip.setPixelColor(j, 255 * factor1 + 0 * factor2, 0 * factor1 + 255 * factor2, 0 * factor1 + 0 * factor2); + ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, j, modifier * 255 * factor1 + 0 * factor2, modifier * 0 * factor1 + 255 * factor2, modifier * 0 * factor1 + 0 * factor2)); + + break; + case 1: factor1 = 1.0 - ((float)(ind % 60 - 1 * 20) / 20); + factor2 = (float)((int)(ind - 20) % 60) / 20; + ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, j, modifier * 0 * factor1 + 0 * factor2, modifier * 255 * factor1 + 0 * factor2, modifier * 0 * factor1 + 255 * factor2)); + break; + case 2: factor1 = 1.0 - ((float)(ind % 60 - 2 * 20) / 20); + factor2 = (float)((int)(ind - 40) % 60) / 20; + ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, j, modifier * 0 * factor1 + 255 * factor2, modifier * 0 * factor1 + 0 * factor2, modifier * 255 * factor1 + 0 * factor2)); + break; + } + } + if(effStep >= 60) {__reset(); return 0x03; } + else effStep++; + return 0x01; +} + + + + + diff --git a/main/ledstrip.h b/main/ledstrip.h --- a/main/ledstrip.h +++ b/main/ledstrip.h @@ -3,5 +3,7 @@ void ledstrip_init(void); - +void ledstrip_set(uint32_t r, uint32_t g, uint32_t b); +void ledstrip_set_modifier(float frac); +void ledstrip_refresh(void); #endif \ No newline at end of file diff --git a/main/main.c b/main/main.c --- a/main/main.c +++ b/main/main.c @@ -45,10 +45,12 @@ void app_main(void) // Connect to wifi wifi_init(); + ledstrip_init(); + + // Initialize OSC osc_init(); - ledstrip_init(); // Initialize canbus //can_init(); @@ -57,8 +59,8 @@ void app_main(void) while(1) { display_gui_process(); - - vTaskDelay(pdMS_TO_TICKS(1000)); + ledstrip_refresh(); + vTaskDelay(pdMS_TO_TICKS(10)); } } diff --git a/main/osc_control.c b/main/osc_control.c --- a/main/osc_control.c +++ b/main/osc_control.c @@ -5,6 +5,7 @@ #include #include "display.h" #include +#include "ledstrip.h" #define WIFI_SSID "" #define WIFI_PASS "" @@ -53,6 +54,10 @@ static bool callback(const char *topic, case 'f': snprintf(out+strlen(out), 128-strlen(out), "Value: %f", values[i].f); ESP_LOGI(TAG, "==> f: %f", values[i].f); + uint8_t val = values[i].f * 255; + // ledstrip_set(val, val, val); + ledstrip_set_modifier(values[i].f); + break; case 'd': ESP_LOGI(TAG, "==> d: %f", values[i].d);