Changeset - e4270e6d9529
[Not reviewed]
default
0 3 0
Ethan Zonca - 9 months ago 2024-08-25 21:24:17
ez@ethanzonca.com
Clean up LED strip implementation, add single LED chase
3 files changed with 100 insertions and 58 deletions:
0 comments (0 inline, 0 general)
main/ledstrip.c
Show inline comments
 
#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 120
 
#define LED_STRIP_NUM_LEDS 150
 
#define LED_STRIP_RMT_FREQ_HZ  (10 * 1000 * 1000)
 
static const char *TAG = "ledstrip";
 

	
 

	
 
// Private Prototypes
 
static uint8_t strip0_loop0func();
 
static uint8_t strip0_loop0_eff0();
 
static uint8_t process_effect_rainbow();
 
static uint8_t process_effect_singlechase();
 

	
 

	
 
// Private variables
 
static led_strip_handle_t led_strip;
 

	
 
static effect_t current_effect = EFFECT_NONE;
 

	
 
// 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,
 
    };
 
@@ -46,106 +49,97 @@ void ledstrip_init(void)
 
    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)
 
{
 
  current_effect = effect;
 
}
 

	
 

	
 
// Set entire strip to RGB value
 
void ledstrip_set(uint32_t r, uint32_t g, uint32_t b)
 
{
 
    for (int i = 0; i < LED_STRIP_NUM_LEDS; i++) {
 
        ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, i, r, g, b));
 
    }
 
    led_strip_refresh(led_strip);
 
}
 

	
 

	
 
// Run effect and refresh strip
 
void ledstrip_refresh(void)
 
{
 
  if(strip0_loop0func() & 0x01)
 
  switch(current_effect)
 
  {
 

	
 
    case EFFECT_RAINBOW:
 
    {
 
      if(process_effect_rainbow() & 0x01)
 
        led_strip_refresh(led_strip);
 
    } break;
 
    
 
    case EFFECT_SINGLECHASE:
 
    {
 
      if(process_effect_singlechase() & 0x01)
 
    led_strip_refresh(led_strip);
 
    } break;
 

	
 
    case EFFECT_NONE:
 
    default:
 
    {
 
      ledstrip_set(10,10,10);
 
      led_strip_refresh(led_strip);
 
    } break;
 
}
 
}
 

	
 

	
 

	
 

	
 

	
 

	
 

	
 

	
 

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

	
 
typedef struct Loop
 
{
 
  uint8_t currentChild;
 
  uint8_t childs;
 
  bool timeBased;
 
  uint16_t cycles;
 
  uint16_t currentTime;
 
} loop_t;
 

	
 
loop_t strip0loop0 = 
 
{
 
    .currentTime = 0,
 
    .currentChild = 0,
 
    .childs = 1,
 
    .timeBased = false,
 
    .cycles = 1,
 
};
 

	
 

	
 

	
 

	
 
static 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;
 
}
 

	
 

	
 
static uint8_t strip0_loop0_eff0() {
 
static uint8_t process_effect_rainbow() {
 
    // 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++) {
 
  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;
 
@@ -153,14 +147,31 @@ static uint8_t strip0_loop0_eff0() {
 
              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() {
 
  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)));
 
      ESP_LOGI(TAG, "Friggn intensity %f", intensity);
 
    }
 
    else
 
      ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, j, 0,0,0));
 
  }
 
  countloc = (countloc + 1) % LED_STRIP_NUM_LEDS;
 

	
 
  return 0x01;
 
}
 

	
main/ledstrip.h
Show inline comments
 
#ifndef _LED_STRIPPER_H
 
#define _LED_STRIPPER_H
 

	
 

	
 
typedef enum _effect {
 
  EFFECT_NONE = 0,
 
  EFFECT_RAINBOW,
 
  EFFECT_SINGLECHASE,
 
} effect_t;
 

	
 

	
 
void ledstrip_init(void);
 
void ledstrip_set(uint32_t r, uint32_t g, uint32_t b);
 
void ledstrip_set_modifier(float frac);
 
void ledstrip_set_effect(effect_t effect);
 
void ledstrip_refresh(void);
 
#endif
 
\ No newline at end of file
main/osc_control.c
Show inline comments
 
@@ -8,76 +8,99 @@
 
#include "ledstrip.h"
 

	
 
#define WIFI_SSID ""
 
#define WIFI_PASS ""
 

	
 
#define OSC_ADDRESS ""
 
#define OSC_PORT 0
 

	
 
static const char *TAG = "osc_control";
 

	
 
esp_osc_client_t client;
 

	
 
static void sender() {
 
static void sender()
 
{
 
  // select targets
 
  esp_osc_target_t targets[2] = {
 
      esp_osc_target("127.0.0.1", 9000),
 
      esp_osc_target(OSC_ADDRESS, OSC_PORT),
 
  };
 

	
 
  for (;;) {
 
    // delay
 
    vTaskDelay(1000 / portTICK_PERIOD_MS);
 

	
 
    // send messages
 
    for (size_t i = 0; i < 2; i++) {
 
      esp_osc_send(&client, &targets[i], "test", "ihfdsb", 42, (int64_t)84, 3.14f, 6.28, "foo", 3, "bar");
 
    }
 
  }
 
}
 

	
 
static bool callback(const char *topic, const char *format, esp_osc_value_t *values) {
 

	
 
static bool callback(const char *topic, const char *format, esp_osc_value_t *values)
 
{
 
  // log message
 
  ESP_LOGI(TAG, "got message: %s (%s)", topic, format);
 

	
 

	
 
  char out[512] = {0};
 
  snprintf(out, 128, "topic: %s\nformat: %s\n", topic, format);
 

	
 
  for (size_t i = 0; i < strlen(format); i++) {
 
    switch (format[i]) {
 
  for (size_t i = 0; i < strlen(format); i++) 
 
  {
 
    switch (format[i])
 
    {
 
      case 'i':
 
      {
 
        snprintf(out+strlen(out), 128-strlen(out), "Value: %ld", values[i].i);
 
        ESP_LOGI(TAG, "==> i: %ld", values[i].i);
 
        break;
 
        if(strcmp(topic, "/led_effect") == 0)
 
        {
 
          ledstrip_set_effect(values[i].i);
 
        }
 
      } break;
 

	
 
      case 'h':
 
      {
 
        ESP_LOGI(TAG, "==> h: %lld", values[i].h);
 
        break;
 
      }  break;
 

	
 
      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);
 
        if(strcmp(topic, "/led_modifier") == 0)
 
        {
 
        ledstrip_set_modifier(values[i].f);
 
        }
 
      } break;
 

	
 
        break;
 
      case 'd':
 
      {
 
        ESP_LOGI(TAG, "==> d: %f", values[i].d);
 
        break;
 
      } break;
 

	
 
      case 's':
 
      {
 
        ESP_LOGI(TAG, "==> s: %s", values[i].s);
 
        break;
 
      }  break;
 

	
 
      case 'b':
 
      {
 
        ESP_LOGI(TAG, "==> b: %.*s (%d)", values[i].bl, values[i].b, values[i].bl);
 
        break;
 
    }
 

	
 
    }
 
  }
 

	
 
  display_update_text(out);
 

	
 
  return true;
 
}
 

	
 
static void receiver() {
 
  for (;;) {
 
    // receive messages
 
    esp_osc_receive(&client, callback);
 
  }
0 comments (0 inline, 0 general)