爱了~嵌入式工程师的520表白
点灯,嵌入式工程师难以割舍的独有情怀。也只有他们才能这么大胆,用耀眼绿光在温馨霓虹中跳脱闪现,变幻中演绎与众不同的浪漫爱意。
520点灯表白,为爱发光——今晚就脱单!快来~
本教程示例采用合宙LuatOS-SoC系列Air105开发板、WS2812系列LED光源演示,文末【阅读原文】链接获取最新demo源码。
1
LuatOS-SoC驱动WS2812的三种方式
WS2812是一款比较常用的外控集成LED光源,控制简单,应用领域极广——包括但不限于表白。
具有低电压驱动、环保节能、亮度高、散射角度大、一致性好、低功率及超长寿命等优点。
驱动WS2812需要满足如下条件,显然不是所有MCU都能支持的:
LuatOS-SoC三种驱动方式
合宙LuatOS-SoC在支持GPIO直驱的情况下,加入了PWM和SPI驱动的方法:
sensor.ws2812b_pwm(pin,data)
sensor.ws2812b_spi(pin,data)
下面看看这两个函数对硬件要求:
sensor.ws2812b_pwm(pin,data):
800k的PWM输出频率sensor.ws2812b_spi(pin,data):
5Mbps的SPI输出速率
显然,这要求就很低了。三种驱动方式,总有一款适合你:
Air101/Air103系列:
推荐使用SPI驱动方式;
Air105系列:
推荐使用PWM驱动方式,也可使用SPI驱动方式;GPIO直驱也可以,但实际有点勉强。
2
520表白程序开发/demo下载
首先看一下API相关示例说明,参见:
https://wiki.luatos.com/api/sensor.html#sensor-ws2812b-pin-data-t0h-t0l-t1h-t1l
接下来,我们正式编写表白程序。建议手机横屏查看,或文末链接下载最新demo源码。
01首先将我们要显示的内容,换算成GBR颜色数组:
local show_520 = {
{0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff},
{0x0000ff,0x00ff00,0x00ff00,0x0000ff,0x0000ff,0x00ff00,0x00ff00,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff},
{0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x0000ff,0x00ff00,0x00ff00,0x00ff00,0x0000ff,0x00ff00,0x00ff00,0x00ff00,0x0000ff,0x00ff00,0x00ff00,0x00ff00,0x0000ff,0x0000ff,0x0000ff,0x0000ff},
{0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x0000ff,0x00ff00,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x00ff00,0x0000ff,0x00ff00,0x0000ff,0x00ff00,0x0000ff,0x0000ff,0x0000ff,0x0000ff},
{0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x0000ff,0x00ff00,0x00ff00,0x00ff00,0x0000ff,0x00ff00,0x00ff00,0x00ff00,0x0000ff,0x00ff00,0x0000ff,0x00ff00,0x0000ff,0x0000ff,0x0000ff,0x0000ff},
{0x0000ff,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x00ff00,0x0000ff,0x00ff00,0x0000ff,0x0000ff,0x0000ff,0x00ff00,0x0000ff,0x00ff00,0x0000ff,0x0000ff,0x0000ff,0x0000ff},
{0x0000ff,0x0000ff,0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x0000ff,0x0000ff,0x0000ff,0x00ff00,0x00ff00,0x00ff00,0x0000ff,0x00ff00,0x00ff00,0x00ff00,0x0000ff,0x00ff00,0x00ff00,0x00ff00,0x0000ff,0x0000ff,0x0000ff,0x0000ff},
{0x0000ff,0x0000ff,0x0000ff,0x00ff00,0x00ff00,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff,0x0000ff},
}local show_520_w = 24local show_520_h = 8
local ws2812_w = 8local ws2812_h = 8
local buff = zbuff.create({ws2812_w,ws2812_h,24},0x000000)
local function ws2812_roll_show(show_data,data_w)
local m = 0
while 1 do
for j=0,ws2812_w-1 do
if j%2==0 then
for i=ws2812_w-1,0,-1 do
if m+ws2812_w-i>data_w then
buff:pixel(i,j,show_data[j+1][m+ws2812_w-i-data_w]) else
buff:pixel(i,j,show_data[j+1][m+ws2812_w-i]) end
end
else
for i=0,ws2812_w-1 do
if m+i+1>data_w then
buff:pixel(i,j,show_data[j+1][m+i+1-data_w]) else
buff:pixel(i,j,show_data[j+1][m+i+1]) end
end
end
end
m = m+1
if m==data_w then m=0 end
sensor.ws2812b_pwm(5,buff)--此处使用pwm方法驱动,当然也可以用gpio,spi方式,API详情查看wiki https://wiki.luatos.com/api/sensor.html
-- sensor.ws2812b_spi(0,buff)
sys.wait(300)
end
end
好,我们的点灯表白程序写好了。前面没看视频的同学,来看一下点灯效果吧:
每个闪耀的LED,都是满满爱意
你学会了吗?
快来试试看!
发挥你的点灯创意为爱发光吧
- 特别鸣谢 -
LuatOS-SoC小分队
技术支持及爱意呈现
@与点
@打盹的消防车
@wendal
@小李同学
感谢所有支持与提供建议帮助的社群小伙伴
- 最新点灯表白教程 -
https://doc.openluat.com/article/4716
- 最新demo下载 -
https://gitee.com/openLuat/LuatOS/tree/master/demo/ws2812
更多物联网应用开发
欢迎加入微信/QQ技术交流群探讨沟通
- 合宙技术交流微信群 -
即刻微信/企业微信扫码加入
每个建议都值得关注
每个技能都值得分享
- 合宙技术交流QQ群 -
QQ扫码入群:827963649
行业人士交流分享,让万物互联更简单
▼ 更多惊喜活动 ▼
欢迎登录合宙淘宝官方直营店一探究竟
合宙LuatOS直营店(一)
luat.taobao.com
合宙LuatOS直营店(二)
openluat.taobao.com