]> Repositories - Adafruit_Blinka-hackapet.git/commitdiff
Merge pull request #58 from ladyada/master
authorScott Shawcroft <scott@tannewt.org>
Mon, 3 Dec 2018 07:01:00 +0000 (23:01 -0800)
committerGitHub <noreply@github.com>
Mon, 3 Dec 2018 07:01:00 +0000 (23:01 -0800)
RGBW support for NeoPixels

src/adafruit_blinka/microcontroller/raspi_23/neopixel.py

index 3a92c607ded7a30a6f25f18e973f4b1266549950..3d670c703ce2404ed9a099a980e244872997d7f2 100644 (file)
@@ -59,6 +59,8 @@ def neopixel_write(gpio, buf):
     
         resp = ws.ws2811_init(_led_strip)
         if resp != ws.WS2811_SUCCESS:
+            if resp == -5:
+                raise RuntimeError("NeoPixel support requires running with sudo, please try again!")
             message = ws.ws2811_get_return_t_str(resp)
             raise RuntimeError('ws2811_init failed with code {0} ({1})'.format(resp, message))
         atexit.register(neopixel_cleanup)
@@ -67,12 +69,20 @@ def neopixel_write(gpio, buf):
     if gpio._pin.id != ws.ws2811_channel_t_gpionum_get(channel):
         raise RuntimeError("Raspberry Pi neopixel support is for one strip only!")
 
+    if ws.ws2811_channel_t_strip_type_get(channel) == ws.WS2811_STRIP_RGB:
+        bpp = 3
+    else:
+        bpp = 4
     # assign all colors!
-    for i in range(len(buf) // 3):
-        r = buf[3*i]
-        g = buf[3*i+1]
-        b = buf[3*i+2]
-        pixel = (r << 16) | (g << 8) | b
+    for i in range(len(buf) // bpp):
+        r = buf[bpp*i]
+        g = buf[bpp*i+1]
+        b = buf[bpp*i+2]
+        if bpp == 3:
+            pixel = (r << 16) | (g << 8) | b
+        else:
+            w = buf[bpp*i+3]
+            pixel = (w << 24) | (r << 16) | (g << 8) | b
         ws.ws2811_led_set(channel, i, pixel)
     
     resp = ws.ws2811_render(_led_strip)