Beispiel #1
0
    ////////////////////////////////////////////////////////////////////////////////
    // ConfigureAnt
    //
    // You can find how to initialize devices on thisisant.com in the download documents section
    // ANT+ DEVICE PROFILES
    //
    ////////////////////////////////////////////////////////////////////////////////
    public void ConfigureAnt(ANT_ReferenceLibrary.ChannelType channelType, byte userChannel, ushort deviceNum, byte deviceType, byte transType, byte radioFreq, ushort channelPeriod, bool pairing, int USBNum)
    {
        this.channelType   = channelType;
        this.userChannel   = userChannel;
        this.deviceNum     = deviceNum;
        this.deviceType    = deviceType;
        this.transType     = transType;
        this.radioFreq     = radioFreq;
        this.channelPeriod = channelPeriod;
        this.pairing       = pairing;

        RXQueue                  = new Queue <byte[]>(16);
        messageQueue             = new Queue <ANT_Response>(16);
        device                   = AntManager.Instance.devices[USBNum];
        channel                  = device.getChannel(userChannel);
        channel.channelResponse += new dChannelResponseHandler(ChannelResponse);
        channel.assignChannel(channelType, 0, 0);
        channel.setChannelID(deviceNum, pairing, deviceType, transType, 0);
        channel.setChannelFreq(radioFreq, 0);
        channel.setChannelPeriod(channelPeriod, 0);
        channel.setLowPrioritySearchTimeout(0);
        isBackgroundScan = false;
        channel.openChannel();

        broadcasting = true;
    }
Beispiel #2
0
    public void ConfigureScan(byte userChannel)
    {
        this.userChannel = userChannel;
        RXQueue          = new Queue <byte[]>(16);
        messageQueue     = new Queue <ANT_Response>(16);
        device           = AntManager.Instance.device;
        device.enableRxExtendedMessages(true, 500);
        channel = device.getChannel(userChannel);
        channel.channelResponse += new dChannelResponseHandler(ChannelResponse);

        channel.assignChannelExt(ANT_ReferenceLibrary.ChannelType.ADV_TxRx_Only_or_RxAlwaysWildCard_0x40, 0, ANT_ReferenceLibrary.ChannelTypeExtended.ADV_AlwaysSearch_0x01, 500);
        channel.setChannelID(0, false, 0, 0, 500);
        channel.setChannelFreq(57, 500);
        channel.setChannelSearchTimeout(0);
        channel.setLowPrioritySearchTimeout((byte)0xFF);
        isBackgroundScan = true;
        channel.openChannel();

        broadcasting = true;
    }
Beispiel #3
0
    public void ConfigureContinuousScan(ANT_ReferenceLibrary.ChannelType channelType, byte radioFreq, ushort USBNum)
    {
        userChannel  = 0;
        RXQueue      = new Queue <byte[]>(16);
        messageQueue = new Queue <ANT_Response>(16);
        device       = AntManager.Instance.devices[USBNum];
        device.enableRxExtendedMessages(true, 500);
        channel = device.getChannel(0);
        channel.channelResponse += new dChannelResponseHandler(ChannelResponse);

        channel.assignChannelExt(ANT_ReferenceLibrary.ChannelType.ADV_TxRx_Only_or_RxAlwaysWildCard_0x40, 0, ANT_ReferenceLibrary.ChannelTypeExtended.ADV_AlwaysSearch_0x01, 500);
        channel.setChannelID(0, false, 0, 0, 500);
        channel.setChannelFreq(radioFreq, 500);
        channel.setChannelSearchTimeout(0);
        channel.setLowPrioritySearchTimeout((byte)0xFF);
        isBackgroundScan = true;
        channel.openChannel();
        device.openRxScanMode();
        broadcasting = true;
    }
