Files
@ 37cbb245d93c
Branch filter:
Location: light9/light9/collector/device.py
37cbb245d93c
7.3 KiB
text/x-python
fix tests. add logging, some mypy types.
Ignore-this: 61371f65438a4e77f70d21cc5d5193bf
Ignore-this: 61371f65438a4e77f70d21cc5d5193bf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | from __future__ import division
import logging
import math
from light9.namespaces import L9, RDF, DEV
from rdflib import Literal
from webcolors import hex_to_rgb, rgb_to_hex
from colormath.color_objects import sRGBColor, CMYColor
import colormath.color_conversions
log = logging.getLogger('device')
class Device(object):
pass
class ChauvetColorStrip(Device):
"""
device attrs:
color
"""
class Mini15(Device):
"""
plan:
device attrs
rx, ry
color
gobo
goboShake
imageAim (configured with a file of calibration data)
"""
def clamp255(x):
return min(255, max(0, x))
def _8bit(f):
if not isinstance(f, (int, float)):
raise TypeError(repr(f))
return clamp255(int(f * 255))
def resolve(deviceType, deviceAttr, values):
"""
return one value to use for this attr, given a set of them that
have come in simultaneously. len(values) >= 1.
"""
if len(values) == 1:
return values[0]
if deviceAttr == L9['color']:
rgbs = [hex_to_rgb(v) for v in values]
return rgb_to_hex([max(*component) for component in zip(*rgbs)])
# incomplete. how-to-resolve should be on the DeviceAttr defs in the graph.
if deviceAttr in [L9['rx'], L9['ry'], L9['zoom'], L9['focus'], L9['iris']]:
floatVals = []
for v in values:
if isinstance(v, Literal):
floatVals.append(float(v.toPython()))
elif isinstance(v, (int, float)):
floatVals.append(float(v))
else:
raise TypeError(repr(v))
return Literal(sum(floatVals) / len(floatVals))
return max(values)
def toOutputAttrs(deviceType, deviceAttrSettings):
"""
Given device attr settings like {L9['color']: Literal('#ff0000')},
return a similar dict where the keys are output attrs (like
L9['red']) and the values are suitable for Collector.setAttr
:outputAttrRange happens before we get here.
"""
def floatAttr(attr, default=0):
out = deviceAttrSettings.get(attr)
if out is None:
return default
return float(out.toPython()) if isinstance(out, Literal) else out
def rgbAttr(attr):
color = deviceAttrSettings.get(attr, '#000000')
r, g, b = hex_to_rgb(color)
return r, g, b
def cmyAttr(attr):
rgb = sRGBColor.new_from_rgb_hex(deviceAttrSettings.get(attr, '#000000'))
out = colormath.color_conversions.convert_color(rgb, CMYColor)
return (
_8bit(out.cmy_c),
_8bit(out.cmy_m),
_8bit(out.cmy_y))
def fine16Attr(attr):
x = floatAttr(attr)
hi = _8bit(x)
lo = _8bit((x * 255) % 1.0)
return hi, lo
if deviceType == L9['ChauvetColorStrip']:
r, g, b = rgbAttr(L9['color'])
return {
L9['mode']: 215,
L9['red']: r,
L9['green']: g,
L9['blue']: b
}
elif deviceType == L9['SimpleDimmer']:
return {L9['level']: _8bit(floatAttr(L9['brightness']))}
elif deviceType == L9['Mini15']:
inp = deviceAttrSettings
rx8 = float(inp.get(L9['rx'], 0)) / 540 * 255
ry8 = float(inp.get(L9['ry'], 0)) / 240 * 255
r, g, b = hex_to_rgb(inp.get(L9['color'], '#000000'))
return {
L9['xRotation']: clamp255(int(math.floor(rx8))),
# didn't find docs on this, but from tests it looks like 64 fine steps takes you to the next coarse step
L9['xFine']: _8bit(1 - (rx8 % 1.0)),
L9['yRotation']: clamp255(int(math.floor(ry8))),
L9['yFine']: _8bit((ry8 % 1.0) / 4),
L9['rotationSpeed']: 0,
L9['dimmer']: 255,
L9['red']: r,
L9['green']: g,
L9['blue']: b,
L9['colorChange']: 0,
L9['colorSpeed']: 0,
L9['goboShake']: 0,
L9['goboChoose']: 0,
}
elif deviceType == L9['ChauvetHex12']:
out = {}
out[L9['red']], out[L9['green']], out[L9['blue']] = r, g, b = rgbAttr(L9['color'])
out[L9['amber']] = 0
out[L9['white']] = min(r, g, b)
out[L9['uv']] = _8bit(floatAttr(L9['uv']))
return out
elif deviceType == L9['Source4LedSeries2']:
out = {}
out[L9['red']], out[L9['green']], out[L9['blue']] = rgbAttr(L9['color'])
out[L9['strobe']] = 0
out[L9['fixed255']] = 255
for num in range(7):
out[L9['fixed128_%s' % num]] = 128
return out
elif deviceType == L9['MacAura']:
out = {
L9['shutter']: 22,
L9['dimmer']: 255,
L9['zoom']: _8bit(floatAttr(L9['zoom'])),
L9['fixtureControl']: 0,
L9['colorWheel']: 0,
L9['colorTemperature']: 128,
L9['fx1Select']: 0,
L9['fx1Adjust']: 0,
L9['fx2Select']: 0,
L9['fx2Adjust']: 0,
L9['fxSync']: 0,
L9['auraShutter']: 22,
L9['auraDimmer']: 0,
L9['auraColorWheel']: 0,
L9['auraRed']: 0,
L9['auraGreen']: 0,
L9['auraBlue']: 0,
}
out[L9['pan']], out[L9['panFine']] = fine16Attr(L9['rx'])
out[L9['tilt']], out[L9['tiltFine']] = fine16Attr(L9['ry'])
out[L9['red']], out[L9['green']], out[L9['blue']] = rgbAttr(L9['color'])
out[L9['white']] = 0
return out
elif deviceType == L9['MacQuantum']:
out = {
L9['dimmerFadeLo']: 0,
L9['fixtureControl']: 0,
L9['fx1Select']: 0,
L9['fx1Adjust']: 0,
L9['fx2Select']: 0,
L9['fx2Adjust']: 0,
L9['fxSync']: 0,
}
# note these values are set to 'fade', so they update slowly. Haven't found where to turn that off.
out[L9['cyan']], out[L9['magenta']], out[L9['yellow']] = cmyAttr(L9['color'])
out[L9['focusHi']], out[L9['focusLo']] = fine16Attr(L9['focus'])
out[L9['panHi']], out[L9['panLo']] = fine16Attr(L9['rx'])
out[L9['tiltHi']], out[L9['tiltLo']] = fine16Attr(L9['ry'])
out[L9['zoomHi']], out[L9['zoomLo']] = fine16Attr(L9['zoom'])
out[L9['dimmerFadeHi']] = 0 if deviceAttrSettings.get(L9['color'], '#000000') == '#000000' else 255
out[L9['goboChoice']] = {
L9['open']: 0,
L9['spider']: 36,
L9['windmill']: 41,
L9['limbo']: 46,
L9['brush']: 51,
L9['whirlpool']: 56,
L9['stars']: 61,
}[deviceAttrSettings.get(L9['gobo'], L9['open'])]
# my goboSpeed deviceAttr goes 0=stopped to 1=fastest (using one direction only)
x = .5 + .5 * floatAttr(L9['goboSpeed'])
out[L9['goboSpeedHi']] = _8bit(x)
out[L9['goboSpeedLo']] = _8bit((x * 255) % 1.0)
strobe = floatAttr(L9['strobe'])
if strobe < .1:
out[L9['shutter']] = 30
else:
out[L9['shutter']] = 50 + int(150 * (strobe - .1) / .9)
out.update( {
L9['colorWheel']: 0,
L9['goboStaticRotate']: 0,
L9['prismRotation']: _8bit(floatAttr(L9['prism'])),
L9['iris']: _8bit(floatAttr(L9['iris']) * (200/255)),
})
return out
else:
raise NotImplementedError('device %r' % deviceType)
|