/// <summary>
        /// Query the devices connected. Not ready until this finishes.
        /// </summary>
        private async Task QueryDevices()
        {
            await Task.Run(async() =>
            {
                DeviceStream.Write(new byte[] { 0x20, 0x03 });
                while (true)
                {
                    byte[] readBuf = DeviceStream.Read();

                    if (readBuf[0] == 0x21 && readBuf[1] == 0x03)
                    {
                        // We received the device query reply
                        byte[] device1 = new byte[6];
                        byte[] device2 = new byte[6];
                        Array.Copy(readBuf, 15, device1, 0, 6);
                        Array.Copy(readBuf, 21, device2, 0, 6);
                        string deviceStr1 = Util.ByteArrayToString(device1);
                        string deviceStr2 = Util.ByteArrayToString(device2);

                        Channel1 = NzxtLedDevice.FromDeviceIdentifier(deviceStr1);
                        Channel2 = NzxtLedDevice.FromDeviceIdentifier(deviceStr2);

                        break;
                    }
                    await Task.Delay(10).ConfigureAwait(false);
                }
                Ready = true;
            });
        }
        /// <summary>
        /// Send color array in RGB format
        /// </summary>
        public void SendRGB(byte channel, byte[] colors)
        {
            if (channel != 1 && channel != 2)
            {
                throw new ArgumentException("Channel must be 1 or 2");
            }

            if (colors.Length % 3 != 0)
            {
                throw new ArgumentException("Color array length must be multiple of 3");
            }

            byte[] buffer = null;
            using (MemoryStream strm = new MemoryStream(4 + colors.Length * 3))
            {
                strm.Write(new byte[] { 0x22, 0x10, channel, 0x00 }, 0, 4);
                for (int i = 0; i < colors.Length / 3; i += 1)
                {
                    strm.Write(new byte[] { colors[i * 3 + 1], colors[i * 3], colors[i * 3 + 2] }, 0, 3);
                }
                buffer = strm.ToArray();
            }
            DeviceStream.Write(buffer);
            DeviceStream.Write(new byte[] { 0x22, 0x11, channel });
            //0x22,0xA0,0x02,0x00,0x01,0x00,0x00,0x10,0x00,0x00,0x80,0x00,0x32,0x00,0x00,0x01
            DeviceStream.Write(new byte[] { 0x22, 0xA0, channel, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x80, 0x00, 0x32, 0x00, 0x00, 0x01 });
        }
Exemple #3
0
        public static void WriteBootLoaderToDisk(string DiskId, IDeviceProfile deviceProfile)
        {
            var          chunkSize = 131072;
            DeviceStream ds        = new DeviceStream(DiskId, FileAccess.ReadWrite);

            ds.Seek(0, SeekOrigin.Begin);
            byte[] bootloader = File.ReadAllBytes(deviceProfile.Bootloader());

            var sectors = bootloader.Count() / chunkSize;

            DateTime startTime = DateTime.Now;

            using (BinaryReader br = new BinaryReader(new MemoryStream(bootloader)))
                for (int i = 0; i < sectors; i++)
                {
                    var buff = br.ReadBytes(chunkSize);
                    ds.Write(buff, 0, chunkSize);
                    Logging.ShowProgress((UInt64)bootloader.Count(), startTime, (UInt64)((i + 1) * chunkSize), (UInt64)((i + 1) * chunkSize), false);
                }
            Logging.Log("");

            ds.Dispose();
        }
Exemple #4
0
        private void TransmitCommand()
        {
            ET232Command command = null;

            lock (_thread_interlock)
            {
                if (CommandQueue.Count > 0)
                {
                    command = CommandQueue.First();
                    CommandQueue.RemoveAt(0);
                }
            }

            if (command != null)
            {
                if (command.IsWrite())
                {
                    if (command.Address == AddressByte.Pot_A)
                    {
                        lock (_thread_interlock) command.Data = Governor.RegulateA(command.Data);
                    }
                    if (command.Address == AddressByte.Pot_B)
                    {
                        lock (_thread_interlock) command.Data = Governor.RegulateB(command.Data);
                    }
                }

                byte[] data = command.Format();
                last_command      = command;
                last_command_time = UnixTime.Current();
                lock (_thread_interlock)
                {
                    DeviceStream.Write(data, 0, data.Length);
                    _state = State.command;
                }
            }
        }
 protected override void When()
 {
     DeviceStream.Write(BufferToWrite, 3, 5);
 }
