From 9e956e518d7d61a0199e7153ef9d7780aad0abda Mon Sep 17 00:00:00 2001 From: Max Carlson <> Date: Fri, 2 Feb 2024 16:34:28 +0200 Subject: [PATCH 1/2] Add array/list-like methods --- neopixel.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/neopixel.py b/neopixel.py index 6bb00a4..7b3d4dd 100644 --- a/neopixel.py +++ b/neopixel.py @@ -229,6 +229,7 @@ class Neopixel: npix[15:21] = (255,0,0) # <- sets 16,17 .. 20 to red npix[21:29:2] = (0,0,255) # <- sets 21,23,25,27 to blue npix[1::2] = (0,0,0) # <- sets all odd pixels to 'off' + npix[:] = [(0,5,0),(0,5,0)] # <- replaces all pixels with those from the array (the 'slice' cases pass idx as a 'slice' object, and set_pixel processes the slice) @@ -236,7 +237,17 @@ class Neopixel: :param rgb_w: Tuple of form (r, g, b) or (r, g, b, w) representing color to be used :return: """ - self.set_pixel(idx, rgb_w) + if type(rgb_w) is list: + for i in range(self.num_leds): + self.set_pixel(i, rgb_w[i]) + else: + self.set_pixel(idx, rgb_w) + + def __len__(self): + return self.num_leds + + def __getitem__(self, idx): + return self.get_pixel(idx) def colorHSV(self, hue, sat, val): """ From 89b06f4383210cbfad322f7de93d6dbaf178c458 Mon Sep 17 00:00:00 2001 From: blaz-r Date: Sun, 31 Mar 2024 19:46:20 +0200 Subject: [PATCH 2/2] Decouple color and pixel indexing --- neopixel.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/neopixel.py b/neopixel.py index 86bb459..8621194 100644 --- a/neopixel.py +++ b/neopixel.py @@ -234,12 +234,16 @@ class Neopixel: set_pixel processes the slice) :param idx: Index can either be indexing number or slice - :param rgb_w: Tuple of form (r, g, b) or (r, g, b, w) representing color to be used - :return: + :param rgb_w: Tuple (or list of tuples) of form (r, g, b) or (r, g, b, w) representing color to be used + :return: None """ if type(rgb_w) is list: - for i in range(self.num_leds): - self.set_pixel(i, rgb_w[i]) + # set some subset, if idx is a slice: + if type(idx) is slice: + for rgb_i, pixel_i in enumerate(range(*idx.indices(self.num_leds))): + self.set_pixel(pixel_i, rgb_w[rgb_i]) + else: + raise ValueError("Index must be a slice when setting multiple pixels as list") else: self.set_pixel(idx, rgb_w)