Skip to content

Commit

Permalink
Merge pull request #10 from Footleg/main
Browse files Browse the repository at this point in the history
Added a method of read the colour of any pixel
  • Loading branch information
blaz-r committed Jan 22, 2023
2 parents 3f50b85 + d913fdf commit 8012e3b
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion neopixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,29 @@ def set_pixel(self, pixel_num, rgb_w, how_bright=None):
else:
self.pixels[pixel_num] = pix_value

def get_pixel(self, pixel_num):
"""
Get red, green, blue and white (if applicable) values of pixel on position <pixel_num>
:param pixel_num: Index of pixel to be set
:return rgb_w: Tuple of form (r, g, b) or (r, g, b, w) representing color to be used
"""
balance = self.pixels[pixel_num]
sh_R, sh_G, sh_B, sh_W = self.shift
if self.W_in_mode:
w = (balance >> sh_W) & 255
b = (balance >> sh_B) & 255
r = (balance >> sh_R) & 255
g = (balance >> sh_G) & 255
red = int(r * 255 / self.brightness() )
green = int(g * 255 / self.brightness() )
blue = int(b * 255 / self.brightness() )
if self.W_in_mode:
white = int(w * 255 / self.brightness() )
return (red,green,blue,white)
else:
return (red,green,blue)

def __setitem__(self, idx, rgb_w):
"""
if npix is a Neopixel object,
Expand Down Expand Up @@ -325,4 +348,4 @@ def clear(self):
:return: None
"""
self.pixels = array.array("I", [0] * self.num_leds)
self.pixels = array.array("I", [0] * self.num_leds)

0 comments on commit 8012e3b

Please sign in to comment.