Exemple #6
0
        static void Main(string[] args)
        {
            if (args.Length != 3 && args.Length != 1 && args.Length != 2 && args.Length != 4)
            {
                Console.WriteLine("Usage:   StreamTerminal [<email> <password>] <device_id> [--echo]");
                Console.WriteLine("Example: StreamTerminal.exe echo.u.nabto.net");
                Console.WriteLine("         When the stream to echo.u.nabto.net is created select the echo service by typing 'echo<enter>' at the prompt.");
                Console.WriteLine("         Default user is 'guest'.");
                Console.WriteLine("         To enable local echo use the '--echo' switch.");
                Environment.Exit(1);
            }

            string email    = "guest";
            string password = "";
            string deviceId = "";

            if (args[0].Contains("@"))
            {
                email    = args[0];
                password = args[1];
                deviceId = args[2];
            }
            else
            {
                deviceId = args[0];
            }

            var localEcho = false;

            if (args[args.Length - 1] == "--echo")
            {
                localEcho = true;
            }

            if (!deviceId.Contains("."))
            {
                throw new Exception("Invalid device ID");
            }

            Console.WriteLine("Connecting...");

            try
            {
                Console.WriteLine("Starting Nabto...");
                NabtoClient nabto = new NabtoClient(); // Get a reference to the Nabto client runtime and start it up.

                Console.WriteLine("Creating session...");
                using (Session session = nabto.CreateSession(email, password)) // Create a session using the specified user credentials.
                {
                    Console.WriteLine("Creating connection to device...");
                    using (DeviceConnection deviceConnection = session.CreateDeviceConnection(deviceId)) // create connection to device
                    {
                        Console.WriteLine("Creating stream...");
                        using (DeviceStream stream = deviceConnection.CreateStream()) // create stream to device
                        {
                            var encoding      = new UTF8Encoding();
                            var run           = true;
                            var buffer        = new byte[1500];
                            var stringBuilder = new StringBuilder();

                            Console.WriteLine("Connected. Press ctrl+¨ to disconnect.");

                            while (run)
                            {
                                if (stream.DataAvailable)                                // data received from device?
                                {
                                    var length  = stream.Read(buffer, 0, buffer.Length); // read data into buffer
                                    var message = encoding.GetString(buffer, 0, length); // convert to text
                                    Console.Write(message);
                                }

                                if (Console.KeyAvailable)        // user input?
                                {
                                    while (Console.KeyAvailable) // read all user input
                                    {
                                        var key = Console.ReadKey(true);
                                        if (key.KeyChar == 0x1d) // terminate
                                        {
                                            run = false;
                                            Console.WriteLine();
                                            Console.WriteLine("Disconnecting...");
                                        }
                                        else if (key.Key == ConsoleKey.Enter)
                                        {
                                            stringBuilder.Append(Environment.NewLine);
                                        }
                                        else
                                        {
                                            stringBuilder.Append(key.KeyChar); // gather all characters from user
                                        }
                                    }

                                    if (stringBuilder.Length > 0)
                                    {
                                        var s = stringBuilder.ToString(); // text from user
                                        stringBuilder.Clear();

                                        var b = encoding.GetBytes(s); // convert to byte array
                                        stream.Write(b, 0, b.Length); // ...and send it to the device

                                        if (localEcho)
                                        {
                                            Console.Write(s);
                                        }
                                    }
                                }

                                Thread.Sleep(1);
                            }
                        }
                    }
                }

                Console.WriteLine("Disconnected.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }