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

Some plane, sphere effects #15

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
25 changes: 25 additions & 0 deletions sim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
plt.style.use("dark_background")

coords = list(map(eval, open("Python/coords.txt").readlines()))
x = list(c[0] for c in coords)
y = list(c[1] for c in coords)
z = list(c[2] for c in coords)

class board:
D18 = None

class neopixel:
class NeoPixel:
def __init__(self, _pin, n, *args, **kwargs):
self.pixels = [(0, 0, 0)] * n
self.ax = plt.axes(projection="3d")

def __setitem__(self, index, color):
self.pixels[index] = (color[1] / 255.0, color[0] / 255.0, color[2] / 255.0, 1)

def show(self):
self.ax.scatter(x, y, z, c=self.pixels)
plt.pause(1 / 1000)
self.ax.cla()
131 changes: 131 additions & 0 deletions xmaslights-moreplanes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
def xmaslight():
# This is the code from my

#NOTE THE LEDS ARE GRB COLOUR (NOT RGB)

# Here are the libraries I am currently using:
import time
import board
import neopixel
#from sim import board
#from sim import neopixel
import re
import math

# You are welcome to add any of these:
import random
import numpy as np
# import scipy
# import sys

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

# IMPORT THE COORDINATES (please don't break this bit)

coordfilename = "Python/coords.txt"

fin = open(coordfilename,'r')
coords_raw = fin.readlines()

coords_bits = [i.split(",") for i in coords_raw]

coords = []

for slab in coords_bits:
new_coord = []
for i in slab:
new_coord.append(int(re.sub(r'[^-\d]','', i)))
coords.append(new_coord)

#set up the pixels (AKA 'LEDs')
PIXEL_COUNT = len(coords) # this should be 500

pixels = neopixel.NeoPixel(board.D18, PIXEL_COUNT, auto_write=False)

# YOU CAN EDIT FROM HERE DOWN
np.random.seed(int(time.time()))

def length(v):
return np.sqrt(np.sum(np.square(v)))

def normalize(v):
return np.divide(v, length(v))

def clamp(amin, amax, v):
return np.maximum(amin, np.minimum(amax, v))

def randomDirVec(components = 3):
return normalize([2.0*np.random.random() - 1.0 for x in range(components)])

def randomColorVec(components = 3):
return normalize([np.random.random() for x in range(components)])

def randomPlane():
# random direction, depth offset, color
ret = (randomDirVec(), 50.0*np.random.random(), randomColorVec())
print("generating plane: ", ret)
return ret

# some tweakables
NUMPLANES = 3
RANDOM_PLANES = True

# maximum brightness, to avoid catching trees on fire
BRIGHTNESS = 192
# brightness falloff (over distance to the plane, square)
FALLOFF = 1000.0
# how far to move each plane, +/-
PLANE_DISTANCE = 300
# how many "frames" to wait before adding a new plane
PLANE_TIMER = 128
# controls how fast planes move, "frames" / divider (in radians)
TIME_DIVIDER = 16.0

if RANDOM_PLANES:
planes = [randomPlane() for n in range(NUMPLANES)]

else:
# move syncronized planes over each axis
planes = [[x, 0, x] for x in [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
]]

# reusing run as "frame" counter here
run = 1
slow = 0
while run:
time.sleep(slow)
run += 1

LED = 0
while LED < len(coords):
pixColor = [0, 0, 0]
for thing in planes:
planeDir, depth, color = thing
# XXX: reuse depth for sin offset here, to add some variation to the plane position
dist = np.dot(planeDir, coords[LED]) + depth + PLANE_DISTANCE*math.sin(depth + run / TIME_DIVIDER)
falloff = FALLOFF / max(1.0, dist ** 2.0)
pixColor += np.multiply(color, falloff)

linearColor = clamp(0.0, 1.0, pixColor) / len(planes)
gammaCorrected = np.power(linearColor, 1.0 / 2.2)
pixels[LED] = list(gammaCorrected * BRIGHTNESS)
LED += 1

# 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()

# now we get ready for the next cycle
if RANDOM_PLANES and run % PLANE_TIMER == 0:
planes = planes[1:] + [randomPlane()]

return 'DONE'


# yes, I just put this at the bottom so it auto runs
xmaslight()
111 changes: 111 additions & 0 deletions xmaslights-sphere.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
def xmaslight():
# This is the code from my

#NOTE THE LEDS ARE GRB COLOUR (NOT RGB)

# Here are the libraries I am currently using:
import time
import board
import neopixel
#from sim import board
#from sim import neopixel
import re
import math

# You are welcome to add any of these:
import random
import numpy as np
# import scipy
# import sys

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

# IMPORT THE COORDINATES (please don't break this bit)

coordfilename = "Python/coords.txt"

fin = open(coordfilename,'r')
coords_raw = fin.readlines()

coords_bits = [i.split(",") for i in coords_raw]

coords = []

for slab in coords_bits:
new_coord = []
for i in slab:
new_coord.append(int(re.sub(r'[^-\d]','', i)))
coords.append(new_coord)

#set up the pixels (AKA 'LEDs')
PIXEL_COUNT = len(coords) # this should be 500

pixels = neopixel.NeoPixel(board.D18, PIXEL_COUNT, auto_write=False)

# YOU CAN EDIT FROM HERE DOWN
np.random.seed(int(time.time()))

def length(v):
return np.sqrt(np.sum(np.square(v)))

def normalize(v):
return np.divide(v, length(v))

def clamp(amin, amax, v):
return np.maximum(amin, np.minimum(amax, v))

def randomDirVec(components = 3):
return normalize([2.0*np.random.random() - 1.0 for x in range(components)])

def randomColorVec(components = 3):
return normalize([np.random.random() for x in range(components)])

# maximum brightness, to avoid catching trees on fire
BRIGHTNESS = 192
# brightness falloff (over distance to the plane, square)
FALLOFF = 1000.0
# how far to move each plane, +/-
RADIUS = 225
# how far to move each plane, +/-
PLANE_DISTANCE = 125
# how many "frames" to wait before adding a new plane
PLANE_TIMER = 128
# controls how fast planes move, "frames" / divider (in radians)
TIME_DIVIDER = 16.0

INNER_COLOR = normalize([0.33, 0, 1.0])
OUTER_COLOR = normalize([0.4, 0.75, 0.1])

# reusing run as "frame" counter here
run = 1
slow = 0
while run:
time.sleep(slow)
run += 1

LED = 0
while LED < len(coords):
dist = length(coords[LED]) - RADIUS + PLANE_DISTANCE*math.sin(run / TIME_DIVIDER)
falloff = FALLOFF / max(1.0, dist ** 2.0)

if dist >= 0:
pixColor = np.multiply(OUTER_COLOR, falloff)
else:
pixColor = np.multiply(INNER_COLOR, falloff)

linearColor = clamp(0.0, 1.0, pixColor)
gammaCorrected = np.power(linearColor, 1.0 / 2.2)
pixels[LED] = list(gammaCorrected * BRIGHTNESS)
LED += 1

# 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()

return 'DONE'


# yes, I just put this at the bottom so it auto runs
xmaslight()