modified: noblocking_turtle_shell.py
modified: turtle_shell.py
This commit is contained in:
parent
718e9700ca
commit
1a6e43dfdd
@ -1,60 +1,76 @@
|
|||||||
import cmd
|
import cmd
|
||||||
from threading import Thread, Event
|
import threading
|
||||||
from turtle import bye
|
from queue import Queue
|
||||||
from queue import Queue, Empty
|
from turtle import forward
|
||||||
|
|
||||||
from equipment.turtle_device import TurtleDevice
|
from equipment.turtle_device import TurtleDevice
|
||||||
|
|
||||||
|
|
||||||
class TurtleDeviceThread(Thread):
|
class TurtleDeviceThread(threading.Thread):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.device = TurtleDevice()
|
self.device = TurtleDevice()
|
||||||
self.queue = Queue()
|
|
||||||
|
self.queue = Queue[tuple]()
|
||||||
|
|
||||||
|
self.device.open()
|
||||||
|
self.device.execute("speed", 1)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
try:
|
action, args, kwargs = self.queue.get()
|
||||||
item = self.queue.get()
|
if action == "exit":
|
||||||
except self.queue.Empty:
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
if (item == 'exit'):
|
|
||||||
break
|
|
||||||
self.device.execute(*item)
|
|
||||||
self.queue.task_done()
|
self.queue.task_done()
|
||||||
|
break
|
||||||
|
self.device.execute(action, *args, **kwargs)
|
||||||
|
self.queue.task_done()
|
||||||
|
|
||||||
|
def add_task(self, action, *args, **kwargs):
|
||||||
|
self.queue.put((action, args, kwargs))
|
||||||
|
|
||||||
|
|
||||||
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"
|
||||||
prompt = '(turtle) '
|
prompt = "(turtle) "
|
||||||
file = None
|
file = None
|
||||||
|
|
||||||
def __init__(self, turtle_thread: TurtleDeviceThread):
|
def __init__(self, turtle_thread: TurtleDeviceThread):
|
||||||
super(NoBlockingTurtleShell, self).__init__()
|
super().__init__()
|
||||||
self.turtle_thread = turtle_thread
|
self.turtle_thread = turtle_thread
|
||||||
|
|
||||||
|
@property
|
||||||
|
def turtle_device(self):
|
||||||
|
return self.turtle_thread.device
|
||||||
|
|
||||||
def do_execute(self, arg):
|
def do_execute(self, arg):
|
||||||
self.turtle_thread.queue.put(parse(arg))
|
command_and_args = arg.split()
|
||||||
|
if len(command_and_args) == 1:
|
||||||
|
self.turtle_thread.add_task(command)
|
||||||
|
return
|
||||||
|
command = command_and_args[0]
|
||||||
|
args = tuple(int(c) if c.isdecimal() else c for c in command_and_args[1:])
|
||||||
|
self.turtle_thread.add_task(command, *args)
|
||||||
|
|
||||||
def do_exit(self, arg):
|
|
||||||
self.close()
|
def do_forward(self, arg):
|
||||||
self.turtle_thread.queue.put("exit")
|
'Move the turtle forward by the specified distance: FORWARD 10'
|
||||||
|
self.do_execute("forward 100")
|
||||||
|
def do_right(self, arg):
|
||||||
|
'Turn turtle right by given number of degrees: RIGHT 20'
|
||||||
|
self.do_execute("right 100")
|
||||||
|
def do_left(self, arg):
|
||||||
|
'Turn turtle left by given number of degrees: LEFT 90'
|
||||||
|
self.do_execute("left 100")
|
||||||
|
def do_bye(self, arg):
|
||||||
|
'Stop recording, close the turtle window, and exit: BYE'
|
||||||
print('Thank you for using Turtle')
|
print('Thank you for using Turtle')
|
||||||
|
self.turtle_thread.add_task("exit")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def close(self):
|
if __name__ == "__main__":
|
||||||
if self.file:
|
|
||||||
self.file.close()
|
|
||||||
self.file = None
|
|
||||||
|
|
||||||
def parse(arg):
|
|
||||||
'Convert a series of zero or more numbers to an argument tuple'
|
|
||||||
return tuple(arg.split())
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
turtle_thread = TurtleDeviceThread()
|
turtle_thread = TurtleDeviceThread()
|
||||||
turtle_thread.start()
|
turtle_thread.daemon = True
|
||||||
NoBlockingTurtleShell(turtle_thread).cmdloop()
|
thread_shell = threading.Thread(target=NoBlockingTurtleShell(turtle_thread).cmdloop)
|
||||||
|
thread_shell.start()
|
||||||
|
turtle_thread.run()
|
||||||
|
thread_shell.join()
|
||||||
|
@ -10,7 +10,6 @@ class TurtleShell(cmd.Cmd):
|
|||||||
# ----- basic turtle commands -----
|
# ----- basic turtle commands -----
|
||||||
def do_forward(self, arg):
|
def do_forward(self, arg):
|
||||||
'Move the turtle forward by the specified distance: FORWARD 10'
|
'Move the turtle forward by the specified distance: FORWARD 10'
|
||||||
|
|
||||||
forward(*parse(arg))
|
forward(*parse(arg))
|
||||||
def do_right(self, arg):
|
def do_right(self, arg):
|
||||||
'Turn turtle right by given number of degrees: RIGHT 20'
|
'Turn turtle right by given number of degrees: RIGHT 20'
|
||||||
|
Loading…
Reference in New Issue
Block a user