Exemplo n.º 1
0
        static bool RunScanCmd(string[] args)
        {
            byte start = 0, end = 128;

            if (args.Length > 3)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                ConsoleManager.WriteLine("Too many arguments");
                Console.ResetColor();
                return(false);
            }

            if (args.Length > 1)
            {
                if (!TryParseByte(args[1], out start))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    ConsoleManager.WriteLine("Could not parse scan range start");
                    Console.ResetColor();
                    return(false);
                }
            }
            if (args.Length > 2)
            {
                if (!TryParseByte(args[2], out end))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    ConsoleManager.WriteLine("Could not parse scan range end");
                    Console.ResetColor();
                    return(false);
                }
            }

            byte deviceAddress = start;

            for (int i = start; i < end; i++)
            {
                if (cmdCancelSource.Token.IsCancellationRequested)
                {
                    return(false);
                }

                uint       bytesTransferred;
                FtdiStatus status = channel.ReadFromRegister(deviceAddress, 0, 0, buffer, out bytesTransferred, writeOptions, readOptions);
                if (status == FtdiStatus.Ok)
                {
                    ConsoleManager.WriteLine("Found device: " + FormatByteOutput(deviceAddress));
                }
                else if (status != FtdiStatus.DeviceNotFound)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    ConsoleManager.WriteLine("FTDI Error: " + status);
                    Console.ResetColor();
                }

                deviceAddress++;
            }

            return(true);
        }
