Example #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)
    {
        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.device;

        channel = device.getChannel(userChannel);
        channel.channelResponse += new dChannelResponseHandler(ChannelResponse);
        channel.assignChannel(channelType, 0, 500);
        channel.setChannelID(deviceNum, pairing, deviceType, transType, 500);
        channel.setChannelFreq(radioFreq, 500);
        channel.setChannelPeriod(channelPeriod, 500);
        isBackgroundScan = false;
        channel.openChannel();
        broadcasting = true;
    }
Example #2
0
    public AntChannel OpenChannel(ANT_ReferenceLibrary.ChannelType channelType, byte userChannel, ushort deviceNum, byte deviceType, byte transType, byte radioFreq, ushort channelPeriod, bool pairing)
    {
        AntChannel channel = this.gameObject.AddComponent <AntChannel>();

        channelList.Add(channel);
        channel.ConfigureAnt(channelType, userChannel, deviceNum, deviceType, transType, radioFreq, channelPeriod, pairing);
        return(channel);
    }
Example #3
0
        /// <summary>
        /// Start ANT+ driver and open ports for connection.
        /// </summary>
        public static void Start()
        {
            // Set to receive mode
            channelType = ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00;

            try
            {
                ConfigureANT();
            }
            catch (Exception ex)
            {
                throw new Exception("Couldn't configure ANT+: " + ex.Message + Environment.NewLine);
            }
        }
Example #4
0
File: ANT.cs Project: HubWhale/Bike
 public static void Tramp_Start()
 {
     ANT.bDone         = false;
     ANT.bDisplay      = true;
     ANT.bBroadcasting = false;
     ANT.channelType   = ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00;
     try
     {
         ANT.Tramp_ConfigureANT();
     }
     catch (Exception ex)
     {
         throw new Exception("Demo failed: " + ex.Message + Environment.NewLine);
     }
 }
Example #5
0
        ////////////////////////////////////////////////////////////////////////////////
        // Start
        //
        // Start the ANT+ slave.
        //
        // ucChannelType_:  ANT Channel Type. 0 = Master, 1 = Slave
        //                  If not specified, 2 is passed in as invalid.
        ////////////////////////////////////////////////////////////////////////////////
        public static void Start(bool display = false)
        {
            Init();
            bDisplay = display;
            bBroadcasting = false;

            channelType = ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00;

            try
            {
                ConfigureANT();

                return;
            }
            catch (Exception ex)
            {
                throw new Exception("ANT+ setup failed: " + ex.Message + Environment.NewLine);
            }
        }
Example #6
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;
    }
Example #7
0
        ////////////////////////////////////////////////////////////////////////////////
        // Start
        //
        // Start the demo program.
        //
        // ucChannelType_:  ANT Channel Type. 0 = Master, 1 = Slave
        //                  If not specified, 2 is passed in as invalid.
        ////////////////////////////////////////////////////////////////////////////////
        static void Start(byte ucChannelType_)
        {
            byte ucChannelType = ucChannelType_;

            bDone         = false;
            bDisplay      = true;
            bBroadcasting = false;


            if (ucChannelType == 0)
            {
                channelType = ANT_ReferenceLibrary.ChannelType.BASE_Master_Transmit_0x10;
            }
            else if (ucChannelType == 1)
            {
                channelType = ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00;
            }
            try
            {
                ConfigureANT();

                while (!bDone)
                {
                    System.Threading.Thread.Sleep(1000);
                }
                // Clean up ANT
                Console.WriteLine("Disconnecting module...");
                ANT_Device.shutdownDeviceInstance(ref device0);  // Close down the device completely and completely shut down all communication
                Console.WriteLine("Demo has completed successfully!");
                return;
            }
            catch (Exception ex)
            {
                throw new Exception("Demo failed: " + ex.Message + Environment.NewLine);
            }
        }
Example #8
0
 /// <summary>
 /// Creates and fills the ChannelStatus
 /// </summary>
 /// <param name="BasicStatus"></param>
 /// <param name="networkNumber"></param>
 /// <param name="ChannelType"></param>
 public ANT_ChannelStatus(ANT_ReferenceLibrary.BasicChannelStatusCode BasicStatus, byte networkNumber, ANT_ReferenceLibrary.ChannelType ChannelType)
 {
     this.BasicStatus   = BasicStatus;
     this.networkNumber = networkNumber;
     this.ChannelType   = ChannelType;
 }
