数码小叶 发表于 2022-11-13 20:33

【行空板 Python编程学习主控板】四:驱动RGB led灯

本帖最后由 数码小叶 于 2022-11-13 20:31 编辑

<p><span style="font-size:16px;">行空板外设采用的是pinpong库去驱动,之前没有接触过pinpong库,所以还是需要先了解下。在官方文档里有介绍&ldquo;<em>pinpong库是一套控制开源硬件主控板的Python库,基于Firmata协议并兼容MicroPython语法。借助于pinpong库,直接用Python代码就能给各种常见的开源硬件编程。pinpong库的设计,是为了让开发者在开发过程中不用被繁杂的硬件型号束缚,而将重点转移到软件的实现。</em>&rdquo;</span></p>

<p><span style="font-size:16px;">pinpong库的文档结构很清晰,示例以及教程都是由易到难,由简单到复杂</span></p>

<p></p>

<p>&nbsp;</p>

<p><span style="font-size:16px;">&ldquo;<em>pinpong库由于支持众多的主控板及开源硬件,因此分成了3个包,board、extension和libs,board包中放置主板支持的功能及常用库,extension为定制类主控,libs中放置其他传感器的扩展库</em>&rdquo;,因此使用时,需要先导入</span></p>

<pre>
<code>from pinpong.board import xxx   #board包中的库
from pinpong.libs.xxx    #libs包中的库
from pinpong.extension.xxx    #extension包中的主板

Board().begin() #初始化,连接协处理器,检查固件为空或版本不对则自动烧录</code></pre>

<p></p>

<p>&nbsp;</p>

<p><span style="font-size:16px;">行空板背面有三个led在一起,一个红色的POWER,一个红色的USER,一个蓝色的L。行空板没有提供原理图,在引脚说明里可以找到L的定义 :P25,因此可以pinpong来点亮L试试。</span></p>

<p><span style="font-size:16px;">在pinpong库文档里,有Pin类介绍说明</span></p>

<p></p>

<p>&nbsp;</p>

<p><span style="font-size:16px;">导入需要的模块,需要延时操作,因此也导入了time模块</span></p>

<pre>
<code>from pinpong.board import Board,Pin
import time

Board().begin()
led = Pin(Pin.P25, Pin.OUT)

while True:
led.value(1)
time.sleep(0.5)
led.value(0)
time.sleep(0.5)</code></pre>

<p><iframe allowfullscreen="true" frameborder="0" height="510" src="https://training.eeworld.com.cn/shareOpenCourseAPI?isauto=true&amp;lessonid=34845" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
&nbsp;</p>

<p>&nbsp;</p>

<p><span style="font-size:16px;">驱动单个的led已经ok,就可以去驱动WS2812B了,WS2812B是比较常见的RGB led,其优点是集发光和控制于一体,数据协议采用单线归零码的通讯方式</span></p>

<p></p>

<p>&nbsp;</p>

<p><span style="font-size:16px;">之所以应用广泛,就是因为只需要一个引脚就可以控制N个LED,只要电流足够。</span></p>

<p></p>

<p>&nbsp;</p>

<p><span style="font-size:16px;">在pinpong库里,有WS2812的应用示例-2-07-neopixel:WS2812灯带,根据应用示例,只需要导入neopixel类,就可以很方便的直接操作了</span></p>

<pre>
<code>from pinpong.board import Board,Pin,NeoPixel
from unihiker import GUI
import time

u_gui=GUI()
Board().begin()
NEOPIXEL_PIN = Pin(Pin.P1)
PIXELS_NUM = 6

u_gui.draw_text(text="测试五:Mind+",x=20,y=160,font_size=20, color="#CC33CC")
np1 = NeoPixel(NEOPIXEL_PIN,PIXELS_NUM)
np1 = (0,255,0)

while True:
   time.sleep(1)</code></pre>

<p></p>

<pre>
<code>while True:
    for RGB_LED in range(0, 6):
      np1 = (0,255,0)
      time.sleep(0.5)
    for RGB_LED in range(0, 6):
      np1 = (0,0,0)
      time.sleep(0.5)</code></pre>

<p><iframe allowfullscreen="true" frameborder="0" height="510" src="https://training.eeworld.com.cn/shareOpenCourseAPI?isauto=true&amp;lessonid=34846" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
<iframe allowfullscreen="true" frameborder="0" height="510" src="https://training.eeworld.com.cn/shareOpenCourseAPI?isauto=true&amp;lessonid=34847" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
&nbsp;</p>

<p><span style="font-size:16px;">NeoPixel类里实现了很多操作,比如移位操作(设定好的颜色依次移动),循环操作(设定好的颜色和灯数循环移动)、随机色等等,几乎满足了常见的LED灯串的需求。</span></p>

lugl4313820 发表于 2022-11-14 08:05

<p>这是自己打了一个底板吗?非常牛的操作!</p>

freebsder 发表于 2022-11-14 19:09

<p>确实比C码一堆代码方便!</p>

数码小叶 发表于 2022-11-14 21:19

freebsder 发表于 2022-11-14 19:09
确实比C码一堆代码方便!

<p>那得感谢前人,用C封装好了库<img height="28" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/grinning-face-with-smiling-eyes_1f601.png" width="28" /><img height="28" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/grinning-face-with-smiling-eyes_1f601.png" width="28" /></p>

数码小叶 发表于 2022-11-14 21:20

lugl4313820 发表于 2022-11-14 08:05
这是自己打了一个底板吗?非常牛的操作!

<p>额。。。买的成品<img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/wanwan34.gif" width="48" /><img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/wanwan34.gif" width="48" /><img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/wanwan34.gif" width="48" /></p>

freebsder 发表于 2022-11-15 16:54

数码小叶 发表于 2022-11-14 21:19
那得感谢前人,用C封装好了库

<p>如果不考虑效率和资源用量,嵌入式都能用这种做开放,时间上确实快多了。</p>

数码小叶 发表于 2022-11-15 21:59

freebsder 发表于 2022-11-15 16:54
如果不考虑效率和资源用量,嵌入式都能用这种做开放,时间上确实快多了。

<p>确实,投入时间几乎是几何倍数的减少,犹如用树莓派做核心板一个效果<img height="28" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/grinning-face-with-smiling-eyes_1f601.png" width="28" /><img height="28" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/grinning-face-with-smiling-eyes_1f601.png" width="28" /></p>
页: [1]
查看完整版本: 【行空板 Python编程学习主控板】四:驱动RGB led灯