From: Bernard Blackham Date: Sun, 27 Jun 2004 03:39:41 +0000 (+0000) Subject: Hopefully make it more responsive. Untested. X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=356b151a7886090c530488f1ced311130923cbbc;p=zanchey%2Fdispense2.git Hopefully make it more responsive. Untested. --- diff --git a/sql-edition/servers/VendServer.py b/sql-edition/servers/VendServer.py index 2627cd3..447dcf7 100755 --- a/sql-edition/servers/VendServer.py +++ b/sql-edition/servers/VendServer.py @@ -80,16 +80,14 @@ def door_open_mode(vending_machine): print "Entering open door mode" v.display("DOOR OPEN") while True: - v.wait_for_events(1) - while True: - e = v.next_event() - if e == None: break - (event, params) = e - if event == DOOR: - if params == 1: # door closed - v.display("BYE BYE!") - time.sleep(1) - return + e = v.next_event() + if e == None: break + (event, params) = e + if event == DOOR: + if params == 1: # door closed + v.display("BYE BYE!") + time.sleep(1) + return if __name__ == '__main__': # Open vending machine via LAT @@ -136,10 +134,8 @@ if __name__ == '__main__': need_repaint = True continue - v.wait_for_events(1) while True: - v.wait_for_events(0) - e = v.next_event() + e = v.next_event(1) if e == None: break (event, params) = e print e diff --git a/sql-edition/servers/VendingMachine.py b/sql-edition/servers/VendingMachine.py index 18f3067..52b66cf 100644 --- a/sql-edition/servers/VendingMachine.py +++ b/sql-edition/servers/VendingMachine.py @@ -1,3 +1,4 @@ +# vim:ts=4 import re, pg from CRC import do_crc from select import select @@ -83,7 +84,7 @@ class VendingMachine: elif code == '401': self.events.append((DOOR, 1)) elif code == '610': - self.events_append((SWITCH, None)) + self.events.append((SWITCH, None)) self.interpret_switches(text) elif code[0] == '2': self.events.append((KEY, int(code[1:3]))) @@ -140,19 +141,17 @@ class VendingMachine: (code, string) = self.get_response() return (code == '300', code, string) - def next_event(self): - if len(self.events) > 0: - ret = self.events[0] - del self.events[0] - return ret - return None - - def wait_for_events(self, timeout = None): - if self.events: return True - (r, _, _) = select([self.rfh], [], [], timeout) - if not r: return False - event_added = False + def next_event(self, timeout = None): + # we don't want to buffer in the serial port, so we get all the events + # we can ASAP. + if len(self.events) > 0: timeout = 0 while True: - self.get_response(async = True) - (r, _, _) = select([self.rfh], [], [], 0) - if not r: return event_added + (r, _, _) = select([self.rfh], [], [], timeout) + if r: + self.get_response(async = True) + else: + break + if len(self.events) == 0: return None + ret = self.events[0] + del self.events[0] + return ret