7 See documentation for brainstem here:
8 https://acroname.com/reference/python/index.html
14 if __name__ ==
'__main__':
15 import os, sys, getopt
17 ourname = os.path.basename( sys.argv[0] )
18 print(
'Syntax: acroname [options]' )
19 print(
' Control the acroname USB hub' )
21 print(
' --enable Enable all ports' )
22 print(
' --recycle Recycle all ports' )
25 opts,args = getopt.getopt( sys.argv[1:],
'',
26 longopts = [
'help',
'recycle',
'enable' ])
27 except getopt.GetoptError
as err:
37 except ModuleNotFoundError:
38 log.w(
'No acroname library is available!' )
48 super().
__init__( self, message
or 'no Acroname module found' )
53 Return all Acroname module specs in a list. Raise NoneFoundError if one is not found!
56 log.d(
'discovering Acroname modules ...' )
60 specs = brainstem.discover.findAllModules( brainstem.link.Spec.USB )
73 Connect to the hub. Raises RuntimeError on failure
78 hub = brainstem.stem.USBHub3p()
86 result = hub.connectFromSpec( spec )
87 if result != brainstem.result.Result.NO_ERROR:
88 raise RuntimeError(
"failed to connect to acroname (result={})".format( result ))
90 log.d(
'connected to', spec )
95 Yields all hub port numbers
97 from rspy
import lsusb
102 hubs = set( lsusb.devices_by_vendor(
'24ff' ))
105 for dev,dev_port
in lsusb.tree():
110 if dev_port.startswith( port +
'.' ):
113 ports.add( dev_port )
134 :return: a list of all possible ports, even if currently unoccupied or disabled
141 :return: a list of all ports currently occupied (and enabled)
146 occupied_ports.append( port )
147 return occupied_ports
155 if port < 0
or port > 7:
156 raise ValueError(
"port number must be [0-7]" )
159 status = hub.usb.getPortState( port )
161 if status.value == 0:
163 if status.value == 11:
164 return "Disconnected"
165 if status.value > 100:
167 return "Unknown Error ({})".format( status.value )
170 def enable_ports( ports = None, disable_other_ports = False, sleep_on_change = 0 ):
172 Set enable state to provided ports
173 :param ports: List of port numbers; if not provided, enable all ports
174 :param disable_other_ports: if True, the ports not in the list will be disabled
175 :param sleep_on_change: Number of seconds to sleep if any change is made
176 :return: True if no errors found, False otherwise
183 if ports
is None or port
in ports:
185 action_result = hub.usb.setPortEnable( port )
186 if action_result != brainstem.result.Result.NO_ERROR:
191 elif disable_other_ports:
193 action_result = hub.usb.setPortDisable( port )
194 if action_result != brainstem.result.Result.NO_ERROR:
199 if changed
and sleep_on_change:
201 time.sleep( sleep_on_change )
208 :param ports: List of port numbers
209 :return: True if no errors found, False otherwise
215 action_result = hub.usb.setPortDisable( port )
216 if action_result != brainstem.result.Result.NO_ERROR:
224 Disable and enable a port
225 :param timeout: how long to wait before re-enabling
226 :return: True if everything OK, False otherwise
234 time.sleep( timeout )
243 Set USB ports to USB2
251 for port
in portlist:
252 hub.usb.setSuperSpeedDataEnable( port )
253 hub.usb.setHiSpeedDataEnable( port )
254 hub.usb.setSuperSpeedDataDisable( port )
259 Set USB ports to support USB3
267 for port
in portlist:
268 hub.usb.setSuperSpeedDataEnable( port )
269 hub.usb.setHiSpeedDataEnable( port )
270 hub.usb.setHiSpeedDataDisable( port )
276 if port < 0
or port > 7:
277 raise ValueError(
"port number can be only within 0 and 7 (inclusive)")
280 micro_volt = hub.usb.getPortVoltage( port )
281 micro_curr = hub.usb.getPortCurrent( port )
282 volt = float(micro_volt.value) / 10.0 ** 6
283 amps = float(micro_curr.value) / 10.0 ** 6
290 Based on last two USB location index, provide the port number
292 acroname_port_usb_map = {(4, 4): 0,
301 return acroname_port_usb_map[(first_usb_index, second_usb_index)]
304 if __name__ ==
'__main__':
306 if opt
in (
'--enable'):
309 elif opt
in (
'--recycle'):