Exemplo n.º 2
0
        static bool RunWriteCmd(string[] args)
        {
            byte deviceAddress;

            if (args.Length < 3)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                ConsoleManager.WriteLine("Not enough arguments");
                Console.ResetColor();
                return(false);
            }

            if (!TryParseByte(args[1], out deviceAddress))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                ConsoleManager.WriteLine("Could not parse device address");
                Console.ResetColor();
                return(false);
            }

            uint bytesToTransfer = 0;

            for (int i = 2; i < args.Length; i++)
            {
                byte dataByte;

                if (!TryParseByte(args[i], out dataByte))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    ConsoleManager.WriteLine("Could not parse register address");
                    Console.ResetColor();
                    return(false);
                }

                buffer[bytesToTransfer++] = dataByte;
            }

            if (cmdCancelSource.Token.IsCancellationRequested)
            {
                return(false);
            }

            uint       bytesTransferred;
            FtdiStatus status = channel.Write(deviceAddress, bytesToTransfer, buffer, out bytesTransferred, writeOptions);

            if (status != FtdiStatus.Ok)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                ConsoleManager.WriteLine("FTDI Error: " + status);
                Console.ResetColor();
                return(false);
            }

            ConsoleManager.WriteLine("Successfully wrote " + bytesToTransfer + " bytes to device address " + FormatByteOutput(deviceAddress));

            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the Channel class from a channel
        /// index.
        /// </summary>
        /// <remarks>
        /// This constructor replaces I2C_OpenChannel.
        /// </remarks>
        /// <param name="index">The index of the channel.</param>
        public Channel(uint index)
        {
            FtdiStatus status = NativeMethods.I2C_OpenChannel(index, out handle);

            if (status != FtdiStatus.Ok)
            {
                Dispose();
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Reads from an I2C Register.
        /// </summary>
        /// <remarks>
        /// This function calls <see cref="Write{T}(byte, uint, T[,], out uint, TransferOptions)"/> with the register address
        /// and then calls <see cref="Read{T}(byte, uint, T[,], out uint, TransferOptions)"/> with the given buffer.
        /// </remarks>
        /// <param name="deviceAddress">The address of the device to write to.</param>
        /// <param name="registerAddress">The address of the register to read from.</param>
        /// <param name="sizeToTransfer">The size, in bytes, to transfer.</param>
        /// <param name="buffer">The buffer to write from.</param>
        /// <param name="sizeTransferred">The size, in bytes, that were transferred.</param>
        /// <param name="options">The options to transfer with.</param>
        /// <returns>The status after calling this method.</returns>
        public FtdiStatus ReadFromRegister <T>(byte deviceAddress, byte registerAddress, uint sizeToTransfer, T[,] buffer, out uint sizeTransferred, TransferOptions writeOptions, TransferOptions readOptions)
            where T : struct
        {
            GCHandle   handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            FtdiStatus status = ReadFromRegister(deviceAddress, registerAddress, sizeToTransfer, handle.AddrOfPinnedObject(), out sizeTransferred, writeOptions, readOptions);

            handle.Free();

            return(status);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Writes to an I2C device.
        /// </summary>
        /// <typeparam name="T">The type of data to transfer in buffer.</typeparam>
        /// <param name="deviceAddress">The address of the device to write to.</param>
        /// <param name="sizeToTransfer">The size, in bytes, to transfer.</param>
        /// <param name="buffer">The buffer to write from.</param>
        /// <param name="sizeTransferred">The size, in bytes, that were transferred.</param>
        /// <param name="options">The options to transfer with.</param>
        /// <returns>The status after calling this method.</returns>
        public FtdiStatus Write <T>(byte deviceAddress, uint sizeToTransfer, T[,] buffer, out uint sizeTransferred, TransferOptions options)
            where T : struct
        {
            GCHandle   handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            FtdiStatus status = Write(deviceAddress, sizeToTransfer, handle.AddrOfPinnedObject(), out sizeTransferred, options);

            handle.Free();

            return(status);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Reads from an I2C Register.
        /// </summary>
        /// <remarks>
        /// This function calls <see cref="Write(byte, uint, IntPtr, out uint, TransferOptions)"/> with the register address
        /// and then calls <see cref="Read(byte, uint, IntPtr, out uint, TransferOptions)"/> with the given buffer.
        /// </remarks>
        /// <param name="deviceAddress">The address of the device to write to.</param>
        /// <param name="registerAddress">The address of the register to read from.</param>
        /// <param name="sizeToTransfer">The size, in bytes, to transfer.</param>
        /// <param name="buffer">The buffer to write from.</param>
        /// <param name="sizeTransferred">The size, in bytes, that were transferred.</param>
        /// <param name="options">The options to transfer with.</param>
        /// <returns>The status after calling this method.</returns>
        public FtdiStatus ReadFromRegister(byte deviceAddress, byte registerAddress, uint sizeToTransfer, IntPtr buffer, out uint sizeTransferred, TransferOptions writeOptions, TransferOptions readOptions)
        {
            FtdiStatus status = Write(deviceAddress, 1, registerAddress, out sizeTransferred, writeOptions);

            if (status != FtdiStatus.Ok)
            {
                return(status);
            }

            status = Read(deviceAddress, sizeToTransfer, buffer, out sizeTransferred, readOptions);

            /*if (sizeToTransfer > 1)
             *      return Read(deviceAddress, sizeToTransfer, buffer + 1, out sizeTransferred, readOptions);
             * else*/
            return(status);
        }
Exemplo n.º 7
0
        static bool RunReadLoopCmd(string[] args)
        {
            byte deviceAddress, registerAddress;
            uint bytesToTransfer, numLoops;

            if (args.Length < 4 || args.Length > 5)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                ConsoleManager.WriteLine("Invalid number of arguments arguments");
                Console.ResetColor();
                return(false);
            }

            if (!TryParseByte(args[1], out deviceAddress))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                ConsoleManager.WriteLine("Could not parse device address");
                Console.ResetColor();
                return(false);
            }

            if (!uint.TryParse(args[2], out bytesToTransfer))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                ConsoleManager.WriteLine("Could not parse number of bytes to read");
                Console.ResetColor();
                return(false);
            }

            if (!uint.TryParse(args[3], out numLoops))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                ConsoleManager.WriteLine("Could not parse number of loops");
                Console.ResetColor();
                return(false);
            }

            if (args.Length == 4)
            {
                for (int i = 0; i < numLoops; i++)
                {
                    if (cmdCancelSource.Token.IsCancellationRequested)
                    {
                        return(false);
                    }

                    uint       bytesTransferred;
                    FtdiStatus status = channel.Read(deviceAddress, bytesToTransfer, buffer, out bytesTransferred, readOptions | TransferOptions.NoAddress);
                    if (status != FtdiStatus.Ok)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        ConsoleManager.WriteLine("FTDI Error: " + status);
                        Console.ResetColor();
                        return(false);
                    }

                    ConsoleManager.WriteLine("Loop " + i + ": " + BitConverter.ToString(buffer, 0, (int)bytesTransferred));
                }
            }
            else if (args.Length == 5)
            {
                if (!TryParseByte(args[4], out registerAddress))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    ConsoleManager.WriteLine("Could not parse register address");
                    Console.ResetColor();
                    return(false);
                }

                for (int i = 0; i < numLoops; i++)
                {
                    if (cmdCancelSource.Token.IsCancellationRequested)
                    {
                        return(false);
                    }

                    uint       bytesTransferred;
                    FtdiStatus status = channel.ReadFromRegister(deviceAddress, registerAddress, bytesToTransfer, buffer, out bytesTransferred, writeOptions, readOptions);
                    if (status != FtdiStatus.Ok)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        ConsoleManager.WriteLine("FTDI Error: " + status);
                        Console.ResetColor();
                        return(false);
                    }

                    //TODO format output
                    ConsoleManager.WriteLine("Loop " + i + ": " + BitConverter.ToString(buffer, 0, (int)bytesTransferred));
                }
            }

            return(true);
        }