]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/bcm283x/neopixel.py
Update Package Data again
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / bcm283x / neopixel.py
1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 """BCM283x NeoPixel Driver Class"""
5 import time
6 import atexit
7 import _rpi_ws281x as ws
8
9 try:
10     # Used only for typing
11     from typing import Optional
12     from digitalio import DigitalInOut
13 except ImportError:
14     pass
15
16 # LED configuration.
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
24
25 # a 'static' object that we will use to manage our PWM DMA channel
26 # we only support one LED strip per raspi
27 _led_strip = None
28 _buf: Optional[bytearray] = None
29
30
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.
35
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
38         neopixel_cleanup()
39
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()
45         _buf = buf
46
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)
54
55         channel = ws.ws2811_channel_get(_led_strip, _neopixel_detect_channel(gpio))
56
57         # Initialize the channel in use
58         count = 0
59         if len(buf) % 3 == 0:
60             # most common, divisible by 3 is likely RGB
61             LED_STRIP = ws.WS2811_STRIP_RGB
62             count = len(buf) // 3
63         elif len(buf) % 4 == 0:
64             LED_STRIP = ws.SK6812_STRIP_RGBW
65             count = len(buf) // 4
66         else:
67             raise RuntimeError("We only support 3 or 4 bytes-per-pixel")
68
69         ws.ws2811_channel_t_count_set(
70             channel, count
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)
76
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)
80
81         resp = ws.ws2811_init(_led_strip)
82         if resp != ws.WS2811_SUCCESS:
83             if resp == -5:
84                 raise RuntimeError(
85                     "NeoPixel support requires running with sudo, please try again!"
86                 )
87             message = ws.ws2811_get_return_t_str(resp)
88             raise RuntimeError(
89                 "ws2811_init failed with code {0} ({1})".format(resp, message)
90             )
91         atexit.register(neopixel_cleanup)
92
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!")
96
97     if ws.ws2811_channel_t_strip_type_get(channel) == ws.WS2811_STRIP_RGB:
98         bpp = 3
99     else:
100         bpp = 4
101     # assign all colors!
102     for i in range(len(buf) // bpp):
103         r = buf[bpp * i]
104         g = buf[bpp * i + 1]
105         b = buf[bpp * i + 2]
106         if bpp == 3:
107             pixel = (r << 16) | (g << 8) | b
108         else:
109             w = buf[bpp * i + 3]
110             pixel = (w << 24) | (r << 16) | (g << 8) | b
111         ws.ws2811_led_set(channel, i, pixel)
112
113     resp = ws.ws2811_render(_led_strip)
114     if resp != ws.WS2811_SUCCESS:
115         message = ws.ws2811_get_return_t_str(resp)
116         raise RuntimeError(
117             "ws2811_render failed with code {0} ({1})".format(resp, message)
118         )
119     time.sleep(0.001 * ((len(buf) // 100) + 1))  # about 1ms per 100 bytes
120
121
122 def neopixel_cleanup():
123     """Cleanup when we're done"""
124     global _led_strip
125
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)
132         _led_strip = None
133
134
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):
138         return 1
139     return 0