嵌入式工程师的520表白

描述

点灯,嵌入式工程师难以割舍的独有情怀。也只有他们才能这么大胆,用耀眼绿光在温馨霓虹中跳脱闪现,变幻中演绎与众不同的浪漫爱意。

520点灯表白,为爱发光——今晚就脱单!快来~

本教程示例采用合宙LuatOS-SoC系列Air105开发板、WS2812系列LED光源演示

 

 

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下载


 


 

 

接下来,我们正式编写表白程序。

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

 

 

 

 

02之后,申请一个8*8的zbuff空间:

local ws2812_w = 8local ws2812_h = 8
local buff = zbuff.create({ws2812_w,ws2812_h,24},0x000000)

 

 

 

03然后,我们做一个简单的滚动显示函数就可以了:

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,都是满满爱意

你学会了吗?

快来试试看!

发挥你的点灯创意为爱发光吧

 

 

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分