首页 > 其他分享 >ESP8266--SDK开发(驱动WS2812B)

ESP8266--SDK开发(驱动WS2812B)

时间:2022-11-12 10:04:30浏览次数:43  
标签:ESP8266 -- MAX void uint8 os uint32 WS2812B 255


文章目录

  • ​​一、WS2812彩灯介绍​​
  • ​​二、效果展示​​
  • ​​三、七色灯代码​​
  • ​​附:彩虹七色码值​​

一、WS2812彩灯介绍

WS2812是一个集控制电路与发光电路于一体的智能外控LED光源,外型与5050LED灯珠相同,每个灯珠都是一个像素点。像素点内包含数字接口、数据锁存、信号整形放大驱动电路,还包含高精度的内部振荡器和12V高压可编程定电流控制部分。

ESP8266--SDK开发(驱动WS2812B)_像素点


数据协议采用单线归零码的通讯方式,像素点在上电复位以后,DIN端接受从控制器传输过来的数据,首先送过来的24bit数据被第一个像素点提取后,送到像素点内部的数据锁存器,剩余的数据经过内部整形处理电路整形放大后通过DO端口开始转发输出给下一个级联的像素点,每经过一个像素点的传输,信号减少24bit。像素点采用自动整形转发技术,使得该像素点的级联个数不受信号传送的限制,仅仅受限信号传输速度要求。

ESP8266--SDK开发(驱动WS2812B)_像素点_02

按照数据传输协议即可对WS2812进行驱动,当然对于WS2812有许多开源的驱动库,例如:Adafruit_NeoPixel驱动库

#ifndef _Adafruit_NeoPixel_H
#define _Adafruit_NeoPixel_H

#include "c_types.h"
#include "user_interface.h"
#include "ets_sys.h"
#include "gpio.h"

//thanks for https://github.com/cnlohr/ws2812esp8266
//thanks for https://github.com/adafruit/Adafruit_NeoPixel

#define WSGPIO 0 //must use the ESP8266 GPIO0 as the pin to drive WS2812B RGB LED!!!
//user can change
#define PIXEL_MAX 1//the total numbers of LEDs you are used in your project

//You will have to os_intr_lock(); os_intr_unlock();

void setAllColor(uint8_t r, uint8_t g, uint8_t b);
void setOneColor(uint16_t n, uint32_t c);
//void setAllColor(uint32_t c);
void cleanOneColor(uint16_t n, uint32_t c);
void cleanAllColor(void);

void setAllPixel(void);

uint32_t Color(uint8_t r, uint8_t g, uint8_t b);
uint32_t Wheel(uint8_t WheelPos);
void rainbowCycle(uint8_t wait) ;
void theaterChase(uint32_t c, uint8_t wait);
void theaterChaseRainbow(uint8_t wait);
void colorWipe(uint32_t c, uint8_t wait);
void WS2812B_Test(void);
void ICACHE_FLASH_ATTR WS2812B_Init(void);

uint32_t changeL(uint32_t rgb, float k);

#endif
#include "driver/Adafruit_NeoPixel.h"
#include "ets_sys.h"
#include "osapi.h"
/*
#define GPIO_OUTPUT_SET(gpio_no, bit_value) \
gpio_output_set(bit_value<<gpio_no, ((~bit_value)&0x01)<<gpio_no, 1<<gpio_no,0)
*/

//I just used a scope to figure out the right time periods.
void SEND_WS_0() {
uint8_t time;
time = 3;
while (time--)
WRITE_PERI_REG(PERIPHS_GPIO_BASEADDR + GPIO_ID_PIN(WSGPIO), 1);
time = 7;
while (time--)
WRITE_PERI_REG(PERIPHS_GPIO_BASEADDR + GPIO_ID_PIN(WSGPIO), 0);
}

void SEND_WS_1() {
uint8_t time;
time = 7;
while (time--)
WRITE_PERI_REG(PERIPHS_GPIO_BASEADDR + GPIO_ID_PIN(WSGPIO), 1);
time = 3;
while (time--)
WRITE_PERI_REG(PERIPHS_GPIO_BASEADDR + GPIO_ID_PIN(WSGPIO), 0);
}

void // ICACHE_FLASH_ATTR
WS2812Send_8bit(uint8_t dat) {
uint16_t i;
GPIO_OUTPUT_SET(GPIO_ID_PIN(WSGPIO), 0);
ets_intr_lock();
uint8_t mask = 0x80;
uint8_t byte = dat;
while (mask) {
if (byte & mask)
SEND_WS_1();
else
SEND_WS_0();
mask >>= 1;
}

ets_intr_unlock();
}

//GRB format,MSB firsr.
void //ICACHE_FLASH_ATTR
WS2812BSend_24bit(uint8_t R, uint8_t G, uint8_t B) {
WS2812Send_8bit(G);
WS2812Send_8bit(R);
WS2812Send_8bit(B);
}
//delay for millisecond
void HAL_Delay(int time) {
os_delay_us(time * 1000);
}

