Skip to content

Commit

Permalink
♻️rubify code
Browse files Browse the repository at this point in the history
  • Loading branch information
eirvandelden committed Mar 5, 2024
1 parent ce5bb65 commit 2dbc14d
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions libcombustd/communication/ambx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,28 @@ def self.connect
# Open the device if it has been connected before.
# If the device hasn't been opened yet, try to open it otherwise fail
def self.open
if @device.nil?
if !Ambx.connect
return false
end
end
return false if @device.nil? && !Ambx.connect

unless @device.nil?
@handle = @device.open
# we retry a few times to open the device or kill it
unless @handle.nil?
retries = 0
@handle = @device.open
# we retry a few times to open the device or kill it
unless @handle.nil?
retries = 0
begin
begin
begin
error_code = @handle.claim_interface(0)
rescue ArgumentError
end

raise CannotClaimInterfaceError if error_code.nil? # TODO: libusb doesn't return anything on error
return true
rescue CannotClaimInterfaceError
@handle.auto_detach_kernel_driver = true
retries += 1
retry
else
return false
error_code = @handle.claim_interface(0)
rescue ArgumentError
end

raise CannotClaimInterfaceError if error_code.nil? # TODO: libusb doesn't return anything on error
return true
rescue CannotClaimInterfaceError
@handle.auto_detach_kernel_driver = true
retries += 1
retry
else
false
end
end

false
end

# Close the device if it is open.
Expand Down

0 comments on commit 2dbc14d

Please sign in to comment.