aboutsummaryrefslogtreecommitdiff
path: root/operationscore/Input.py
blob: 67a7bb0bbabbe2777210775869d52c5430afc0c2 (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
import threading,time,Util
#Abstract class for inputs.  Inheriting classes should call "respond" to raise
#their event.  Inheriting classes MUST define sensingLoop.  Called at the
#interval specified in RefreshInterval while the input is active.  For example, if you are writing
#webserver, this is where the loop should go.
#Inheriting classes MAY define inputInit.  This is called before the loop
#begins.
import pdb
class Input(threading.Thread):
    #Event scope is a function pointer the function that will get called when
    #an Parent is raised.
    def __init__(self, argDict):
        self.eventQueue = []
        self.parentScope = argDict['parentScope']
        self.argDict = argDict
        if not 'RefreshInterval' in argDict:
            print 'RefreshInterval not defined.  Defaulting to .5s.'
            self.argDict['RefreshInterval'] = 500 
        self.inputInit()
        threading.Thread.__init__(self)
        self.daemon = True #This kills this thread when the main thread stops
    #CHEATING until I can get multiple inheritence working
    def __setitem__(self,k, item):
        self.argDict[k] = item
    def __getitem__(self, item):
        if item in self.argDict:
            return self.argDict[item]
        else:
            return None
    def __getiter__(self):
        return self.argDict.__getiter__()
    def respond(self, eventDict):
        #if eventDict != []:
            #pdb.set_trace()
        self.parentScope.processResponse(self.argDict, eventDict)
    def newEvent(self, event): #Mostly just useful for grabbing events from the
        #computer running the sim (key presses, clicks etc.)
        self.eventQueue.append(event)
    def parentAlive(self):
        try:
            parentAlive = self.parentScope.alive()
            return parentAlive
        except:
            return False
    def run(self):
        while self.parentAlive():
            time.sleep(self.argDict['RefreshInterval']/float(1000))
            self.sensingLoop()
    def sensingLoop(self):
        pass
    def inputInit(self):
        pass