用arduino作为继电器控制neopixel led条带的高级接口
neopyxel的Python项目详细描述
新生儿
Python库,通过一个串行连接的Arduino继电器,可以轻松控制Adafruit的neopxel LED条纹。在
实现的功能
这个库提供了一个易于使用的高级接口来控制Adafruit的Neopixel LED条纹或任何由WS281x控制并由Adafruit's Neopixel Arduino library支持的单独寻址LED条带。这使得开发人员能够利用Python的所有资源和灵活性来开发LED条纹的效果和集成,方法是通过在运行Python的设备和条纹本身之间充当中继的arduino将命令中继到条纹上。在
为此,库使用串行通信向Arduino中继发送命令,并实现一系列类来抽象物理细节和通信过程,总结如下:
NeopyxelRelay对象
建立并处理与Arduino中继和条纹对象的连接。在
add_stripe()# Enables dynamic configuration of stripes on the Arduino Relayflush_stripes()# Flushes stripes setted up on the Arduino Relayset_pixel_color()# Enables concurrent pixel color setting of all stripes connected to the relayshow()# Enables concurrent update of pixels' display of all stripes connected to the relayseg_segment_color()# Enable concurrent control of stripes segments by abstracting stripes length through # relative segment position and length referencingexecute_effect()# Executes an effect defined by Effect classstop_effect()# Stops execution of an effect defined by Effect class
条纹对象
表示连接到中继的特定条带。在
^{pr2}$效果对象
通过实例化继承Effect类的类来定义Effect对象。Effect类为led条纹效果的快速开发和包装提供了一个接口。通过使用set_segment_color函数,可以编写效果,而不管要在其上执行的LED条纹的物理特性如何。效果由一个定义要执行的效果的主函数和两个定义如何启动和终止效果的转换对象组成。在
安装
Arduino继电器设置步骤
- 加载
neopyxel_relay.ino
文件,该文件位于Arduino板上的neopyxel_relay目录下。在 - 将Arduino板连接到将通过USB运行Python代码的设备。在
- 将LED条纹数据管脚连接到Arduino上的数字管脚(注意所选的数字管脚,稍后会用到)。在
使用包管理器pip安装neopyxel。在
pip install neopyxel
使用
简单的像素颜色设置:
importneopyxel# Instantiate NeopyxelRelay (no arguments are needed, but the COMPORT can be specified in case the detection fails)relay=neopyxel.NeopyxelRelay()# Add Striperelay.add_stripe(NUMPIXELS=30,# Number of pixels of the stripePIN=5)# Digital pin to which the stripe is connected# Set the color of pixel (led) 15 to whitewhite=(255,255,255)relay.set_pixel_color(pixel_number=15,color=white)# Update pixel displayrelay.show()
简单效果执行:
importsysimportneopyxelfromneopyxelimporteffects# Instantiate NeopyxelRelay (no arguments are needed, but the COMPORT can be specified in case the detection fails)relay=neopyxel.NeopyxelRelay()# Add Striperelay.add_stripe(NUMPIXELS=30,# Number of pixels of the stripePIN=5)# Digital pin to which the stripe is connected# Execute Effecttry:relay.execute_effect(effects.Fireplace)exceptKeyboardInterrupt:# Try/Except is employed here to properly terminate effect thread on Ctrl+Crelay.stop_effect()relay.flush_stripes()print("Ctrl+C pressed...exit")sys.exit(1)
效果和转换配置与执行
importsysimportneopyxelfromneopyxelimporteffectsfromneopyxelimporttrasitions# Instantiate NeopyxelRelay (no arguments are needed, but the COMPORT can be specified in case the detection fails)relay=neopyxel.NeopyxelRelay()# Add Striperelay.add_stripe(NUMPIXELS=30,# Number of pixels of the stripePIN=5)# Digital pin to which the stripe is connected# Execute Effectambar=(240,80,30)try:relay.execute_effect(EffectClass=effects.SteadyColor,color=ambar,# SteadyColor effect admits color argumentStartTransition=transitions.expand.FromCenter,# These transitions can be usedStopTransition=transitions.expand.FromBorders# by any effects)exceptKeyboardInterrupt:# Try/Except is employed here to properly terminate effect thread on Ctrl+Crelay.stop_effect()relay.flush_stripes()print("Ctrl+C pressed...exit")sys.exit(1)
贡献
欢迎拉取请求。对于重大变化,请先打开一个问题,讨论您希望更改的内容。在
许可证
- 项目
标签: