terom@75: import pvl.web terom@75: from pvl.web import urls, html terom@75: terom@75: import logging; log = logging.getLogger('qmsk.dmx.web') terom@75: terom@75: class Handler (pvl.web.Handler) : terom@75: # Bootstrap terom@75: DOCTYPE = 'html' terom@75: HTML_XMLNS = None terom@75: HTML_LANG = 'en' terom@75: CSS = ( terom@75: '//code.jquery.com/ui/1.10.4/themes/ui-darkness/jquery-ui.css', terom@75: '//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css', terom@75: ) terom@75: JS = ( terom@75: '//code.jquery.com/jquery-2.1.0.js', terom@75: '//code.jquery.com/ui/1.10.4/jquery-ui.js', terom@75: '//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js', terom@80: terom@80: '/static/color-slider.js', terom@75: ) terom@75: terom@75: STYLE = """ terom@75: body { terom@75: padding-top: 2em; terom@75: text-align: center; terom@75: } terom@75: terom@75: .container { terom@75: padding: 2em 1em; terom@75: text-align: left; terom@75: } terom@75: terom@75: .panel { terom@75: width: 30em; terom@75: margin: 1em auto; terom@80: } terom@80: terom@80: input.color-control { terom@80: width: 5em; terom@80: } terom@80: terom@80: div#color { terom@80: width: 5em; terom@80: height: 5em; terom@80: terom@80: margin: 1em auto; terom@80: } terom@80: terom@80: div.color-slider { terom@80: margin: 1em; terom@80: } terom@80: terom@80: div.color-slider#slider-r .ui-slider-range { terom@80: background: #ff0000; terom@80: } terom@80: terom@80: div.color-slider#slider-g .ui-slider-range { terom@80: background: #00ff00; terom@80: } terom@80: terom@80: div.color-slider#slider-b .ui-slider-range { terom@80: background: #0000ff; terom@80: } terom@75: """ terom@75: terom@75: # test terom@80: TITLE = u"DMX Control" terom@75: terom@75: def process (self) : terom@76: if self.request.method == 'POST' : terom@76: self.color = tuple((int(x, 16) if x else 0) for x in ( terom@76: self.request.form.get('r'), terom@76: self.request.form.get('g'), terom@76: self.request.form.get('b'), terom@76: )) terom@75: terom@75: r, g, b = self.color terom@75: terom@75: self.app.dmx_color(r, g, b, 255) terom@75: terom@76: else : terom@76: self.color = None terom@76: terom@75: log.info("%s", self.color) terom@75: terom@75: def render (self) : terom@76: if self.color : terom@76: r, g, b = self.color terom@76: else : terom@76: r = g = b = None terom@75: terom@80: def color_swatch () : terom@80: return html.div(id='color', terom@80: style='background-color: rgb({r}, {g}, {b})'.format(r=r, g=g, b=b) terom@80: )(' '), terom@80: terom@75: def color_input (name, value) : terom@75: color = dict(r=0, g=0, b=0) terom@75: bgcolor = dict(r=0, g=0, b=0) terom@75: terom@75: if value : terom@75: color[name] = value terom@80: alpha = value / 255.0 terom@76: else : terom@76: alpha = 0 terom@76: terom@75: bgcolor[name] = 255 terom@75: terom@80: return html.input(type='text', name=name, terom@80: class_ = 'form-control color-control', terom@80: placeholder = name, terom@80: id = name, terom@80: terom@76: value = '{v:02x}'.format(v=value) if value else None, terom@80: style = 'background-color: rgba({r}, {g}, {b}, {a:0.2f})'.format(a=alpha, **bgcolor), terom@75: ) terom@75: terom@75: return html.div(class_='container')( terom@75: html.div(class_='panel')( terom@80: color_swatch(), terom@80: html.div(id='slider-r', class_='color-slider')(' '), terom@80: html.div(id='slider-g', class_='color-slider')(' '), terom@80: html.div(id='slider-b', class_='color-slider')(' '), terom@80: html.form(action='.', method='POST', class_='form-inline')( terom@80: #html.label(for_='color', class_='control-label')("Color"), terom@75: html.div(class_='form-group')( terom@75: color_input('r', r), terom@80: ), terom@80: html.div(class_='form-group')( terom@75: color_input('g', g), terom@80: ), terom@80: html.div(class_='form-group')( terom@75: color_input('b', b), terom@75: ), terom@75: html.div(class_='form-group')( terom@75: html.button(type='submit', class_='btn btn-primary')("Go"), terom@75: ), terom@75: ) terom@75: ) terom@75: ) terom@75: terom@75: class DMXWebApplication (pvl.web.Application) : terom@75: URLS = urls.Map(( terom@75: urls.rule('/', Handler), terom@75: )) terom@75: terom@75: def __init__ (self, dmx, **opts) : terom@75: super(DMXWebApplication, self).__init__(**opts) terom@75: terom@75: self.dmx = dmx terom@75: terom@75: def dmx_color (self, r, g, b, a=255) : terom@82: # Stairville LED Par56 terom@82: self.dmx[1] = (0, r, g, b, 0) terom@75: terom@82: # 4ch dimmer terom@82: self.dmx[5] = (a, a, a, a) terom@82: terom@82: # American DJ - Mega Tri 60 - Mode 2 terom@82: self.dmx[10] = (r, g, b, 0, a)