Example #9
0
        ////////////////////////////////////////////////////////////////////////////////
        // Start
        //
        // Start the demo program.
        //
        // ucChannelType_:  ANT Channel Type. 0 = Master, 1 = Slave
        //                  If not specified, 2 is passed in as invalid.
        ////////////////////////////////////////////////////////////////////////////////
        static void Start(byte ucChannelType_)
        {
            byte ucChannelType = ucChannelType_;

            bDone         = false;
            bDisplay      = true;
            bBroadcasting = false;

            PrintMenu();

            // If a channel type has not been set at the command line,
            // prompt the user to specify one now
            do
            {
                if (ucChannelType == CHANNEL_TYPE_INVALID)
                {
                    Console.WriteLine("Channel Type? (Master = 0, Slave = 1)");
                    try
                    {
                        ucChannelType = byte.Parse(Console.ReadLine());
                    }
                    catch (Exception)
                    {
                        ucChannelType = CHANNEL_TYPE_INVALID;
                    }
                }

                if (ucChannelType == 0)
                {
                    channelType = ANT_ReferenceLibrary.ChannelType.BASE_Master_Transmit_0x10;
                }
                else if (ucChannelType == 1)
                {
                    channelType = ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00;
                }
                else
                {
                    ucChannelType = CHANNEL_TYPE_INVALID;
                    Console.WriteLine("Error: Invalid channel type");
                }
            } while (ucChannelType == CHANNEL_TYPE_INVALID);

            try
            {
                ConfigureANT();

                while (!bDone)
                {
                    string command = Console.ReadLine();
                    switch (command)
                    {
                    case "M":
                    case "m":
                    {
                        PrintMenu();
                        break;
                    }

                    case "Q":
                    case "q":
                    {
                        // Quit
                        Console.WriteLine("Closing Channel");
                        bBroadcasting = false;
                        channel0.closeChannel();
                        break;
                    }

                    case "A":
                    case "a":
                    {
                        // Send Acknowledged Data
                        byte[] myTxBuffer = { 1, 2, 3, 4, 5, 6, 7, 8 };
                        channel0.sendAcknowledgedData(myTxBuffer);
                        break;
                    }

                    case "B":
                    case "b":
                    {
                        // Send Burst Data (10 packets)
                        byte[] myTxBuffer = new byte[8 * 10];
                        for (byte i = 0; i < 8 * 10; i++)
                        {
                            myTxBuffer[i] = i;
                        }
                        channel0.sendBurstTransfer(myTxBuffer);
                        break;
                    }

                    case "R":
                    case "r":
                    {
                        // Reset the system and start over the test
                        ConfigureANT();
                        break;
                    }

                    case "C":
                    case "c":
                    {
                        // Request capabilities
                        ANT_DeviceCapabilities devCapab = device0.getDeviceCapabilities(500);
                        Console.Write(devCapab.printCapabilities() + Environment.NewLine);
                        break;
                    }

                    case "V":
                    case "v":
                    {
                        // Request version
                        // As this is not available in all ANT parts, we should not wait for a response, so
                        // we do not specify a timeout
                        // The response - if available - will be processed in DeviceResponse
                        device0.requestMessage(ANT_ReferenceLibrary.RequestMessageID.VERSION_0x3E);
                        break;
                    }

                    case "S":
                    case "s":
                    {
                        // Request channel status
                        ANT_ChannelStatus chStatus = channel0.requestStatus(500);

                        string[] allStatus = { "STATUS_UNASSIGNED_CHANNEL",
                                               "STATUS_ASSIGNED_CHANNEL",
                                               "STATUS_SEARCHING_CHANNEL",
                                               "STATUS_TRACKING_CHANNEL" };
                        Console.WriteLine("STATUS: " + allStatus[(int)chStatus.BasicStatus]);
                        break;
                    }

                    case "I":
                    case "i":
                    {
                        // Request channel ID
                        ANT_Response respChID           = device0.requestMessageAndResponse(ANT_ReferenceLibrary.RequestMessageID.CHANNEL_ID_0x51, 500);
                        ushort       usDeviceNumber     = (ushort)((respChID.messageContents[2] << 8) + respChID.messageContents[1]);
                        byte         ucDeviceType       = respChID.messageContents[3];
                        byte         ucTransmissionType = respChID.messageContents[4];
                        Console.WriteLine("CHANNEL ID: (" + usDeviceNumber.ToString() + "," + ucDeviceType.ToString() + "," + ucTransmissionType.ToString() + ")");
                        break;
                    }

                    case "D":
                    case "d":
                    {
                        bDisplay = !bDisplay;
                        break;
                    }

                    case "U":
                    case "u":
                    {
                        // Print out information about the device we are connected to
                        Console.WriteLine("USB Device Description");

                        // Retrieve info
                        Console.WriteLine(String.Format("   VID: 0x{0:x}", device0.getDeviceUSBVID()));
                        Console.WriteLine(String.Format("   PID: 0x{0:x}", device0.getDeviceUSBPID()));
                        Console.WriteLine(String.Format("   Product Description: {0}", device0.getDeviceUSBInfo().printProductDescription()));
                        Console.WriteLine(String.Format("   Serial String: {0}", device0.getDeviceUSBInfo().printSerialString()));
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                    System.Threading.Thread.Sleep(0);
                }
                // Clean up ANT
                Console.WriteLine("Disconnecting module...");
                ANT_Device.shutdownDeviceInstance(ref device0);  // Close down the device completely and completely shut down all communication
                Console.WriteLine("Demo has completed successfully!");
                return;
            }
            catch (Exception ex)
            {
                throw new Exception("Demo failed: " + ex.Message + Environment.NewLine);
            }
        }
Example #10
0
 /// <summary>
 /// Creates and fills the ChannelStatus
 /// </summary>
 /// <param name="BasicStatus"></param>
 /// <param name="networkNumber"></param>
 /// <param name="ChannelType"></param>
 public ANT_ChannelStatus(ANT_ReferenceLibrary.BasicChannelStatusCode BasicStatus, byte networkNumber, ANT_ReferenceLibrary.ChannelType ChannelType)
 {
     this.BasicStatus = BasicStatus;
     this.networkNumber = networkNumber;
     this.ChannelType = ChannelType;
 }
        private void setupAndOpen(ANT_Device deviceToSetup, ANT_ReferenceLibrary.ChannelType channelType)
        {
            //We try-catch and forward exceptions to the calling function to handle and pass the errors to the user
            try
            {
                //To access an ANTChannel on a paticular device we need to get the channel from the device
                //Once again, this ensures you have a valid object associated with a real-world ANTChannel
                //ie: You can only get channels that actually exist
                ANT_Channel channel0 = deviceToSetup.getChannel(0);

                //Almost all functions in the library have two overloads, one with a response wait time and one without
                //If you give a wait time, you can check the return value for success or failure of the command, however
                //the wait time version is blocking. 500ms is usually a safe value to ensure you wait long enough for any response.
                //But with no wait time, the command is simply sent and you have to monitor the device response for success or failure.

                //To setup channels for communication there are three mandatory operations assign, setID, and Open
                //Various other settings such as message period and network key affect communication
                //between two channels as well, see the documentation for further details on these functions.

                //So, first we assign the channel, we have already been passed the channelType which is an enum that has various flags
                //If we were doing something more advanced we could use a bitwise or ie:base|adv1|adv2 here too
                //We also use net 0 which has the public network key by default
                if (channel0.assignChannel(channelType, 0, 500))
                {
                    threadSafePrintLine("Ch assigned to " + channelType + " on net 0.", textBox_Display);
                }
                else
                {
                    throw new Exception("Channel assignment operation failed.");
                }

                //Next we have to set the channel id. Slaves will only communicate with a master device that
                //has the same id unless one or more of the id parameters are set to a wild card 0. If wild cards are included
                //the slave will search until it finds a broadcast that matches all the non-wild card parameters in the id.
                //For now we pick an arbitrary id so that we can ensure we match between the two devices.
                //The pairing bit ensures on a search that you only pair with devices that also are requesting
                //pairing, but we don't need it here so we set it to false
                if (channel0.setChannelID(12345, false, 67, 89, 500))
                {
                    threadSafePrintLine("Set channel ID to 12345, 67, 89", textBox_Display);
                }
                else
                {
                    throw new Exception("Set Channel ID operation failed.");
                }

                //Setting the channel period isn't mandatory, but we set it slower than the default period so messages aren't coming so fast
                //The period parameter is divided by 32768 to set the period of a message in seconds. So here, 16384/32768 = 1/2 sec/msg = 2Hz
                if (channel0.setChannelPeriod(16384, 500))
                {
                    threadSafePrintLine("Message Period set to 16384/32768 seconds per message", textBox_Display);
                }

                //Now we open the channel
                if (channel0.openChannel(500))
                {
                    threadSafePrintLine("Opened Channel" + Environment.NewLine, textBox_Display);
                }
                else
                {
                    throw new Exception("Channel Open operation failed.");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Setup and Open Failed. " + ex.Message + Environment.NewLine);
            }
        }
Example #12
0
        ////////////////////////////////////////////////////////////////////////////////
        // Start
        //
        // Start the demo program.
        // 
        // ucChannelType_:  ANT Channel Type. 0 = Master, 1 = Slave
        //                  If not specified, 2 is passed in as invalid.
        ////////////////////////////////////////////////////////////////////////////////
        static void Start(byte ucChannelType_)
        {
            byte ucChannelType = ucChannelType_;
            bDone = false;
            bDisplay = true;
            bBroadcasting = false;

            PrintMenu();

            // If a channel type has not been set at the command line,
            // prompt the user to specify one now
            do
            {
                if (ucChannelType == CHANNEL_TYPE_INVALID)
                {
                    Console.WriteLine("Channel Type? (Master = 0, Slave = 1)");
                    try
                    {
                        ucChannelType = byte.Parse(Console.ReadLine());
                    }
                    catch (Exception)
                    {
                        ucChannelType = CHANNEL_TYPE_INVALID;
                    }
                }

                if (ucChannelType == 0)
                {
                    channelType = ANT_ReferenceLibrary.ChannelType.BASE_Master_Transmit_0x10;
                }
                else if (ucChannelType == 1)
                {
                    channelType = ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00;
                }
                else
                {
                    ucChannelType = CHANNEL_TYPE_INVALID;
                    Console.WriteLine("Error: Invalid channel type");
                }
            } while (ucChannelType == CHANNEL_TYPE_INVALID);

            try
            {
                ConfigureANT();

                while (!bDone)
                {
                    string command = Console.ReadLine();
                    switch (command)
                    {
                        case "M":
                        case "m":
                        {
                            PrintMenu();
                            break;
                        }
                        case "Q":
                        case "q":
                        {
                            // Quit
                            Console.WriteLine("Closing Channel");
                            bBroadcasting = false;
                            channel0.closeChannel();
                            break;
                        }
                        case "A":
                        case "a":
                        {
                            // Send Acknowledged Data
                            byte[] myTxBuffer = { 1, 2, 3, 4, 5, 6, 7, 8 };
                            channel0.sendAcknowledgedData(myTxBuffer);
                            break;
                        }
                        case "B":
                        case "b":
                        {
                            // Send Burst Data (10 packets)
                            byte[] myTxBuffer = new byte[8 * 10];
                            for (byte i = 0; i < 8 * 10; i++)
                                myTxBuffer[i] = i;
                            channel0.sendBurstTransfer(myTxBuffer);
                            break;
                        }

                        case "R":
                        case "r":
                        {
                            // Reset the system and start over the test
                            ConfigureANT();
                            break;
                        }

                        case "C":
                        case "c":
                        {
                            // Request capabilities
                            ANT_DeviceCapabilities devCapab = device0.getDeviceCapabilities(500);
                            Console.Write(devCapab.printCapabilities() + Environment.NewLine);
                            break;
                        }
                        case "V":
                        case "v":
                        {
                            // Request version
                            // As this is not available in all ANT parts, we should not wait for a response, so
                            // we do not specify a timeout
                            // The response - if available - will be processed in DeviceResponse
                            device0.requestMessage(ANT_ReferenceLibrary.RequestMessageID.VERSION_0x3E);                    
                            break;
                        }
                        case "S":
                        case "s":
                        {
                            // Request channel status
                            ANT_ChannelStatus chStatus = channel0.requestStatus(500);

                            string[] allStatus = { "STATUS_UNASSIGNED_CHANNEL",
                                                    "STATUS_ASSIGNED_CHANNEL",
                                                    "STATUS_SEARCHING_CHANNEL",
                                                    "STATUS_TRACKING_CHANNEL"};
                            Console.WriteLine("STATUS: " + allStatus[(int)chStatus.BasicStatus]);
                            break;
                        }
                        case "I":
                        case "i":
                        {
                            // Request channel ID
                            ANT_Response respChID = device0.requestMessageAndResponse(ANT_ReferenceLibrary.RequestMessageID.CHANNEL_ID_0x51, 500);
                            ushort usDeviceNumber = (ushort) ((respChID.messageContents[2] << 8) + respChID.messageContents[1]);
                            byte ucDeviceType = respChID.messageContents[3];
                            byte ucTransmissionType = respChID.messageContents[4];
                            Console.WriteLine("CHANNEL ID: (" + usDeviceNumber.ToString() + "," + ucDeviceType.ToString() + "," + ucTransmissionType.ToString() + ")");
                            break;
                        }
                        case "D":
                        case "d":
                        {
                            bDisplay = !bDisplay;
                            break;
                        }
			            case "U":
			            case "u":
		                {
				            // Print out information about the device we are connected to
				            Console.WriteLine("USB Device Description");
				            
                            // Retrieve info
                            Console.WriteLine(String.Format("   VID: 0x{0:x}", device0.getDeviceUSBVID()));
                            Console.WriteLine(String.Format("   PID: 0x{0:x}", device0.getDeviceUSBPID()));
                            Console.WriteLine(String.Format("   Product Description: {0}", device0.getDeviceUSBInfo().printProductDescription()));
                            Console.WriteLine(String.Format("   Serial String: {0}", device0.getDeviceUSBInfo().printSerialString()));	
				            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    System.Threading.Thread.Sleep(0);
                }
                // Clean up ANT
                Console.WriteLine("Disconnecting module...");
                ANT_Device.shutdownDeviceInstance(ref device0);  // Close down the device completely and completely shut down all communication
                Console.WriteLine("Demo has completed successfully!");
                return;
            }
            catch (Exception ex)
            {
                throw new Exception("Demo failed: " + ex.Message + Environment.NewLine);
            }
        }
Example #13
0
        ////////////////////////////////////////////////////////////////////////////////
        // ConfigureANT
        //
        // Resets the system, configures the ANT channel and starts the demo
        ////////////////////////////////////////////////////////////////////////////////
        void ConfigureANT(byte USER_NETWORK_NUM, ANT_ReferenceLibrary.ChannelType channelType, byte USER_DEVICETYPE, ushort USER_CHANNELPERIOD, ANT_Channel channel)
        {
            textBoxGeneralLog.Text = "configure";

            Console.WriteLine("Resetting module...");
            //  device0.ResetSystem();     // Soft reset
            System.Threading.Thread.Sleep(500);    // Delay 500ms after a reset

            // If you call the setup functions specifying a wait time, you can check the return value for success or failure of the command
            // This function is blocking - the thread will be blocked while waiting for a response.
            // 500ms is usually a safe value to ensure you wait long enough for any response
            // If you do not specify a wait time, the command is simply sent, and you have to monitor the protocol events for the response,
            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("Assigning channel...");
            if (channel.assignChannel(channelType, USER_NETWORK_NUM, 500))
            {
                Console.WriteLine("Channel assigned");
            }
            else
            {
                throw new Exception("Error assigning channel");
            }

            Console.WriteLine("Setting Channel ID...");
            if (channel.setChannelID(USER_DEVICENUM, false, USER_DEVICETYPE, USER_TRANSTYPE, 500))  // Not using pairing bit
            {
                Console.WriteLine("Channel ID set");
            }
            else
            {
                throw new Exception("Error configuring Channel ID");
            }

            Console.WriteLine("Setting Radio Frequency...");
            if (channel.setChannelFreq(USER_RADIOFREQ, 500))
            {
                Console.WriteLine("Radio Frequency set");
            }
            else
            {
                throw new Exception("Error configuring Radio Frequency");
            }

            Console.WriteLine("Setting Channel Period...");
            if (channel.setChannelPeriod(USER_CHANNELPERIOD, 500))
            {
                Console.WriteLine("Channel Period set");
            }
            else
            {
                throw new Exception("Error configuring Channel Period");
            }

            Console.WriteLine("Opening channel...");
            bBroadcasting = true;
            if (channel.openChannel(500))
            {
                Console.WriteLine("Channel opened");
            }
            else
            {
                bBroadcasting = false;
                throw new Exception("Error opening channel");
            }
        }