Advertisement
Guest User

Untitled

a guest
Feb 18th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. import time
  2. import math
  3. import json
  4. import socket
  5.  
  6. class Lights(object):
  7.     def __init__(self, host, port, lights_base, channels):
  8.         self.host = host
  9.         self.port = port
  10.         self.lights_base = lights_base
  11.         self.channels = channels
  12.  
  13.     def _send_cmd(self, cmd, params):
  14.         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
  15.         sock.connect((self.host, self.port))
  16.         fp = sock.makefile()
  17.         json.dump({"jsonrpc":"2.0",
  18.                 "id":1,
  19.                 "method": cmd,
  20.                 "params": params}, fp)
  21.         fp.write("\n")
  22.         fp.flush()
  23.         resp = json.loads(sock.recv(65535))
  24.         fp.close()
  25.         return resp
  26.  
  27.     def _channel_dict(self, channels):
  28.         return dict([(l,v) for v,l in zip(channels, xrange(self.lights_base, 3 * (self.lights_base + self.channels)))])
  29.  
  30.     def fade_to(self, channels):
  31.         return self._send_cmd('fadeTo', [self._channel_dict(channels)])
  32.  
  33.     def set_channels(self, channels):
  34.         return self._send_cmd('setChannels', [self._channel_dict(channels)])
  35.  
  36.     def list(self):
  37.         return self._send_cmd('list', [])
  38.  
  39.     def vars(self, method):
  40.         return self._send_cmd('vars', [method])
  41.  
  42. if __name__ == '__main__':
  43.     lights = Lights('vinculum', 4444, 32, 6)
  44.     #print lights.set_channels((6 * (255, 0, 0)))
  45.     #print lights.list()
  46.     #print lights.vars('fadeTo')
  47.  
  48.     def f(t):
  49.         r = int(127 * (math.sin(2*t) + math.cos(0.5*t)**2))+64
  50.         g = int(127 * (math.sin(2.4*t) + math.cos(1*t)**3))+64
  51.         b = int(127 * (math.sin(3.2*t) + math.cos(2.7*t)**2))+64
  52.         #g = int(255 * math.cos(5*t)**3)
  53.         #b = int(255 * math.sin(3*t)**2)
  54.         return (r, g, b)
  55.  
  56.     step_size = 0.01
  57.     t = 0
  58.     while True:
  59.         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))
  60.         time.sleep(0.01)
  61.         t += step_size
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement