aboutsummaryrefslogtreecommitdiff
path: root/operationscore/Input.py
diff options
context:
space:
mode:
authorGravatar Russell Cohen <rcoh@mit.edu>2010-11-24 01:09:12 -0500
committerGravatar Russell Cohen <rcoh@mit.edu>2010-11-24 01:09:12 -0500
commitb042647b68abdc82490ca6e059993b8eba28904c (patch)
treea9ee95a38e98b377c251b7b2e9af9cbd8056cf7c /operationscore/Input.py
parent407ac922fc4178021cf3a16dfb1bd875b6083ac4 (diff)
Refactoring complete! Made modules/packages as appropriate. Finally.
Diffstat (limited to 'operationscore/Input.py')
-rw-r--r--operationscore/Input.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/operationscore/Input.py b/operationscore/Input.py
new file mode 100644
index 0000000..1ba4528
--- /dev/null
+++ b/operationscore/Input.py
@@ -0,0 +1,46 @@
+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 'InputId' in argDict:
+ raise Exception('InputId must be defined in config xml')
+ 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
+ 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
+
+