Skip to content

Commit

Permalink
2019-06-03 Add ws2812 driver and examples
Browse files Browse the repository at this point in the history
2019-06-03 Add ws2812 driver and examples
  • Loading branch information
xuhongv committed Jun 3, 2019
1 parent 4f5c890 commit fc85150
Show file tree
Hide file tree
Showing 8 changed files with 1,493 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 10_ws2812_RMT/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#

PROJECT_NAME := esp32-ws2812

include $(IDF_PATH)/make/project.mk

7 changes: 7 additions & 0 deletions 10_ws2812_RMT/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
WS2812 driver and rainbow demo by Chris Osborn <[email protected]>

This is a driver for the WS2812 RGB LEDs which uses the RMT peripheral
on the ESP32. It uses interrupts from the RMT peripheral to keep the
buffer filled until all the bytes have been sent to the LED array.

For more information see http://insentricity.com/a.cl/268
5 changes: 5 additions & 0 deletions 10_ws2812_RMT/components/ws2812/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
COMPONENT_SRCDIRS := .
LIB_PATH := $(COMPONENT_PATH)/lib/libws2812.a
COMPONENT_ADD_INCLUDEDIRS += ws2812/include
COMPONENT_ADD_LDFLAGS := $(LIB_PATH)
COMPONENT_ADD_LINKER_DEPS := $(LIB_PATH)
67 changes: 67 additions & 0 deletions 10_ws2812_RMT/components/ws2812/include/ws2812.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef WS2812_DRIVER_H
#define WS2812_DRIVER_H

#include <stdint.h>

typedef union {
struct __attribute__ ((packed)) {
uint8_t r, g, b;
};
uint32_t num;
} rgbValue;


/**
* @description: 转换rgb
* @param {type}
* @return:
*/
inline rgbValue getRGBValue(uint8_t r, uint8_t g, uint8_t b)
{
rgbValue v;
v.r = r;
v.g = g;
v.b = b;
return v;
}


/**
* @description: 初始化io口
* @param {type} gpio: 驱动ws2812的gpio口,比如 18
* counts: 灯珠个数
* @return:
*/
void ws2812_init(int gpio,unsigned int counts);

/**
* @description: 设置颜色
* @param {type} 此发送格式为rgb顺序
* @return:
*/
void ws2812_setColor(uint8_t r, uint8_t g, uint8_t b);

/**
* @description: 设置颜色
* @param {type} 此发送格式为grb顺序
* @return:
*/
void ws2812_setColor_grb(uint8_t g, uint8_t r, uint8_t b);


/**
* @description: 设置颜色数组,具体见demo
* @param {type}
* @return:
*/
void ws2812_setColors(rgbValue *valueArray);

/**
* @description: 设置灯珠个数
* @param {type}
* @return:
*/
void ws2812_setCounts(unsigned int counts);


#endif
10 changes: 10 additions & 0 deletions 10_ws2812_RMT/main/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# Main Makefile. This is basically the same as a component makefile.
#
# This Makefile should, at the very least, just include $(SDK_PATH)/make/component_common.mk. By default,
# this will take the sources in the src/ directory, compile them and link them into
# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#

#include $(IDF_PATH)/make/component_common.mk
115 changes: 115 additions & 0 deletions 10_ws2812_RMT/main/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@


#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <soc/rmt_struct.h>
#include <esp_system.h>
#include <nvs_flash.h>
#include <driver/gpio.h>
#include <stdio.h>
#include "ws2812.h"

#define WS2812_PIN 22
#define WS2812_COUNTS 20 // 灯珠个数
#define delay_ms(ms) vTaskDelay((ms) / portTICK_RATE_MS)

/**
* @description: 彩虹渐变效果演示
* @param {type}
* @return:
*/
void TaskRainbow(void *pvParameters)
{
const uint8_t anim_step = 10;
const uint8_t anim_max = 250;

const uint8_t delay = 25; // 2种颜色之间的渐变时间间隔

rgbValue color = getRGBValue(anim_max, 0, 0);
uint8_t step = 0;
rgbValue color2 = getRGBValue(anim_max, 0, 0);
uint8_t step2 = 0;
rgbValue *pixels;

pixels = malloc(sizeof(rgbValue) * WS2812_COUNTS);

while (1)
{
color = color2;
step = step2;

for (uint8_t i = 0; i < WS2812_COUNTS; i++)
{
pixels[i] = color;

if (i == 1)
{
color2 = color;
step2 = step;
}

switch (step)
{
case 0:
color.g += anim_step;
if (color.g >= anim_max)
step++;
break;
case 1:
color.r -= anim_step;
if (color.r == 0)
step++;
break;
case 2:
color.b += anim_step;
if (color.b >= anim_max)
step++;
break;
case 3:
color.g -= anim_step;
if (color.g == 0)
step++;
break;
case 4:
color.r += anim_step;
if (color.r >= anim_max)
step++;
break;
case 5:
color.b -= anim_step;
if (color.b == 0)
step = 0;
break;
}
}

ws2812_setColors(pixels);

delay_ms(delay);
}
}

void app_main()
{
nvs_flash_init();

//初始化
ws2812_init(WS2812_PIN, WS2812_COUNTS);
//彩虹渐变效果演示
//xTaskCreate(TaskRainbow, "TaskRainbow Demo", 4096, NULL, 10, NULL);


/* 下面演示如果看到实际效果应该是 红色---绿色---蓝色
* 但是如果看到的是 绿色---红色---蓝色 请替换调用 ws2812_setColor_grb()方法即可!
* 原因在于:不同的灯珠生产商家定义不一样
*/
while (1)
{
ws2812_setColor(254,0,0);//红色
delay_ms(1000);
ws2812_setColor(0,254,0);//绿色
delay_ms(1000);
ws2812_setColor(0,0,254);//蓝色
delay_ms(1000);
}
}
Loading

0 comments on commit fc85150

Please sign in to comment.