Skip to content

Instantly share code, notes, and snippets.

@soup-bowl
Last active November 27, 2023 12:40
Show Gist options
  • Save soup-bowl/dae257518868e2ffcbb283ae2902aa67 to your computer and use it in GitHub Desktop.
Save soup-bowl/dae257518868e2ffcbb283ae2902aa67 to your computer and use it in GitHub Desktop.
LED Christmas Tree for Raspberry Pi - https://thepihut.com/products/3d-xmas-tree-for-raspberry-pi
from gpiozero import LEDBoard, PWMLED
from gpiozero.tools import random_values
from signal import pause
from time import time, sleep, strftime
class Tree(object):
def __init__(self):
self.active = False
self.tree = LEDBoard(*range(4,28),pwm=True)
self.star = PWMLED(2)
def is_on(self):
return self.active
def lights_twinkle(self):
self.star.on()
for led in self.tree:
led.source_delay = 0.1
led.source = random_values()
self.active = True
def lights_off(self):
self.star.off()
for led in self.tree:
led.source_delay = 0.1
led.source = [0]
self.active = False
def main():
tree = Tree()
while True:
currenttime = int(strftime('%H'))
if currenttime > 7 and currenttime < 22:
tree.lights_twinkle()
else:
tree.lights_off()
sleep(30)
main()
@soup-bowl
Copy link
Author

soup-bowl commented Dec 15, 2021

Added to .bash_aliases:

alias christmas="nohup python3 ~/dae257518868e2ffcbb283ae2902aa67/tree.py > /dev/null 2>&1&"

Breakdown:

  • Alias says "this word 'christmas' is to run the command in quotes".
  • nohup means no signal hang-up. When we inveitably close SSH, we don't want that to also mean "die Bart script, die".
  • Python is the language this script runs (python3 and python3-gpiozero are (usually) already installed).
  • Next up is where this tree.py file is located.
  • > means "redirect any output to this place", which is then set to null, so it goes straight into the trash.
  • 2>&1 means any error outputs (stderr) gets treated as regular output (stdout), which is subsequently trashed thanks to above.
  • Final & returns our Shell prompt back, despite the fact the script is still running.

Altogether, it means the script will run but won't tell you the output, gives you command control back, and doesn't associate you shutting down your command window as the script dies too.

Adding this whole command to .bash_aliases on a bash system will mean each load-up of terminal will make the above command available.

EDIT: Latest change means it'll run (very ineffectively) between 8 am and 11 pm.

EDIT2023: Adjusted to use Python 3 - Python 2 really shouldn't be used any more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment