Compare commits

..

No commits in common. "fef5385da77cf21e9d272cb345cbcf50f1389c07" and "9973abad787d5113f209e8714988b3fae83e8588" have entirely different histories.

View File

@ -10,7 +10,7 @@ import phue, pygame, colorsys
viewsize = 600
shrink = 0.9
vertices = [(viewsize*(1 + shrink*x)/2, viewsize*(1 + shrink*y)/2) for x, y in
[(0, 0)] + [(sin(n*pi/5), -cos(n*pi/5)) for n in range(10)] + [(0, 0)]
[(0, 0)] + [(cos(-n*pi/5), sin(-n*pi/5)) for n in range(10)] + [(0, 0)]
]
# edges as vertex pairs. FireStar will rest on vertices 6 (A), 5 (B), 7 (C)
@ -55,8 +55,8 @@ clock = pygame.time.Clock()
# --- copied from firestar.py
##b = phue.Bridge('172.18.130.12')
b = None
b = phue.Bridge('172.18.130.12')
##b = None
Nedges = 30;
edgecode = [None] * Nedges;
@ -72,10 +72,7 @@ lasthue = 65535
fullbright = 254
fullsat = 254
# --- control methods and diagnostics
updates = 0
new_updates = 0
# --- control methods
def set_light_hsv(e, color, hit_bridge):
command = {
@ -83,86 +80,39 @@ def set_light_hsv(e, color, hit_bridge):
'bri' : int(color[2]*fullbright),
'sat' : int(color[1]*fullsat)
}
if hit_bridge:
if b != None:
if hit_bridge and b != None:
b.set_light(edges[e].light_id, command)
global new_updates
new_updates += 1
rgb_color = tuple(255*c for c in colorsys.hsv_to_rgb(*color))
pygame.draw.aaline(screen, rgb_color, vertices[vertex_adj[e][0]], vertices[vertex_adj[e][1]])
# === messages and mode selection ===
mode = 0
# data labels
updates_label = font.render('updates', True, (255, 255, 255))
energy_label = font.render('energy', True, (255, 255, 255))
position_label = [
font.render('temp[0]', True, (255, 255, 255)),
font.render('charge[0]', True, (255, 255, 255))
]
# instructions
mode_inds = [
[
font.render('[d] heat', True, (255, 255, 255)),
font.render('[f] wave', True, (150, 150, 150))
],
[
font.render('[d] heat', True, (150, 150, 150)),
font.render('[f] wave', True, (255, 255, 255))
]
]
bop_instr = [
font.render('press [j] to bop', True, (255, 255, 255)),
font.render('press [j] or [k] to bop', True, (255, 255, 255))
]
drain_instr = font.render('hold [space] to drain', True, (255, 255, 255))
# set up background
background = pygame.Surface(screen.get_size()).convert()
background.fill((32, 32, 32))
background.blit(drain_instr, (340, 60 + viewsize))
# === interaction parameters ===
# these frame rate and threshold settings are untested, but the exponential
# moving average update frequency suggests they should be just barely in bounds
frame_rate = 10
ema_weight = 2/(frame_rate+1)
frame_rate = 2
inductance = 0.001
therm_conduct = 0.01
therm_conduct = 0.001
drain = False
drain_rate = 0.1
# === phase space ===
# heat equation
temp = 30*[0]
new_temp = 30*[0]
# wave equation
charge = 12*[0]
current = 30*[0]
litness = 30*[float("inf")]
change_threshold = 0.15
last_litness = 30*[float("inf")]
change_threshold = 0.05
# === main loop ===
def heat_evolution():
for e, f in cyc_edge_adj:
flow = therm_conduct*(temp[e] - temp[f])
new_temp[e] -= flow
new_temp[f] += flow
for e in range(30):
flow = therm_conduct*(current[e] - current[f])
current[e] -= flow
current[f] += flow
if drain:
new_temp[e] *= 1 - drain_rate
temp[e] = max(new_temp[e], 0)
current[v] *= 1 - drain_rate
def wave_evolution():
# use verlet integration, first updating the currents and then using the new
@ -178,42 +128,36 @@ def wave_evolution():
for v in range(12):
charge[v] *= 1 - drain_rate
def set_heat_light(e, i):
new_litness = 1 - exp(-2*i)
if abs(new_litness - litness[e]) > change_threshold:
hit_bridge = True
litness[e] = new_litness
else:
hit_bridge = False
set_light_hsv(e, (0.167*litness[e], 1 - 0.5*litness[e]*litness[e], litness[e]), hit_bridge)
def set_wave_light(e, i):
new_litness = 1 - exp(-2*i*i)
if abs(new_litness - litness[e]) > change_threshold:
hit_bridge = True
litness[e] = new_litness
else:
hit_bridge = False
set_light_hsv(e, (0.45 + litness[e]*0.25, 1 - 0.5*litness[e]*litness[e], litness[e]), hit_bridge)
litness = 1 - exp(-2*i*i)
hit_bridge = abs(litness - last_litness[e]) > change_threshold
set_light_hsv(e, (0.45 + litness*0.25, 1 - 0.5*litness*litness, litness), hit_bridge)
if hit_bridge:
last_litness[e] = litness
def handle_heat_events():
global drain, mode
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_j:
new_temp[0] += 10
elif event.key == pygame.K_SPACE:
drain = True
elif event.key == pygame.K_f:
mode = 1
elif event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
drain = False
elif event.type == pygame.QUIT:
quit()
def show_data(screen):
energy = 0.5*(sum(q*q for q in charge) + inductance*sum(i*i for i in current))
text = font.render('energy', True, (255, 255, 255))
screen.blit(text, (20, viewsize))
text = font.render(str(energy), True, (255, 255, 255))
screen.blit(text, (120, viewsize))
text = font.render('charge[0]', True, (255, 255, 255))
screen.blit(text, (20, 30 + viewsize))
text = font.render(str(charge[0]), True, (255, 255, 255))
screen.blit(text, (120, 30 + viewsize))
def handle_wave_events():
global drain, mode
if __name__ == '__main__':
# set up background
background = pygame.Surface(screen.get_size()).convert()
background.fill((32, 32, 32))
text = font.render('press [j] or [k] to pluck', True, (255, 255, 255))
background.blit(text, (340, viewsize))
text = font.render('hold [space] to drain', True, (255, 255, 255))
background.blit(text, (340, 30 + viewsize))
to_light = 0
while True:
# handle events
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_j:
@ -224,69 +168,26 @@ def handle_wave_events():
charge[7] -= 100
elif event.key == pygame.K_SPACE:
drain = True
elif event.key == pygame.K_d:
mode = 0
pygame.draw.aaline(screen, (255, 255, 255), (0, 0), (viewsize, viewsize))
elif event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
drain = False
elif event.type == pygame.QUIT:
quit()
def show_data(screen):
# light updates
global updates, new_updates
updates = ema_weight*new_updates + (1 - ema_weight)*updates
new_updates = 0
updates_value = font.render(str(frame_rate * updates), True, (255, 255, 255))
screen.blit(updates_label, (20, viewsize))
screen.blit(updates_value, (120, viewsize))
# energy
if mode == 0:
energy = sum(temp)
elif mode == 1:
energy = 0.5*(sum(q*q for q in charge) + inductance*sum(i*i for i in current))
energy_value = font.render(str(energy), True, (255, 255, 255))
screen.blit(energy_label, (20, 30 + viewsize))
screen.blit(energy_value, (120, 30 + viewsize))
# a position
##position_value = None
if mode == 0:
position_value = font.render(str(temp[0]), True, (255, 255, 255))
elif mode == 1:
position_value = font.render(str(charge[0]), True, (255, 255, 255))
screen.blit(position_label[mode], (20, 60 + viewsize))
screen.blit(position_value, (120, 60 + viewsize))
# instructions
screen.blit(mode_inds[mode][0], (340, viewsize))
screen.blit(mode_inds[mode][1], (420, viewsize))
screen.blit(bop_instr[mode], (340, 30 + viewsize))
if __name__ == '__main__':
while True:
# handle events
if mode == 0:
handle_heat_events()
elif mode == 1:
handle_wave_events()
# clear screen
screen.blit(background, (0, 0))
# show data and instructions
# show data
show_data(screen)
# show state and evolve state
if mode == 0:
for e in range(30):
set_heat_light(e, temp[e])
heat_evolution()
elif mode == 1:
# show state
for e in range(30):
set_wave_light(e, current[e])
# evolve state
wave_evolution()
##heat_evolution()
# step
pygame.display.flip()