【模块第三弹】哪吒中的LEDC的驱动
-
实现了控制LEDC的接口,通过宏定义控制LEDC的个数,前提是需要在dts中打开这么多个LEDC设备。封装成内部接口,当然也可以自定义修改。
- 集成颜色解码接口,使用宏定义声明颜色,调用颜色解码接口即可显示出对应的颜色。
- 目前有三种流水样式,其他流水样式可以参考这三种流水样式做修改。
代码
main.c
#include "ws2812.h" int main(int argc, char const *argv[]) { printf("demo 1\n"); //LEDC_LiuShui(); //LED_LiuShui2(); LiuSui3(); return 0; }
代码
ws2812.h
#ifndef WS2812_H #define WS2812_H #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> typedef unsigned char uint8; typedef unsigned int uint32; #define LED_NUM 32 #define LEDC_RED_PATH "/sys/class/leds/sunxi_led%dr/brightness" #define LEDC_GREEN_PATH "/sys/class/leds/sunxi_led%dg/brightness" #define LEDC_BLUE_PATH "/sys/class/leds/sunxi_led%db/brightness" #define delay_ms(x) (usleep(x*1000)) #define MediumTurquoise 0x48D1CC #define Gold 0xFFD700 #define DarkGray 0xA9A9A9 #define Magenta 0xFF00FF #define Yellow 0xFFFF00 int LED_LiuShui2(); int LEDC_LiuShui(); int LED_Color(int led,uint8 R,uint8 G,uint8 B); int ColorDecode(uint32 color ,uint8 *R,uint8 *G,uint8 *B); int LiuSui3(); #endif
代码
ws2812.c
#include "ws2812.h" static int export_fd; static int color[3] = {0,0,255}; //RGB int LED_LiuShui2(){ int i; int mode = 0; int R= 0 ,G = 0,B = 0; while(1){ if(i == 32) { i = 0; mode += 1; } if(mode == 0){ R = 255; G = 0; B = 0; } if (mode == 1){ R = 0; G = 255; B = 0; } if(mode == 2){ R = 0; G = 0; B = 255; } if(mode == 3) { mode = 0; printf("delate 0\n"); } LED_Color(i,R,G,B); i++; delay_ms(20); } return 1; } int LEDC_LiuShui(){ int i = 0,len; char ledPath[45] = {0}; char led_write[3] = {0}; printf("demo 1 \n"); while(1){ for(i = 0 ; i < LED_NUM ; i++){ printf("LED %d \n",i); sprintf(ledPath,LEDC_BLUE_PATH,i); export_fd = open(ledPath, O_WRONLY); if(-1 == export_fd) { printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__); return -1; } len = sprintf(led_write,"%d",color[2]); if(-1 == write(export_fd, led_write, len)) { printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__); close(export_fd); return -1; } close(export_fd); if(i>3){ sprintf(ledPath,LEDC_BLUE_PATH,i-3); export_fd = open(ledPath, O_WRONLY); if(-1 == export_fd) { printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__); return -1; } len = sprintf(led_write,"%d",0); if(-1 == write(export_fd, led_write, len)) { printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__); close(export_fd); return -1; } close(export_fd); } else { sprintf(ledPath,LEDC_BLUE_PATH,i+29); export_fd = open(ledPath, O_WRONLY); if(-1 == export_fd) { printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__); return -1; } len = sprintf(led_write,"%d",0); if(-1 == write(export_fd, led_write, len)) { printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__); close(export_fd); return -1; } close(export_fd); } delay_ms(50); } } printf("demo 1 end \n"); return 1; } int LED_Color(int led,uint8 R,uint8 G,uint8 B) { int len; char ledPath[45] = {0}; char led_write[3] = {0}; sprintf(ledPath,LEDC_BLUE_PATH,led); export_fd = open(ledPath, O_WRONLY); if(-1 == export_fd) { printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__); return -1; } len = sprintf(led_write,"%d",B); if(-1 == write(export_fd, led_write, len)) { printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__); close(export_fd); return -1; } close(export_fd); sprintf(ledPath,LEDC_GREEN_PATH,led); export_fd = open(ledPath, O_WRONLY); if(-1 == export_fd) { printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__); return -1; } len = sprintf(led_write,"%d",G); if(-1 == write(export_fd, led_write, len)) { printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__); close(export_fd); return -1; } close(export_fd); sprintf(ledPath,LEDC_RED_PATH,led); export_fd = open(ledPath, O_WRONLY); if(-1 == export_fd) { printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__); return -1; } len = sprintf(led_write,"%d",R); if(-1 == write(export_fd, led_write, len)) { printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__); close(export_fd); return -1; } close(export_fd); } int ColorDecode(uint32 color ,uint8 *R,uint8 *G,uint8 *B){ *R = (color >> 16) & 0x000011; *G = (color >> 8 ) & 0x000011; *B = (color >> 0 ) & 0x000011; } int LiuSui3(){ int i; int mode = 0; uint8 R= 0 ,G = 0,B = 0; while(1){ if(i == 32) { i = 0; mode += 1; } if(mode == 0){ ColorDecode(MediumTurquoise,&R,&G,&B); } if (mode == 1){ ColorDecode(Gold,&R,&G,&B); } if(mode == 2){ ColorDecode(DarkGray,&R,&G,&B); } if(mode == 3) { mode = 0; printf("delate 0\n"); } LED_Color(i,R,G,B); i++; delay_ms(20); } return 1; }
Makefile
CC := riscv64-unknown-linux-gnu-gcc target = ws2812 LDFLAGS ?= -lm CFLAGS ?= -I . CSRCS += ws2812.c MAINSRC = main.c all : default OBJEXT ?= .o COBJS = $(CSRCS:.c=$(OBJEXT)) MAINOBJ = $(MAINSRC:.c=$(OBJEXT)) # SRCS = $(CSRCS) $(MAINSRC)s %.o: %.c @$(CC) $(CFLAGS) -c $< -o $@ @echo "CC $<" default: $(COBJS) $(MAINOBJ) $(CC) -o $(target) $(MAINOBJ) $(COBJS) $(LDFLAGS) clean: rm -f $(target) $(AOBJS) $(COBJS) $(MAINOBJ)
-
代码文件:WS2812.tar.gz
-
Holihi~看看实际效果
-
闲鱼上面看到的,显示屏是WS2812
-
不错,买了根灯条回来,效果是挺漂亮的。不过FIFO最大32的深度,如果更多颗灯要怎么编程呢?
-
LEDC在一个机型上要真正用起来,的确是个比较吸引视觉的部件,但32还是少了点
Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号