Exemple #1
0
        static async Task Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("Missing firmware name argument");
                Environment.Exit(1);
            }

            var fwFile = args[0];

            byte[] fwData = null;
            Console.Write("Checking firmware... ");
            try {
#if WINDOWS_UWP
                var file = await StorageFile.GetFileFromPathAsync(fwFile);

                var buf = await FileIO.ReadBufferAsync(file);

                fwData = buf.ToArray();
#else
                fwData = File.ReadAllBytes(fwFile);
#endif
                Firmware.Validate(fwData);
                Console.WriteLine("OK.");
            }
            catch (Exception ex) {
                handleError(ex);
            }

            Samba device = null;

            try {
                var devices = await Samba.FindAllAsync();

                device = devices.FirstOrDefault();
                if (device == null)
                {
                    Console.Error.WriteLine("NXT not found. Is it properly plugged in via USB?");
                    Environment.Exit(1);
                }
            }
            catch (Exception ex) {
                handleError(ex);
            }

            try {
                nxt = await device.OpenAsync();
            }
            catch (Exception ex) {
                handleError(ex);
            }

            Console.WriteLine("NXT device in reset mode located and opened.");
            Console.WriteLine("Starting firmware flash procedure now...");

            try {
                using (var progress = new ProgressHelper("flashing")) {
                    await device.FlashAsync(fwData, progress);
                }
            }
            catch (Exception ex) {
                handleError(ex);
            }

            Console.WriteLine("Firmware flash complete.");

            try {
                await device.GoAsync(0x00100000);
            }
            catch (Exception ex) {
                handleError(ex);
            }

            Console.WriteLine("New firmware started!");

            try {
                nxt.Dispose();
            }
            catch (Exception ex) {
                handleError(ex);
            }
        }
Exemple #2
0
        static async Task <int> Main(string[] args)
        {
            if (args.Length > 1)
            {
                Console.Error.WriteLine("Too many arguments");
                return(1);
            }

            if (args.Length < 1)
            {
                Console.Error.WriteLine("Missing firmware file argument");
                return(1);
            }

            var fwFile = args[0];

            byte[] fwData = null;
            Console.Write("Reading firmware file... ");
            try {
                fwData = File.ReadAllBytes(fwFile);
                Firmware.Validate(fwData);
                Console.WriteLine("OK.");
            }
            catch (Exception ex) {
                Console.Error.WriteLine(ex.Message);
                return(1);
            }

            Console.Write("Searching for NXTs... ");
            var findFwDevicesTask     = Samba.FindAllAsync();
            var findNxtUsbDevicesTask = NxtDevice.FindAllUsbAsync();

            var fwDevices  = (await findFwDevicesTask).ToList();
            var nxtDevices = (await findNxtUsbDevicesTask).ToList();

            var totalCount = fwDevices.Count + nxtDevices.Count;

            if (totalCount == 0)
            {
                Console.Error.WriteLine("None found.");
                return(1);
            }
            Console.WriteLine("OK.");

            var message = $"Finished flashing {{0}} of {totalCount}";

            using (var progressBar = new ProgressBar(totalCount, string.Format(message, 0))) {
                async Task rebootToFirmwareMode(NxtDevice device)
                {
                    using (var childProgressBar = progressBar.Spawn(1, "Starting firmware loader...")) {
                        try {
                            using (var nxt = await device.ConnectAsync()) {
                                if (await nxt.SendCommandAsync(SystemCommand.Boot()))
                                {
                                    // TODO: search for firmware device
                                }
                                else
                                {
                                    // TODO: error message
                                }
                            }
                        }
                        catch {
                        }
                    }
                }

                await Task.WhenAll(nxtDevices.Select(d => rebootToFirmwareMode(d)));

                async Task loadFirmware(Samba device)
                {
                    var childProgressBar = new ProgressHelper(progressBar, $"NXT on {device.PortName}");

                    try {
                        using (await device.OpenAsync()) {
                            // Write the firmware to flash memory
                            await device.FlashAsync(fwData, childProgressBar);

                            childProgressBar.ReportSuccess();
                            // reboot NXT
                            await device.GoAsync(0x00100000);
                        }
                    }
                    catch (Exception ex) {
                        childProgressBar.ReportError(ex.Message);
                    }
                    // hmm... possible race condition with progressBar.CurrentTick
                    progressBar.Tick(string.Format(message, progressBar.CurrentTick + 1));
                }

                await Task.WhenAll(fwDevices.Select(d => loadFirmware(d)));
            }

            return(0);
        }