Skip to content

Instantly share code, notes, and snippets.

@bennuttall
Last active November 26, 2019 00:25
Show Gist options
  • Save bennuttall/c67a482e4e3989120df4983dc992aefb to your computer and use it in GitHub Desktop.
Save bennuttall/c67a482e4e3989120df4983dc992aefb to your computer and use it in GitHub Desktop.
from tree import Tree
from colorzero import Color
tree = Tree(25, mosi_pin=12, clock_pin=25)
while True:
try:
txt = input('Enter a colour name: ')
c = Color(txt)
print(c)
tree.color = c
except ValueError:
print("Invalid colour")
from tree import Tree
from colorzero import Color
from time import sleep
tree = Tree(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)
tree.color = c
sleep(0.01)
from tree import Tree
from time import sleep
tree = Tree(25, mosi_pin=12, clock_pin=25)
while True:
for c in ((1, 0, 0), (0, 1, 0), (0, 0, 1)):
print(c)
tree.color = c
sleep(0.1)
from gpiozero import SPIDevice
from colorzero import Color, Hue
class Tree(SPIDevice):
def __init__(self, pixels, brightness=0.5, *args, **kwargs):
super(Tree, self).__init__(*args, **kwargs)
self._pixels = pixels
self._value = [(0, 0, 0)] * pixels
self.brightness = brightness
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 brightness(self):
return self._brightness
@brightness.setter
def brightness(self, brightness):
max_brightness = 31
self._brightness_bits = int(brightness * max_brightness)
self._brightness = brightness
self.value = self.value
@property
def value(self):
return self._value
@value.setter
def value(self, value):
start_of_frame = [0]*4
end_of_frame = [0]*5
# SSSBBBBB (start, brightness)
brightness = 0b11100000 | self._brightness_bits
pixels = [[int(255*v) for v in p] for p in value]
pixels = [[brightness, 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(Tree, self).close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment