root/usb_console/rcmd_frontend.py

Revision 602:bb62c7392d44, 1.6 kB (checked in by Maxime Petazzoni <maxime.petazzoni@…>, 7 months ago)

Improve the user interface by providing a readline-powered
prompt, with command history.

  • Property exe set to *
Line 
1#!/usr/bin/env python
2
3# Copyright (c) 2008 the NxOS developers
4#
5# See AUTHORS for a full list of the developers.
6#
7# Redistribution of this file is permitted under
8# the terms of the GNU Public License (GPL) version 2.
9
10import atexit
11import code
12import nxt
13import os
14import readline
15import struct
16import sys
17import time
18from nxt.lowlevel import get_device
19
20NXOS_INTERFACE = 0
21
22class RcmdConsole(code.InteractiveConsole):
23
24  def __init__(self, locals=None, filename="<console>",
25               histfile=os.path.expanduser("~/.nxos-rcmd.hist")):
26    code.InteractiveConsole.__init__(self)
27    self.init_history(histfile)
28
29  def init_history(self, histfile):
30    readline.parse_and_bind("tab: complete")
31    if hasattr(readline, "read_history_file"):
32      try:
33        readline.read_history_file(histfile)
34      except IOError:
35        pass
36      atexit.register(self.save_history, histfile)
37
38  def save_history(self, histfile):
39    readline.write_history_file(histfile)
40
41
42  def set_brick(self, brick):
43    self.brick = brick
44
45  def push(self, line):
46    if line == 'quit' or line == 'exit':
47      print "Use Ctrl-D (i.e. EOF) to exit"
48      return False
49
50    self.brick.write(line)
51    if line == 'end':
52      sys.exit(0)
53
54    return False
55
56def main():
57    print "Looking for NXT...",
58    brick = get_device(0x0694, 0xFF00, timeout=60)
59    if not brick:
60        print "not found!"
61        return False
62
63    brick.open(NXOS_INTERFACE)
64    print "ok."
65
66    prompt = RcmdConsole()
67    prompt.set_brick(brick)
68    prompt.interact('Remote robot command console.')
69
70    print "Closing link...",
71    brick.write('end')
72    print "done."
73
74if __name__ == "__main__":
75    main()
Note: See TracBrowser for help on using the browser.