diff options
Diffstat (limited to 'inputs')
-rw-r--r-- | inputs/PygameInput.py | 35 | ||||
-rw-r--r-- | inputs/RandomLocs.py | 3 | ||||
-rw-r--r-- | inputs/TCPInput.py | 18 | ||||
-rw-r--r-- | inputs/UDPInput.py | 10 |
4 files changed, 43 insertions, 23 deletions
diff --git a/inputs/PygameInput.py b/inputs/PygameInput.py index 27b82b0..399a77e 100644 --- a/inputs/PygameInput.py +++ b/inputs/PygameInput.py @@ -6,18 +6,27 @@ from pygame.locals import * #This class processes input from an already running pygame instance and passes #it to the parent. This class requires an already running pygame instance. class PygameInput(Input): + """PygameInput is an input tied to the PygameDisplay. Specify: + <FollowMouse>True</FollowMouse> to receive an input every frame specifying the current mouse + position. + <Keyboard>True</Keyboard> to grab keystrokes + <Clicks>True</Clicks> to grab clicks. + + NB: If follow mouse is enabled, PygameInput will not return mouse and keypresses. You can, however, + instantiate other PygameInputs in the XML that will capture mouse and keypresses.""" def sensingLoop(self): - #try: - if self['FollowMouse']: - self.respond({Strings.LOCATION: pygame.mouse.get_pos()}) - return - for event in pygame.event.get(): - if event.type is KEYDOWN: - if event.key == 27: - self.die() - self.respond({Strings.LOCATION: (5,5),'Key': event.key}) - if event.type is MOUSEBUTTONDOWN: + if self['FollowMouse']: + self.respond({Strings.LOCATION: pygame.mouse.get_pos()}) + return + for event in pygame.event.get(): + if event.type is KEYDOWN: + if event.key == 27: + self.die() + if self['Keyboard']: + self.respond({'Key': event.key}) + return + else: + pygame.event.post(event) + if event.type is MOUSEBUTTONDOWN: + if self['Clicks']: self.respond({Strings.LOCATION: pygame.mouse.get_pos()}) - #except: - #raise Exception('Pygame not initialized. Pygame must be \ - #initialized.') diff --git a/inputs/RandomLocs.py b/inputs/RandomLocs.py index d1ce1c7..2719981 100644 --- a/inputs/RandomLocs.py +++ b/inputs/RandomLocs.py @@ -4,6 +4,9 @@ import util.Geo as Geo import util.Strings as Strings from operationscore.Input import * class RandomLocs(Input): + """RandomLocs is an Input that generates RandomLocations at a preset time interval. Just a + prototype, some assembly required.""" + def inputInit(self): self['LastEvent'] = clock.time() def sensingLoop(self): #TODO: move to params diff --git a/inputs/TCPInput.py b/inputs/TCPInput.py index 5bf06bf..17ea7e6 100644 --- a/inputs/TCPInput.py +++ b/inputs/TCPInput.py @@ -3,7 +3,13 @@ import pdb from operationscore.Input import * import socket, json, time import logging as main_log +import string + class TCPInput(Input): + """TCPInput is a input to receive input on a TCP port. In its current incarnation, it parses + json data into python dicts. Warning: contains a bug where init will hang until it receives a + connection. Specify: + <Port> -- Port number to listen on.""" def inputInit(self): self.HOST = '' # Symbolic name meaning all available interfaces self.PORT = self.argDict['Port'] # Arbitrary non-privileged port @@ -22,7 +28,9 @@ class TCPInput(Input): if not data or 'end' in data: # data end, close socket main_log.debug('End in data') + print 'end of stream' self.IS_RESPONDING = 0 + self.conn.close() self.sock.close() if self.IS_RESPONDING == 1: # if 'responding', respond to the received data @@ -30,20 +38,16 @@ class TCPInput(Input): for datagroup in data.split('\n'): if datagroup != None and datagroup != '': dataDict = json.loads(datagroup) - # socketDict = {'data':dataDict, 'address':self.address} - socketDict = {Strings.LOCATION: (dataDict['x'], dataDict['y'])} # like PygameInput - print 'input' - self.respond(socketDict) + self.respond(dataDict) except Exception as exp: print str(exp) else: # if not 'responding', don't respond to data and restart socket # * an incomplete hack for now. will be changed if same-type-multi-Input is implemented. - time.sleep(1) + + self.IS_RESPONDING = 1 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.bind((self.HOST, self.PORT)) self.sock.listen(1) (self.conn, self.address) = self.sock.accept() - self.IS_RESPONDING = 1 - diff --git a/inputs/UDPInput.py b/inputs/UDPInput.py index 7d5609e..83e2f77 100644 --- a/inputs/UDPInput.py +++ b/inputs/UDPInput.py @@ -1,13 +1,17 @@ from operationscore.Input import * import socket class UDPInput(Input): + """UDPInput is a barebones UDP Input class. It takes any data it receives and adds it to the + 'data' element of the response dict. It also notes the 'address'. Specify: + <Port> -- the Port to listen on.""" + def inputInit(self): HOST = '' # Symbolic name meaning all available interfaces PORT = self.argDict['Port'] # Arbitrary non-privileged port self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock.bind((HOST, PORT)) def sensingLoop(self): - (data,address) = self.sock.recvfrom(1024) - dataDict = {'data':data, 'address':address} - self.respond(dataDict) + (data,address) = self.sock.recvfrom(1024) + dataDict = {'data':data, 'address':address} + self.respond(dataDict) |