static uint8_t rBuffer[PIXEL_MAX] = { 0 };
static uint8_t gBuffer[PIXEL_MAX] = { 0 };
static uint8_t bBuffer[PIXEL_MAX] = { 0 };

void setAllColor(uint8_t r, uint8_t g, uint8_t b)
{
uint8_t i;
for (i = 0; i < PIXEL_MAX; i++) {
rBuffer[i] = r;
gBuffer[i] = g;
bBuffer[i] = b;
}
}

void setOneColor(uint16_t n, uint32_t c) {
rBuffer[n] = (uint8_t) (c >> 16);
gBuffer[n] = (uint8_t) (c >> 8);
bBuffer[n] = (uint8_t) c;
}

//void setAllColor(uint32_t c) {
// uint8_t r, g, b;
// r = (uint8_t) (c >> 16);
// g = (uint8_t) (c >> 8);
// b = (uint8_t) c;
// uint8_t i;
// for (i = 0; i < PIXEL_MAX; i++) {
// rBuffer[i] = r;
// gBuffer[i] = g;
// bBuffer[i] = b;
// }
//}

void cleanOneColor(uint16_t n, uint32_t c) {
rBuffer[n] = 0;
gBuffer[n] = 0;
bBuffer[n] = 0;
}

void cleanAllColor() {
uint8_t i;
for (i = 0; i < PIXEL_MAX; i++) {
rBuffer[i] = 0;
gBuffer[i] = 0;
bBuffer[i] = 0;
}
}

void setAllPixel(void) {
uint8_t i;
for (i = 0; i < PIXEL_MAX; i++) {
WS2812BSend_24bit(rBuffer[i], gBuffer[i], bBuffer[i]);
}
}

uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
return ((uint32_t) r << 16) | ((uint32_t) g << 8) | b;
}
uint32_t Wheel(uint8_t WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

void rainbow(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256; j++) {
for (i = 0; i < PIXEL_MAX; i++) {
setOneColor(i, Wheel((i + j) & 255));
}
HAL_Delay(wait);
}
setAllPixel();
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;

for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
for (i = 0; i < PIXEL_MAX; i++) {
setOneColor(i, Wheel(((i * 256 / PIXEL_MAX) + j) & 255));
}
HAL_Delay(wait);
}
setAllPixel();
}
//Theatre-style crawling lights.o??¨¹¦Ì?
void theaterChase(uint32_t c, uint8_t wait) {
int i, j, q;
for (j = 0; j < 10; j++) { //do 10 cycles of chasing
for (q = 0; q < 3; q++) {
for (i = 0; i < PIXEL_MAX; i = i + 1) //turn every one pixel on
{
setOneColor(i + q, c);
}
HAL_Delay(wait);

for (i = 0; i < PIXEL_MAX; i = i + 1) //turn every one pixel off
{
setOneColor(i + q, 0);
}
}
}
setAllPixel();
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
int i, j, q;
for (j = 0; j < 256; j++) { // cycle all 256 colors in the wheel
for (q = 0; q < 3; q++) {
for (i = 0; i < PIXEL_MAX; i = i + 1) //turn every one pixel on
{
setOneColor(i + q, Wheel((i + j) % 255));
}

HAL_Delay(wait);

for (i = 0; i < PIXEL_MAX; i = i + 1) //turn every one pixel off
{
setOneColor(i + q, 0);
}
}
}
setAllPixel();
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
uint16_t i = 0;
for (i = 0; i < PIXEL_MAX; i++) {
setOneColor(i, c);
HAL_Delay(wait);
}
}

void //ICACHE_FLASH_ATTR
WS2812B_Init(void) {
setAllColor(0,0,0);
setAllPixel();
}

void WS2812B_Test(void) {
HAL_Delay(500);
rainbowCycle(20);
theaterChaseRainbow(50);
}

使用注意事项

1、将文件复制到自己的SDK工程

复制文件到文件夹,Clean Project一下就会自动加载到IDE中

ESP8266--SDK开发(驱动WS2812B)_ESP8266_03


ESP8266--SDK开发(驱动WS2812B)_ESP8266_04


2、WS2812B的数据线一定要使用GPIO0

ESP8266--SDK开发(驱动WS2812B)_像素点_05


3、设置WS2812B的小灯个数

ESP8266--SDK开发(驱动WS2812B)_嵌入式硬件_06


4、导入头文件并初始化

#include "driver/Adafruit_NeoPixel.h"

//初始化GPIO0
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0);
GPIO_OUTPUT_SET(0, 0);
os_delay_us(200);

//初始化WS2812B
WS2812B_Init();

5、设置/清除RGB的值

设置了RGB的值,只是将颜色值写入颜色缓存数组而已,必须发送到WS2812B才能改变颜色

ESP8266--SDK开发(驱动WS2812B)_嵌入式硬件_07


5、发送RGB的值

