溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python怎么實現(xiàn)炫彩跑馬燈

發(fā)布時間:2022-01-13 10:31:17 來源:億速云 閱讀:194 作者:iii 欄目:互聯(lián)網(wǎng)科技

本文小編為大家詳細介紹“Python怎么實現(xiàn)炫彩跑馬燈”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“Python怎么實現(xiàn)炫彩跑馬燈”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

    一、實驗目的

    了解ws2812b的工作原理
    學習ws2812b的驅(qū)動方法

    二、實驗器材

    TPYBoard v102 1塊
    ws2812b RGB-Ring-8 1個
    micro USB數(shù)據(jù)線 1條
    杜邦線 若干

    三、WS2812B的介紹

    WS2812B是一個集控制電路與發(fā)光電路于一體的智能外控LED光源。 其外型與一個5050LED燈珠相同, 每個元件即為一個像素點。像素點內(nèi)部包含了智能數(shù)字接口數(shù)據(jù)鎖存信號整形放大驅(qū)動電路, 還包含有高精度的內(nèi)部振蕩器和可編程定電流控制部分, 有效保證了像素點光的顏色高度一致。

    數(shù)據(jù)協(xié)議采用單線歸零碼的通訊方式, 像素點在上電復位以后, DIN端接受從控制器傳輸過來的數(shù)據(jù), 首先送過來的24bit數(shù)據(jù)被第一個像素點提取后, 送到像素點內(nèi)部的數(shù)據(jù)鎖存器, 剩余的數(shù)據(jù)經(jīng)過內(nèi)部整形處理電路整形放大后通過DO端口開始轉(zhuǎn)發(fā)輸出給下一個級聯(lián)的像素點, 每經(jīng)過一個像素點的傳輸, 信號減少24bit。像素點采用自動整形轉(zhuǎn)發(fā)技術(shù), 使得該像素點的級聯(lián)個數(shù)不受信號傳送的限制, 僅僅受限信號傳輸速度要求。

    實物圖
    Python怎么實現(xiàn)炫彩跑馬燈

    上圖是8個燈珠的。
    WS2812B的引腳說明:

Python怎么實現(xiàn)炫彩跑馬燈
    硬件連接
    將TPYBoard v102與WS2812B的接線示意圖,如下:

Python怎么實現(xiàn)炫彩跑馬燈
    程序源碼如下:

import   pyb
import   math
  
from   ws2812 import WS2812
  
  
ring   = WS2812(spi_bus=1, led_count=8, intensity=0.1)
  
  
def   data_generator(led_count):
    data = [(0, 0, 0) for i in   range(led_count)]
    step = 0
    while True:
        red = int((1 + math.sin(step *   0.1324)) * 127)
        green = int((1 + math.sin(step *   0.1654)) * 127)
        blue = int((1 + math.sin(step * 0.1))   * 127)
        data[step % led_count] = (red, green,   blue)
        yield data
        step += 1
  
  
for   data in data_generator(ring.led_count):
    ring.show(data)
    pyb.delay(100)


    里面還需要引入一個ws2812.py 文件。內(nèi)容如下:

import   gc
import   pyb
  
  
class   WS2812:
    """
    Driver for WS2812 RGB LEDs. May be used   for controlling single LED or chain
    of LEDs.
  
    Example of use:
  
        chain = WS2812(spi_bus=1,   led_count=4)
        data = [
            (255, 0, 0),    # red
            (0, 255, 0),    # green
            (0, 0, 255),    # blue
            (85, 85, 85),   # white
        ]
        chain.show(data)
  
    Version: 1.0
    """
    buf_bytes = (0x11, 0x13, 0x31, 0x33)
  
    def __init__(self, spi_bus=1,   led_count=1, intensity=1):
        """
        Params:
        * spi_bus = SPI bus ID (1 or 2)
        * led_count = count of LEDs
        * intensity = light intensity (float   up to 1)
        """
        self.led_count = led_count
        self.intensity = intensity
  
        # prepare SPI data buffer (4 bytes   for each color)
        self.buf_length = self.led_count * 3   * 4
        self.buf = bytearray(self.buf_length)
  
        # SPI init
        self.spi = pyb.SPI(spi_bus,   pyb.SPI.MASTER, baudrate=3200000, polarity=0, phase=1)
  
        # turn LEDs off
        self.show([])
  
    def show(self, data):
        """
        Show RGB data on LEDs. Expected data   = [(R, G, B), ...] where R, G and B
        are intensities of colors in range   from 0 to 255. One RGB tuple for each
        LED. Count of tuples may be less than   count of connected LEDs.
        """
        self.fill_buf(data)
        self.send_buf()
  
    def send_buf(self):
        """
        Send buffer over SPI.
        """
        self.spi.send(self.buf)
        gc.collect()
  
    def update_buf(self, data, start=0):
        """
        Fill a part of the buffer with RGB   data.
  
        Order of colors in buffer is changed   from RGB to GRB because WS2812 LED
        has GRB order of colors. Each color   is represented by 4 bytes in buffer
        (1 byte for each 2 bits).
  
        Returns the index of the first unfilled   LED
  
        Note: If you find this function ugly,   it's because speed optimisations
        beated purity of code.
        """
  
        buf = self.buf
        buf_bytes = self.buf_bytes
        intensity = self.intensity
  
        mask = 0x03
        index = start * 12
        for red, green, blue in data:
            red = int(red * intensity)
            green = int(green * intensity)
            blue = int(blue * intensity)
  
            buf[index] = buf_bytes[green   >> 6 & mask]
            buf[index+1] = buf_bytes[green   >> 4 & mask]
            buf[index+2] = buf_bytes[green   >> 2 & mask]
            buf[index+3] = buf_bytes[green   & mask]
  
            buf[index+4] = buf_bytes[red   >> 6 & mask]
            buf[index+5] = buf_bytes[red   >> 4 & mask]
            buf[index+6] = buf_bytes[red   >> 2 & mask]
            buf[index+7] = buf_bytes[red   & mask]
  
            buf[index+8] = buf_bytes[blue   >> 6 & mask]
            buf[index+9] = buf_bytes[blue   >> 4 & mask]
            buf[index+10] = buf_bytes[blue   >> 2 & mask]
            buf[index+11] = buf_bytes[blue   & mask]
  
            index += 12
  
        return index // 12
  
    def fill_buf(self, data):
        """
        Fill buffer with RGB data.
  
        All LEDs after the data are turned   off.
        """
        end = self.update_buf(data)
  
        # turn off the rest of the LEDs
        buf = self.buf
        off = self.buf_bytes[0]
        for index in range(end * 12,   self.buf_length):
            buf[index] = off
            index += 1

讀到這里,這篇“Python怎么實現(xiàn)炫彩跑馬燈”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI