1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
4 """BCM283x NeoPixel Driver Class"""
7 import _rpi_ws281x as ws
10 # Used only for typing
11 from typing import Optional
12 from digitalio import DigitalInOut
17 # pylint: disable=redefined-outer-name,too-many-branches,too-many-statements
18 # pylint: disable=global-statement,protected-access
19 LED_FREQ_HZ = 800000 # Frequency of the LED signal. We only support 800KHz
20 LED_DMA_NUM = 10 # DMA channel to use, can be 0-14.
21 LED_BRIGHTNESS = 255 # We manage the brightness in the neopixel library
22 LED_INVERT = 0 # We don't support inverted logic
23 LED_STRIP = None # We manage the color order within the neopixel library
25 # a 'static' object that we will use to manage our PWM DMA channel
26 # we only support one LED strip per raspi
28 _buf: Optional[bytearray] = None
31 def neopixel_write(gpio: DigitalInOut, buf: bytearray) -> None:
32 """NeoPixel Writing Function"""
33 global _led_strip # we'll have one strip we init if its not at first
34 global _buf # we save a reference to the buf, and if it changes we will cleanup and re-init.
36 if _led_strip is None or buf is not _buf:
37 # This is safe to call since it doesn't do anything if _led_strip is None
40 # Create a ws2811_t structure from the LED configuration.
41 # Note that this structure will be created on the heap so you
42 # need to be careful that you delete its memory by calling
43 # delete_ws2811_t when it's not needed.
44 _led_strip = ws.new_ws2811_t()
47 # Initialize all channels to off
48 for channum in range(2):
49 channel = ws.ws2811_channel_get(_led_strip, channum)
50 ws.ws2811_channel_t_count_set(channel, 0)
51 ws.ws2811_channel_t_gpionum_set(channel, 0)
52 ws.ws2811_channel_t_invert_set(channel, 0)
53 ws.ws2811_channel_t_brightness_set(channel, 0)
55 channel = ws.ws2811_channel_get(_led_strip, _neopixel_detect_channel(gpio))
57 # Initialize the channel in use
60 # most common, divisible by 3 is likely RGB
61 LED_STRIP = ws.WS2811_STRIP_RGB
63 elif len(buf) % 4 == 0:
64 LED_STRIP = ws.SK6812_STRIP_RGBW
67 raise RuntimeError("We only support 3 or 4 bytes-per-pixel")
69 ws.ws2811_channel_t_count_set(
71 ) # we manage 4 vs 3 bytes in the library
72 ws.ws2811_channel_t_gpionum_set(channel, gpio._pin.id)
73 ws.ws2811_channel_t_invert_set(channel, LED_INVERT)
74 ws.ws2811_channel_t_brightness_set(channel, LED_BRIGHTNESS)
75 ws.ws2811_channel_t_strip_type_set(channel, LED_STRIP)
77 # Initialize the controller
78 ws.ws2811_t_freq_set(_led_strip, LED_FREQ_HZ)
79 ws.ws2811_t_dmanum_set(_led_strip, LED_DMA_NUM)
81 resp = ws.ws2811_init(_led_strip)
82 if resp != ws.WS2811_SUCCESS:
85 "NeoPixel support requires running with sudo, please try again!"
87 message = ws.ws2811_get_return_t_str(resp)
89 "ws2811_init failed with code {0} ({1})".format(resp, message)
91 atexit.register(neopixel_cleanup)
93 channel = ws.ws2811_channel_get(_led_strip, _neopixel_detect_channel(gpio))
94 if gpio._pin.id != ws.ws2811_channel_t_gpionum_get(channel):
95 raise RuntimeError("Raspberry Pi neopixel support is for one strip only!")
97 if ws.ws2811_channel_t_strip_type_get(channel) == ws.WS2811_STRIP_RGB:
102 for i in range(len(buf) // bpp):
107 pixel = (r << 16) | (g << 8) | b
110 pixel = (w << 24) | (r << 16) | (g << 8) | b
111 ws.ws2811_led_set(channel, i, pixel)
113 resp = ws.ws2811_render(_led_strip)
114 if resp != ws.WS2811_SUCCESS:
115 message = ws.ws2811_get_return_t_str(resp)
117 "ws2811_render failed with code {0} ({1})".format(resp, message)
119 time.sleep(0.001 * ((len(buf) // 100) + 1)) # about 1ms per 100 bytes
122 def neopixel_cleanup():
123 """Cleanup when we're done"""
126 if _led_strip is not None:
127 # Ensure ws2811_fini is called before the program quits.
128 ws.ws2811_fini(_led_strip)
129 # Example of calling delete function to clean up structure memory. Isn't
130 # strictly necessary at the end of the program execution here, but is good practice.
131 ws.delete_ws2811_t(_led_strip)
135 def _neopixel_detect_channel(gpio: DigitalInOut) -> int:
136 """Detect the channel for a given GPIO, added for support PWM1 pins"""
137 if gpio._pin.id in (13, 19, 41, 45):