-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusb_utils.py
72 lines (56 loc) · 1.72 KB
/
usb_utils.py
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
# -*-: coding utf-8 -*-
""" USB utilities. """
from __future__ import absolute_import
import os
import re
import subprocess
import usb.core
import usb.util
class USB:
class Device:
unknown, respeaker, conexant = range(3)
@staticmethod
def get_boards():
try:
all_devices = usb.core.find(find_all=True)
except Exception as e:
if str(e) != "No backend available":
raise
# When no USB port:
return USB.Device.unknown
if not all_devices:
return USB.Device.unknown
for board in all_devices:
try:
devices = board.product.lower()
if devices.find("respeaker") >= 0:
return USB.Device.respeaker
elif devices.find("conexant") >= 0:
return USB.Device.conexant
except Exception as e:
continue
return USB.Device.unknown
@staticmethod
def get_usb_led_device():
devices = USB.lsusb()
if not devices:
return None
devices = devices.lower()
if devices.find("respeaker") >= 0:
return USB.Device.respeaker
elif devices.find("conexant") >= 0:
# TODO: check if this is the string to look for
return USB.Device.conexant
return USB.Device.unknown
@staticmethod
def lsusb():
FNULL = open(os.devnull, 'w')
try:
# Raspberry
return subprocess.check_output(["lsusb"])
except:
try:
# OSX
return subprocess.check_output(["system_profiler", "SPUSBDataType"])
except:
return None