Python rpi_ws281x library All In One

发布时间 2023-06-02 17:13:01作者: xgqfrms

Python rpi_ws281x library All In One

Raspberry Pi & Python & WS2812B RGB LED Strip

rpi_ws281x

from rpi_ws281x import PixelStrip, Color, ws

PixelStrip
Color

$ sudo pip install rpi_ws281x

$ sudo pip3 install rpi_ws281x

https://github.com/rpi-ws281x

https://github.com/rpi-ws281x/rpi-ws281x-python

API docs ?

https://github.com/rpi-ws281x/rpi-ws281x-python/blob/master/library/rpi_ws281x.py#L72-L88

https://github.com/rpi-ws281x/rpi-ws281x-python/tree/master/examples

https://github.com/rpi-ws281x/rpi-ws281x-python/blob/master/examples/neopixelclock.py#L48

bugs ❌

https://github.com/rpi-ws281x/rpi-ws281x-python/issues/95

#!/usr/bin/env python3
# coding: utf8

import time
import argparse
from rpi_ws281x import PixelStrip, Color, ws
# from rpi_ws281x import *

# LED strip configuration
LED_COUNT = 60            # Number of LED pixels.
LED_PIN = 18                  # BCM GPIO pin connected to the pixels (18 uses `PWM`).
# 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 频率不同,5050 LED ❓
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
#LED_STRIP = ws.WS2812B_STRIP
LED_STRIP = ws.SK6812_STRIP_GRB

# ...

errors ❌

? 只会变白色,不会变成其他颜色

??? PWM 需要 hex 编码

#!/usr/bin/env python3
# coding: utf8

from time import sleep
from rpi_ws281x import PixelStrip, Color

# LED strip configuration:
LED_COUNT = 60        # Number of LED pixels.
LED_PIN = 18          # GPIO pin connected to the pixels (18 uses PWM!).
# 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)
# color channel GRB ❓
LED_BRIGHTNESS = 50   # Set to 0 for darkest and 255 for brightest
LED_INVERT = True     # 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

# Create NeoPixel object with appropriate configuration.
strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)

# Initialize the library (must be called once before other functions).
strip.begin()
print("Test beginning ✅")

def clear_buffer():
  for i in range(LED_COUNT):
    strip.setPixelColor(i, Color(0, 0, 0))
  strip.show()
  sleep(1)

def test():
  print("strip.numPixels() =", strip.numPixels())
  for i in range(strip.numPixels()):
    # white ✅
    # strip.setPixelColor(i, Color(255, 255, 255))
    # red ❌ 不生效 ?
    strip.setPixelColor(i, Color(255, 0, 0))
  strip.show()
  sleep(1)

try:
  test()
except KeyboardInterrupt:
  print('Ctrl + C exit ✅')
except RuntimeError as error:
  print("error =", error, error.args[0])
  pass
except Exception as error:
  print("exception =", error)
  raise error
finally:
  print("after three seconds, auto clear buffer! ?")
  sleep(3.0)
  clear_buffer()
  print("Test finished ?")

exit()

image

https://github.com/rpi-ws281x/rpi-ws281x-python/issues/95#issuecomment-1549991731

LED_INVERT = False


#!/usr/bin/env python3
# coding: utf8

from time import sleep
from rpi_ws281x import PixelStrip, Color

# LED strip configuration:
LED_COUNT = 60        # Number of LED pixels.
LED_PIN = 18          # GPIO pin connected to the pixels (18 uses PWM!).
# 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 a signal (try 10)
# color channel GRB ❓
LED_BRIGHTNESS = 50   # 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

# Create a NeoPixel object with the appropriate configuration.
strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)

# Initialize the library (must be called once before other functions).
strip.begin()
print("Test beginning ✅")

def clear_buffer():
  for i in range(LED_COUNT):
    strip.setPixelColor(i, Color(0, 0, 0))
  strip.show()
  sleep(1)

def test():
  print("strip.numPixels() =", strip.numPixels())
  for i in range(strip.numPixels()):
    # white ✅
    # strip.setPixelColor(i, Color(255, 255, 255))
    # red ❌
    strip.setPixelColor(i, Color(255, 0, 0))
  strip.show()
  sleep(1)

try:
  test()
except KeyboardInterrupt:
  print('Ctrl + C exit ✅')
except RuntimeError as error:
  print("error =", error, error.args[0])
  pass
except Exception as error:
  print("exception =", error)
  raise error
finally:
  print("after three seconds, auto clear buffer! ?")
  sleep(3.0)
  clear_buffer()
  print("Test finished ?")

exit()

image

demos

(? 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

RPi.GPIO

#!/usr/bin/env python3
# coding: utf8

import RPi.GPIO as GPIO

from datetime import datetime
import time
import sys

arg1 = sys.argv[1]
print("arg1 =", arg1);

# shell 获取时间戳 ✅
# SH_DATE=$(TZ=':Asia/Shanghai' date '+%Y-%m-%d %T');
# datetime = $(date '+%Y-%m-%d %T')

# Python 获取时间戳 ✅
now = datetime.now()
# 转换为指定的格式
currentTime = now.strftime("%Y-%m-%d %H:%M:%S")
print("⏰ current datetime =", currentTime);

# $ pinout 命令查看,或 https://pinout.xyz/
# 指定 BCM 模式下的 GPIO 针脚编号是 12
# 对应的物理针脚编号是 32
PIN = 12
# BCM 模式
GPIO.setmode(GPIO.BCM)

# 指定 GPIO 针脚为一个电流输出针脚
GPIO.setup(PIN, GPIO.OUT)
# 输出低电平
GPIO.output(PIN, GPIO.LOW)

# index
i = 0
# 类型转换,str => int
n = int(arg1)

print("n =", n)
print('开始闪烁⏳')

while (i < n):
  print("i =", i)
  # 高电平,LED 点亮
  GPIO.output(PIN, GPIO.HIGH)
  # 休眠 1 秒,防止 LED 长时间点亮烧坏了
  time.sleep(1.0)
  # 低电平,LED 熄灭
  GPIO.output(PIN, GPIO.LOW)
  # 休眠 1 秒
  time.sleep(1.0)
  i = i + 1

# 输出低电平,LED 关闭
# GPIO.output(PIN, GPIO.LOW)
# 清除,释放内存
GPIO.cleanup()

print('结束闪烁 ??')

refs

RPi.GPIO

https://www.cnblogs.com/xgqfrms/p/17374377.html

rpi_ws281x

https://www.cnblogs.com/xgqfrms/p/17387711.html

WS2811_STRIP_GRB

color channel:
GRB => WS2811_STRIP_GRB(RGB LEDs Strip, 3参数)GRBW => WS2811_STRIP_GRBW (RGBW LEDs Strip, 4 参数)

https://www.cnblogs.com/xgqfrms/p/17394537.html

Adafruit CircuitPython NeoPixel All In One

https://www.cnblogs.com/xgqfrms/p/17451650.html



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 ?️,侵权必究⚠️!