]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/bcm283x/neopixel.py
Merge pull request #88 from NVIDIA-AI-IOT-private/jetson
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / bcm283x / neopixel.py
1 import time
2 import math
3 import _rpi_ws281x as ws
4 import atexit
5
6 # LED configuration.
7 LED_CHANNEL    = 0
8 LED_FREQ_HZ    = 800000     # Frequency of the LED signal.  We only support 800KHz
9 LED_DMA_NUM    = 10         # DMA channel to use, can be 0-14.
10 LED_BRIGHTNESS = 255        # We manage the brightness in the neopixel library
11 LED_INVERT     = 0          # We don't support inverted logic
12 LED_STRIP      = None # We manage the color order within the neopixel library
13
14 # a 'static' object that we will use to manage our PWM DMA channel
15 # we only support one LED strip per raspi
16 _led_strip = None
17
18 def neopixel_write(gpio, buf):
19     global _led_strip # we'll have one strip we init if its not at first
20
21     if _led_strip is None:
22         # Create a ws2811_t structure from the LED configuration.
23         # Note that this structure will be created on the heap so you
24         # need to be careful that you delete its memory by calling
25         # delete_ws2811_t when it's not needed.
26         _led_strip = ws.new_ws2811_t()
27
28         # Initialize all channels to off
29         for channum in range(2):
30             channel = ws.ws2811_channel_get(_led_strip, channum)
31             ws.ws2811_channel_t_count_set(channel, 0)
32             ws.ws2811_channel_t_gpionum_set(channel, 0)
33             ws.ws2811_channel_t_invert_set(channel, 0)
34             ws.ws2811_channel_t_brightness_set(channel, 0)
35
36         channel = ws.ws2811_channel_get(_led_strip, LED_CHANNEL)
37
38         # Initialize the channel in use
39         count = 0
40         if len(buf) % 3 == 0:
41             # most common, divisible by 3 is likely RGB
42             LED_STRIP = ws.WS2811_STRIP_RGB
43             count = len(buf)//3
44         elif len(buf) % 4 == 0:
45             LED_STRIP = ws.SK6812_STRIP_RGBW
46             count = len(buf)//4
47         else:
48             raise RuntimeError("We only support 3 or 4 bytes-per-pixel")
49
50         ws.ws2811_channel_t_count_set(channel, count) # we manage 4 vs 3 bytes in the library
51         ws.ws2811_channel_t_gpionum_set(channel, gpio._pin.id)
52         ws.ws2811_channel_t_invert_set(channel, LED_INVERT)
53         ws.ws2811_channel_t_brightness_set(channel, LED_BRIGHTNESS)
54         ws.ws2811_channel_t_strip_type_set(channel, LED_STRIP)
55
56         # Initialize the controller
57         ws.ws2811_t_freq_set(_led_strip, LED_FREQ_HZ)
58         ws.ws2811_t_dmanum_set(_led_strip, LED_DMA_NUM)
59
60         resp = ws.ws2811_init(_led_strip)
61         if resp != ws.WS2811_SUCCESS:
62             if resp == -5:
63                 raise RuntimeError("NeoPixel support requires running with sudo, please try again!")
64             message = ws.ws2811_get_return_t_str(resp)
65             raise RuntimeError('ws2811_init failed with code {0} ({1})'.format(resp, message))
66         atexit.register(neopixel_cleanup)
67
68     channel = ws.ws2811_channel_get(_led_strip, LED_CHANNEL)
69     if gpio._pin.id != ws.ws2811_channel_t_gpionum_get(channel):
70         raise RuntimeError("Raspberry Pi neopixel support is for one strip only!")
71
72     if ws.ws2811_channel_t_strip_type_get(channel) == ws.WS2811_STRIP_RGB:
73         bpp = 3
74     else:
75         bpp = 4
76     # assign all colors!
77     for i in range(len(buf) // bpp):
78         r = buf[bpp*i]
79         g = buf[bpp*i+1]
80         b = buf[bpp*i+2]
81         if bpp == 3:
82             pixel = (r << 16) | (g << 8) | b
83         else:
84             w = buf[bpp*i+3]
85             pixel = (w << 24) | (r << 16) | (g << 8) | b
86         ws.ws2811_led_set(channel, i, pixel)
87
88     resp = ws.ws2811_render(_led_strip)
89     if resp != ws.WS2811_SUCCESS:
90         message = ws.ws2811_get_return_t_str(resp)
91         raise RuntimeError('ws2811_render failed with code {0} ({1})'.format(resp, message))
92     time.sleep(0.001 * ((len(buf)//100)+1))  # about 1ms per 100 bytes
93
94
95 def neopixel_cleanup():
96     global _led_strip
97
98     if _led_strip is not None:
99         # Ensure ws2811_fini is called before the program quits.
100         ws.ws2811_fini(_led_strip)
101         # Example of calling delete function to clean up structure memory.  Isn't
102         # strictly necessary at the end of the program execution here, but is good practice.
103         ws.delete_ws2811_t(_led_strip)
104         _led_strip = None