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
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# USB comunication with USB4butia (USB4all) board
#
# Copyright (c) 2012-2013 Alan Aguiar alanjas@hotmail.com
# Copyright (c) 2012-2013 Butiá Team butia@fing.edu.uy
# Butia is a free and open robotic platform
# www.fing.edu.uy/inco/proyectos/butia
# Facultad de Ingeniería - Universidad de la República - Uruguay
#
# 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 2 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
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import usb
USB4ALL_VENDOR = 0x04d8
USB4ALL_PRODUCT = 0x000c
USB4ALL_CONFIGURATION = 1
USB4ALL_INTERFACE = 0
ADMIN_MODULE_IN_ENDPOINT = 0x01
ADMIN_MODULE_OUT_ENDPOINT = 0x81
READ_HEADER_SIZE = 3
TIMEOUT = 250
ERROR = -1
class usb_device():
def __init__(self, dev):
self.dev = dev
self.debug = False
def _debug(self, message, err=''):
if self.debug:
print message, err
def open_device(self):
"""
Open the baseboard, configure the interface
"""
try:
if self.dev.is_kernel_driver_active(USB4ALL_INTERFACE):
self.dev.detach_kernel_driver(USB4ALL_INTERFACE)
self.dev.set_configuration(USB4ALL_CONFIGURATION)
except usb.USBError, err:
self._debug('ERROR:com_usb:open_device', err)
raise
def close_device(self):
"""
Close the comunication with the baseboard
"""
self.dev = None
def read(self, size):
"""
Read from the device length bytes
"""
try:
return self.dev.read(ADMIN_MODULE_OUT_ENDPOINT, size, USB4ALL_INTERFACE, TIMEOUT)
except Exception, err:
self._debug('ERROR:com_usb:read', err)
raise
def write(self, data):
"""
Write in the device: data
"""
try:
return self.dev.write(ADMIN_MODULE_IN_ENDPOINT, data, USB4ALL_INTERFACE, TIMEOUT)
except Exception, err:
self._debug('ERROR:com_usb:write', err)
raise
def get_address(self):
"""
Get unique address for the usb
"""
if self.dev is not None:
return self.dev.address
else:
return None
def get_info(self):
"""
Get the device info such as manufacturer, etc
"""
try:
names = usb.util.get_string(self.dev, 255, 1, None).encode('ascii')
copy = usb.util.get_string(self.dev, 255, 2, None).encode('ascii')
sn = usb.util.get_string(self.dev, 255, 3, None).encode('ascii')
return [names, copy, sn]
except Exception, err:
self._debug('ERROR:com_usb:get_info', err)
raise
def find():
"""
List all busses and returns a list of baseboards detected
"""
l = []
for b in usb.core.find(find_all=True, idVendor=USB4ALL_VENDOR, idProduct=USB4ALL_PRODUCT):
l.append(usb_device(b))
return l
|