1. 犀牛前端部落首页
  2. Arduino和RaspberryPi

树莓派连接控制WS2801灯带

前言

WS2801是一个可编程的灯带,用树莓派来控制WS2801可以实现多种场景,比如实现接入HomeBridge用苹果(Iphone)手机的HomeKit来实现语音控制该灯带,本文仅介绍如何用树莓派来控制WS2801。

材料准备

  1. 树莓派
  2. 电源(给WS2801供电)
  3. WS2801

电路图

WS2801 灯带 树莓派 开关电源
 5V  —  +V
 CK / CI  Pin 23 (SCKL)  —
 SI / DI  Pin 19 (MOSI)  —
 GND  Pin 6 (GND)  -V or COM

树莓派连接控制WS2801灯带

我用了一个220V转5v的电源来给WS2801供电,在该电路中我们用到了树莓派的SCKL和MOSI针脚,该针脚是基于SPI通信的。

以下是我的连接图,供大家参考。

树莓派连接控制WS2801灯带

正文

首先我们需要开启树莓派的SPI,大家可以看我之前写的文章。《树莓派知识解答》

安装python库

sudo apt-get install python-pip -y

安装WS2801库

sudo pip install adafruit-ws2801

现在我们已经安装好所需要的环境了,是不是很简单?接下来我们就可以开始写代码了。

创建一个新的python文件(sudo nano test.py),将下面代码复制进去。

[cc lang=”python”]
# Simple demo of of the WS2801/SPI-like addressable RGB LED lights.
import time
import RPi.GPIO as GPIO

# Import the WS2801 module.
import Adafruit_WS2801
import Adafruit_GPIO.SPI as SPI

# Configure the count of pixels:
PIXEL_COUNT = 32

# Alternatively specify a hardware SPI connection on /dev/spidev0.0:
SPI_PORT = 0
SPI_DEVICE = 0
pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE), gpio=GPIO)

# Define the wheel function to interpolate between different hues.
def wheel(pos):
if pos < 85:
return Adafruit_WS2801.RGB_to_color(pos * 3, 255 – pos * 3, 0)
elif pos < 170: pos -= 85 return Adafruit_WS2801.RGB_to_color(255 – pos * 3, 0, pos * 3) else: pos -= 170 return Adafruit_WS2801.RGB_to_color(0, pos * 3, 255 – pos * 3) # Define rainbow cycle function to do a cycle of all hues. def rainbow_cycle_successive(pixels, wait=0.1): for i in range(pixels.count()): # tricky math! we use each pixel as a fraction of the full 96-color wheel # (thats the i / strip.numPixels() part) # Then add in j which makes the colors go around per pixel # the % 96 is to make the wheel cycle around pixels.set_pixel(i, wheel(((i * 256 // pixels.count())) % 256) ) pixels.show() if wait > 0:
time.sleep(wait)

def rainbow_cycle(pixels, wait=0.005):
for j in range(256): # one cycle of all 256 colors in the wheel
for i in range(pixels.count()):
pixels.set_pixel(i, wheel(((i * 256 // pixels.count()) + j) % 256) )
pixels.show()
if wait > 0:
time.sleep(wait)

def rainbow_colors(pixels, wait=0.05):
for j in range(256): # one cycle of all 256 colors in the wheel
for i in range(pixels.count()):
pixels.set_pixel(i, wheel(((256 // pixels.count() + j)) % 256) )
pixels.show()
if wait > 0:
time.sleep(wait)

def brightness_decrease(pixels, wait=0.01, step=1):
for j in range(int(256 // step)):
for i in range(pixels.count()):
r, g, b = pixels.get_pixel_rgb(i)
r = int(max(0, r – step))
g = int(max(0, g – step))
b = int(max(0, b – step))
pixels.set_pixel(i, Adafruit_WS2801.RGB_to_color( r, g, b ))
pixels.show()
if wait > 0:
time.sleep(wait)

def blink_color(pixels, blink_times=5, wait=0.5, color=(255,0,0)):
for i in range(blink_times):
# blink two times, then wait
pixels.clear()
for j in range(2):
for k in range(pixels.count()):
pixels.set_pixel(k, Adafruit_WS2801.RGB_to_color( color[0], color[1], color[2] ))
pixels.show()
time.sleep(0.08)
pixels.clear()
pixels.show()
time.sleep(0.08)
time.sleep(wait)

def appear_from_back(pixels, color=(255, 0, 0)):
pos = 0
for i in range(pixels.count()):
for j in reversed(range(i, pixels.count())):
pixels.clear()
# first set all pixels at the begin
for k in range(i):
pixels.set_pixel(k, Adafruit_WS2801.RGB_to_color( color[0], color[1], color[2] ))
# set then the pixel at position j
pixels.set_pixel(j, Adafruit_WS2801.RGB_to_color( color[0], color[1], color[2] ))
pixels.show()
time.sleep(0.02)

if __name__ == “__main__”:
# Clear all the pixels to turn them off.
pixels.clear()
pixels.show() # Make sure to call show() after changing any pixels!

rainbow_cycle_successive(pixels, wait=0.1)
rainbow_cycle(pixels, wait=0.01)

brightness_decrease(pixels)

appear_from_back(pixels)

for i in range(3):
blink_color(pixels, blink_times = 1, color=(255, 0, 0))
blink_color(pixels, blink_times = 1, color=(0, 255, 0))
blink_color(pixels, blink_times = 1, color=(0, 0, 255))

rainbow_colors(pixels)

brightness_decrease(pixels)

[/cc]

 

 

原创文章,作者:犀牛前端部落,如若转载,请注明出处:https://www.pipipi.net/99.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注