1 Star 2 Fork 1

chengrui_78 / AIR101-WS2812

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.lua 10.19 KB
一键复制 编辑 原始数据 按行查看 历史
chengrui_78 提交于 2021-10-24 23:35 . uart-2812
---------------------------------------------------------------------------------------------------------------------------------------
--版权声明:本demo集源于上海合宙官方技术团队的DEMO(MIT),并参考了合宙开源社区的众多大佬无私分享的代码,包括但不限于 稀饭放姜、Wendal、晨旭等
-- 目前参考到的开源项目有: iRTU(MIT)、LuatOS(MIT)
-- 欲获取更多luat 代码请访问 doc.openluat.com
-- 如果您觉得本demo集包含了未经由您授权的代码,请联系 64034373@qq.com
---------------------------------------------------------------------------------------------------------------------------------------
PROJECT = "uart_WS2812"
VERSION = "1.0.0"
-- @模块功能:ws2812b 8X8矩阵屏幕字符驱动
-- @author miuser
-- @module midemo.ws2812b
-- @license MIT
-- @copyright miuser@luat
-- @release 2020-10-10
--------------------------------------------------------------------------
--帮助系统显示用的模块说明
--HELP=[[8X8方形ws2812b全彩LED阵列驱动模块]]
--HELP=[[连线方法:uartTX-电平转换-(9014换向)-I,外部5V<->+,外部电源<->GND<->GND]]
---HELP=[[使用方法:]]
--HELP=[[2812DISPLAY,str str为英文字符串]]
--HELP=[[显示完毕后回显2812DISPLAY,str->DONE]]
--HELP=[[例子:]]
--HELP=[[2812DISPLAY,HELLO LUAT]]
--HELP=[[显示完毕后回显2812DISPLAY,HELLO LUAT->DONE]]
-- @使用方法 2812DISPLAY,str 显示一个字符
-- @显示完成后,系统收到消息 WS2812B_TRANSMIT_DONE XXXXX
-- @显示完成后,终端收到消息 XXXXX transmited to WSMatrix
-- LuaTools需要PROJECT和VERSION这两个信息
-- 引入必要的库文件(lua编写), 内部库不需要require
local sys = require "sys"
log.info("main", "uart_WS2812")
local uartid = 2
--初始化
local result = uart.setup(
uartid,--串口id
1500000,--波特率
7,--数据位
1--停止位
)
--英文字符集
local lib_5X7={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5F,0x00,0x00,0x00,0x07,0x00,0x07,0x00,0x14,0x7F,0x14,0x7F,0x14,0x24,0x2A,0x07,0x2A,0x12,0x23,0x13,0x08,0x64,0x62,0x37,0x49,0x55,0x22,0x50,0x00,0x05,0x03,0x00,0x00,0x00,0x1C,0x22,0x41,0x00,0x00,0x41,0x22,0x1C,0x00,0x08,0x2A,0x1C,0x2A,0x08,0x08,0x08,0x3E,0x08,0x08,0x00,0x50,0x30,0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x00,0x60,0x60,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x3E,0x51,0x49,0x45,0x3E,0x00,0x42,0x7F,0x40,0x00,0x42,0x61,0x51,0x49,0x46,0x21,0x41,0x45,0x4B,0x31,0x18,0x14,0x12,0x7F,0x10,0x27,0x45,0x45,0x45,0x39,0x3C,0x4A,0x49,0x49,0x30,0x01,0x71,0x09,0x05,0x03,0x36,0x49,0x49,0x49,0x36,0x06,0x49,0x49,0x29,0x1E,0x00,0x36,0x36,0x00,0x00,0x00,0x56,0x36,0x00,0x00,0x00,0x08,0x14,0x22,0x41,0x14,0x14,0x14,0x14,0x14,0x41,0x22,0x14,0x08,0x00,0x02,0x01,0x51,0x09,0x06,0x32,0x49,0x79,0x41,0x3E,0x7E,0x11,0x11,0x11,0x7E,0x7F,0x49,0x49,0x49,0x36,0x3E,0x41,0x41,0x41,0x22,0x7F,0x41,0x41,0x22,0x1C,0x7F,0x49,0x49,0x49,0x41,0x7F,0x09,0x09,0x01,0x01,0x3E,0x41,0x41,0x51,0x32,0x7F,0x08,0x08,0x08,0x7F,0x00,0x41,0x7F,0x41,0x00,0x20,0x40,0x41,0x3F,0x01,0x7F,0x08,0x14,0x22,0x41,0x7F,0x40,0x40,0x40,0x40,0x7F,0x02,0x04,0x02,0x7F,0x7F,0x04,0x08,0x10,0x7F,0x3E,0x41,0x41,0x41,0x3E,0x7F,0x09,0x09,0x09,0x06,0x3E,0x41,0x51,0x21,0x5E,0x7F,0x09,0x19,0x29,0x46,0x46,0x49,0x49,0x49,0x31,0x01,0x01,0x7F,0x01,0x01,0x3F,0x40,0x40,0x40,0x3F,0x1F,0x20,0x40,0x20,0x1F,0x7F,0x20,0x18,0x20,0x7F,0x63,0x14,0x08,0x14,0x63,0x03,0x04,0x78,0x04,0x03,0x61,0x51,0x49,0x45,0x43,0x00,0x00,0x7F,0x41,0x41,0x02,0x04,0x08,0x10,0x20,0x41,0x41,0x7F,0x00,0x00,0x04,0x02,0x01,0x02,0x04,0x40,0x40,0x40,0x40,0x40,0x00,0x01,0x02,0x04,0x00,0x20,0x54,0x54,0x54,0x78,0x7F,0x48,0x44,0x44,0x38,0x38,0x44,0x44,0x44,0x20,0x38,0x44,0x44,0x48,0x7F,0x38,0x54,0x54,0x54,0x18,0x08,0x7E,0x09,0x01,0x02,0x08,0x14,0x54,0x54,0x3C,0x7F,0x08,0x04,0x04,0x78,0x00,0x44,0x7D,0x40,0x00,0x20,0x40,0x44,0x3D,0x00,0x00,0x7F,0x10,0x28,0x44,0x00,0x41,0x7F,0x40,0x00,0x7C,0x04,0x18,0x04,0x78,0x7C,0x08,0x04,0x04,0x78,0x38,0x44,0x44,0x44,0x38,0x7C,0x14,0x14,0x14,0x08,0x08,0x14,0x14,0x18,0x7C,0x7C,0x08,0x04,0x04,0x08,0x48,0x54,0x54,0x54,0x20,0x04,0x3F,0x44,0x40,0x20,0x3C,0x40,0x40,0x20,0x7C,0x1C,0x20,0x40,0x20,0x1C,0x3C,0x40,0x30,0x40,0x3C,0x44,0x28,0x10,0x28,0x44,0x0C,0x50,0x50,0x50,0x3C,0x44,0x64,0x54,0x4C,0x44,0x00,0x08,0x36,0x41,0x00,0x00,0x00,0x7F,0x00,0x00,0x00,0x41,0x36,0x08,0x00,0x02,0x01,0x02,0x04,0x02,0xff,0xff,0xff,0xff,0xff}
Xmax=8
Ymax=8
--8X8二维数组存储点阵信息
local Screen_R={}
local Screen_G={}
local Screen_B={}
--前景笔刷颜色 R,G,B
local FrontColor={255,255,255}
--产生一个字节的发送码
--产生一个字节的发送码
local function genByte(databyte)
--按硬件编码送出的字节
local zero=0xFE
local one=0xF0
local section={}
for i=1,8 do
if databyte & (0x01 << (8-i))==0 then
section[i] = zero
else
section[i] = one
end--log.info("midemo.ws2812b","section"..i.."="..section[i])
end
byet1=section[1]
byet2=section[2]
byet3=section[3]
byet4=section[4]
byet5=section[5]
byet6=section[6]
byet7=section[7]
byet8=section[8]
local ret=pack.pack("b",byet1)..pack.pack("b",byet2)..pack.pack("b",byet3)..pack.pack("b",byet4)..pack.pack("b",byet5)..pack.pack("b",byet6)..pack.pack("b",byet7)..pack.pack("b",byet8)
--log.info("midemo.ws2812b","bytes="..string.toHex(ret))
return ret
end
local function genDot(R,G,B)
--log.info("midemo.ws2812b","R="..R.." G="..G.." B="..B)
return genByte(G)..genByte(R)..genByte(B)
end
--初始化屏幕
function init()
for i=1,8 do
Screen_R[i]={}
Screen_G[i]={}
Screen_B[i]={}
end
log.info("midemo.ws2812b","init",result)
local PanelMatrix=""
for y=1,Ymax do
for x=1,Xmax do
Screen_R[x][y]=0
Screen_G[x][y]=0
Screen_B[x][y]=0
PanelMatrix=PanelMatrix..genDot(Screen_R[x][y],Screen_G[x][y],Screen_B[x][y])
end
end
end
--刷新屏幕
function update()
local PanelMatrix=""
for y=1,Ymax do
for x=1,Xmax do
local dot=""
if (y%2==0) then
dot=genDot(Screen_R[8-x+1][y],Screen_G[8-x+1][y],Screen_B[8-x+1][y])
else
dot=genDot(Screen_R[x][y],Screen_G[x][y],Screen_B[x][y])
end
PanelMatrix=PanelMatrix..dot
end
end
uart.write(uartid,PanelMatrix)
end
--清屏
function clr()
for y=1,Ymax do
for x=1,Xmax do
Screen_R[x][y]=0
Screen_G[x][y]=0
Screen_B[x][y]=0
end
end
end
--画点
function DrawDot(x,y)
--log.info("midemo.ws2812b","x="..x.." y="..y)
Screen_R[x][y]=FrontColor[1]
Screen_G[x][y]=FrontColor[2]
Screen_B[x][y]=FrontColor[3]
end
--在指定坐标处显示一个字符
function ShowChar(xpos, Show_char)
c = string.byte(Show_char) - 0x20 -- 获取字符的偏移量
for x = 1, 5 do -- 循环5次(5列)
line=lib_5X7[5*c+x]
if ((x+xpos)>8 or (x+xpos)<1)then
--log.info("midemo.ws2812b","jump line because x+pos=",tostring(x+xpos))
else
for y = 1, 8 do
if (line~=nil) then
if (line & (0x01 << (8-y))) ~=0 then Screen_R[x+xpos][y]=FrontColor[1] end
if (line & (0x01 << (8-y))) ~=0 then Screen_G[x+xpos][y]=FrontColor[2] end
if (line & (0x01 << (8-y))) ~=0 then Screen_B[x+xpos][y]=FrontColor[3] end
--log.info("midemo.ws2812b","Screen_R"..x+xpos.."="..FrontColor[1])
-- log.info("midemo.ws2812b","Screen_G"..x+xpos.."="..FrontColor[1])
-- log.info("midemo.ws2812b","Screen_B"..x+xpos.."="..FrontColor[2])
end
end
end
end
end
--显示背景花纹
function DrawRandBackground(i)
for yy=1,Ymax do
for xx=1,Xmax do
if (i%6)==0 then val1=1 val2=0 val3=1 end
if (i%6)==1 then val1=2 val2=0 val3=0 end
if (i%6)==2 then val1=1 val2=1 val3=0 end
if (i%6)==3 then val1=0 val2=2 val3=0 end
if (i%6)==4 then val1=0 val2=1 val3=1 end
if (i%6)==5 then val1=0 val2=0 val3=2 end
--log.info("midemo.ws2812b","rand="..val1)
Screen_R[xx][yy]=0
Screen_G[xx][yy]=0
Screen_B[xx][yy]=0
end
end
end
--显示一个字符串
function ShowString(ShowStr)
running=1
i=0
sys.taskInit(function()
--ShowStr=" HELLO LUAT!"
log.info("midemo.ws2812b","restarting w2812b")
while running==1 do
--log.info("midemo.ws2812b","refreshing w2812b")
DrawRandBackground(i)
FrontColor={255,0,0}
for ii=1,#ShowStr do
str=string.sub(ShowStr,ii,ii)
pos=(ii-1)*6-i
--log.info("midemo.ws2812b","showing str "..str.." at pos "..pos)
if (ii%6==0) then FrontColor={255,255,255} end
if (ii%6==1) then FrontColor={255,0,255} end
if (ii%6==2) then FrontColor={0,255,255} end
if (ii%6==3) then FrontColor={255,0,255} end
if (ii%6==4) then FrontColor={255,255,0} end
if (ii%6==5) then FrontColor={0,255,255} end
ShowChar(pos,str)
end
update()
--collectgarbage("collect")
i=i+1
if i>=#ShowStr*6 then
i=0
running=0
end
end
end)
end
A=pack.pack("b24",0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE)
local clr1 = A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A.. A
uart.write(uartid,clr1)
timer.mdelay(200)
init()
ShowString("UART WS2812 LUAT 10.1 happy I wish Shenzhou 13 a successful launch!")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Lua
1
https://gitee.com/chengrui-seventy-eight/HU.AIR101.git
git@gitee.com:chengrui-seventy-eight/HU.AIR101.git
chengrui-seventy-eight
HU.AIR101
AIR101-WS2812
pwm-WS2812

搜索帮助

344bd9b3 5694891 D2dac590 5694891