Added proper gamma correction and multiple steps to mainhall-dmx
[cerebrum.git] / tools / mainhall-rgb.py
blob728e1479ef69cffa36224f76c4f9bf134f848c4d
1 #!/usr/bin/env python3
3 import time
4 import copy
5 import json
6 import requests
7 from pylibcerebrum.serial_mux import SerialMux
9 BASE_URI ='http://10.0.1.43/dmxacl/json/'
10 PORT = '/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A700fmkX-if00-port0'
11 BAUDRATE = 57600
12 GAMMA = 2.5
14 s = SerialMux(PORT, BAUDRATE)
15 print('discovering cerebrum devices')
16 results = []
17 while not results:
18 results = s.discover()
19 print(results)
20 print('opening first device')
21 g = s.open(0)
22 print('initializing device')
23 print(dir(g))
24 g.schnurlinks.state = 1
25 g.schnurmitte.state = 1
26 g.schnurrechts.state = 1
27 print('starting event loop')
29 def xform(o, i):
30 c = (o/255)**(1/GAMMA) #inverse gamma correction
31 c = round(c*4)
32 c = (c+i)%5
33 c = c/4
34 return round((c**GAMMA)*255)
36 def inc_color(r,g,b):
37 state = requests.post(BASE_URI, data='{"method": "lightSync.pull", "params": [], "id": 0}').json()['result']
38 for v in state:
39 v['red'] = xform(v['red'], r)
40 v['green'] = xform(v['green'], g)
41 v['blue'] = xform(v['blue'], b)
42 requests.post(BASE_URI, data=json.dumps({'method': 'lightSync.push', 'params': [state], 'id': 0}))
44 oldstate = None
45 sr, sg, sb = False, False, False
46 while 1:
47 if g.schnurlinks.state:
48 if sr:
49 inc_color(1,0,0)
50 sr = False
51 else:
52 sr = True
53 if g.schnurmitte.state:
54 if sg:
55 inc_color(0,1,0)
56 sg = False
57 else:
58 sg = True
59 if g.schnurrechts.state:
60 if sb:
61 inc_color(0,0,1)
62 sb = False
63 else:
64 sb = True
65 time.sleep(0.1)