Fixed the trafotron
[cerebrum.git] / tools / mainhall-rgb.py
bloba2db8d3718f0dea39bab7f0a749d8e8a288a7f3f
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
4 import time
5 import copy
6 import json
7 import requests
8 import os
9 from pylibcerebrum.serial_mux import SerialMux
11 BASE_URI = 'http://10.0.1.43/dmxacl/json/'
12 CBEAM = 'http://c-beam.cbrp3.c-base.org/rpc/'
13 PORT = '/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A700fmkX-if00-port0'
14 BAUDRATE = 57600
15 GAMMA = 2.5
16 CACERT = os.path.join(os.path.dirname(__file__), 'cacert.pem')
18 s = SerialMux(PORT, BAUDRATE)
19 print('discovering cerebrum devices')
20 results = []
21 while not results:
22 results = s.discover()
23 print(results)
24 print('opening first device')
25 g = s.open(0)
26 print('initializing device')
27 print(dir(g))
28 g.schnurlinks.state = 1
29 g.schnurmitte.state = 1
30 g.schnurrechts.state = 1
31 print('starting event loop')
33 def xform(o, i):
34 c = (o/255)**(1/GAMMA) #inverse gamma correction
35 c = round(c*4)
36 c = (c+i)%5
37 c = c/4
38 return round((c**GAMMA)*255)
40 def inc_color(r,g,b):
41 state = requests.post(BASE_URI, data='{"method": "lightSync.pull", "params": [], "id": 0}').json()['result']
42 for v in state:
43 v['red'] = xform(v['red'], r)
44 v['green'] = xform(v['green'], g)
45 v['blue'] = xform(v['blue'], b)
46 try:
47 requests.post(BASE_URI, data=json.dumps({'method': 'lightSync.push', 'params': [state], 'id': 0}))
48 except requests.HTTPError as e:
49 print('Cannot set DMX color:', e)
51 def sendstate(schnur, state):
52 try:
53 requests.post(CBEAM, data=json.dumps({'method': 'barschnur', 'params': [schnur, state], 'id': 0}), verify=CACERT)
54 except requests.HTTPError as e:
55 print('Cannot send event to c-beam:', e)
57 oldstate = None
58 sr, sg, sb = False, False, False
59 while 1:
60 if g.schnurlinks.state:
61 if sr:
62 sendstate('links', False)
63 inc_color(1,0,0)
64 sr = False
65 else:
66 if not sr:
67 sendstate('links', True)
68 sr = True
69 if g.schnurmitte.state:
70 if sg:
71 sendstate('mitte', False)
72 inc_color(0,1,0)
73 sg = False
74 else:
75 if not sg:
76 sendstate('mitte', True)
77 sg = True
78 if g.schnurrechts.state:
79 if sb:
80 sendstate('rechts', False)
81 inc_color(0,0,1)
82 sb = False
83 else:
84 if not sb:
85 sendstate('rechts', True)
86 sb = True
87 time.sleep(0.1)