private void DeviceConnected(bool isConnected)
        {
            if (isConnected)
            {
                System.Diagnostics.Trace.WriteLine("Connected " + DateTime.Now);

                try
                {
                    m_dataflash = m_connector.ReadDataflash();
                }
                catch
                {
                    return;
                }

                m_connectedDeviceProductId = m_dataflash.ProductId;
                m_deviceInfo      = USBConnector.GetDeviceInfo(m_connectedDeviceProductId);
                m_hardwareVersion = (m_dataflash.HardwareVersion / 100f).ToString("0.00", CultureInfo.InvariantCulture);
                m_firmwareVersion = (m_dataflash.FirmwareVersion / 100f).ToString("0.00", CultureInfo.InvariantCulture);

                UpdateUI(() =>
                {
                    DeviceNameTextBox.Text      = m_deviceInfo.Name;
                    HardwareVersionTextBox.Text = m_hardwareVersion;
                    FirmwareVersionTextBox.Text = m_firmwareVersion;
                    BootModeTextBox.Text        = m_dataflash.LoadFromLdrom ? "LDROM" : "APROM";
                    UpdateStatusLabel.Text      = @"Device is ready.";
                    SetUpdaterButtonsState(true);
                });
            }
            else
            {
                System.Diagnostics.Trace.WriteLine("Disconnected " + DateTime.Now);
                m_connectedDeviceProductId = null;
                m_dataflash = null;

                UpdateUI(() =>
                {
                    DeviceNameTextBox.Clear();
                    HardwareVersionTextBox.Clear();
                    FirmwareVersionTextBox.Clear();
                    BootModeTextBox.Clear();
                    UpdateStatusLabel.Text = @"Waiting for device...";
                    SetUpdaterButtonsState(false);
                });
            }
        }
        private void InitializeWorkspaceFromDataflash([NotNull] Dataflash dataflash)
        {
            if (dataflash == null)
            {
                throw new ArgumentNullException("dataflash");
            }

            DeviceNameLabel.Text        = USBConnector.GetDeviceInfo(dataflash.InfoBlock.ProductID).Name;
            FirmwareVersionTextBox.Text = (dataflash.InfoBlock.FWVersion / 100f).ToString("0.00", CultureInfo.InvariantCulture);
            BuildTextBox.Text           = m_simple.Build.ToString();
            HardwareVersionTextBox.Text = (dataflash.ParamsBlock.HardwareVersion / 100f).ToString("0.00", CultureInfo.InvariantCulture);
            BootModeTextBox.Text        = dataflash.ParamsBlock.BootMode.ToString();

            // General -> Power & Temp Tab
            PowerUpDown.Value      = Math.Max(1, Math.Min(dataflash.ParamsBlock.Power / 10m, 75));
            TCPowerUpDown.Value    = Math.Max(1, Math.Min(dataflash.ParamsBlock.TCPower / 10m, 75));
            Step1WCheckBox.Checked = dataflash.ParamsBlock.Status.Step1W;

            TemperatureTypeComboBox.SelectItem(dataflash.ParamsBlock.IsCelsius);
            TemperatureUpDown.Value             = dataflash.ParamsBlock.Temperature;
            TemperatureDominantCheckBox.Checked = dataflash.ParamsBlock.Status.TemperatureDominant;

            PreheatTypeComboBox.SelectItem(dataflash.ParamsBlock.Status.PreheatPercent);
            PreheatPowerUpDown.Value = dataflash.ParamsBlock.Status.PreheatPercent ? dataflash.ParamsBlock.PreheatPwr : Math.Max(1, Math.Min(dataflash.ParamsBlock.PreheatPwr / 10m, 75));
            PreheatTimeUpDown.Value  = dataflash.ParamsBlock.PreheatTime / 100m;

            // General -> Coils Manager Tab
            ResistanceNiUpDown.Value     = dataflash.ParamsBlock.ResistanceNi / 100m;
            ResistanceNiCheckBox.Checked = dataflash.ParamsBlock.ResistanceNiLocked;

            ResistanceTiUpDown.Value     = dataflash.ParamsBlock.ResistanceTi / 100m;
            ResistanceTiCheckBox.Checked = dataflash.ParamsBlock.ResistanceTiLocked;

            ResistanceSSUpDown.Value     = dataflash.ParamsBlock.ResistanceSS / 100m;
            ResistanceSSCheckBox.Checked = dataflash.ParamsBlock.ResistanceSSLocked;

            ResistanceTCRUpDown.Value     = dataflash.ParamsBlock.ResistanceTCR / 100m;
            ResistanceTCRCheckBox.Checked = dataflash.ParamsBlock.ResistanceTCRLocked;

            TCRM1UpDown.Value = dataflash.ParamsBlock.TCR[0];
            TCRM2UpDown.Value = dataflash.ParamsBlock.TCR[1];
            TCRM3UpDown.Value = dataflash.ParamsBlock.TCR[2];

            // General -> Modes Tab
            SelectedModeComboBox.SelectItem(dataflash.ParamsBlock.SelectedMode);
            TempNiModeCheckBox.Checked = !dataflash.ParamsBlock.DisabledModes.HasFlag(VapeModes.TempNi);
            TempTiModeCheckBox.Checked = !dataflash.ParamsBlock.DisabledModes.HasFlag(VapeModes.TempTi);
            TempSSModeCheckBox.Checked = !dataflash.ParamsBlock.DisabledModes.HasFlag(VapeModes.TempSS);
            TCRModeCheckBox.Checked    = !dataflash.ParamsBlock.DisabledModes.HasFlag(VapeModes.TCR);
            PowerModeCheckBox.Checked  = !dataflash.ParamsBlock.DisabledModes.HasFlag(VapeModes.Power);
            BypassModeCheckBox.Checked = !dataflash.ParamsBlock.DisabledModes.HasFlag(VapeModes.Bypass);
            SmartModeCheckBox.Checked  = !dataflash.ParamsBlock.DisabledModes.HasFlag(VapeModes.Start);

            // General -> Controls Tab
            Clicks2ComboBox.SelectItem(dataflash.ParamsBlock.MClicks[0]);
            Clicks3ComboBox.SelectItem(dataflash.ParamsBlock.MClicks[1]);
            Clicks4ComboBox.SelectItem(dataflash.ParamsBlock.MClicks[2]);
            WakeUpByPlusMinusCheckBox.Checked = dataflash.ParamsBlock.Status.WakeUpByPlusMinus;

            // General -> Stats Tab
            PuffsUpDown.Value     = Math.Max(0, Math.Min(dataflash.InfoBlock.PuffCount, 99999));
            PuffsTimeUpDown.Value = Math.Max(0, Math.Min(dataflash.InfoBlock.TimeCount / 10m, 99999));

            // Screen -> Display Tab
            BrightnessTrackBar.Value    = dataflash.ParamsBlock.Contrast;
            IdleTimeUpDow.Value         = dataflash.ParamsBlock.ScreenDimTimeout;
            StealthModeCheckBox.Checked = dataflash.ParamsBlock.StealthOn;
            FlippedModeCheckBox.Checked = dataflash.ParamsBlock.Status.Flipped;

            // Screen -> Layout Tab
            ThirdLineContentComboBox.SelectItem(dataflash.ParamsBlock.ThirdLineContent);
            BatteryPercentsCheckBox.Checked = dataflash.ParamsBlock.Status.BatteryPercent;
            ShowLogoCheckBox.Checked        = !dataflash.ParamsBlock.Status.NoLogo;

            if (!dataflash.ParamsBlock.Status.AnalogClock)
            {
                ClockTypeComboBox.SelectItem(ClockType.Disabled);
            }
            else if (dataflash.ParamsBlock.Status.AnalogClock && dataflash.ParamsBlock.Status.DigitalClock)
            {
                ClockTypeComboBox.SelectItem(ClockType.Digital);
            }
            else if (dataflash.ParamsBlock.Status.AnalogClock)
            {
                ClockTypeComboBox.SelectItem(ClockType.Analog);
            }
            UseClassicMenuCheckBox.Checked = dataflash.ParamsBlock.Status.UseClassicMenu;

            // Screen -> Screensaver Tab
            ScreensaverTypeComboBox.SelectItem(dataflash.ParamsBlock.ScreensaverType);
            ScreenProtectionTimeComboBox.SelectItem(dataflash.ParamsBlock.ScreenProtectionTime);

            // Developer -> Expert
            ShuntCorrectionUpDown.Value = Math.Max((byte)85, Math.Min(dataflash.ParamsBlock.ShuntCorrection, (byte)115));
            BatteryModelComboBox.SelectItem(dataflash.ParamsBlock.SelectedBatteryModel);
        }