Skip to content

Commit

Permalink
Made libusb base core reset on open, making communication much more r…
Browse files Browse the repository at this point in the history
…eliable

Fixes openyou#8
  • Loading branch information
qdot committed Aug 8, 2011
1 parent b1b09cb commit ee607b2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
5 changes: 0 additions & 5 deletions python/antprotocol/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ def open(self, vid = None, pid = None):
self.init()
return True

def reset_connection(self):
super(FitBitANT, self).reset_connection()
self._connection.set_configuration()
self._receive()

def init(self):
# Device setup
# bmRequestType, bmRequest, wValue, wIndex, data
Expand Down
12 changes: 11 additions & 1 deletion python/antprotocol/libusb.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,18 @@ def open(self, vid = None, pid = None):
idProduct = pid)
if self._connection is None:
return False


# For some reason, we have to set config, THEN reset,
# otherwise we segfault back in the ctypes (on linux, at
# least).
self._connection.set_configuration()
self._connection.reset()
# The we have to set our configuration again
self._connection.set_configuration()

# Then we should get back a reset check, with 0x80
# (SUSPEND_RESET) as our status
self._check_reset_response(0x80)
return True

def close(self):
Expand Down
10 changes: 6 additions & 4 deletions python/antprotocol/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ def _event_to_string(self, event):
except:
return "%02x" % event

def _check_reset_response(self):
def _check_reset_response(self, status):
data = self._receive()

# Expect a startup message return
if data[2] == 0x6f:
if data[2] == 0x6f and data[3] == status:
return
raise ANTStatusException("Reset expects message type 0x6f, got %02x" % (data[2]))
raise ANTStatusException("Reset expects message type 0x6f status 0x%02x, got type 0x%02x status 0x%02x" % (status, data[2], data[3]))

def _check_ok_response(self):
# response packets will always be 7 bytes
Expand All @@ -148,7 +148,9 @@ def reset(self):
# According to protocol docs, the system will take a maximum
# of .5 seconds to restart
time.sleep(.6)
self._check_reset_response()
# This is a requested reset, so we expect back 0x20
# (COMMAND_RESET)
self._check_reset_response(0x20)

def set_channel_frequency(self, freq):
self._send_message(0x45, self._chan, freq)
Expand Down

0 comments on commit ee607b2

Please sign in to comment.