diff --git a/interactions.py b/interactions.py new file mode 100644 index 0000000..4331426 --- /dev/null +++ b/interactions.py @@ -0,0 +1,194 @@ +#!/usr/bin/python3 + +from math import pi, cos, sin, exp + +import phue, pygame, colorsys + +# === graph data === + +# vertices as screen positions +viewsize = 600 +shrink = 0.9 +vertices = [(viewsize*(1 + shrink*x)/2, viewsize*(1 + shrink*y)/2) for x, y in + [(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) +vertex_adj = [ + (0, 1), (0, 3), (0, 5), (0, 7), (0, 9), + (2, 8), (2, 6), (6, 10), (10, 4), (4, 8), + (2, 9), (2, 11), (2, 5), + (4, 1), (4, 9), (4, 7), + (6, 3), (6, 11), (6, 9), + (8, 5), (8, 11), (8, 1), + (10, 7), (10, 11), (10, 3), + (9, 3), (9, 5), + (1, 5), (1, 7), + (3, 7) +] + +# cyclic edge adjacencies +cyc_edge_adj = [ + (0, 1), (1, 2), (2, 3), (3, 4), (4, 0), + (5, 10), (10, 11), (11, 12), (12, 6), (6, 5), + (6, 16), (16, 17), (17, 18), (18, 7), (7, 6), + (7, 22), (22, 23), (23, 24), (24, 8), (8, 7), + (8, 13), (13, 14), (14, 15), (15, 9), (9, 8), + (5, 9), (9, 19), (19, 20), (20, 21), (21, 5), + (4, 10), (10, 25), (25, 26), (26, 18), (18, 4), + (3, 15), (15, 29), (29, 28), (28, 22), (22, 3), + (2, 12), (12, 27), (27, 26), (26, 19), (19, 2), + (1, 24), (24, 25), (25, 29), (29, 16), (16, 1), + (0, 21), (21, 28), (28, 27), (27, 13), (13, 0), + (14, 11), (11, 23), (23, 20), (20, 17), (17, 14) +] + +# === pygame setup === + +pygame.init() +screen = pygame.display.set_mode((viewsize, viewsize + 100)) +pygame.display.set_caption('Preview') +font = pygame.font.Font(None, 25) +clock = pygame.time.Clock() + +# === bridge setup and control === + +# --- copied from firestar.py + +b = phue.Bridge('172.18.130.12') +##b = None +Nedges = 30; +edgecode = [None] * Nedges; + +# The edges are numbered as in info/SSDedges.obj; enter the two-letter code for +# the lightstrip inserted through each edge in the block below: + +exec(open("edgecodes.py").read()) + +if b != None: + edges = [ b[code] for code in edgecode ] + +lasthue = 65535 +fullbright = 254 +fullsat = 254 + +# --- control methods + +def set_light_hsv(e, color, hit_bridge): + command = { + 'hue' : int(color[0]*lasthue), + 'bri' : int(color[2]*fullbright), + 'sat' : int(color[1]*fullsat) + } + if hit_bridge and b != None: + b.set_light(edges[e].light_id, command) + 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]]) + +# === interaction parameters === + +frame_rate = 2 + +inductance = 0.001 + +therm_conduct = 0.001 + +drain = False +drain_rate = 0.1 + +# === phase space === + +charge = 12*[0] +current = 30*[0] + +last_litness = 30*[float("inf")] +change_threshold = 0.05 + +# === main loop === + +def heat_evolution(): + for e, f in cyc_edge_adj: + flow = therm_conduct*(current[e] - current[f]) + current[e] -= flow + current[f] += flow + if drain: + current[v] *= 1 - drain_rate + +def wave_evolution(): + # use verlet integration, first updating the currents and then using the new + # currents to update the charges. + for e in range(30): + u, v = vertex_adj[e] + current[e] += inductance*(charge[u] - charge[v]) + for e in range(30): + u, v = vertex_adj[e] + charge[u] -= current[e] + charge[v] += current[e] + if drain: + for v in range(12): + charge[v] *= 1 - drain_rate + +def set_wave_light(e, i): + 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 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)) + +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: + charge[0] += 100 + charge[11] -= 100 + elif event.key == pygame.K_k: + charge[5] += 100 + charge[7] -= 100 + elif event.key == pygame.K_SPACE: + drain = True + 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() + + # clear screen + screen.blit(background, (0, 0)) + + # show data + show_data(screen) + + # show state + for e in range(30): + set_wave_light(e, current[e]) + + # evolve state + wave_evolution() + ##heat_evolution() + + # step + pygame.display.flip() + clock.tick(frame_rate)