ESP8266--SDK开发(驱动WS2812B)_单片机_08


二、效果展示

ESP8266--SDK开发(驱动WS2812B)_#include_09


三、七色灯代码

#include "driver/Adafruit_NeoPixel.h"

/******************************************************************************
* FunctionName : user_init
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_init(void)
{
uart_init(115200,115200);
os_delay_us(1000);
os_printf("\r\n=================================================\r\n");
os_printf("\t Project:\t%s\r\n", ProjectName);
os_printf("\t SDK version:\t%s", system_get_sdk_version());
os_printf("\r\n=================================================\r\n");

PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0);
GPIO_OUTPUT_SET(0, 0);
os_delay_us(200);

WS2812B_Init();

while(1) {
//红
setAllColor(255,0,0);
setAllPixel();
os_delay_us(300000);

//橙
setAllColor(255,165,0);
setAllPixel();
os_delay_us(300000);

//黄
setAllColor(255,255,0);
setAllPixel();
os_delay_us(300000);

//绿
setAllColor(0,255,0);
setAllPixel();
os_delay_us(300000);

//蓝
setAllColor(0,0,255);
setAllPixel();
os_delay_us(300000);

//靛
setAllColor(0,255,255);
setAllPixel();
os_delay_us(300000);

//紫
setAllColor(139,0,255);
setAllPixel();
os_delay_us(300000);
}
}

附:彩虹七色码值

彩虹七色16进制码
红色 #FF0000
橙色 #FF7F00
黄色 #FFFF00
绿色 #00FF00
青色 #00FFFF
蓝色 #0000FF
紫色 #8B00FF

彩虹七色RGB值
赤色 【RGB】255, 0, 0
橙色 【RGB】 255, 165, 0
黄色 【RGB】255, 255, 0
绿色 【RGB】0, 255, 0
青色 【RGB】0, 255, 255
蓝色 【RGB】0, 0, 255
紫色 【RGB】139, 0, 255


标签:ESP8266,--,MAX,void,uint8,os,uint32,WS2812B,255
From: https://blog.51cto.com/wangjichuan/5846517

相关文章

  • idea的代码突然消失了怎么复原
    错误场景:我在IDEA开发项目过程中,修改了代码,想要提交我写的代码就点击了idea中自带的git的拉取代码按钮,但是手贱的我又点击了idea下面进度条的关闭。问题描述以上操作直接导......
  • 10. 对象深拷贝问题
    首先,要知道基本数据保存在栈内存,对象数据保存在堆内存,对象地址(就是变量名)保存在栈内存,浅拷贝只会复制栈内存的内容,而深拷贝会复制栈和堆内存中的内容;所以深拷贝的对象有......
  • MyBatisPlus查询字段值为null时,不会被映射返回的问题
    背景:最近在加入了公司新的项目组,开发新的项目,项目采用前后端分离的开发模式,在做列表查询接口时,需要多表的关联查询,我的做法是直接在mapper.xml中写了sql,然后直接返回数据,查......
  • python 某猫投诉加密解析
    importhashlibimportjsonimportrandomimporttimeimportrequestssha256=hashlib.sha256()'''某猫投诉https://jiangsu.tousu.sina.com.cn/'''classBlackCatComplain......
  • execsnoop安装
    execsnoop就是一个专为短时进程设计的工具。它通过ftrace实时监控进程的exec()行为,并输出短时进程的基本信息,包括进程PID、父进程PID、命令行参数以及执行的结果。ex......
  • MAUI新生-XAML语法基础:语法入门Element&Property&Event&Command
    一、XAML(MAUI的XAML)和HTML两者相似,都是标签语言(也叫标记)组成的树形文档。每个标签元素,可视为一个对象,通过“键=值”形式的标签属性(Attribute),为对象的属性(Property)、事件......
  • 解决服务器I/O过高的问题
    问题缘起:当我习惯性地用top查看任务运行状态时,发现我运行的100个任务,只有3个在运行,其他都在摸鱼状态。同时发现我的任务进程都是"D"状态(未截图),而不是R(运行)状态。1.top......
  • 写作满分范文2
    DueAttentionShouldBeGiventoSpelling如今不少学生在英语学习中不重视拼写;出现这种情况的原因;这种现象导致的结果;Nowadaysfewerandfewerstudentspayattentiont......
  • 5.信号处理(1) --常用信号平滑去噪的方法
    前言:最近研究汽车碰撞的加速度信号,在信号的采集过程中难免遇到噪音,导致信号偏差,为了更好的反映系统情况,故常需要信号去噪,本文分享一些常用信号平滑去噪的方法。关键字:信号;去......
  • 8.图像视觉(1) --鱼眼摄像头标定和图像畸变矫正
    前言:最近项目上研究鱼眼摄像头的画面畸变问题,对比了基于Matlab和PythonOpencv的方法,分别进行了摄像头的标定和图像矫正,实际结果个人认为Opencv的效果为佳,本文分享一下基于M......