def test_default_channel_setup(self): hr = HeartRate(self.node) self.assertEqual(0x39, hr.channel.frequency) self.assertEqual(8070, hr.channel.period) self.assertEqual(30, hr.channel.searchTimeout) # TODO device is the wrong name. The ANT docs refer to this # structure as a channel ID pairing_device = Device(0, 0x78, 0) self.assertEqual(pairing_device.number, hr.channel.device.number) self.assertEqual(pairing_device.type, hr.channel.device.type) self.assertEqual(pairing_device.transmissionType, hr.channel.device.transmissionType) public_network = Network(key=NETWORK_KEY_ANT_PLUS, name='N:ANT+') self.assertEqual(public_network.key, hr.channel.assigned_network.key) self.assertEqual(public_network.name, hr.channel.assigned_network.name) self.assertEqual(public_network.number, hr.channel.assigned_network.number) self.assertEqual(CHANNEL_TYPE_TWOWAY_RECEIVE, hr.channel.assigned_channel_type) self.assertEqual(0, hr.channel.assigned_channel_number) self.assertEqual(True, hr.channel.open_called)
def __init__(self, search_timeout_sec=120): """ Create Ant+ node, network, and initialize all attributes """ self.search_timeout_sec = search_timeout_sec self.device = driver.USB2Driver() self.antnode = Node(self.device) self.network = Network(key=NETWORK_KEY_ANT_PLUS, name='N:ANT+') # Start search for sensors and register callbacks: self.device_power_meter = BicyclePower(self.antnode, self.network, callbacks={ 'onDevicePaired': self._on_device_found, 'onPowerData': self._on_power_data, 'onChannelClosed': self._on_channel_closed, 'onSearchTimeout': self._on_search_timeout }) self._power_meter_status = AntSensors.SensorStatus(fresh_time_s=2) self.device_heart_rate = HeartRate(self.antnode, self.network, callbacks={ 'onDevicePaired': self._on_device_found, 'onHeartRateData': self._on_heartrate_data, 'onChannelClosed': self._on_channel_closed, 'onSearchTimeout': self._on_search_timeout }) self._heart_rate_status = AntSensors.SensorStatus(fresh_time_s=2) self._reconnect = True # Heartrate fields self._heartrate_bpm = None self._rr_interval_ms = None self._hr_event_time_ms = None # Power meter fields self._instantaneous_power_watts = None self._cadence_rpm = None self._accumulated_power_watts = None self._power_event_count = None
def __init__(self, node, device_id=0, transmission_type=0, callback=None): """Open a channel for heart rate data Device pairing is performed by using a device_id and transmission_type of 0. Once a device has been identified for pairing, a new channel should be created for the identified device. """ self.node = node self.device_id = device_id self.transmission_type = transmission_type self.callback = callback self.lock = Lock() self._computed_heart_rate = None self._detected_device = None self._state = None if not self.node.running: raise Exception('Node must be running') if not self.node.networks: raise Exception('Node must have an available network') public_network = Network(key=NETWORK_KEY_ANT_PLUS, name='N:ANT+') self.node.setNetworkKey(NETWORK_NUMBER_PUBLIC, public_network) self.channel = self.node.getFreeChannel() # todo getFreeChannel() can fail self.channel.registerCallback(_EventHandler(self)) self.channel.assign(public_network, CHANNEL_TYPE_TWOWAY_RECEIVE) self.channel.setID(0x78, device_id, transmission_type) self.channel.frequency = 0x39 self.channel.period = 8070 self.channel.searchTimeout = 30 # note, this is not in seconds self.channel.open() self._state = STATE_SEARCHING
def setUp(self): self.event_machine = FakeEventMachine() self.node = FakeNode(self.event_machine) self.network = Network(key=NETWORK_KEY_ANT_PLUS, name='N:ANT+')
print("#########################################################") #-------------------------------------------------# # Initialization # #-------------------------------------------------# try: reset_USB_Device() except Exception as ex: print(ex) antnode = Node(driver.USB2Driver(log=LOG, debug=DEBUG, idProduct=0x1008)) try: antnode.start() network = Network(key=NETWORK_KEY_ANT_PLUS, name='N:ANT+') antnode.setNetworkKey(NETWORK_NUMBER_PUBLIC, network) myTrainer = bikeTrainer( antnode, network, { 'onDevicePaired': device_paired, 'onSearchTimeout': search_timed_out, 'onChannelClosed': channel_closed, 'onBikeTrainer': bike_Trainer }) # Unpaired, search: myTrainer.open() print('ANT started. Connecting to devices...') except ANTException as err: print(f'Could not start ANT.\n{err}') #######################################################################################
def __init__(self): os.system("usbreset 0fcf:1008") self.node = Node(driver.USB2Driver()) self.network = Network(key=NETWORK_KEY_ANT_PLUS, name='N:ANT+')
def main(): global mainloop idProduct = prepare_usb("ANTUSB") device = driver.USB2Driver(log=LOG, debug=DEBUG, idProduct=idProduct) antnode = Node(device) antnode.start() network = Network(key=NETWORK_KEY_ANT_PLUS, name='N:ANT+') antnode.setNetworkKey(NETWORK_NUMBER_PUBLIC, network) POWER_SENSOR_ID = id_from_serial(1) #POWER_SENSOR_ID = 12345 print("Start power meter ID %d" % POWER_SENSOR_ID) pm = PowerMeterTx(antnode, network, POWER_SENSOR_ID) def device_found(self, channelID): print("Detect %s" % str(channelID)) def updatePower(self, cadence, accPower, power): pm.update(cadence, accPower, power) def heartrate_data(computed_heartrate, event_time_ms, rr_interval_ms): if HR_DEBUG: if rr_interval_ms is not None: print( "Heart rate: %d, event time(ms): %d, rr interval (ms): %d" % (computed_heartrate, event_time_ms, rr_interval_ms)) else: print("Heart rate: %d" % (computed_heartrate, )) ble_hrm_update_hr(computed_heartrate) hr = HeartRate(antnode, network, callbacks={ 'onDevicePaired': device_found, 'onHeartRateData': heartrate_data }) fe = FitnessEquipment(antnode, network, callbacks={ 'onDevicePaired': device_found, 'onPowerUpdated': updatePower }) # Unpaired, search: hr.open() pm.open() fe.open() # Paired to a specific device: #fe.open(ChannelID(53708, 17, 37)) #hr.open(ChannelID(21840, 120 ,81)) print("++++++++++++++++++++++++++++++++++") monitor = None mainloop = GLib.MainLoop() ble_hrm_start() #while True: # try: # time.sleep(1) # except KeyboardInterrupt: # break try: mainloop.run() except KeyboardInterrupt: print("KeyboardInterrupt") ble_hrm_stop() print("+++++++++++++++++++---------------") fe.close() pm.close() hr.close() antnode.stop()