Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added gentlegiantJGC examples #61

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/ggjgc_bouncing_balls/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ggjgc_bouncing_balls.py

This spawns a number of balls of light in the tree and bounces them around the bounding box of the tree.

Requires matts_tree_helpers.py from https://github.com/gentlegiantJGC/xmastree2021-simulator

# ggjgc_bouncing_balls.csv

The baked version of the python program for Matt's tree.

Runs at 30fps. This is baked into the FRAME_TIME column as read by https://github.com/standupmaths/xmastree2021/pull/30
894 changes: 894 additions & 0 deletions examples/ggjgc_bouncing_balls/ggjgc_bouncing_balls.csv

Large diffs are not rendered by default.

89 changes: 89 additions & 0 deletions examples/ggjgc_bouncing_balls/ggjgc_bouncing_balls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Here are the libraries I am currently using:
import random
from colorsys import hsv_to_rgb
import numpy

# You are welcome to add any of these:
# import time
# import math
# import random
# import scipy
# import sys

from matts_tree_helpers import get_coords_pixels, FrameManager


def bouncing_balls():
# NOTE THE LEDS ARE GRB COLOUR (NOT RGB)

# If you want to have user changeable values, they need to be entered from the command line
# so import sys and use sys.argv[0] etc.
# some_value = int(sys.argv[0])

coords, pixels = get_coords_pixels("coords_2021.csv")

# YOU CAN EDIT FROM HERE DOWN

frame_time = 1 / 30

ball_count = 10

# find the bounds of the LEDs
bounds = [(min(axis), max(axis)) for axis in zip(*coords)]
tree_height = bounds[2][1] - bounds[2][0]

ball_radius = 0.1 * tree_height
speed = 0.2 * tree_height

ball_locations = [
[random.uniform(axis_min, axis_max) for axis_min, axis_max in bounds]
for _ in range(ball_count)
]
ball_velocities = []
for _ in range(ball_count):
v = numpy.random.rand(3) - 0.5
ball_velocities.append(speed * v / numpy.linalg.norm(v))

def hue_to_grb(hue):
rgb = hsv_to_rgb(hue, 1, 1)
return 255 * rgb[1], 255 * rgb[0], 255 * rgb[2]

ball_colours = [hue_to_grb(i / ball_count) for i in range(ball_count)]

while True:
with FrameManager(frame_time):
# find which ball each pixel is in (if any)
for i, coord in enumerate(coords):
for ball_location, ball_colour in zip(ball_locations, ball_colours):
if (
sum((bax - cax) ** 2 for bax, cax in zip(ball_location, coord))
** 0.5
< ball_radius
):
pixels[i] = ball_colour
break
else:
pixels[i] = (0, 0, 0)

# use the show() option as rarely as possible as it takes ages
# do not use show() each time you change a LED but rather wait until you have changed them all
pixels.show()

# find the new ball locations
for i, (ball_location, ball_velocity) in enumerate(
zip(ball_locations, ball_velocities)
):
ball_locations[i] = ball_location = [
loc + vel * frame_time
for loc, vel in zip(ball_location, ball_velocity)
]
ball_velocities[i] = [
vel if axis_min < loc < axis_max else -vel
for loc, vel, (axis_min, axis_max) in zip(
ball_location, ball_velocity, bounds
)
]


if __name__ == "__main__":
bouncing_balls()
11 changes: 11 additions & 0 deletions examples/ggjgc_fireworks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ggjgc_fireworks.py

This creates a firework in the tree that explodes.

Requires matts_tree_helpers.py from https://github.com/gentlegiantJGC/xmastree2021-simulator

# ggjgc_fireworks.csv

The baked version of the python program for Matt's tree.

Runs at 30fps. This is baked into the FRAME_TIME column as read by https://github.com/standupmaths/xmastree2021/pull/30
198 changes: 198 additions & 0 deletions examples/ggjgc_fireworks/ggjgc_fireworks.csv

Large diffs are not rendered by default.

101 changes: 101 additions & 0 deletions examples/ggjgc_fireworks/ggjgc_fireworks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Here are the libraries I am currently using:
from typing import Tuple
from dataclasses import dataclass
import random
from colorsys import hsv_to_rgb as hsv_to_rgb

# You are welcome to add any of these:
# import numpy
# import time
# import math
# import scipy
# import sys

from matts_tree_helpers import get_coords_pixels, FrameManager


def hue_to_rgb(hue):
return tuple(int(a*255) for a in hsv_to_rgb(hue, 1, 1))


@dataclass
class Particle:
position: Tuple[float, float, float] = (0, 0, 0)
velocity: Tuple[float, float, float] = (0, 0, 0)
colour: Tuple[int, int, int] = (0, 0, 0)


def fireworks():
# NOTE THE LEDS ARE GRB COLOUR (NOT RGB)

# If you want to have user changeable values, they need to be entered from the command line
# so import sys and use sys.argv[0] etc.
# some_value = int(sys.argv[0])

coords, pixels = get_coords_pixels("coords_2021.csv")

# YOU CAN EDIT FROM HERE DOWN

# find some information about the tree
y_coords = list(zip(*coords))[2]
max_y = max(y_coords)
min_y = min(y_coords)
tree_height = max_y - min_y

# config settings
frame_time = 1 / 30
particle_count = 100 # The number of particles
particle_distance = tree_height * 0.1 # the maximum distance an LED can be considered near a particle
particle_velocity = tree_height * 0.04 # the maximum velocity in each axis the particle can travel

# the particle start position
start = tuple(sum(ax)/len(coords) for ax in zip(*coords))

while True:
colours = [hue_to_rgb(random.random()) for _ in range(3)]
particles = []
for _ in range(particle_count):
# generate a number of particles
velocity = tuple(random.random()*2-1 for _ in range(3))
mag = sum(v ** 2 for v in velocity) ** 0.5
velocity = tuple(particle_velocity * v / mag for v in velocity)
particles.append(
Particle(
start, # The start position of the particle
velocity, # The velocity of the particle
random.choice(colours) # The colour of the particle
)
)

firework_has_particles = True # Used to track if particles are still on the tree
while firework_has_particles:
with FrameManager(frame_time):
# turn all the LEDs to off
for i in range(len(coords)):
pixels[i] = (0, 0, 0)

firework_has_particles = False
# find which LED each particle is closest to
for particle in particles:
dist = tree_height * 10
led = None
for i, coord in enumerate(coords):
led_dist = sum((c2 - c1) ** 2 for c1, c2 in zip(coord, particle.position)) ** 0.5
if led_dist < particle_distance and led_dist < dist:
led = i
dist = led_dist
if led is not None:
pixels[led] = particle.colour
firework_has_particles = True

# use the show() option as rarely as possible as it takes ages
# do not use show() each time you change a LED but rather wait until you have changed them all
pixels.show()

# update the particle location
for particle in particles:
particle.position = tuple(sum(ax) for ax in zip(particle.position, particle.velocity))


if __name__ == "__main__":
fireworks()
11 changes: 11 additions & 0 deletions examples/ggjgc_rainbow_orbit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ggjgc_rainbow_orbit.py

This is a rainbow effect that rotates around the tree over time.

Requires matts_tree_helpers.py from https://github.com/gentlegiantJGC/xmastree2021-simulator

# ggjgc_rainbow_orbit.csv

The baked version of the python program for Matt's tree.

Runs at 30fps. This is baked into the FRAME_TIME column as read by https://github.com/standupmaths/xmastree2021/pull/30
Loading