Skip to content

Commit

Permalink
add ws2812 example
Browse files Browse the repository at this point in the history
  • Loading branch information
rillhu committed Apr 13, 2020
1 parent 609ac21 commit 4d878b3
Show file tree
Hide file tree
Showing 10 changed files with 581 additions and 0 deletions.
3 changes: 3 additions & 0 deletions components/ws2812/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
COMPONENT_ADD_INCLUDEDIRS := .
COMPONENT_SRCDIRS := .

58 changes: 58 additions & 0 deletions components/ws2812/ws2812_control.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "ws2812_control.h"
#include "driver/rmt.h"

// Configure these based on your project needs ********
#define LED_RMT_TX_CHANNEL RMT_CHANNEL_0
#define LED_RMT_TX_GPIO 18
// ****************************************************

#define BITS_PER_LED_CMD 24
#define LED_BUFFER_ITEMS ((NUM_LEDS * BITS_PER_LED_CMD))

// These values are determined by measuring pulse timing with logic analyzer and adjusting to match datasheet.
#define T0H 14 // 0 bit high time
#define T1H 52 // 1 bit high time
#define TL 52 // low time for either bit

// This is the buffer which the hw peripheral will access while pulsing the output pin
rmt_item32_t led_data_buffer[LED_BUFFER_ITEMS];

void setup_rmt_data_buffer(struct led_state new_state);

void ws2812_control_init(void)
{
rmt_config_t config;
config.rmt_mode = RMT_MODE_TX;
config.channel = LED_RMT_TX_CHANNEL;
config.gpio_num = LED_RMT_TX_GPIO;
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;

ESP_ERROR_CHECK(rmt_config(&config));
ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));
}

void ws2812_write_leds(struct led_state new_state) {
setup_rmt_data_buffer(new_state);
ESP_ERROR_CHECK(rmt_write_items(LED_RMT_TX_CHANNEL, led_data_buffer, LED_BUFFER_ITEMS, false));
ESP_ERROR_CHECK(rmt_wait_tx_done(LED_RMT_TX_CHANNEL, portMAX_DELAY));
}

void setup_rmt_data_buffer(struct led_state new_state)
{
for (uint32_t led = 0; led < NUM_LEDS; led++) {
uint32_t bits_to_send = new_state.leds[led];
uint32_t mask = 1 << (BITS_PER_LED_CMD - 1);
for (uint32_t bit = 0; bit < BITS_PER_LED_CMD; bit++) {
uint32_t bit_is_set = bits_to_send & mask;
led_data_buffer[led * BITS_PER_LED_CMD + bit] = bit_is_set ?
(rmt_item32_t){{{T1H, 1, TL, 0}}} :
(rmt_item32_t){{{T0H, 1, TL, 0}}};
mask >>= 1;
}
}
}
24 changes: 24 additions & 0 deletions components/ws2812/ws2812_control.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef WS2812_CONTROL_H
#define WS2812_CONTROL_H
#include <stdint.h>

#ifndef NUM_LEDS
#define NUM_LEDS 30
#endif

// This structure is used for indicating what the colors of each LED should be set to.
// There is a 32bit value for each LED. Only the lower 3 bytes are used and they hold the
// Red (byte 2), Green (byte 1), and Blue (byte 0) values to be set.
struct led_state {
uint32_t leds[NUM_LEDS];
};

// Setup the hardware peripheral. Only call this once.
void ws2812_control_init(void);

// Update the LEDs to the new state. Call as needed.
// This function will block the current task until the RMT peripheral is finished sending
// the entire sequence.
void ws2812_write_leds(struct led_state new_state);

#endif
12 changes: 12 additions & 0 deletions examples/led-strip/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#

EXTRA_COMPONENT_DIRS += \
$(abspath ../../components/wolfssl)\
$(abspath ../../components/homekit)\
$(abspath ../../components/ws2812)

PROJECT_NAME := homekit-esp32-ledstrip
include $(IDF_PATH)/make/project.mk
2 changes: 2 additions & 0 deletions examples/led-strip/components/ledrgb/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COMPONENT_ADD_INCLUDEDIRS := ./
COMPONENT_SRCDIRS := ./
Loading

0 comments on commit 4d878b3

Please sign in to comment.