summaryrefslogtreecommitdiff
path: root/host/utils
diff options
context:
space:
mode:
authorNicolas Schodet2011-05-08 15:43:13 +0200
committerNicolas Schodet2011-05-08 15:44:08 +0200
commitb313abd7449af357d6815e1116975d5987a39364 (patch)
tree32c936f6bb7d49e217067d356f927dfeba8a3cbd /host/utils
parentfd2feb1325a6899fa20fd9996e22d22205c01f85 (diff)
digital/*/tools: use robot specific init parameters
Diffstat (limited to 'host/utils')
-rw-r--r--host/utils/init_proto.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/host/utils/init_proto.py b/host/utils/init_proto.py
new file mode 100644
index 00000000..f5c2bfa4
--- /dev/null
+++ b/host/utils/init_proto.py
@@ -0,0 +1,36 @@
+"""Helper to create a Proto instance."""
+import proto.popen_io
+import serial
+import optparse
+
+def init_proto (default_robot, proto_class, init_module = None, init = None):
+ """Helper to create a Proto instance from command line arguments."""
+ if init_module is None and init is None:
+ init = { }
+ # Parse arguments.
+ parser = optparse.OptionParser (
+ usage = "usage: %prog [options] TTY|! PROGRAM...",
+ description = "TTY is a device name (example: %prog "
+ "/dev/ttyUSB0), PROGRAM is a host program with its arguments "
+ "(example: %prog -- ! ../src/board.host board_arg).")
+ if init_module:
+ parser.add_option ('-r', '--robot', help = "use specified robot",
+ metavar = 'NAME', default = default_robot)
+ (options, args) = parser.parse_args ()
+ if init_module and options.robot is None:
+ parser.error ("no robot specified")
+ if not args:
+ parser.error ("not enough arguments")
+ # Create parameters.
+ if args[0] == '!':
+ io = proto.popen_io.PopenIO (args[1:])
+ if init_module:
+ init = init_module.host[options.robot]
+ else:
+ if len (args) != 1:
+ parser.error ("too many arguments after device")
+ io = serial.Serial (args[0])
+ if init_module:
+ init = init_module.target[options.robot]
+ return proto_class (io, **init)
+