-
-
Save RZRZR/cab5bd9440d8f6b1061b5d2294abe0de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from gpiozero import SPIDevice | |
from colorzero import Color, Hue | |
class Apa102Pixel(SPIDevice): | |
def __init__(self, *args, **kwargs): | |
super(Apa102Pixel, self).__init__(*args, **kwargs) | |
self._brightness = 255.0 | |
self._value = (0, 0, 0) | |
@property | |
def value(self): | |
return self._value | |
@value.setter | |
def value(self, value): | |
r, g, b = [int(self._brightness * v) for v in value] | |
data = [0]*4 + [0b11100000 | 31, b, g, r] + [0]*5 | |
self._spi.transfer(data) | |
self._value = value | |
def on(self): | |
self.value = (1, 1, 1) | |
def off(self): | |
self.value = (0, 0, 0) | |
def close(self): | |
self.off() | |
super(Apa102Pixel, self).close() | |
class Apa102PixelStrip(SPIDevice): | |
def __init__(self, pixels, *args, **kwargs): | |
super(Apa102PixelStrip, self).__init__(*args, **kwargs) | |
self._brightness = 255.0 | |
self._pixels = pixels | |
self.off() | |
@property | |
def color(self): | |
return None | |
@color.setter | |
def color(self, c): | |
r, b, g = c | |
self.value = ((r, g, b),) * self._pixels | |
@property | |
def value(self): | |
return self._value | |
@value.setter | |
def value(self, value): | |
start_of_frame = [0]*4 | |
end_of_frame = [0]*5 | |
start_of_pixel = 0b11100000 | 31 # Start bits and 5-bit brightness (0-31) | |
pixels = [[int(self._brightness*v) for v in p] for p in value] | |
pixels = [[start_of_pixel, b, g, r] for r, g, b in pixels] | |
pixels = [i for p in pixels for i in p] | |
data = start_of_frame + pixels + end_of_frame | |
self._spi.transfer(data) | |
self._value = value | |
def on(self): | |
self.value = ((1, 1, 1),) * self._pixels | |
def off(self): | |
self.value = ((0, 0, 0),) * self._pixels | |
def close(self): | |
self.off() | |
super(Apa102PixelStrip, self).close() | |
blinkt = Apa102PixelStrip(25, mosi_pin=12, clock_pin=25) | |
while True: | |
try: | |
txt = input('Enter a colour name: ') | |
c = Color(txt) | |
print(c) | |
blinkt.color = c | |
except ValueError: | |
print("Invalid colour") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from gpiozero import SPIDevice | |
from colorzero import Color, Hue | |
class Apa102Pixel(SPIDevice): | |
def __init__(self, *args, **kwargs): | |
super(Apa102Pixel, self).__init__(*args, **kwargs) | |
self._brightness = 255.0 | |
self._value = (0, 0, 0) | |
@property | |
def value(self): | |
return self._value | |
@value.setter | |
def value(self, value): | |
r, g, b = [int(self._brightness * v) for v in value] | |
data = [0]*4 + [0b11100000 | 31, b, g, r] + [0]*5 | |
self._spi.transfer(data) | |
self._value = value | |
def on(self): | |
self.value = (1, 1, 1) | |
def off(self): | |
self.value = (0, 0, 0) | |
def close(self): | |
self.off() | |
super(Apa102Pixel, self).close() | |
class Apa102PixelStrip(SPIDevice): | |
def __init__(self, pixels, *args, **kwargs): | |
super(Apa102PixelStrip, self).__init__(*args, **kwargs) | |
self._brightness = 255.0 | |
self._pixels = pixels | |
self.off() | |
@property | |
def color(self): | |
return None | |
@color.setter | |
def color(self, c): | |
r, b, g = c | |
self.value = ((r, g, b),) * self._pixels | |
@property | |
def value(self): | |
return self._value | |
@value.setter | |
def value(self, value): | |
start_of_frame = [0]*4 | |
end_of_frame = [0]*5 | |
start_of_pixel = 0b11100000 | 31 # Start bits and 5-bit brightness (0-31) | |
pixels = [[int(self._brightness*v) for v in p] for p in value] | |
pixels = [[start_of_pixel, b, g, r] for r, g, b in pixels] | |
pixels = [i for p in pixels for i in p] | |
data = start_of_frame + pixels + end_of_frame | |
self._spi.transfer(data) | |
self._value = value | |
def on(self): | |
self.value = ((1, 1, 1),) * self._pixels | |
def off(self): | |
self.value = ((0, 0, 0),) * self._pixels | |
def close(self): | |
self.off() | |
super(Apa102PixelStrip, self).close() | |
blinkt = Apa102PixelStrip(25, mosi_pin=12, clock_pin=25) | |
y = Color('yellow') | |
r = Color('red') | |
while True: | |
for c in y.gradient(r, steps=20): | |
print(c) | |
blinkt.color = c | |
time.sleep(0.01) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from gpiozero import SPIDevice | |
from colorzero import Color, Hue | |
class Apa102Pixel(SPIDevice): | |
def __init__(self, *args, **kwargs): | |
super(Apa102Pixel, self).__init__(*args, **kwargs) | |
self._brightness = 255.0 | |
self._value = (0, 0, 0) | |
@property | |
def value(self): | |
return self._value | |
@value.setter | |
def value(self, value): | |
r, g, b = [int(self._brightness * v) for v in value] | |
data = [0]*4 + [0b11100000 | 31, b, g, r] + [0]*5 | |
self._spi.transfer(data) | |
self._value = value | |
def on(self): | |
self.value = (1, 1, 1) | |
def off(self): | |
self.value = (0, 0, 0) | |
def close(self): | |
self.off() | |
super(Apa102Pixel, self).close() | |
class Apa102PixelStrip(SPIDevice): | |
def __init__(self, pixels, *args, **kwargs): | |
super(Apa102PixelStrip, self).__init__(*args, **kwargs) | |
self._brightness = 255.0 | |
self._pixels = pixels | |
self.off() | |
@property | |
def color(self): | |
return None | |
@color.setter | |
def color(self, c): | |
r, b, g = c | |
self.value = ((r, g, b),) * self._pixels | |
@property | |
def value(self): | |
return self._value | |
@value.setter | |
def value(self, value): | |
start_of_frame = [0]*4 | |
end_of_frame = [0]*5 | |
start_of_pixel = 0b11100000 | 31 # Start bits and 5-bit brightness (0-31) | |
pixels = [[int(self._brightness*v) for v in p] for p in value] | |
pixels = [[start_of_pixel, b, g, r] for r, g, b in pixels] | |
pixels = [i for p in pixels for i in p] | |
data = start_of_frame + pixels + end_of_frame | |
self._spi.transfer(data) | |
self._value = value | |
def on(self): | |
self.value = ((1, 1, 1),) * self._pixels | |
def off(self): | |
self.value = ((0, 0, 0),) * self._pixels | |
def close(self): | |
self.off() | |
super(Apa102PixelStrip, self).close() | |
blinkt = Apa102PixelStrip(25, mosi_pin=12, clock_pin=15) | |
while True: | |
for c in ((1, 0, 0), (0, 1, 0), (0, 0, 1)): | |
print(c) | |
blinkt.color = c | |
time.sleep(0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment