mirror of
https://github.com/badgeteam/mch2022-template-app.git
synced 2024-12-23 07:36:46 +00:00
Merge pull request #20 from badgeteam/renze/ws2812-as-module
Move WS2812 driver to it's own repository
This commit is contained in:
commit
28123c8b62
5 changed files with 4 additions and 163 deletions
3
.gitmodules
vendored
3
.gitmodules
vendored
|
@ -22,3 +22,6 @@
|
||||||
[submodule "components/mch2022-rp2040"]
|
[submodule "components/mch2022-rp2040"]
|
||||||
path = components/mch2022-rp2040
|
path = components/mch2022-rp2040
|
||||||
url = https://github.com/badgeteam/esp32-component-mch2022-rp2040.git
|
url = https://github.com/badgeteam/esp32-component-mch2022-rp2040.git
|
||||||
|
[submodule "components/ws2812"]
|
||||||
|
path = components/ws2812
|
||||||
|
url = https://github.com/badgeteam/esp32-component-ws2812.git
|
||||||
|
|
1
components/ws2812
Submodule
1
components/ws2812
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit c1c750d2ce0a487622fb311cce19ea899d49ffe6
|
|
@ -1,4 +0,0 @@
|
||||||
idf_component_register(
|
|
||||||
SRCS "ws2812.c"
|
|
||||||
INCLUDE_DIRS include
|
|
||||||
)
|
|
|
@ -1,35 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <esp_err.h>
|
|
||||||
#include <driver/gpio.h>
|
|
||||||
|
|
||||||
__BEGIN_DECLS
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize the leds driver. (configure SPI bus and GPIO pins)
|
|
||||||
* @return ESP_OK on success; any other value indicates an error
|
|
||||||
*/
|
|
||||||
extern esp_err_t ws2812_init(gpio_num_t aGpioPin);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Enable power to the leds bar.
|
|
||||||
* @return ESP_OK on success; any other value indicates an error
|
|
||||||
*/
|
|
||||||
extern esp_err_t ws2812_enable(int gpio_pin);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Disable power to the leds bar.
|
|
||||||
* @return ESP_OK on success; any other value indicates an error
|
|
||||||
*/
|
|
||||||
extern esp_err_t ws2812_disable(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send color-data to the leds bus.
|
|
||||||
* @param data the data-bytes to send on the bus.
|
|
||||||
* @param len the data-length.
|
|
||||||
* @return ESP_OK on success; any other value indicates an error
|
|
||||||
*/
|
|
||||||
extern esp_err_t ws2812_send_data(uint8_t *data, int len);
|
|
||||||
|
|
||||||
__END_DECLS
|
|
|
@ -1,124 +0,0 @@
|
||||||
//This driver uses the ESP32 RMT peripheral to drive "Neopixel" compatible LEDs
|
|
||||||
//The usage of the RMT peripheral has been implemented using work by JSchaenzie:
|
|
||||||
//you can find his work at https://github.com/JSchaenzle/ESP32-NeoPixel-WS2812-RMT
|
|
||||||
|
|
||||||
#include <sdkconfig.h>
|
|
||||||
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <freertos/FreeRTOS.h>
|
|
||||||
#include <freertos/task.h>
|
|
||||||
#include <esp_err.h>
|
|
||||||
#include <esp_log.h>
|
|
||||||
|
|
||||||
#include <driver/gpio.h>
|
|
||||||
#include <driver/rmt.h>
|
|
||||||
|
|
||||||
static const char *TAG = "ws2812";
|
|
||||||
|
|
||||||
#define WS2812_RMT_CHANNEL RMT_CHANNEL_0
|
|
||||||
|
|
||||||
#define T0H 14 // 0 bit high time
|
|
||||||
#define T1H 52 // 1 bit high time
|
|
||||||
#define TL 52 // low time for either bit
|
|
||||||
|
|
||||||
static bool gActive = false;
|
|
||||||
rmt_item32_t* gBuffer = NULL;
|
|
||||||
int gBufferLength = 0;
|
|
||||||
gpio_num_t gPin;
|
|
||||||
|
|
||||||
esp_err_t ws2812_init(gpio_num_t aGpioPin) {
|
|
||||||
if (gActive) return ESP_OK;
|
|
||||||
gpio_config_t io_conf = {
|
|
||||||
.intr_type = GPIO_INTR_DISABLE,
|
|
||||||
.mode = GPIO_MODE_OUTPUT,
|
|
||||||
.pin_bit_mask = 1LL << aGpioPin,
|
|
||||||
.pull_down_en = 0,
|
|
||||||
.pull_up_en = 0,
|
|
||||||
};
|
|
||||||
esp_err_t res = gpio_config(&io_conf);
|
|
||||||
if (res != ESP_OK) return res;
|
|
||||||
rmt_config_t config;
|
|
||||||
config.rmt_mode = RMT_MODE_TX;
|
|
||||||
config.channel = WS2812_RMT_CHANNEL;
|
|
||||||
config.gpio_num = aGpioPin;
|
|
||||||
config.mem_block_num = 3;
|
|
||||||
config.tx_config.loop_en = false;
|
|
||||||
config.tx_config.carrier_en = false;
|
|
||||||
config.tx_config.idle_output_en = true;
|
|
||||||
config.tx_config.idle_level = 0;
|
|
||||||
config.clk_div = 2;
|
|
||||||
res = rmt_config(&config);
|
|
||||||
if (res != ESP_OK) return res;
|
|
||||||
res = rmt_driver_install(config.channel, 0, 0);
|
|
||||||
if (res != ESP_OK) return res;
|
|
||||||
gActive = true;
|
|
||||||
gPin = aGpioPin;
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t ws2812_deinit(void) {
|
|
||||||
if (!gActive) return ESP_OK;
|
|
||||||
esp_err_t res = rmt_driver_uninstall(WS2812_RMT_CHANNEL);
|
|
||||||
if (res != ESP_OK) return res;
|
|
||||||
gpio_config_t io_conf = {
|
|
||||||
.intr_type = GPIO_INTR_DISABLE,
|
|
||||||
.mode = GPIO_MODE_INPUT,
|
|
||||||
.pin_bit_mask = 1LL << gPin,
|
|
||||||
.pull_down_en = 0,
|
|
||||||
.pull_up_en = 0,
|
|
||||||
};
|
|
||||||
res = gpio_config(&io_conf);
|
|
||||||
if (res != ESP_OK) return res;
|
|
||||||
gActive = false;
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t ws2812_prepare_data(uint8_t *data, int len)
|
|
||||||
{
|
|
||||||
if (gBuffer != NULL) return ESP_FAIL;
|
|
||||||
gBuffer = calloc(len * 8, sizeof(rmt_item32_t));
|
|
||||||
if (gBuffer == NULL) return ESP_FAIL;
|
|
||||||
gBufferLength = len * 8;
|
|
||||||
for (uint32_t pos = 0; pos < len; pos++) {
|
|
||||||
uint32_t mask = 1 << 7;
|
|
||||||
for (uint8_t i = 0; i < 8; i++) {
|
|
||||||
bool bit = data[pos] & mask;
|
|
||||||
gBuffer[pos*8 + i] = bit ?
|
|
||||||
(rmt_item32_t){{{T1H, 1, TL, 0}}} :
|
|
||||||
(rmt_item32_t){{{T0H, 1, TL, 0}}};
|
|
||||||
mask >>= 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t ws2812_free_data() {
|
|
||||||
if (!gBuffer) return ESP_FAIL;
|
|
||||||
free(gBuffer);
|
|
||||||
gBuffer = NULL;
|
|
||||||
gBufferLength = 0;
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t ws2812_send_data(uint8_t *data, int len)
|
|
||||||
{
|
|
||||||
if (!gActive) return ESP_FAIL;
|
|
||||||
esp_err_t res = ws2812_prepare_data(data, len);
|
|
||||||
if (res != ESP_OK) return res;
|
|
||||||
res = rmt_write_items(WS2812_RMT_CHANNEL, gBuffer, gBufferLength, false);
|
|
||||||
if (res != ESP_OK) {
|
|
||||||
ws2812_free_data();
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
res = rmt_wait_tx_done(WS2812_RMT_CHANNEL, portMAX_DELAY);
|
|
||||||
if (res != ESP_OK) {
|
|
||||||
ws2812_free_data();
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
res = ws2812_free_data();
|
|
||||||
return res;
|
|
||||||
}
|
|
Loading…
Reference in a new issue