樹莓派控制WS2812


https://tutorials-raspberrypi.com/connect-control-raspberry-pi-ws2812-rgb-led-strips/

 

 

 

 

4b配置過程

硬件連接

這個圖只是個示意圖,如果燈比較少直接樹莓派5V供電

如果燈比較多,可以參看上圖的外接供電

 

 

 

 

 VCC-5v

gnd-gnd

vin-18引腳

 

 

 環境配置

sudo apt-get update
sudo apt-get install gcc make build-essential python-dev git scons swig

 

下載源碼

git clone https://github.com/jgarff/rpi_ws281x

 編譯

cd rpi_ws281x/
sudo scons

  

 

 編譯python版本

cd python
sudo python setup.py build
sudo python setup.py install

 

 

 

 examples文件夾下面是使用樣例

 

 

標准測試樣例

 

 執行之前需要將編譯的庫拷貝過來 或者 運行程序直接添加環境

 

 

 

 

 

 拷貝到測試樣例

 

 運行代碼 必須從命令行以sudo 運行 否則報錯

 

 

 原理例成被我改了 未來使用必然配合別的程序,然而彩虹燈這樣的效果需要占據一個線程

因此重新封裝成一個類,並加入了多進程單獨控制燈(線程本質上在樹莓派還是一個線程)

如何使用

1新建一個工程,拷貝一個依賴文件到工程同目錄

 

 

2把這個代碼創建一個文件LED_Class放在同工程

  詳見后面LED_Class.py 文件

 

3要使用的test.py代碼加入這個類

from LED_Class import  LED

一定要從命令行 sudo 啟動 text.py

 

from LED_Class import LED
import time

led_use=LED()
led_use.process_test()
while 1:
    print('這是主進程!')
    time.sleep(2.0)#延遲2秒

  

 

LED_Class.py 文件

 

#!/usr/bin/env python3

import time
from neopixel import *
import argparse
from multiprocessing import Process

class LED(object):
    def __init__(self):
        LED_COUNT      = 8       # 燈的數目
        LED_PIN        = 18      # pwm管教口 18引腳 樹莓派.
        #LED_PIN        = 10      # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
        LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
        LED_DMA        = 10      # DMA channel to use for generating signal (try 10)
        LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest
        LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
        LED_CHANNEL    = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53
        # 工藝參數
        #parser = argparse.ArgumentParser()
        #parser.add_argument('-c', '--clear', action='store_true', help='clear the display on exit')
        #args = parser.parse_args()
        # 創建LED控制對象
        self.strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
        self.strip.begin()
        print('LED初始化成功')
    #功能一-逐個變色- 
    # colorWipe(self.strip, Color(255, 0, 0))  # Red wipe
    # 所有燈逐個變成紅色
    def colorWipe(self,color, wait_ms=50):
        """Wipe color across display a pixel at a time."""
        for i in range(self.strip.numPixels()):
            self.strip.setPixelColor(i, color)
            self.strip.show()
            time.sleep(wait_ms/1000.0)
    #功能二-交替閃爍
    # theaterChase(self.strip, Color(127, 127, 127))  # White
    # 白色交替閃爍
    def theaterChase(self, color, wait_ms=50, iterations=10):
        """Movie theater light style chaser animation."""
        for j in range(iterations):
            for q in range(3):
                for i in range(0, self.strip.numPixels(), 3):
                    self.strip.setPixelColor(i+q, color)
                self.strip.show()
                time.sleep(wait_ms/1000.0)
                for i in range(0, self.strip.numPixels(), 3):
                    self.strip.setPixelColor(i+q, 0)
    # 支撐函數
    def wheel(self,pos):
        """Generate rainbow colors across 0-255 positions."""
        if pos < 85:
            return Color(pos * 3, 255 - pos * 3, 0)
        elif pos < 170:
            pos -= 85
            return Color(255 - pos * 3, 0, pos * 3)
        else:
            pos -= 170
            return Color(0, pos * 3, 255 - pos * 3)
    #功能三-彩虹色整體統一柔和漸變-每個燈顏色同一時間相同
    def rainbow(self, wait_ms=20, iterations=1):
        """Draw rainbow that fades across all pixels at once."""
        for j in range(256*iterations):
            for i in range(self.strip.numPixels()):
                self.strip.setPixelColor(i, self.wheel((i+j) & 255))
            self.strip.show()
            time.sleep(wait_ms/1000.0)
    #功能四-彩虹色每一個燈各自柔和漸變-每個燈顏色同一時間不同
    def rainbowCycle(self, wait_ms=20, iterations=5):
        """Draw rainbow that uniformly distributes itself across all pixels."""
        for j in range(256*iterations):
            for i in range(self.strip.numPixels()):
                self.strip.setPixelColor(i, self.wheel((int(i * 256 / self.strip.numPixels()) + j) & 255))
            self.strip.show()
            time.sleep(wait_ms/1000.0)
    #功能五-彩虹色統一閃爍流動變色-每個燈顏色同一時間相同
    def theaterChaseRainbow(self,wait_ms=50):
        """Rainbow movie theater light style chaser animation."""
        for j in range(256):
            for q in range(3):
                for i in range(0, self.strip.numPixels(), 3):
                    self.strip.setPixelColor(i+q, self.wheel((i+j) % 255))
                self.strip.show()
                time.sleep(wait_ms/1000.0)
                for i in range(0, self.strip.numPixels(), 3):
                    self.strip.setPixelColor(i+q, 0)
    #單進程全部函數測試
    def test(self):       
        print ('Color wipe animations.')
        self.colorWipe( Color(155, 0, 0))  # Red wipe
        self.colorWipe( Color(0, 255, 0))  # Blue wipe
        self.colorWipe( Color(0, 0, 255))  # Green wipe
        print ('Theater chase animations.')
        self.theaterChase( Color(127, 127, 127))  # White theater chase
        self.theaterChase( Color(127,   0,   0))  # Red theater chase
        self.theaterChase(Color(  0,   0, 127))  # Blue theater chase
        print ('Rainbow animations.')
        self.rainbow()
        self.rainbowCycle()
        self.theaterChaseRainbow()
    #多進程全部函數測試    
    def process_test(self):
        print('process_begin')
        p= Process(target=self.test)
        p.deamon=True
        p.start()
        print('process_end')
        
#-----------------------------       
#LED函數初始化      
#led_use=LED()

#1單個函數測試
#led_use.colorWipe(Color(100, 0, 0))

#2單進程全部函數測試
#led_use.test()

#3多進程全部函數測試
'''
led_use.process_test()
while 1:
    print('這是主進程!')
    time.sleep(2.0)#延遲2秒
'''





   

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM