2020
2121DIR_IN = 0x80
2222
23+
2324class MIDI :
25+ """
26+ Stream-like MIDI device for use with `adafruit_midi` and similar upstream
27+ MIDI parser libraries.
28+
29+ :param device: a ``usb.core.Device` object which implements
30+ ``read(endpoint, buffer)`` and ``write(endpoint,buffer)``
31+ :param float timeout: timeout in seconds to wait for read or write operation
32+ to succeeds. Default to None, i.e. reads and writes will block.
33+ """
34+
2435 def __init__ (self , device , timeout = None ):
2536 self .interface_number = 0
2637 self .in_ep = 0
@@ -42,14 +53,16 @@ def __init__(self, device, timeout=None):
4253 descriptor_len = config_descriptor [i ]
4354 descriptor_type = config_descriptor [i + 1 ]
4455 if descriptor_type == adafruit_usb_host_descriptors .DESC_CONFIGURATION :
56+ # pylint: disable=unused-variable
4557 config_value = config_descriptor [i + 5 ]
58+ # pylint: enable=unused-variable
4659 elif descriptor_type == adafruit_usb_host_descriptors .DESC_INTERFACE :
4760 interface_number = config_descriptor [i + 2 ]
4861 interface_class = config_descriptor [i + 5 ]
4962 interface_subclass = config_descriptor [i + 6 ]
5063 midi_interface = interface_class == 0x1 and interface_subclass == 0x3
5164 if midi_interface :
52- self .interface_number = interface_number
65+ self .interface_number = interface_number
5366
5467 elif descriptor_type == adafruit_usb_host_descriptors .DESC_ENDPOINT :
5568 endpoint_address = config_descriptor [i + 2 ]
@@ -65,20 +78,39 @@ def __init__(self, device, timeout=None):
6578 device .detach_kernel_driver (self .interface_number )
6679
6780 def read (self , size ):
81+ """
82+ Read bytes. If ``nbytes`` is specified then read at most that many
83+ bytes. Otherwise, read everything that arrives until the connection
84+ times out. Providing the number of bytes expected is highly recommended
85+ because it will be faster. If no bytes are read, return ``None``.
86+
87+ .. note:: When no bytes are read due to a timeout, this function returns ``None``.
88+ This matches the behavior of `io.RawIOBase.read` in Python 3, but
89+ differs from pyserial which returns ``b''`` in that situation.
90+
91+ :return: Data read
92+ :rtype: bytes or None
93+ """
94+
6895 if self ._remaining == 0 :
6996 try :
7097 n = self .device .read (self .in_ep , self .buf , self .timeout_ms )
7198 self ._remaining = n - 1
7299 self .start = 1
73- except usb .core .USBTimeoutError as e :
100+ except usb .core .USBTimeoutError :
74101 pass
75102 size = min (size , self ._remaining )
76- b = self .buf [self .start : self .start + size ]
103+ b = self .buf [self .start : self .start + size ]
77104 self .start += size
78105 self ._remaining -= size
79106 return b
80107
81108 def readinto (self , buf ):
109+ """Read bytes into the ``buf``. Read at most ``len(buf)`` bytes.
110+
111+ :return: number of bytes read and stored into ``buf``
112+ :rtype: int or None (on a non-blocking error)
113+ """
82114 b = self .read (len (buf ))
83115 n = len (b )
84116 if n :
@@ -87,4 +119,9 @@ def readinto(self, buf):
87119
88120 def __repr__ (self ):
89121 # also idProduct/idVendor for vid/pid
90- return "MIDI Device " + str (self .device .manufacturer ) + "/" + str (self .device .product )
122+ return (
123+ "MIDI Device "
124+ + str (self .device .manufacturer )
125+ + "/"
126+ + str (self .device .product )
127+ )
0 commit comments