print ' sending pixels forever (control-c to exit)...' print n_pixels = 1250 # number of pixels in the included "wall" layout fps = 20 # frames per second # how many sine wave cycles are squeezed into our n_pixels # 24 happens to create nice diagonal stripes on the wall layout freq_r = 24 freq_g = 24 freq_b = 24 # how many seconds the sine waves take to shift through a complete cycle speed_r = 7 speed_g = -13 speed_b = 19 start_time = time.time() while True: t = time.time() - start_time pixels = [] for ii in range(n_pixels): pct = ii / n_pixels r = opc_client.remap(math.cos((t/speed_r + pct*freq_r)*math.pi*2), -1, 1, 0, 256) g = opc_client.remap(math.cos((t/speed_g + pct*freq_g)*math.pi*2), -1, 1, 0, 256) b = opc_client.remap(math.cos((t/speed_b + pct*freq_b)*math.pi*2), -1, 1, 0, 256) pixels.append((r, g, b)) opc_client.put_pixels(SOCK, 0, pixels) time.sleep(1 / fps)
def pixel_color(t, coord, ii, n_pixels, random_values): """Compute the color of a given pixel. t: time in seconds since the program started. ii: which pixel this is, starting at 0 coord: the (x, y, z) position of the pixel as a tuple n_pixels: the total number of pixels random_values: a list containing a constant random value for each pixel Returns an (r, g, b) tuple in the range 0-255 """ # make moving stripes for x, y, and z x, y, z = coord y += opc_client.cos(x + 0.2 * z, offset=0, period=1, minn=0, maxx=0.6) z += opc_client.cos(x, offset=0, period=1, minn=0, maxx=0.3) x += opc_client.cos(y + z, offset=0, period=1.5, minn=0, maxx=0.2) # rotate x, y, z = y, z, x # shift some of the pixels to a new xyz location if ii % 7 == 0: x += ((ii * 123) % 5) / n_pixels * 32.12 y += ((ii * 137) % 5) / n_pixels * 22.23 z += ((ii * 147) % 7) / n_pixels * 44.34 # make x, y, z -> r, g, b sine waves r = opc_client.cos(x, offset=t / 4, period=2, minn=0, maxx=1) g = opc_client.cos(y, offset=t / 4, period=2, minn=0, maxx=1) b = opc_client.cos(z, offset=t / 4, period=2, minn=0, maxx=1) r, g, b = opc_client.contrast((r, g, b), 0.5, 1.5) # a moving wave across the pixels, usually dark. # lines up with the wave of twinkles fade = opc_client.cos(t - ii / n_pixels, offset=0, period=7, minn=0, maxx=1) ** 20 r *= fade g *= fade b *= fade # # stretched vertical smears # v = opc_client.cos(ii / n_pixels, offset=t*0.1, period = 0.07, minn=0, maxx=1) ** 5 * 0.3 # r += v # g += v # b += v # twinkle occasional LEDs twinkle_speed = 0.07 twinkle_density = 0.1 twinkle = (random_values[ii] * 7 + time.time() * twinkle_speed) % 1 twinkle = abs(twinkle * 2 - 1) twinkle = opc_client.remap(twinkle, 0, 1, -1 / twinkle_density, 1.1) twinkle = opc_client.clamp(twinkle, -0.5, 1.1) twinkle **= 5 twinkle *= opc_client.cos(t - ii / n_pixels, offset=0, period=7, minn=0, maxx=1) ** 20 twinkle = opc_client.clamp(twinkle, -0.3, 1) r += twinkle g += twinkle b += twinkle # apply gamma curve # only do this on live leds, not in the simulator # r, g, b = opc_client.gamma((r, g, b), 2.2) return (r * 256, g * 256, b * 256)
print n_pixels = 255 # TODO: command line this snake_length = 20 snake_head = 0 fps = 2 for tick in itertools.count(): pixels = [] for index in range(n_pixels): snake_pos = snake_head - index hue = 0 saturation = 0 value = 0 if snake_pos >= 0 and snake_pos < snake_length: hue = (5.0/6.0) * snake_pos / snake_length saturation = 1 value = 1 r, g, b = map(lambda x:opc_client.remap(x,0.0,1.0,0,255), colorsys.hsv_to_rgb(hue, saturation, value)) pixels.append((r, g, b)) opc_client.put_pixels(SOCK, 0, pixels) time.sleep(1 / fps) snake_head += 1 if snake_head + snake_length > n_pixels: snake_head = 0
def pixel_color(t, coord, ii, n_pixels, random_values): """Compute the color of a given pixel. t: time in seconds since the program started. ii: which pixel this is, starting at 0 coord: the (x, y, z) position of the pixel as a tuple n_pixels: the total number of pixels random_values: a list containing a constant random value for each pixel Returns an (r, g, b) tuple in the range 0-255 """ # make moving stripes for x, y, and z x, y, z = coord y += opc_client.cos(x + 0.2*z, offset=0, period=1, minn=0, maxx=0.6) z += opc_client.cos(x, offset=0, period=1, minn=0, maxx=0.3) x += opc_client.cos(y + z, offset=0, period=1.5, minn=0, maxx=0.2) # rotate x, y, z = y, z, x # # shift some of the pixels to a new xyz location # if ii % 17 == 0: # x += ((ii*123)%5) / n_pixels * 32.12 + 0.1 # y += ((ii*137)%5) / n_pixels * 22.23 + 0.1 # z += ((ii*147)%7) / n_pixels * 44.34 + 0.1 # make x, y, z -> r, g, b sine waves r = opc_client.cos(x, offset=t / 4, period=2.5, minn=0, maxx=1) g = opc_client.cos(y, offset=t / 4, period=2.5, minn=0, maxx=1) b = opc_client.cos(z, offset=t / 4, period=2.5, minn=0, maxx=1) r, g, b = opc_client.contrast((r, g, b), 0.5, 1.4) clampdown = (r + g + b)/2 clampdown = opc_client.remap(clampdown, 0.4, 0.5, 0, 1) clampdown = opc_client.clamp(clampdown, 0, 1) clampdown *= 0.9 r *= clampdown g *= clampdown b *= clampdown # # shift the color of a few outliers # if random_values[ii] < 0.03: # r, g, b = b, g, r # black out regions r2 = opc_client.cos(x, offset=t / 10 + 12.345, period=4, minn=0, maxx=1) g2 = opc_client.cos(y, offset=t / 10 + 24.536, period=4, minn=0, maxx=1) b2 = opc_client.cos(z, offset=t / 10 + 34.675, period=4, minn=0, maxx=1) clampdown = (r2 + g2 + b2)/2 clampdown = opc_client.remap(clampdown, 0.2, 0.3, 0, 1) clampdown = opc_client.clamp(clampdown, 0, 1) r *= clampdown g *= clampdown b *= clampdown # color scheme: fade towards blue-and-orange # g = (r+b) / 2 g = g * 0.6 + ((r+b) / 2) * 0.4 # # stretched vertical smears # v = opc_client.cos(ii / n_pixels, offset=t*0.1, period = 0.07, minn=0, maxx=1) ** 5 * 0.3 # r += v # g += v # b += v # fade behind twinkle fade = opc_client.cos(t - ii/n_pixels, offset=0, period=7, minn=0, maxx=1) ** 20 fade = 1 - fade*0.2 r *= fade g *= fade b *= fade # twinkle occasional LEDs twinkle_speed = 0.07 twinkle_density = 0.1 twinkle = (random_values[ii]*7 + time.time()*twinkle_speed) % 1 twinkle = abs(twinkle*2 - 1) twinkle = opc_client.remap(twinkle, 0, 1, -1/twinkle_density, 1.1) twinkle = opc_client.clamp(twinkle, -0.5, 1.1) twinkle **= 5 twinkle *= opc_client.cos(t - ii/n_pixels, offset=0, period=7, minn=0, maxx=1) ** 20 twinkle = opc_client.clamp(twinkle, -0.3, 1) r += twinkle g += twinkle b += twinkle # apply gamma curve # only do this on live leds, not in the simulator #r, g, b = opc_client.gamma((r, g, b), 2.2) return (r*256, g*256, b*256)