Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import math
- import json
- import socket
- class Lights(object):
- def __init__(self, host, port, lights_base, channels):
- self.host = host
- self.port = port
- self.lights_base = lights_base
- self.channels = channels
- def _send_cmd(self, cmd, params):
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
- sock.connect((self.host, self.port))
- fp = sock.makefile()
- json.dump({"jsonrpc":"2.0",
- "id":1,
- "method": cmd,
- "params": params}, fp)
- fp.write("\n")
- fp.flush()
- resp = json.loads(sock.recv(65535))
- fp.close()
- return resp
- def _channel_dict(self, channels):
- return dict([(l,v) for v,l in zip(channels, xrange(self.lights_base, 3 * (self.lights_base + self.channels)))])
- def fade_to(self, channels):
- return self._send_cmd('fadeTo', [self._channel_dict(channels)])
- def set_channels(self, channels):
- return self._send_cmd('setChannels', [self._channel_dict(channels)])
- def list(self):
- return self._send_cmd('list', [])
- def vars(self, method):
- return self._send_cmd('vars', [method])
- if __name__ == '__main__':
- lights = Lights('vinculum', 4444, 32, 6)
- #print lights.set_channels((6 * (255, 0, 0)))
- #print lights.list()
- #print lights.vars('fadeTo')
- def f(t):
- r = int(127 * (math.sin(2*t) + math.cos(0.5*t)**2))+64
- g = int(127 * (math.sin(2.4*t) + math.cos(1*t)**3))+64
- b = int(127 * (math.sin(3.2*t) + math.cos(2.7*t)**2))+64
- #g = int(255 * math.cos(5*t)**3)
- #b = int(255 * math.sin(3*t)**2)
- return (r, g, b)
- step_size = 0.01
- t = 0
- while True:
- lights.set_channels(f(t) + f(t+25*step_size) + f(t+50*step_size) + f(t+75*step_size) + f(t+125*step_size) + f(t+100*step_size))
- time.sleep(0.01)
- t += step_size
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement