Adafruit CircuitPython NeoPixel All In One

xgqfrms / 2023-06-02 / 原文

Adafruit CircuitPython NeoPixel All In One

Raspberry Pi & Python & WS2812B RGB LED Strip

neopixel

# install for current user
$ pip3 install adafruit-circuitpython-neopixel

# install system-wide
$ sudo pip3 install adafruit-circuitpython-neopixel

https://pypi.org/project/adafruit-circuitpython-neopixel/

https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel

docs

https://readthedocs.org/projects/adafruit-circuitpython-neopixel/

https://docs.circuitpython.org/projects/neopixel/en/latest/
https://docs.circuitpython.org/projects/neopixel/en/latest/index.html
https://docs.circuitpython.org/projects/neopixel/en/3.1.1/_modules/neopixel.html

API

image

neopixel.GRB= 'GRB'
Green Red Blue

neopixel.GRBW= 'GRBW'
Green Red Blue White

Class:

neopixel.NeoPixel(pin: Pin, n: int, *, bpp: int = 3, brightness: float = 1.0, auto_write: bool = True, pixel_order: str = None)

A sequence of neopixels.

Parameters:
pin (Pin) – The pin to output neopixel data on.

n (int) – The number of neopixels in the chain

bpp (int) – Bytes per pixel. 3 for RGB and 4 for RGBW pixels.

brightness (float) – Brightness of the pixels between 0.0 and 1.0 where 1.0 is full brightness

auto_write (bool) – True if the neopixels should immediately change when set. If False, show must be called explicitly.

pixel_order (str) – Set the pixel color channel order. GRBW is set by default.

https://docs.circuitpython.org/projects/neopixel/en/latest/api.html

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

import neopixel
import board
from time import sleep

# neopixel.NeoPixel(pin: Pin, n: int, *, bpp: int = 3, brightness: float = 1.0, auto_write: bool = True, pixel_order: str = None)
# pixels = neopixel.NeoPixel(board.D18, 60, brightness=0.2, auto_write: False, pixel_order: neopixel.GRB)
pixels = neopixel.NeoPixel(board.D18, 60, brightness=0.2, auto_write: True, pixel_order: neopixel.GRB)

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

import neopixel
import board
from time import sleep

# neopixel.NeoPixel(pin: Pin, n: int, *, bpp: int = 3, brightness: float = 1.0, auto_write: bool = True, pixel_order: str = None)
pixels = neopixel.NeoPixelboard.NEOPIXEL, 60, brightness=0.2, auto_write: False, pixel_order: neopixel.GRB)
pixels = neopixel.NeoPixel(board.NEOPIXEL, 60, brightness=0.2, auto_write: True, pixel_order: neopixel.GRB)

Python 函数参数的传递方式 ???

匿名参数
具名参数

  1. 具名参数前面的参数可以省略参数名
  2. 具名参数后面的参数必须写参数名
#!/usr/bin/env python3
# coding: utf8

def func_args(arg1: int, arg2: str = None, arg3: float = 1.0, arg4: bool = True):
  print("arg1 {}".format(arg1))
  print("arg2 {}".format(arg2))
  print("arg3 {}".format(arg3))
  print("arg4 {}".format(arg4))

# 全部使用匿名参数 ✅
func_args(60, "LEDs", 0.2, False)

# 全部使用匿名参数 ✅
func_args(arg1: 60, arg2: "LEDs", arg3: 0.2, arg4: ,False)

# 混合使用,前面匿名参数,后面具名参数 ✅
func_args(60, "LEDs", arg3: 0.2, arg4: False)

# 混合使用,前面匿名参数,中间具名参数,后面匿名参数 ❌
func_args(60, "LEDs", arg3: 0.2,  False)

Python online REPL

https://www.runoob.com/try/runcode.php?filename=HelloWorld&type=python3

bugs ❌

https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel/issues/151

https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel/issues/153

demos

Feather M0 Express and Metro M0 Express

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

import board
import neopixel

# 1 LEDs ❓
pixels = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixels[0] = (10, 0, 0)

image

https://www.adafruit.com/product/3403

image

https://www.adafruit.com/product/3505

Circuit Playground Express

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

import board
import neopixel

# 10 LEDs❓
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, auto_write=False)

pixels[0] = (10, 0, 0)
pixels[9] = (0, 10, 0)

pixels.show()

image

https://www.adafruit.com/product/3333

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

import board
import neopixel

pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, auto_write=False)

# RGB test
pixels[0] = (255, 0, 0)
pixels[1] = (0, 255, 0)
pixels[2] = (0, 0, 255)

pixels.show()

refs

RPi.GPIO

rpi_ws281x

WS2811_STRIP_GRB

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



©xgqfrms 2012-2021

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