aboutsummaryrefslogtreecommitdiff
path: root/Util.py
blob: 07ce545af5f6f1ef6bd6ca27761e8b3aa172e346 (plain)
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
223
224
225
226
227
228
229
230
231
232
import pdb
from xml.etree.ElementTree import ElementTree
import math,struct
from bisect import *
#import json # json.loads() to decode string; json.dumps() to encode data
import socket
import random
from pygame.locals import *
import time as clock
from pixelevents.StepEvent import *

VERSION = 0x0001
MAGIC = 0x4adc0104
MOREMAGIC = 0xdeadbeef
DEEPMAGIC = 0xc001d00d
MAGICHASH = 0x69000420
PORTOUT = 0x0108
classArgsMem = {}
UNI = 0
colorByteMem = {}
CONFIG_PATH = 'config/'
kinetDict = {'flags': 0, 'startcode': 0, 'pad':0}
componentDict = {}
#Only for rough estimates.  Kindof lazy on specifics.
def pointWithinBoundingBox(point, bb): #this could be in 4 lines, but I'm lazy.
    return sum([(point[i % 2] <= bb[i]) == (i>1) for i in range(4)]) == 4 
print pointWithinBoundingBox((118,21), (10,8,298,42))
def addLocations(l1,l2):
    return tuple([l1[i]+l2[i] for i in range(len(l1))])
def setScreen(screen):
    globals()["screen"] = screen
def getScreen():
    return screen
def setComponentDict(componentDictRef):
    globals()["componentDict"] = componentDictRef
def getComponentById(cid):
    if cid in componentDict:
        return componentDict[cid]
    else:
        return None
def addPixelEventIfMissing(responseDict):
    if not 'PixelEvent' in responseDict:
        if 'Color' in responseDict:
            color = responseDict['Color']
        else:
            raise Exception('Need Color.  Probably')
        responseDict['PixelEvent'] = StepEvent.generate(300, color)
def gaussian(x,height,center,width):
    a=height
    b=center
    c=width
    return a*math.exp(-((x-b)**2)/(2*c**2))
def dist(l1, l2):
    return math.sqrt(sum([(l1[i]-l2[i])**2 for i in range(len(l1))]))
def time():
    return clock.time()*1000
def randomColor():
    return [random.randint(0,255) for i in range(3)]
def chooseRandomColor(colorList):
    return random.choice(colorList)
def loadParamRequirementDict(className):
    if not className in classArgsMem: #WOO CACHING
        classArgsMem[className] = fileToDict(CONFIG_PATH + className) 
    return classArgsMem[className]
def loadConfigFile(fileName):
    try:
        fileName = CONFIG_PATH + fileName
        if '.params' in fileName:
            return fileToDict(fileName)
        if '.xml' in fileName:
            config = ElementTree()
            config.parse(fileName)
            return config
    except:
        return None
def fileToDict(fileName):
    fileText = ''
    try:
        print 'File Read'
        with open(fileName) as f:
            for line in f:
                fileText += line.rstrip('\n').lstrip('\t') + ' ' 
    except IOError:
        return {}
    if fileText == '':
        return {}
    return eval(fileText)
def find_le(a, x):
    'Find rightmost value less than or equal to x'
    return bisect_right(a, x)-1

def find_ge(a, x):
    'Find leftmost value greater than x'
    return bisect_left(a, x)
def safeColor(c):
    return [min(channel,255) for channel in c]
def combineColors(c1,c2):
    return safeColor([c1[i]+c2[i] for i in range(min(len(c1),len(c2)))])
def multiplyColor(color, percent):
    return safeColor([channel*(percent) for channel in color])
#parses arguments into python objects if possible, otherwise leaves as strings
def generateArgDict(parentNode, recurse=False):
    args = {}
    for arg in parentNode.getchildren():
        key = arg.tag
        if arg.getchildren() != []:
            value = generateArgDict(arg, True)
        else:
            #convert into python if possible, otherwise don't
            try:
                value = eval(arg.text)
            except (NameError,SyntaxError):
                value = str(arg.text)
        if key in args: #build of lists of like-elements
            if type(args[key]) != type([]):
                args[key] = [args[key]]
            args[key].append(value)
        else:
            args[key]=value
    #if we should be a list but we aren't:
    if len(args.keys()) == 1 and recurse:
        return args[args.keys()[0]]
    return args
