| 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 | |
|---|
| 10 | import atexit |
|---|
| 11 | import code |
|---|
| 12 | import nxt |
|---|
| 13 | import os |
|---|
| 14 | import readline |
|---|
| 15 | import struct |
|---|
| 16 | import sys |
|---|
| 17 | import time |
|---|
| 18 | from nxt.lowlevel import get_device |
|---|
| 19 | |
|---|
| 20 | NXOS_INTERFACE = 0 |
|---|
| 21 | |
|---|
| 22 | class 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 | |
|---|
| 56 | def 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 | |
|---|
| 74 | if __name__ == "__main__": |
|---|
| 75 | main() |
|---|