Files
@ 48d2337d76c2
Branch filter:
Location: protofusion-esp32-template/main/ledstrip.c
48d2337d76c2
6.6 KiB
text/plain
Functional OSC TX and RX
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | #include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "led_strip.h"
#include "esp_log.h"
#include "esp_err.h"
#include "ledstrip.h"
#include "esp_log.h"
// Settings
#define LED_STRIP_GPIO 11
#define LED_STRIP_NUM_LEDS 250
#define LED_STRIP_RMT_FREQ_HZ (10 * 1000 * 1000)
static const char *TAG = "ledstrip";
// Private Prototypes
static uint8_t process_effect_rainbow(void);
static uint8_t process_effect_singlechase(void);
static uint8_t process_effect_cauldron(void);
// Private variables
static led_strip_handle_t led_strip;
static effect_t current_effect = EFFECT_NONE;
static led_mode_t mode = LEDMODE_AUTO;
// Initialize WS2812 strip
void ledstrip_init(void)
{
// LED strip general initialization, according to your led board design
led_strip_config_t strip_config = {
.strip_gpio_num = LED_STRIP_GPIO,
.max_leds = LED_STRIP_NUM_LEDS,
.led_pixel_format = LED_PIXEL_FORMAT_GRB,
.led_model = LED_MODEL_WS2812,
.flags.invert_out = false,
};
// LED strip backend configuration: RMT
led_strip_rmt_config_t rmt_config = {
.clk_src = RMT_CLK_SRC_DEFAULT,
.resolution_hz = LED_STRIP_RMT_FREQ_HZ,
.flags.with_dma = true, // DMA feature is available on ESP target like ESP32-S3
};
// LED Strip object handle
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
ESP_LOGI(TAG, "Created LED strip object with RMT backend");
}
// From OSC for testing
static float modifier = 0.0;
void ledstrip_set_modifier(float frac)
{
modifier = frac;
}
void ledstrip_set_effect(effect_t effect)
{
mode = LEDMODE_AUTO;
current_effect = effect;
}
// Set entire strip to RGB value
void ledstrip_set(uint32_t r, uint32_t g, uint32_t b)
{
mode = LEDMODE_MANUAL;
for (int i = 0; i < LED_STRIP_NUM_LEDS; i++) {
ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, i, g, r, b));
}
led_strip_refresh(led_strip);
}
// Run effect and refresh strip
void ledstrip_refresh(void)
{
for(;;)
{
if(mode == LEDMODE_MANUAL)
{
// do nothing
}
else
{
switch(current_effect)
{
case EFFECT_RAINBOW:
{
if(process_effect_rainbow() & 0x01)
led_strip_refresh(led_strip);
} break;
case EFFECT_SINGLECHASE:
{
uint8_t ret = process_effect_singlechase();
if(ret & 0x01)
led_strip_refresh(led_strip);
// Chase complete, return to manual mode
if(ret & 0x02)
current_effect = EFFECT_NONE;
} break;
case EFFECT_CAULDRON:
{
if(process_effect_cauldron() & 0x01)
led_strip_refresh(led_strip);
} break;
case EFFECT_NONE:
default:
{
ledstrip_set(10,10,10);
led_strip_refresh(led_strip);
} break;
}
}
// TODO: DelayUntil
vTaskDelay(pdMS_TO_TICKS(10));
}
}
static uint8_t effect = -1;
static uint8_t effects = 120;
static uint16_t effStep;
static unsigned long effStart;
static void __reset(void)
{
effStep = 0;
effect = (effect + 1) % effects;
effStart = xTaskGetTickCount() * portTICK_PERIOD_MS;
}
static uint8_t process_effect_rainbow(void) {
// 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<LED_STRIP_NUM_LEDS;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;
}
static uint32_t countloc = 0;
static uint8_t process_effect_singlechase(void) {
if((xTaskGetTickCount() * portTICK_PERIOD_MS) - effStart < 20 * (effStep)) return 0x00;
float intensity = (float)countloc / (float)LED_STRIP_NUM_LEDS;
for(uint16_t j=0;j<LED_STRIP_NUM_LEDS;j++) {
if(j == countloc)
{
ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, j, 255, 255 * (1.0-intensity), 255 * (1.0-intensity)));
}
else
ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, j, 0,0,0));
}
countloc = (countloc + 1) % LED_STRIP_NUM_LEDS;
if(countloc == 0)
return 0b11;
else
return 0b01;
}
//127, 122
// static uint32_t countloc = 0;
float intensity = 1.0f;
uint8_t intensity_up = 0;
uint8_t modmod = 0;
uint32_t last_modmod = 0;
static uint8_t process_effect_cauldron(void) {
if((xTaskGetTickCount() * portTICK_PERIOD_MS) - effStart < 20 * (effStep)) return 0x00;
if(intensity_up)
{
intensity += 0.01f;
if(intensity > 0.98f)
intensity_up = 0;
}
else
{
intensity -= 0.01f;
if(intensity < 0.7f)
intensity_up = 1;
}
// ESP_LOGI(TAG, "Friggn intensity %f", intensity);
if((xTaskGetTickCount() * portTICK_PERIOD_MS) - last_modmod > 25)
{
last_modmod = (xTaskGetTickCount() * portTICK_PERIOD_MS);
modmod +=1;
}
for(uint16_t j=0;j<LED_STRIP_NUM_LEDS;j++) {
float posmod = (float)((j)%10) / 5.0f;
posmod /= 1.0;
posmod += 0.0;
// ESP_LOGI(TAG, "Friggn intensity %f", posmod);
ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, j, 255 * (intensity), 120 * (intensity) * posmod, 0));
// ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, j, 0,0,0));
}
//countloc = (countloc + 1) % LED_STRIP_NUM_LEDS;
return 0x01;
}
|