Skip to content

Commit

Permalink
Removed tracker class
Browse files Browse the repository at this point in the history
Moved fitbit specific ant functions into Fitbit class with tracker functions
Made base a member of fitbit, which currently needs to be passed in to Fitbit class
  • Loading branch information
Kyle Machulis committed May 26, 2011
1 parent ceeb395 commit bf4cffd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
6 changes: 6 additions & 0 deletions python/antprotocol/bases.py
Expand Up @@ -10,6 +10,7 @@ class GarminANT(ANTlibusb):
VID = 0x0fcf
PID = 0x1008


class FitBitANT(ANTlibusb):
"""Class that represents the fitbit base. Due to the extra
hardware to handle tracker connection and charging, has an extra
Expand All @@ -29,6 +30,11 @@ def open(self, vid = None, pid = None):
return False
self.init()
return True

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

def init(self):
# Device setup
Expand Down
11 changes: 2 additions & 9 deletions python/antprotocol/libusb.py
Expand Up @@ -55,15 +55,12 @@ def __init__(self, chan=0x0, debug=False):
self._connection = False
self.timeout = 1000

def open(self, vid = None, pid = None):
if vid is None:
vid = self.VID
if pid is None:
pid = self.PID
def open(self, vid, pid):
self._connection = usb.core.find(idVendor = vid,
idProduct = pid)
if self._connection is None:
return False

self._connection.set_configuration()
return True

Expand All @@ -86,7 +83,3 @@ def _receive(self, size=4096):
if self._debug:
self.data_received(''.join(map(chr, r)))
return r

def reset_connection(self):
self._connection.reset()
# self._connection.set_configuration()
23 changes: 11 additions & 12 deletions python/fitbit.py
Expand Up @@ -67,8 +67,8 @@
# - Implementing data clearing

import itertools, sys, random, operator, datetime
from antprotocol.bases import FitBitANT
from antprotocol.libusb import ANTlibusb
from antprotocol.bases import FitBitANT, GarminANT
from antprotocol.protocol import ANTReceiveException

class FitBit(object):
"""Class to represent the fitbit tracker device, the portion of
Expand Down Expand Up @@ -164,7 +164,6 @@ def init_device_channel(self, channel):
self.base.open_channel()

def init_tracker_for_transfer(self):
self.base.reset_connection()
self.init_fitbit()
self.wait_for_beacon()
self.reset_tracker()
Expand All @@ -174,7 +173,7 @@ def init_tracker_for_transfer(self):
cid = [random.randint(0,254), random.randint(0,254)]
self.base.send_acknowledged_data([0x78, 0x02] + cid + [0x00, 0x00, 0x00, 0x00])
self.base.close_channel()
self.base.init_device_channel(cid + [0x01, 0x01])
self.init_device_channel(cid + [0x01, 0x01])
self.wait_for_beacon()
self.ping_tracker()

Expand Down Expand Up @@ -204,8 +203,8 @@ def _get_tracker_burst(self):
def run_opcode(self, opcode, payload = None):
self.send_tracker_packet(opcode)
data = self.base.receive_acknowledged_reply()
if data[0] != self.tracker.current_packet_id:
raise Exception("Tracker Packet IDs don't match! %02x %02x" % (data[0], self.tracker.current_packet_id))
if data[0] != self.current_packet_id:
raise Exception("Tracker Packet IDs don't match! %02x %02x" % (data[0], self.current_packet_id))
if data[1] == 0x42:
return self.get_data_bank()
if data[1] == 0x61:
Expand Down Expand Up @@ -244,14 +243,14 @@ def get_tracker_info(self):
return data

def send_tracker_packet(self, packet):
p = [self.tracker.gen_packet_id()] + packet
p = [self.gen_packet_id()] + packet
self.base.send_acknowledged_data(p)

def ping_tracker(self):
self.base.send_acknowledged_data([0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])

def check_tracker_data_bank(self, index):
self.base.send_tracker_packet([0x60, 0x00, 0x02, index, 0x00, 0x00, 0x00])
self.send_tracker_packet([0x60, 0x00, 0x02, index, 0x00, 0x00, 0x00])
return self._get_tracker_burst()

def run_data_bank_opcode(self, index):
Expand All @@ -261,8 +260,8 @@ def get_data_bank(self):
data = []
while 1:
try:
bank = self.check_tracker_data_bank(self.tracker.current_bank_id)
self.tracker.current_bank_id += 1
bank = self.check_tracker_data_bank(self.current_bank_id)
self.current_bank_id += 1
except ANTReceiveException:
continue
if len(bank) == 0:
Expand Down Expand Up @@ -312,7 +311,7 @@ def parse_bank1_data(self, data):
print "Time: %s Daily Steps: %d" % (record_date, daily_steps)

def main():
base = FitBitANT(True)
base = GarminANT(True)
if not base.open():
print "No devices connected!"
return 1
Expand Down Expand Up @@ -343,7 +342,7 @@ def main():

# for i in range(0, len(d), 14):
# print ["%02x" % x for x in d[i:i+14]]
device.close()
base.close()
return 0

if __name__ == '__main__':
Expand Down

0 comments on commit bf4cffd

Please sign in to comment.