#Given a dictionary of connections, returns their topological ordering -- (the
#order in which they can be visited such that all parents have been visited
#before their children.  Returns the order or None if no such ordering exists
#(the graph contains a cycle).
def topologicalSort(adjacencyDict):
    def dfsVisit(vertex):
        gray[vertex] = 1
        for child in adjacencyDict[vertex]:
            if not child in visited:
                if child in gray: #We have a cycle.  No topological ordering
                    #exists!
                    raise Exception('Cycle!') 
                dfsVisit(child)
        orderedList.insert(0, vertex)
        visited[vertex] = 1
    orderedList = []
    visited = {}
    gray = {}
    for vertex in adjacencyDict:
        try:
            if not vertex in visited:
                dfsVisit(vertex)
        except:
            return None #cycle
    return orderedList
def topoTest():
    adj = {'a':['d','c'], 'b':['c'], 'c':['e'], 'd':['e'], 'e':[]}
    print topologicalOrdering(adj)
def getConnectedSocket(ip,port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    print (ip, port)
    sock.connect((ip, port))
    return sock
def composePixelStripData(pixelStrip):
    packet = bytearray()
    for light in pixelStrip:
        color = light.state()
        for channel in color: #skip the last value, its an
            #alpha value
            packet.append(struct.pack('B', channel))
    return packet
#    packet = [0]*len(pixelStrip.pixels)*3 #preallocate for speed
#    for i in range(len(pixelStrip.pixels)): 
#color = pixelStrip.pixels[i].state()
#packet[i:i+2] = color
#    return bytearray(packet)
def composePixelStripPacket(pixelStrip,port):
    packet = bytearray()
    data = composePixelStripData(pixelStrip)
    subDict = dict(kinetDict)
    subDict['len'] = 38000 #I have no idea why this works.
    subDict['port'] = port
    #pdb.set_trace()
    packet.extend(kinetPortOutPacket(subDict))
    packet.append(0x0)
    packet.extend(data)
    return packet
def kinetHeader():
    header = bytearray()
    header.extend(struct.pack('L', MAGIC))
    header.extend(struct.pack('H', VERSION))
    header.extend(struct.pack('H', PORTOUT))
    header.extend(struct.pack('L', 0))
    return header
def kinetPortOut():
    header = kinetHeader()
    header.extend(struct.pack('L', UNI))
    return header
def kinetPortOutPayload(argDict):
    payload = bytearray()
    payload.extend(struct.pack('B', argDict['port']))
    #payload.append(0x00) #somepadding? lolwtf.
    payload.extend(struct.pack('H', argDict['flags']))
    #payload.append(0x00) #somepadding? lolwtf.
    payload.extend(struct.pack('H', argDict['len']))
    payload.extend(struct.pack('H', argDict['startcode']))
    #pdb.set_trace()
    return payload
def kinetPortOutPacket(payloadArgs):
    packet = bytearray()
    packet.extend(kinetPortOut())
    packet.extend(kinetPortOutPayload(payloadArgs))
    return packet
def testXMLParse(fileName):
    #pdb.set_trace()
    config = ElementTree()
    config.parse(fileName)
    print generateArgDict(config.find('ChildElement'))
    print generateArgDict(config.find('Renderer'))
class Stopwatch:
    def __init__(self):
        self.running = False
        self.startTime = -1
        self.stopTime = -1
    def start(self):
        self.startTime = Util.time()
        self.running = True
    def elapsed(self):
        if self.running:
            return Util.time()-self.startTime
        else:
            return self.stopTime - self.startTime
    def stop(self):
        self.stopTime = Util.time()
        self.running = False
##CONSTANTS##
location = 'Location'