added a new LEDstrip module based on the espressif component
This commit is contained in:
@@ -11,6 +11,7 @@ idf_component_register(
|
||||
|
||||
"platform/src/isl29125.cpp"
|
||||
"platform/src/logger.cpp"
|
||||
"platform/src/prgm_ledstrip.cpp"
|
||||
"platform/src/wifi.cpp"
|
||||
# "old/src/led_strip_encoder.c"
|
||||
# "old/src/ledmatrix.cpp"
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
## IDF Component Manager Manifest File
|
||||
dependencies:
|
||||
espressif/led_strip: "^2.5.3"
|
||||
## Required IDF version
|
||||
idf:
|
||||
version: ">=4.1.0"
|
||||
# # Put list of dependencies here
|
||||
# # For components maintained by Espressif:
|
||||
# component: "~1.0.0"
|
||||
# # For 3rd party components:
|
||||
# username/component: ">=1.0.0,<2.0.0"
|
||||
# username2/component2:
|
||||
# version: "~1.0.0"
|
||||
# # For transient dependencies `public` flag can be set.
|
||||
# # `public` flag doesn't have an effect dependencies of the `main` component.
|
||||
# # All dependencies of `main` are public by default.
|
||||
# public: true
|
||||
+40
-34
@@ -35,6 +35,7 @@
|
||||
// Platform includes
|
||||
#include "isl29125.h"
|
||||
#include "logger.h"
|
||||
#include "prgm_ledstrip.h"
|
||||
#include "Wifi.h"
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
@@ -111,40 +112,40 @@ extern "C" void app_main(void)
|
||||
LOGGER_PRINT("Release: %d.%d \n\r", MAJORRELEASE, MINORRELEASE);
|
||||
LOGGER_PRINT("Compiled on %s at %s\n\r\n\r\n\r", __DATE__, __TIME__);
|
||||
|
||||
// // -----------------------------------------------------------------------------------------------------------------
|
||||
// // I2C Masterbus for sensoring peripherals
|
||||
// //
|
||||
// i2c_port_t i2c_master_port = I2C_NUM_0;
|
||||
//
|
||||
// i2c_config_t i2cConfig = {
|
||||
// .mode = I2C_MODE_MASTER,
|
||||
// .sda_io_num = (int)2,
|
||||
// .scl_io_num = (int)3,
|
||||
// .sda_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
// .scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
// .master = 400000,
|
||||
// .clk_flags = 0
|
||||
// };
|
||||
//
|
||||
// ESP_ERROR_CHECK(i2c_param_config(i2c_master_port, &i2cConfig));
|
||||
// ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, i2cConfig.mode, 0, 0, 0));
|
||||
//
|
||||
// i2c i2cSensor = i2c(&i2c_master_port);
|
||||
// i2cSensor.open();
|
||||
//
|
||||
// // -----------------------------------------------------------------------------------------------------------------
|
||||
// // I2C RGB Sensor on I2C MasterBus for sensors
|
||||
// //
|
||||
// isl29125 rgbSensor = isl29125(0x44, i2cSensor);
|
||||
// rgbSensor.initialize();
|
||||
// vTaskDelay(100);
|
||||
// rgbSensor.setMode(isl29125::RGB);
|
||||
// rgbSensor.setRange(isl29125::HIGH);
|
||||
// rgbSensor.setResolution(isl29125::RES_16BIT);
|
||||
// vTaskDelay(100);
|
||||
// struct isl29125::rgb_t rgbValue;
|
||||
// rgbSensor.getRGB(&rgbValue);
|
||||
//
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
// I2C Masterbus for sensoring peripherals
|
||||
//
|
||||
i2c_port_t i2c_master_port = I2C_NUM_0;
|
||||
|
||||
i2c_config_t i2cConfig = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = (int)2,
|
||||
.scl_io_num = (int)3,
|
||||
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.master = 400000,
|
||||
.clk_flags = 0
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(i2c_param_config(i2c_master_port, &i2cConfig));
|
||||
ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, i2cConfig.mode, 0, 0, 0));
|
||||
|
||||
i2c i2cSensor = i2c(&i2c_master_port);
|
||||
i2cSensor.open();
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
// I2C RGB Sensor on I2C MasterBus for sensors
|
||||
//
|
||||
isl29125 rgbSensor = isl29125(0x44, i2cSensor);
|
||||
rgbSensor.initialize();
|
||||
vTaskDelay(100);
|
||||
rgbSensor.setMode(isl29125::RGB);
|
||||
rgbSensor.setRange(isl29125::HIGH);
|
||||
rgbSensor.setResolution(isl29125::RES_16BIT);
|
||||
vTaskDelay(100);
|
||||
struct isl29125::rgb_t rgbValue;
|
||||
rgbSensor.getRGB(&rgbValue);
|
||||
|
||||
// gpio debugIO = gpio(19, gpio::Direction_t::GPIO_DIRECTION_OUTPUT, gpio::Value_t::GPIO_VALUE_LOW);
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
@@ -153,6 +154,11 @@ extern "C" void app_main(void)
|
||||
Wifi wifi;
|
||||
wifi.start_client();
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
// Programmable LEDs in a strip
|
||||
//
|
||||
prgm_ledstrip ledstrip = prgm_ledstrip(1, 8);
|
||||
|
||||
|
||||
while(1)
|
||||
{
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "driver/rmt_encoder.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Type of led strip encoder configuration
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t resolution; /*!< Encoder resolution, in Hz */
|
||||
} led_strip_encoder_config_t;
|
||||
|
||||
/**
|
||||
* @brief Create RMT encoder for encoding LED strip pixels into RMT symbols
|
||||
*
|
||||
* @param[in] config Encoder configuration
|
||||
* @param[out] ret_encoder Returned encoder handle
|
||||
* @return
|
||||
* - ESP_ERR_INVALID_ARG for any invalid arguments
|
||||
* - ESP_ERR_NO_MEM out of memory when creating led strip encoder
|
||||
* - ESP_OK if creating encoder successfully
|
||||
*/
|
||||
esp_err_t rmt_new_led_strip_encoder(const led_strip_encoder_config_t *config, rmt_encoder_handle_t *ret_encoder);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,124 +0,0 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_check.h"
|
||||
#include "led_strip_encoder.h"
|
||||
|
||||
static const char *TAG = "led_encoder";
|
||||
|
||||
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;
|
||||
|
||||
static size_t rmt_encode_led_strip(rmt_encoder_t *encoder, rmt_channel_handle_t channel, const void *primary_data, size_t data_size, rmt_encode_state_t *ret_state)
|
||||
{
|
||||
rmt_led_strip_encoder_t *led_encoder = __containerof(encoder, rmt_led_strip_encoder_t, base);
|
||||
rmt_encoder_handle_t bytes_encoder = led_encoder->bytes_encoder;
|
||||
rmt_encoder_handle_t copy_encoder = led_encoder->copy_encoder;
|
||||
rmt_encode_state_t session_state = 0;
|
||||
rmt_encode_state_t state = 0;
|
||||
size_t encoded_symbols = 0;
|
||||
switch (led_encoder->state) {
|
||||
case 0: // send RGB data
|
||||
encoded_symbols += bytes_encoder->encode(bytes_encoder, channel, primary_data, data_size, &session_state);
|
||||
if (session_state & RMT_ENCODING_COMPLETE) {
|
||||
led_encoder->state = 1; // switch to next state when current encoding session finished
|
||||
}
|
||||
if (session_state & RMT_ENCODING_MEM_FULL) {
|
||||
state |= RMT_ENCODING_MEM_FULL;
|
||||
goto out; // yield if there's no free space for encoding artifacts
|
||||
}
|
||||
// fall-through
|
||||
case 1: // send reset code
|
||||
encoded_symbols += copy_encoder->encode(copy_encoder, channel, &led_encoder->reset_code,
|
||||
sizeof(led_encoder->reset_code), &session_state);
|
||||
if (session_state & RMT_ENCODING_COMPLETE) {
|
||||
led_encoder->state = 0; // back to the initial encoding session
|
||||
state |= RMT_ENCODING_COMPLETE;
|
||||
}
|
||||
if (session_state & RMT_ENCODING_MEM_FULL) {
|
||||
state |= RMT_ENCODING_MEM_FULL;
|
||||
goto out; // yield if there's no free space for encoding artifacts
|
||||
}
|
||||
}
|
||||
out:
|
||||
*ret_state = state;
|
||||
return encoded_symbols;
|
||||
}
|
||||
|
||||
static esp_err_t rmt_del_led_strip_encoder(rmt_encoder_t *encoder)
|
||||
{
|
||||
rmt_led_strip_encoder_t *led_encoder = __containerof(encoder, rmt_led_strip_encoder_t, base);
|
||||
rmt_del_encoder(led_encoder->bytes_encoder);
|
||||
rmt_del_encoder(led_encoder->copy_encoder);
|
||||
free(led_encoder);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t rmt_led_strip_encoder_reset(rmt_encoder_t *encoder)
|
||||
{
|
||||
rmt_led_strip_encoder_t *led_encoder = __containerof(encoder, rmt_led_strip_encoder_t, base);
|
||||
rmt_encoder_reset(led_encoder->bytes_encoder);
|
||||
rmt_encoder_reset(led_encoder->copy_encoder);
|
||||
led_encoder->state = 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_new_led_strip_encoder(const led_strip_encoder_config_t *config, rmt_encoder_handle_t *ret_encoder)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
rmt_led_strip_encoder_t *led_encoder = NULL;
|
||||
ESP_GOTO_ON_FALSE(config && ret_encoder, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
|
||||
led_encoder = calloc(1, sizeof(rmt_led_strip_encoder_t));
|
||||
ESP_GOTO_ON_FALSE(led_encoder, ESP_ERR_NO_MEM, err, TAG, "no mem for led strip encoder");
|
||||
led_encoder->base.encode = rmt_encode_led_strip;
|
||||
led_encoder->base.del = rmt_del_led_strip_encoder;
|
||||
led_encoder->base.reset = rmt_led_strip_encoder_reset;
|
||||
// different led strip might have its own timing requirements, following parameter is for WS2812
|
||||
rmt_bytes_encoder_config_t bytes_encoder_config = {
|
||||
.bit0 = {
|
||||
.level0 = 1,
|
||||
.duration0 = 0.3 * config->resolution / 1000000, // T0H=0.3us
|
||||
.level1 = 0,
|
||||
.duration1 = 0.9 * config->resolution / 1000000, // T0L=0.9us
|
||||
},
|
||||
.bit1 = {
|
||||
.level0 = 1,
|
||||
.duration0 = 0.9 * config->resolution / 1000000, // T1H=0.9us
|
||||
.level1 = 0,
|
||||
.duration1 = 0.3 * config->resolution / 1000000, // T1L=0.3us
|
||||
},
|
||||
.flags.msb_first = 1 // WS2812 transfer bit order: G7...G0R7...R0B7...B0
|
||||
};
|
||||
ESP_GOTO_ON_ERROR(rmt_new_bytes_encoder(&bytes_encoder_config, &led_encoder->bytes_encoder), err, TAG, "create bytes encoder failed");
|
||||
rmt_copy_encoder_config_t copy_encoder_config = {};
|
||||
ESP_GOTO_ON_ERROR(rmt_new_copy_encoder(©_encoder_config, &led_encoder->copy_encoder), err, TAG, "create copy encoder failed");
|
||||
|
||||
uint32_t reset_ticks = config->resolution / 1000000 * 50 / 2; // reset code duration defaults to 50us
|
||||
led_encoder->reset_code = (rmt_symbol_word_t) {
|
||||
.level0 = 0,
|
||||
.duration0 = reset_ticks,
|
||||
.level1 = 0,
|
||||
.duration1 = reset_ticks,
|
||||
};
|
||||
*ret_encoder = &led_encoder->base;
|
||||
return ESP_OK;
|
||||
err:
|
||||
if (led_encoder) {
|
||||
if (led_encoder->bytes_encoder) {
|
||||
rmt_del_encoder(led_encoder->bytes_encoder);
|
||||
}
|
||||
if (led_encoder->copy_encoder) {
|
||||
rmt_del_encoder(led_encoder->copy_encoder);
|
||||
}
|
||||
free(led_encoder);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// \file prgm_ledstrip.h
|
||||
/// \brief File description
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
//
|
||||
// vbchaos software design
|
||||
//
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// $Revision: $
|
||||
/// $Author: $
|
||||
/// $Date: $
|
||||
// (c) 2023 vbchaos
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
#ifndef MAIN_PLATFORM_INC_PRGM_LEDSTRIP_H_
|
||||
#define MAIN_PLATFORM_INC_PRGM_LEDSTRIP_H_
|
||||
|
||||
/**
|
||||
* prgm_ledstrip implementation
|
||||
* \defgroup prgm_ledstrip
|
||||
* \brief {group_description}
|
||||
* \addtogroup {Layer}
|
||||
*
|
||||
* Detailed description
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Include files
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
// CompilerIncludes
|
||||
// All include files that are provided by the compiler directly
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
// ProjectIncludes
|
||||
// All include files that are provided by the project
|
||||
#include "FunctionStatus.h"
|
||||
|
||||
#include "led_strip.h"
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Type definitions.
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
class prgm_ledstrip
|
||||
{
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
// Public Section
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
public:
|
||||
|
||||
struct pixel
|
||||
{
|
||||
uint32_t index;
|
||||
uint8_t red;
|
||||
uint8_t green;
|
||||
uint8_t blue;
|
||||
};
|
||||
|
||||
// Class Constructor
|
||||
prgm_ledstrip(uint32_t numberOfLEDs, uint32_t gpio);
|
||||
|
||||
FunctionStatus setPixel(struct pixel& pixel);
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
// Protected Section
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
protected:
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
// Private Section
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
private:
|
||||
uint32_t numberOfLEDs;
|
||||
uint32_t gpio;
|
||||
|
||||
led_strip_handle_t led_strip;
|
||||
led_strip_config_t strip_config;
|
||||
led_strip_rmt_config_t rmt_config;
|
||||
|
||||
};
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
#endif /* MAIN_PLATFORM_INC_PRGM_LEDSTRIP_H_ */
|
||||
@@ -0,0 +1,83 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// \file prgm_ledstrip.cpp
|
||||
/// \brief Description
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
//
|
||||
// vbchaos software design
|
||||
//
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// $Revision: $
|
||||
/// $Author: $
|
||||
/// $Date: $
|
||||
// (c) 2023 vbchaos
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Include files
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <prgm_ledstrip.h>
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Type definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// File-scope variables
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Function definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
prgm_ledstrip::prgm_ledstrip(uint32_t numberOfLEDs, uint32_t gpio) : numberOfLEDs(numberOfLEDs), gpio(gpio)
|
||||
{
|
||||
/* LED strip initialization with the GPIO and pixels number*/
|
||||
memset(&strip_config, 0, sizeof(strip_config));
|
||||
|
||||
strip_config.strip_gpio_num = 8; // The GPIO that connected to the LED strip's data line
|
||||
strip_config.max_leds = 2; // The number of LEDs in the strip,
|
||||
strip_config.led_pixel_format = LED_PIXEL_FORMAT_GRB; // Pixel format of your LED strip
|
||||
strip_config.led_model = LED_MODEL_WS2812; // LED strip model
|
||||
strip_config.flags.invert_out = false; // whether to invert the output signal (useful when your hardware has a level inverter)
|
||||
|
||||
memset(&rmt_config, 0, sizeof(rmt_config));
|
||||
|
||||
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
rmt_config.rmt_channel = 0;
|
||||
#else
|
||||
rmt_config.clk_src = RMT_CLK_SRC_DEFAULT; // different clock source can lead to different power consumption
|
||||
rmt_config.resolution_hz = 10 * 1000 * 1000; // 10MHz
|
||||
rmt_config.flags.with_dma = false; // whether to enable the DMA feature
|
||||
#endif
|
||||
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
|
||||
|
||||
}
|
||||
|
||||
FunctionStatus prgm_ledstrip::setPixel(struct pixel& pixel)
|
||||
{
|
||||
FunctionStatus returnValue = FUNCTION_STATUS_OK;
|
||||
|
||||
ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, pixel.index, pixel.red, pixel.green, pixel.blue));
|
||||
ESP_ERROR_CHECK(led_strip_refresh(led_strip));
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
Reference in New Issue
Block a user