aboutsummaryrefslogtreecommitdiff
path: root/AT91SAM7S256/armdebug/nxt-python-fantom/nxt/usbsock.py
blob: 5ff2b36c5dbf90e32a3c4a6d5d030f89c91b0579 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# nxt.usbsock module -- USB socket communication with LEGO Minstorms NXT
# Copyright (C) 2006, 2007  Douglas P Lau
# Copyright (C) 2009  Marcus Wanner
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

USB_BUFSIZE = 64

try:
    import fantomglue as usb
except ImportError:
    import pyusbglue as usb

from nxt.brick import Brick

class USBSock(object):
    'Object for USB connection to NXT'

    bsize = 60  # USB socket block size

    def __init__(self, device):
        self.sock = usb.USBSocket(device)
        self.debug = True

    def __str__(self):
        return 'USB (%s)' % (self.sock.device_name())

    def connect(self):
        'Use to connect to NXT.'
        if self.debug:
            print 'Connecting via USB...'
        self.sock.connect()
        return Brick(self)

    def close(self):
        'Use to close the connection.'
        if self.debug:
            print 'Closing USB connection...'
        self.sock.close()
        #self.sock = None
        if self.debug:
            print 'USB connection closed.'

    def send(self, data):
        'Use to send raw data over USB connection ***ADVANCED USERS ONLY***'
        if self.debug:
            print 'Send:',
            print ':'.join('%02x' % ord(c) for c in data)
        self.sock.send(data)

    def recv(self, num_bytes=USB_BUFSIZE):
        'Use to recieve raw data over USB connection ***ADVANCED USERS ONLY***'
        data = self.sock.recv(num_bytes)
        if self.debug:
            print 'Recv:',
            print ':'.join('%02x' % ord(c) for c in data)
        return data

    def __del__(self):
        """Destroy interface."""
        if self.sock is not None:
            del self.sock
            if self.debug:
                print 'Deleted USBSocket.'


def _check_brick(arg, value):
    return arg is None or arg == value

def find_bricks(host=None, name=None):
    get_info = False
    'Use to look for NXTs connected by USB only. ***ADVANCED USERS ONLY***'
    for d in usb.find_devices(lookup_names=True):
        if get_info:
            # pyfantom specific debug info
            print " firmware version:", d.get_firmware_version()
            print " get device info:", d.get_device_info()
            rs = d.get_resource_string()
            print " resource string:", rs
        # FIXME: probably should check host and name
        yield USBSock(d)

if __name__ == '__main__':
    write_read = True
    socks = find_bricks()
    for s in socks:
    #llsocks = usb.find_devices()
    #for ls in llsocks:
        #s = USBSock(ls)
        print s.sock
        brick = s.connect()
        if write_read:
            import struct
            # Write VERSION SYS_CMD.
            # Query:
            #  SYS_CMD: 0x01
            #  VERSION: 0x88
            cmd = struct.pack('2B', 0x01, 0x88)
            brick.sock.send(cmd)
            #s.send(cmd)
            #s.sock.send(cmd)
            print "wrote", len(cmd)
            # Response:
            #  REPLY_CMD: 0x02
            #  VERSION: 0x88
            #  SUCCESS: 0x00
            #  PROTOCOL_VERSION minor
            #  PROTOCOL_VERSION major
            #  FIRMWARE_VERSION minor
            #  FIRMWARE_VERSION major
            rep = brick.sock.recv(7)
            #rep = s.recv(7)
            #rep = s.sock.recv(7)
            print "read", struct.unpack('%dB' % len(rep), rep)
            # Same thing, without response.
            #cmd = struct.pack('2B', 0x81, 0x88)
            #brick.sock.send(cmd)
            #print "wrote", cmd
            #rep = brick.sock.recv()
            #print "read", struct.unpack('%dB' % len(rep), rep)
        #s.close()
        #del s
        #brick.sock.close()
        del brick