H/W4
This commit is contained in:
parent
8c988eeefd
commit
80c8c54b61
@ -1,18 +1,29 @@
|
|||||||
import cmd
|
import cmd
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
from queue import Queue
|
from queue import Empty, Queue
|
||||||
|
|
||||||
from equipment.turtle_device import TurtleDevice
|
from equipment.turtle_device import TurtleDevice
|
||||||
|
|
||||||
|
|
||||||
class TurtleDeviceThread(threading.Thread):
|
class TurtleDeviceThread(threading.Thread):
|
||||||
# TODO(Homework 4)
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.device = TurtleDevice()
|
self.device = TurtleDevice()
|
||||||
self.queue = Queue()
|
self.queue = Queue()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
item = self.queue.get()
|
||||||
|
except self.queue.Empty:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
if (item == 'exit'):
|
||||||
|
break
|
||||||
|
self.device.execute(item[0], item[1:])
|
||||||
|
self.queue.task_done()
|
||||||
|
|
||||||
|
|
||||||
class NoBlockingTurtleShell(cmd.Cmd):
|
class NoBlockingTurtleShell(cmd.Cmd):
|
||||||
intro = 'Welcome to the turtle shell. Type help or ? to list commands.\n'
|
intro = 'Welcome to the turtle shell. Type help or ? to list commands.\n'
|
||||||
@ -20,16 +31,16 @@ class NoBlockingTurtleShell(cmd.Cmd):
|
|||||||
file = None
|
file = None
|
||||||
|
|
||||||
def __init__(self, turtle_thread: TurtleDeviceThread):
|
def __init__(self, turtle_thread: TurtleDeviceThread):
|
||||||
pass # TODO(Homework 4)
|
self.turtle_thread = TurtleDeviceThread()
|
||||||
|
|
||||||
def do_execute(self, arg):
|
def do_execute(self, arg):
|
||||||
pass # TODO(Homework 4)
|
self.turtle_thread.queue.put(arg)
|
||||||
|
|
||||||
def do_exit(self, arg):
|
def do_exit(self, arg):
|
||||||
pass # TODO(Homework 4)
|
self.turtle_thread.queue.put('exit')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
turtle_thread = TurtleDeviceThread()
|
turtle_thread = TurtleDeviceThread()
|
||||||
# TODO(Homework 4: Correct start thread)
|
turtle_thread.start()
|
||||||
NoBlockingTurtleShell(turtle_thread).cmdloop()
|
NoBlockingTurtleShell(turtle_thread).cmdloop()
|
Loading…
Reference in New Issue
Block a user