Mercurial > code > home > repos > homeauto
comparison lib/devices_shared/devices_shared.py @ 1433:be9b456717bd
round numbers. py3 updates. ws
Ignore-this: d1191987ce8070c9700a26b89436478a
darcs-hash:284883b8403f0563572a154cca19d5e96a5e5654
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Sun, 11 Aug 2019 03:25:20 -0700 |
parents | 7993c7bcd04a |
children | d442c4d1b9c7 |
comparison
equal
deleted
inserted
replaced
1432:ff868a0abac7 | 1433:be9b456717bd |
---|---|
1 from __future__ import division | |
2 import time | 1 import time |
3 import numpy | 2 import numpy |
4 import logging | 3 import logging |
5 import imageio | 4 import imageio |
6 from rdflib import Namespace, RDF, URIRef, Literal | 5 from rdflib import Namespace, RDF, URIRef, Literal |
7 | 6 |
7 XS = Namespace('http://www.w3.org/2001/XMLSchema#double') | |
8 ROOM = Namespace('http://projects.bigasterisk.com/room/') | 8 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
9 log = logging.getLogger() | 9 log = logging.getLogger() |
10 | 10 |
11 def _rgbFromHex(h): | 11 def _rgbFromHex(h): |
12 rrggbb = h.lstrip('#') | 12 rrggbb = h.lstrip('#') |
56 else: | 56 else: |
57 dur = self.end - self.start | 57 dur = self.end - self.start |
58 self.x = (self.end - now) / dur * self.x0 + (now - self.start) / dur * self.x2 | 58 self.x = (self.end - now) / dur * self.x0 + (now - self.start) / dur * self.x2 |
59 return self.x | 59 return self.x |
60 | 60 |
61 def roundedLit(x): | |
62 return Literal('%g' % x, datatype=XS['double']) | |
63 | |
61 class ScanGroup(object): | 64 class ScanGroup(object): |
62 | 65 |
63 def __init__(self, uri, numLeds): | 66 def __init__(self, uri, numLeds): |
64 self.uri = uri | 67 self.uri = uri |
65 self.current = numpy.zeros((numLeds, 3), dtype=numpy.uint8) | 68 self.current = numpy.zeros((numLeds, 3), dtype=numpy.uint8) |
84 int(self.y.get()), | 87 int(self.y.get()), |
85 int(self.height.get())) | 88 int(self.height.get())) |
86 except IOError as e: | 89 except IOError as e: |
87 log.warn('getPixelColumn %r', e) | 90 log.warn('getPixelColumn %r', e) |
88 log.debug('current = %r', self.current) | 91 log.debug('current = %r', self.current) |
89 | 92 |
90 def currentStatements(self): | 93 def currentStatements(self): |
91 return [ | 94 return [ |
92 (self.uri, RDF.type, ROOM['ScanGroup']), | 95 (self.uri, RDF.type, ROOM['ScanGroup']), |
93 (self.uri, ROOM['xValue'], Literal(self.x.get())), | 96 (self.uri, ROOM['xValue'], roundedLit(self.x.get())), |
94 (self.uri, ROOM['yValue'], Literal(self.y.get())), | 97 (self.uri, ROOM['yValue'], roundedLit(self.y.get())), |
95 (self.uri, ROOM['heightValue'], Literal(self.height.get())), | 98 (self.uri, ROOM['heightValue'], Literal(self.height.get())), |
96 (self.uri, ROOM['src'], Literal(self.src)), | 99 (self.uri, ROOM['src'], Literal(self.src)), |
97 ] | 100 ] |
98 | 101 |
99 def colorForIndex(self, i): | 102 def colorForIndex(self, i): |
100 return list(self.current[i,:]) | 103 return list(self.current[i,:]) |
101 | 104 |
102 args = {ROOM['src']: ('src', str), | 105 args = {ROOM['src']: ('src', str), |
103 ROOM['x']: ('x', int), | 106 ROOM['x']: ('x', float), |
104 ROOM['y']: ('y', int), | 107 ROOM['y']: ('y', float), |
105 ROOM['height']: ('height', int), | 108 ROOM['height']: ('height', float), |
106 ROOM['interpolate']: ('interpolate', lambda x: x), | 109 ROOM['interpolate']: ('interpolate', lambda x: x), |
107 ROOM['rate']: ('rate', float), | 110 ROOM['rate']: ('rate', float), |
108 } | 111 } |
109 | 112 |
110 | 113 |
111 class RgbPixelsAnimation(object): | 114 class RgbPixelsAnimation(object): |
112 | 115 |
113 def __init__(self, graph, uri, updateOutput): | 116 def __init__(self, graph, uri, updateOutput): |
114 """we call updateOutput after any changes""" | 117 """we call updateOutput after any changes""" |
115 self.graph = graph | 118 self.graph = graph |
116 self.uri = uri | 119 self.uri = uri |
117 self.updateOutput = updateOutput | 120 self.updateOutput = updateOutput |
118 self.setupGroups() | 121 self.setupGroups() |
119 | 122 |
120 def setupGroups(self): | 123 def setupGroups(self): |
121 self.groups = {} | 124 self.groups = {} |
122 self.groupWithIndex = {} | 125 self.groupWithIndex = {} |
123 attrStatements = set() | 126 attrStatements = set() |
124 for grp in self.graph.objects(self.uri, ROOM['pixelGroup']): | 127 for grp in self.graph.objects(self.uri, ROOM['pixelGroup']): |
129 self.groups[grp] = [s, e, sg] | 132 self.groups[grp] = [s, e, sg] |
130 for i in range(s, e + 1): | 133 for i in range(s, e + 1): |
131 self.groupWithIndex[i] = sg, i - s | 134 self.groupWithIndex[i] = sg, i - s |
132 attrStatements.update(self.graph.triples((grp, None, None))) | 135 attrStatements.update(self.graph.triples((grp, None, None))) |
133 self.onStatements(attrStatements, _groups=False) | 136 self.onStatements(attrStatements, _groups=False) |
134 | 137 |
135 def maxIndex(self): | 138 def maxIndex(self): |
136 return max(v[1] for v in self.groups.itervalues()) | 139 return max(v[1] for v in self.groups.values()) |
137 | 140 |
138 def hostStatements(self): | 141 def hostStatements(self): |
139 return ( | 142 return ( |
140 [(self.uri, ROOM['pixelGroup'], grp) for grp in self.groups.keys()] + | 143 [(self.uri, ROOM['pixelGroup'], grp) for grp in self.groups.keys()] + |
141 sum([v[2].currentStatements() | 144 sum([v[2].currentStatements() |
142 for v in self.groups.itervalues()], []) + | 145 for v in self.groups.values()], []) + |
143 [] # current | 146 [] # current |
144 ) | 147 ) |
145 | 148 |
146 def getColorOrder(self, graph, uri): | 149 def getColorOrder(self, graph, uri): |
147 colorOrder = graph.value(uri, ROOM['colorOrder'], | 150 colorOrder = graph.value(uri, ROOM['colorOrder'], |
149 head, tail = str(colorOrder).rsplit('/', 1) | 152 head, tail = str(colorOrder).rsplit('/', 1) |
150 if head != str(ROOM['ledColorOrder']): | 153 if head != str(ROOM['ledColorOrder']): |
151 raise NotImplementedError('%r colorOrder %r' % (uri, colorOrder)) | 154 raise NotImplementedError('%r colorOrder %r' % (uri, colorOrder)) |
152 stripType = None | 155 stripType = None |
153 return colorOrder, stripType | 156 return colorOrder, stripType |
154 | 157 |
155 def step(self): | 158 def step(self): |
156 # if animating... | 159 # if animating... |
157 self.updateOutput() | 160 self.updateOutput() |
158 | 161 |
159 def onStatements(self, statements, _groups=True): | 162 def onStatements(self, statements, _groups=True): |
160 needSetup = False | 163 needSetup = False |
161 animateCalls = {} # group uri : kw for animateTo | 164 animateCalls = {} # group uri : kw for animateTo |
162 for s, p, o in statements: | 165 for s, p, o in statements: |
163 if s not in self.groups: | 166 if s not in self.groups: |