Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def object_from_channel(channel, printer=noprint):
"""
Inits an object from a given channel.
This queries the endpoint 0 on that channel to gain information
about the interface, which is then used to init the corresponding object.
"""
printer("Connecting to device on " + channel._name)
try:
json_bytes = channel.remote_endpoint_read_buffer(0)
except (odrive.protocol.TimeoutException, odrive.protocol.ChannelBrokenException):
raise odrive.protocol.DeviceInitException("no response - probably incompatible")
json_crc16 = odrive.protocol.calc_crc16(odrive.protocol.PROTOCOL_VERSION, json_bytes)
channel._interface_definition_crc = json_crc16
try:
json_string = json_bytes.decode("ascii")
except UnicodeDecodeError:
raise odrive.protocol.DeviceInitException("device responded on endpoint 0 with something that is not ASCII")
printer("JSON: " + json_string)
try:
json_data = json.loads(json_string)
except json.decoder.JSONDecodeError:
raise odrive.protocol.DeviceInitException("device responded on endpoint 0 with something that is not JSON")
json_data = {"name": "odrive", "members": json_data}
return create_object("odrive", json_data, None, channel, printer=printer)
def find_all(consider_usb=True, consider_serial=False, printer=noprint):
"""
Returns a generator with all the connected devices that speak the ODrive protocol
"""
channels = iter(())
if (consider_usb):
channels = itertools.chain(channels, find_usb_channels(printer=printer))
if (consider_serial):
channels = itertools.chain(channels, find_serial_channels(printer=printer))
for channel in channels:
# TODO: blacklist known bad channels
try:
yield object_from_channel(channel, printer)
except odrive.protocol.DeviceInitException as ex:
printer(str(ex))
continue
def object_from_channel(channel, printer=noprint):
"""
Inits an object from a given channel.
This queries the endpoint 0 on that channel to gain information
about the interface, which is then used to init the corresponding object.
"""
printer("Connecting to device on " + channel._name)
try:
json_bytes = channel.remote_endpoint_read_buffer(0)
except (odrive.protocol.TimeoutException, odrive.protocol.ChannelBrokenException):
raise odrive.protocol.DeviceInitException("no response - probably incompatible")
json_crc16 = odrive.protocol.calc_crc16(odrive.protocol.PROTOCOL_VERSION, json_bytes)
channel._interface_definition_crc = json_crc16
try:
json_string = json_bytes.decode("ascii")
except UnicodeDecodeError:
raise odrive.protocol.DeviceInitException("device responded on endpoint 0 with something that is not ASCII")
printer("JSON: " + json_string)
try:
json_data = json.loads(json_string)
except json.decoder.JSONDecodeError:
raise odrive.protocol.DeviceInitException("device responded on endpoint 0 with something that is not JSON")
json_data = {"name": "odrive", "members": json_data}
return create_object("odrive", json_data, None, channel, printer=printer)