Beispiel #4
0
        void startNextSearch()
        {
            if (searchChannel != null || antStick == null)  //Check if device is present and the channel is valid
            {
                //Ensure we are still connected
                try
                {
                    searchChannel.requestStatus(1000); //Check if we get an exception...means we are disconnected, otherwise continue
                }
                catch (Exception)
                {
                    try
                    {
                        //We get to this code almost always because the device is dead, so try to restart it
                        ANT_Device.shutdownDeviceInstance(ref antStick);
                        searchChannel = null;
                        foreach (AntPlus_Connection i in deviceList)
                        {
                            i.connectedChannel = null;
                        }

                        findUsableAntDevice();
                        //Now fall through and attempt to restart search
                    }
                    catch (Exception)
                    {
                        //System.Windows.MessageBox.Show("Opening Device Failed. Try removing then re-inserting the stick, then try again.");
                        return;
                    }
                }
            }   //end check if device and search channel (if there is one) are valid

            //Check if we still need to search or have all the equipment already
            List <int> usedChannels = new List <int>();

            foreach (AntPlus_Connection i in deviceList)
            {
                switch (i.getConnStatus())
                {
                case AntPlus_Connection.ConnState.Closed:
                case AntPlus_Connection.ConnState.Searching:
                    i.setConnStatus(AntPlus_Connection.ConnState.InSrchQueue);
                    break;

                case AntPlus_Connection.ConnState.InSrchQueue:
                    break;

                case AntPlus_Connection.ConnState.Connected:
                case AntPlus_Connection.ConnState.DrpdToSrch:
                    usedChannels.Add(i.connectedChannel.getChannelNum());
                    break;
                }
            }

            if (usedChannels.Count == deviceList.Count)
            {
                return;     //we have all the equipment already
            }
            //Get new search channel if neccesary
            if (searchChannel == null)
            {
                if (usedChannels.Count >= numChannelsForDevices)
                {
                    return;     //no free channels
                }
                //Find the first free channel and start the search
                for (int i = 0; i < numChannelsForDevices; ++i)
                {
                    if (!usedChannels.Contains(i))
                    {
                        searchChannel = antStick.getChannel(i);
                        searchChannel.channelResponse += new dChannelResponseHandler(antChannel_channelResponse_FeSearch);
                        break;
                    }
                }
            }

            //Search for a search period for given device parameters
            //Find the next device to search for

            while (true)  //We know there is at least one device we need to search for, because of the check above, so this will never loop infinitely
            {
                ++searchingDeviceIndex;
                if (searchingDeviceIndex >= deviceList.Count)
                {
                    searchingDeviceIndex = 0;
                }
                if (deviceList[searchingDeviceIndex].connectedChannel == null)
                {
                    break;
                }
            }

            //Now set the channel parameters to start the next search
            try
            {
                if (searchChannel == null)
                {
                    throw new ApplicationException("Couldn't allocate a channel for search");
                }

                ds_AntPlus.AntChannelProfile srch = deviceList[searchingDeviceIndex].dataSource.searchProfile;
                deviceList[searchingDeviceIndex].setConnStatus(AntPlus_Connection.ConnState.Searching);

                if (!searchChannel.assignChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00, 0, 500))
                {
                    //Usually because the channel is in wrong state
                    searchChannel.closeChannel(500);
                    searchChannel.unassignChannel(500);
                    if (!searchChannel.assignChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00, 0, 500))
                    {
                        throw new ApplicationException("Failed to assign channel");
                    }
                }

                //Handle setting the search timeout
                byte timeout = 4; //default 4*2.5=10 seconds for each device
                if (deviceList.Count - usedChannels.Count == 1)
                {
                    timeout = 255;  //search forever if we only have one device to find; If one of the other devices resets it will startNextSearch again so we won't get stuck
                }
                if (!searchChannel.setLowPrioritySearchTimeout(timeout, 500))
                {
                    throw new ApplicationException("Failed to set low-pri search timeout");
                }

                if (!searchChannel.setChannelSearchTimeout(0, 500))
                {
                    throw new ApplicationException("Failed to set search timeout");
                }

                if (!searchChannel.setChannelFreq(srch.rfOffset, 500))
                {
                    throw new ApplicationException("Failed to set channel frequency");
                }

                if (!searchChannel.setChannelPeriod(srch.messagePeriod, 500))
                {
                    throw new ApplicationException("Failed to set channel period");
                }

                if (!searchChannel.setChannelID(srch.deviceNumber, srch.pairingEnabled, srch.deviceType, srch.transType, 500))
                {
                    throw new ApplicationException("Failed to set channel ID");
                }

                if (!searchChannel.openChannel(500))
                {
                    throw new ApplicationException("Failed to open channel");
                }
            }
            catch (Exception ex)
            {
                //System.Windows.MessageBox.Show("Search Channel Open Failed: " + ex.Message +". If you still need to connect other fitness equipment, you may need to restart the application.");
            }
        }