udpate led driver

This commit is contained in:
Laila van Reenen 2024-07-12 10:09:04 +02:00
parent 8fcc27af0e
commit 2887173681
Signed by: LailaTheElf
GPG Key ID: 1F4E6EE3E6DDF769
3 changed files with 77 additions and 17 deletions

View File

@ -1,4 +1,3 @@
cmake_minimum_required(VERSION 3.16.0) cmake_minimum_required(VERSION 3.16.0)
include($ENV{IDF_PATH}/tools/cmake/project.cmake) include($ENV{IDF_PATH}/tools/cmake/project.cmake)
list(APPEND EXTRA_COMPONENT_DIRS led_strip)
project(rx_esp32) project(rx_esp32)

View File

@ -1,33 +1,94 @@
#include "led.h" #include "led.h"
#include <stddef.h>
#include "driver/rmt_tx.h"
#include "driver/rmt_types.h"
#include "driver/rmt_encoder.h"
#include "driver/gpio.h" #include "driver/gpio.h"
#include "led_strip.h"
static led_strip_handle_t led_strip; typedef struct {
rmt_encoder_t base;
rmt_encoder_t *bytes_encoder;
rmt_encoder_t *copy_encoder;
int state;
rmt_symbol_word_t reset_code;
} rmt_led_strip_encoder_t;
rmt_channel_handle_t* led_rmt_channel;
rmt_led_strip_encoder_t* led_enc;
rmt_led_strip_encoder_t* led_init_encoder()
{
rmt_led_strip_encoder_t* led_encoder;
led_encoder = calloc(1, sizeof(rmt_led_strip_encoder_t));
rmt_bytes_encoder_config_t bytes_encoder_config;
// different led strip might have its own timing requirements, following parameter is for WS2812
bytes_encoder_config = (rmt_bytes_encoder_config_t) {
.bit0 = {
.level0 = 1,
.duration0 = 0.3, // T0H=0.3us
.level1 = 0,
.duration1 = 0.9, // T0L=0.9us
},
.bit1 = {
.level0 = 1,
.duration0 = 0.9, // T1H=0.9us
.level1 = 0,
.duration1 = 0.3, // T1L=0.3us
},
.flags.msb_first = 1 // WS2812 transfer bit order: G7...G0R7...R0B7...B0
};
rmt_new_bytes_encoder(&bytes_encoder_config, &led_encoder->bytes_encoder);
// rmt_copy_encoder_config_t copy_encoder_config = {};
// rmt_new_copy_encoder(&copy_encoder_config, &led_encoder->copy_encoder);
uint32_t reset_ticks = 10 /*MHz*/ * 280 / 2; // reset code duration defaults to 280us to accomodate WS2812B-V5
led_encoder->reset_code = (rmt_symbol_word_t) {
.level0 = 0,
.duration0 = reset_ticks,
.level1 = 0,
.duration1 = reset_ticks,
};
return led_encoder;
}
void led_init(void) void led_init(void)
{ {
/* LED strip initialization with the GPIO and pixels number*/ /* LED strip initialization with the GPIO and pixels number */
led_strip_config_t strip_config = { rmt_tx_channel_config_t rmt_chan_config = {
.strip_gpio_num = 8, .clk_src = RMT_CLK_SRC_DEFAULT,
.max_leds = 1, .gpio_num = 8,
}; .mem_block_symbols = 48,
led_strip_rmt_config_t rmt_config = { .resolution_hz = 10 * 1000 * 1000, // 10 MHz
.resolution_hz = 10 * 1000 * 1000, // 10MHz .trans_queue_depth = 4,
.flags.with_dma = false, .flags.with_dma = false,
.flags.invert_out = false, // ??
}; };
led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip); rmt_new_tx_channel(&rmt_chan_config, &led_rmt_channel);
led_enc = led_init_encoder();
/* Set all LED off to clear all pixels */ /* Set all LED off to clear all pixels */
led_strip_clear(led_strip); // led_strip_clear(led_strip);
led_setRGB(0, 0, 0);
} }
void led_deinit(void) void led_deinit(void)
{ {
//TODO rmt_del_channel(led_rmt_channel);
rmt_del_encoder(led_enc);
} }
void led_setColor(uint8_t r, uint8_t g, uint8_t b) void led_setRGB(uint8_t r, uint8_t g, uint8_t b)
{ {
led_strip_set_pixel(led_strip, 0, 16, 16, 16); rmt_transmit_config_t tx_conf = {
led_strip_refresh(led_strip); .loop_count = 0,
};
uint8_t pixel = {g, r, b};
rmt_enable(led_rmt_channel);
rmt_transmit(led_rmt_channel, led_enc, pixel, 3, &tx_conf);
rmt_tx_wait_all_done(led_rmt_channel, -1);
rmt_disable(led_rmt_channel);
} }

View File

@ -5,6 +5,6 @@
void led_init(void); void led_init(void);
void led_deinit(void); void led_deinit(void);
void led_setColor(uint8_t r, uint8_t g, uint8_t b); void led_setRGB(uint8_t r, uint8_t g, uint8_t b);
#endif #endif