Files
pwm32e_pro/project/example/ble/ble_peripheral/app/src/ws2815.h
T
moyuhai 41a602f89c V1.0
2026-06-09 16:39:17 +08:00

219 lines
6.7 KiB
C
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*!
* \file ws2815.h
*
* \brief The header of ws2815.c
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip ) Alex-J
*
* \author ( XinChip )
*/
#ifndef __WS2815_H__
#define __WS2815_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <string.h>
#include "xc6xxx.h"
/* 模式选择宏定义 */
/* 可选值: SPI_DMA_MODE / GPIO_DELAY_MODE */
#define SPI_DMA_MODE (0)
#define GPIO_DELAY_MODE (1)
#define WS2815_CONTROL_MODE (SPI_DMA_MODE)
#define WS2815_TX_PIN (1) //GPIO_1
#define LEDC_RGB_CODE_LEN 7
//红外命令
#define ON 0x26
#define OFF 0x25
#define SPEED_IN 0x0A
#define SPEED_RE 0x08
#define MODE_IN 0x5B
#define MODE_RE 0x6E
#define IR_AUTO 0x5C
#define IR_INMUSIC 0x0F
#define IR_DEMUSIC 0x0B
#define IR_TIME1H 0x06 //中部
#define IR_TIME2H 0x5A
#define BRIGHTNESS_IN 0x09
#define BRIGHTNESS_RE 0x1C
#define IR_INTEMP 0x10
#define IR_DETEMP 0x53
#define IR_WHITE 0x52
#define IR_YELLOW 0x0C //7种颜色
#define IR_CHING 0x1A
#define IR_PINK 0x1F
#define IR_RED 0x01
#define IR_GREEN 0x00
#define IR_BLUE 0x02
//数据格式
#define LEDC_BRG 1
#define LEDC_BGR 2
#define LEDC_GRB 3
#define LEDC_GBR 4
#define LEDC_RBG 5
#define LEDC_RGB 6
// RGB format
#define Color_RED 0xff0000 //红色
#define Color_GREEN 0x00ff00 //绿色
#define Color_BLUE 0x0000ff //蓝色
#define Color_WHITE 0xffffff //白色
#define Color_YELLOW 0xffff00 //黄色
#define Color_ORANGE 0xff7800 //橙色
#define Color_BLACK 0x000000 //黑色
#define Color_PURPLE 0x9400D3 //紫色
#define Color_INDIGO 0x480082 //​靛色
#define Color_CHING 0x00ffff //​青色
#define Color_PINK 0xE2001C //粉色
//bool isModeSwitching = false; // 当前是否在切换模式
//色温调节相关数据
#define K2700 0xff5810 //色温阶段1
#define K3000 0xff6818 //色温阶段2
#define K3300 0xff7828 //色温阶段3
#define K3600 0xff8838 //色温阶段4
#define K3900 0xff9848 //色温阶段5
#define K4200 0xffa858 //色温阶段6
#define K4500 0xffb868 //色温阶段7
#define K5000 0xffc878 //色温阶段8
#define K5500 0xffd888 //色温阶段9
#define K6000 0xffe898 //色温阶段10
#define K6500 0xfff8a8 //色温阶段11
// mode3常量定义
#define COLOR_BLOCK_SIZE 5 // 每组颜色灯珠数量
#define RAINBOW_COLORS 7 // 彩虹颜色数量
extern uint8_t auto_Flag;
extern bool shutdown_Flag;
typedef uint32_t u32;
extern uint8_t Muisc_Flag;
extern uint8_t Task_tamp;
extern uint8_t Temp_Flag;
extern uint8_t auto_Flag;
extern uint8_t Muisc_mode;
extern uint8_t Muisc_Flag;
extern volatile uint8_t dma_busy;
extern uint8_t T1H; // 1码高电平(937.5ns)
extern uint8_t T0H;
// 颜色结构体
typedef struct {
uint8_t r; // 红色
uint8_t g; // 绿色
uint8_t b; // 蓝色
} RGB_Color;
typedef struct
{
uint8_t LED_SWITCH; // LEDC开关 (0 = off 1 = on)
uint8_t ledc_kick_state; // LEDC空闲状态(0为空闲 1为忙碌)
uint8_t ledc_fill_state; // LEDC填充计数(看灯光效果)
uint8_t RGB_CODE_LEN; // 实际发送长度((RGB_XLED_NUM * 3) + 3) / 4
uint16_t RGB_XLED_NUM; // 灯珠像素数量(1 - 1000)
uint8_t CURRENT_TASK; // 当前执行任务
uint8_t Temp_Step; // 色温阶段(0-29)共30个阶段
volatile uint16_t LEDC_TICK_Speed; // LEDC调节速度(1 - 10(100ms - 1s))
volatile uint8_t Timer_count; // LEDC时间计数
uint8_t LED_Brightne; // LEDX亮度(0 - 1.0 (100))0-100
uint8_t xianxu;
}LEDX_Task_TypeDef;//灯带相关数据结构体
extern LEDX_Task_TypeDef LED_TaskDef;
typedef void (*pfunc)(void);
#if(WS2815_CONTROL_MODE == SPI_DMA_MODE)
// 配置参数 - 用户可修改
#define WS2815_SPI_IDX (SPI1_IDX)
#define WS2815_DMA_CHANNEL (DMA_CHANNEL0)
#define GPIO_DUMMY (0xFF)
#define LEDS_NUM (700) // 支持的最大LED数量 1000
#define SPI_FREQUENCY (6400000) // SPI频率6.4MHz
// SPI时序参数
//#define T1H (0xf8) // 1码高电平(937.5ns)
//#define T0H (0xc0) // 0码高电平(312.5ns)
#define RESET_BYTES (24) // 30μs / 1.25μs = 24字节
#define DMA_MAX_BLOCK_SIZE (2400) // DMA单次传输最大字节数
//uint16_t LEDS_NUM = 1000;
// 传输状态
typedef enum {
TRANSFER_IDLE,
TRANSFER_LEDS,
TRANSFER_RESET
} TransferState;
#elif(WS2815_CONTROL_MODE == GPIO_DELAY_MODE)
#define LEDS_NUM (16) // 支持的最大LED数量
#if(WS2815_TX_PIN < 16)
#define GPIO_PIN_REG (*(volatile uint32_t *)(0x40001000))
#else
#define GPIO_PIN_REG (*(volatile uint32_t *)(0x40001004))
#endif
#define WS2815_GPIO_PIN_HIGH (GPIO_PIN_REG = 0x10001 << (WS2815_TX_PIN & 0xFUL))
#define WS2815_GPIO_PIN_LOW (GPIO_PIN_REG = 0x10000 << (WS2815_TX_PIN & 0xFUL))
#else
#error "WS2815 CONTROL MODE IS ERR!"
#endif
// 函数声明
int WS2815_Init(void);
void WS2815_SetColor(uint16_t led_num, RGB_Color color);
void WS2815_SetAll(RGB_Color color);
void WS2815_Update(void);
void WS2815_Clear(void);
//灯效函数声明
void light_mode(void);
void LED_Mode_Change(uint8_t mode);//模式修改函数
static uint32_t ws2815_rgb_color_format_conversion(uint32_t display_color,unsigned char display_format);
void ws2815_rgb_color_out_32to24bit_code_fill(uint32_t buf_code_grb[], uint32_t buf_grb[]);
void ws2815_ledc_show(uint8_t display_format,uint32_t LED_Maincolor);
void ws2815_ledc_multicolor(uint8_t display_format,uint8_t direction);
void ledc_dma_kick(uint32_t addr, uint16_t len);
void ws2815_ledc_multstroboscopic(uint8_t display_format);
void ws2815_ledc_singleflu(uint8_t display_format, uint32_t LED_Maincolor, uint32_t LED_Background_color);
void Music_Mode_Change(void);
#ifdef __cplusplus
}
#endif
#endif /* __WS2815_H__ */