|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import socket |
| 3 | +import numpy as np |
| 4 | +import time |
| 5 | +import math |
| 6 | + |
| 7 | +# ============================== |
| 8 | +# Config |
| 9 | +# ============================== |
| 10 | +WIDTH, HEIGHT = 16, 16 # Display size |
| 11 | +SIM_SCALE = 8 # Simulation resolution multiplier |
| 12 | +SIM_W, SIM_H = WIDTH*SIM_SCALE, HEIGHT*SIM_SCALE |
| 13 | + |
| 14 | +NUM_BALLS = 4 |
| 15 | +RADIUS = 3.0 * SIM_SCALE # Radius in simulation pixels |
| 16 | +SPEED = 0.6 * SIM_SCALE # Speed in simulation pixels/frame |
| 17 | +GAMMA = 1.7 |
| 18 | +CAP_VALUE = 3.0 # Max field value before tone-mapping |
| 19 | + |
| 20 | +IP = "192.168.1.112" |
| 21 | +PORT = 4048 |
| 22 | +FPS = 40 |
| 23 | + |
| 24 | +# ============================== |
| 25 | +# DDP sender |
| 26 | +# ============================== |
| 27 | +def create_packet(pixels=None): |
| 28 | + packet = bytearray([ |
| 29 | + 0x41, # Version 1 |
| 30 | + 0x00, # Flags |
| 31 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 32 | + ]) |
| 33 | + data = bytearray([0] * (WIDTH * HEIGHT * 3)) |
| 34 | + if pixels: |
| 35 | + for x, y, brightness in pixels: |
| 36 | + if 0 <= x < WIDTH and 0 <= y < HEIGHT and 0 <= brightness <= 255: |
| 37 | + idx = (y * WIDTH + x) * 3 |
| 38 | + data[idx:idx + 3] = [brightness] * 3 |
| 39 | + packet.extend(data) |
| 40 | + return packet |
| 41 | + |
| 42 | +def send_ddp_packet(ip, port, packet): |
| 43 | + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 44 | + try: |
| 45 | + sock.sendto(packet, (ip, port)) |
| 46 | + finally: |
| 47 | + sock.close() |
| 48 | + |
| 49 | +# ============================== |
| 50 | +# Metaball simulation |
| 51 | +# ============================== |
| 52 | +balls = [] |
| 53 | +for _ in range(NUM_BALLS): |
| 54 | + x = np.random.uniform(0, SIM_W) |
| 55 | + y = np.random.uniform(0, SIM_H) |
| 56 | + vx = np.random.uniform(-SPEED, SPEED) |
| 57 | + vy = np.random.uniform(-SPEED, SPEED) |
| 58 | + balls.append([x, y, vx, vy]) |
| 59 | + |
| 60 | +def attenuation_fn(d, radius=RADIUS): |
| 61 | + if d > radius: |
| 62 | + return 0.0 |
| 63 | + return (1 - (d / radius)**2)**2 |
| 64 | + |
| 65 | +def tone_map(v, gamma=GAMMA): |
| 66 | + v_capped = min(v, CAP_VALUE) |
| 67 | + n = v_capped / CAP_VALUE |
| 68 | + return int(pow(n, 1 / gamma) * 255) |
| 69 | + |
| 70 | +def update_positions(): |
| 71 | + for b in balls: |
| 72 | + b[0] += b[2] |
| 73 | + b[1] += b[3] |
| 74 | + if b[0] < 0 or b[0] >= SIM_W: |
| 75 | + b[2] *= -1 |
| 76 | + if b[1] < 0 or b[1] >= SIM_H: |
| 77 | + b[3] *= -1 |
| 78 | + |
| 79 | +def render_highres(): |
| 80 | + grid = np.zeros((SIM_H, SIM_W), dtype=float) |
| 81 | + for y in range(SIM_H): |
| 82 | + for x in range(SIM_W): |
| 83 | + val = 0.0 |
| 84 | + for bx, by, _, _ in balls: |
| 85 | + d = math.dist((x, y), (bx, by)) |
| 86 | + val += attenuation_fn(d) |
| 87 | + grid[y, x] = val |
| 88 | + return grid |
| 89 | + |
| 90 | +def downsample(grid): |
| 91 | + """Average SIM_SCALE×SIM_SCALE blocks into one pixel""" |
| 92 | + pixels = [] |
| 93 | + for y in range(HEIGHT): |
| 94 | + for x in range(WIDTH): |
| 95 | + block = grid[ |
| 96 | + y*SIM_SCALE:(y+1)*SIM_SCALE, |
| 97 | + x*SIM_SCALE:(x+1)*SIM_SCALE |
| 98 | + ] |
| 99 | + avg_val = block.mean() |
| 100 | + brightness = tone_map(avg_val) |
| 101 | + pixels.append((x, y, brightness)) |
| 102 | + return pixels |
| 103 | + |
| 104 | +# ============================== |
| 105 | +# Main loop |
| 106 | +# ============================== |
| 107 | +def main(): |
| 108 | + try: |
| 109 | + while True: |
| 110 | + update_positions() |
| 111 | + highres = render_highres() |
| 112 | + pixels = downsample(highres) |
| 113 | + packet = create_packet(pixels) |
| 114 | + send_ddp_packet(IP, PORT, packet) |
| 115 | + time.sleep(1 / FPS) |
| 116 | + except KeyboardInterrupt: |
| 117 | + pass |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments