Esempio n. 1
0
        internal void ConfigureANT()
        {
            Console.WriteLine("Resetting module 0 ...");
            device0.ResetSystem();
            System.Threading.Thread.Sleep(500);

            Console.WriteLine("Setting network key...");
            if (device0.setNetworkKey(USER_NETWORK_NUM, USER_NETWORK_KEY, 500))
            {
                Console.WriteLine("Network key set");
            }
            else
            {
                throw new Exception("Error configuring network key");
            }

            Console.WriteLine("Setting Channel ID...");
            channel0.setChannelSearchTimeout((byte)100, 100);
            if (channel0.setChannelID(0, false, 40, 0, 8192))
            {
                Console.WriteLine("Channel ID set");
            }
            else
            {
                Console.WriteLine("Error configuring Channel ID");
            }

            radarEquipmentDisplay = new BikeRadarDisplay(channel0, networkAntPlus);

            radarEquipmentDisplay.DataPageReceived          += radarEquipment_DataPageReceived;
            radarEquipmentDisplay.RadarSensorFound          += radarEquipment_Found;
            radarEquipmentDisplay.RadarTargetsAPageReceived += radarEquipment_RadarTargetsAPageReceived;
            radarEquipmentDisplay.RadarTargetsBPageReceived += radarEquipment_RadarTargetsBPageReceived;
            radarEquipmentDisplay.TurnOn();
        }
Esempio n. 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;
    }
Esempio n. 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;
    }
Esempio n. 4
0
        void ChannelDataResponse(ANT_Response response)
        {
            if (response.isExtended())                              // Check if we are dealing with an extended message
            {
                ANT_ChannelID chID = response.getDeviceIDfromExt(); // Channel ID of the device we just received a message from
                var           pl   = response.getDataPayload();

                if (POWER_DEVICE_TYPE == HRM_DEVICE_TYPE && chID.deviceTypeID == HRM_DEVICE_TYPE) // to debug
                {
                    byte hr = pl[7];

                    // making mock data
                    debug_rawAccumHeartRate += hr;
                    ushort rawAccumPower       = debug_rawAccumHeartRate;
                    byte   rawUpdateEventCount = lastRawUpdateEventCount;
                    rawUpdateEventCount++;

                    var t = ProcessRollOver(rawAccumPower, rawUpdateEventCount);

                    OnAccumPowerReceived(this, t.Item1, t.Item2);
                }
                else if (chID.deviceTypeID == POWER_DEVICE_TYPE)
                {
                    if (pl[0] == STANDARD_POWER_ONLY_DATA_PAGE)
                    {
                        byte rawUpdateEventCount = pl[STANDARD_POWER_ONLY_MSG_UPDATE_EVENT_COUNT_IDX];

                        if (rawUpdateEventCount != lastRawUpdateEventCount)
                        {
                            ushort rawAccumPower = pl[STANDARD_POWER_ONLY_MSG_ACCUMLATED_POWER_MSB_IDX];
                            rawAccumPower = (ushort)((rawAccumPower << 8) + pl[STANDARD_POWER_ONLY_MSG_ACCUMLATED_POWER_LSB_IDX]);
                            var t = ProcessRollOver(rawAccumPower, rawUpdateEventCount);
                            OnAccumPowerReceived(this, t.Item1, t.Item2);
                        }
                    }
                }
                if (!onceConnected)
                {
                    onceConnected = true;
                    channel.setChannelSearchTimeout(255); // infinite
                    OnSearchEnded(this, true, chID.deviceNumber);
                }
                Debug.Write("Ch ID(" + chID.deviceNumber.ToString() + "," + chID.deviceTypeID.ToString() + "," + chID.transmissionTypeID.ToString() + ") - ");
            }

            switch ((ANTMessageID)response.responseID)
            {
            case ANTMessageID.BROADCAST_DATA_0x4E:
            case ANTMessageID.EXT_BROADCAST_DATA_0x5D:
                Debug.Write("Rx:(" + response.antChannel.ToString() + "): ");
                break;

            case ANTMessageID.ACKNOWLEDGED_DATA_0x4F:
            case ANTMessageID.EXT_ACKNOWLEDGED_DATA_0x5E:
                Debug.Write("Acked Rx:(" + response.antChannel.ToString() + "): ");
                break;

            default:
                Debug.Write("Burst(" + response.getBurstSequenceNumber().ToString("X2") + ") Rx:(" + response.antChannel.ToString() + "): ");
                break;
            }

            Debug.WriteLine(BitConverter.ToString(response.getDataPayload()));
        }
Esempio n. 5
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.");
            }
        }