private void InitializeControls()
        {
            var errorIcon = BitmapProcessor.CreateIcon(Resources.exclamation);

            if (errorIcon != null)
            {
                MainErrorProvider.Icon = errorIcon;
            }

            MainContainer.SelectedPage = WelcomePage;

            FirmwareVersionTextBox.ReadOnly  = true;
            FirmwareVersionTextBox.BackColor = Color.White;

            BuildTextBox.ReadOnly  = true;
            BuildTextBox.BackColor = Color.White;

            HardwareVersionTextBox.ReadOnly  = true;
            HardwareVersionTextBox.BackColor = Color.White;

            BootModeTextBox.ReadOnly  = true;
            BootModeTextBox.BackColor = Color.White;

            TraceTextBox.ReadOnly  = true;
            TraceTextBox.BackColor = Color.White;

            InititalizeComboBoxes();

            TemperatureTypeComboBox.SelectedValueChanged += (s, e) =>
            {
                var isCelcius = TemperatureTypeComboBox.GetSelectedItem <bool>();
                if (isCelcius)
                {
                    TemperatureUpDown.Minimum = 100;
                    TemperatureUpDown.Maximum = 315;
                }
                else
                {
                    TemperatureUpDown.Minimum = 200;
                    TemperatureUpDown.Maximum = 600;
                }
            };

            PreheatTypeComboBox.SelectedValueChanged += (s, e) =>
            {
                var isPercents = PreheatTypeComboBox.GetSelectedItem <bool>();
                if (isPercents)
                {
                    PreheatPowerUpDown.DecimalPlaces = 0;
                    PreheatPowerUpDown.Increment     = 1;
                    PreheatPowerUpDown.Minimum       = 100;
                    PreheatPowerUpDown.Maximum       = 250;
                }
                else
                {
                    PreheatPowerUpDown.DecimalPlaces = 1;
                    PreheatPowerUpDown.Increment     = 0.1m;
                    PreheatPowerUpDown.Minimum       = 1;
                    PreheatPowerUpDown.Maximum       = 75;
                }
            };

            SelectedModeComboBox.SelectedValueChanged += (s, e) =>
            {
                var mode = SelectedModeComboBox.GetSelectedItem <VapeMode>();
                switch (mode)
                {
                case VapeMode.TempNi:
                    SetupModesCheckBoxes(TempNiModeCheckBox);
                    break;

                case VapeMode.TempTi:
                    SetupModesCheckBoxes(TempTiModeCheckBox);
                    break;

                case VapeMode.TempSS:
                    SetupModesCheckBoxes(TempSSModeCheckBox);
                    break;

                case VapeMode.TCR:
                    SetupModesCheckBoxes(TCRModeCheckBox);
                    break;

                case VapeMode.Power:
                    SetupModesCheckBoxes(PowerModeCheckBox);
                    break;

                case VapeMode.Bypass:
                    SetupModesCheckBoxes(BypassModeCheckBox);
                    break;

                case VapeMode.Start:
                    SetupModesCheckBoxes(SmartModeCheckBox);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                SelectedTCRComboBox.SelectedIndex = Math.Min(m_dataflash.ParamsBlock.SelectedTCRIndex, (byte)2);
                SelectedTCRComboBox.Visible       = TCRIndexLabel.Visible = mode == VapeMode.TCR;
            };

            BatteryModelComboBox.SelectedValueChanged += (s, e) =>
            {
                var batteryModel = BatteryModelComboBox.GetSelectedItem <BatteryModel>();
                BatteryEditButton.Visible = batteryModel == BatteryModel.Custom;
            };

            PortComboBox.SelectedIndex       = 0;
            BrightnessTrackBar.ValueChanged += (s, e) => BrightnessPercentLabel.Text = (int)(BrightnessTrackBar.Value * 100m / 255) + @"%";

            DownloadButton.Click += DownloadButton_Click;
            UploadButton.Click   += UploadButton_Click;
            ResetButton.Click    += ResetButton_Click;

            BatteryEditButton.Click += BatteryEditButton_Click;

            TakeScreenshotButton.Click += TakeScreenshotButton_Click;
            SaveScreenshotButton.Click += SaveScreenshotButton_Click;
            BroadcastButton.Click      += BroadcastButton_Click;
            RebootButton.Click         += RebootButton_Click;

            ComConnectButton.Click    += ComConnectButton_Click;
            ComDisconnectButton.Click += ComDisconnectButton_Click;
            CommandTextBox.KeyDown    += CommandTextBox_KeyDown;
        }
        private void SaveWorkspaceToDataflash([NotNull] Dataflash dataflash)
        {
            if (dataflash == null)
            {
                throw new ArgumentNullException("dataflash");
            }

            // General -> Power & Temp Tab
            dataflash.ParamsBlock.Power         = (ushort)(PowerUpDown.Value * 10);
            dataflash.ParamsBlock.TCPower       = (ushort)(TCPowerUpDown.Value * 10);
            dataflash.ParamsBlock.Status.Step1W = Step1WCheckBox.Checked;

            dataflash.ParamsBlock.IsCelsius   = TemperatureTypeComboBox.GetSelectedItem <bool>();
            dataflash.ParamsBlock.Temperature = (ushort)TemperatureUpDown.Value;
            dataflash.ParamsBlock.Status.TemperatureDominant = TemperatureDominantCheckBox.Checked;

            dataflash.ParamsBlock.Status.PreheatPercent = PreheatTypeComboBox.GetSelectedItem <bool>();
            dataflash.ParamsBlock.PreheatPwr            = (ushort)(dataflash.ParamsBlock.Status.PreheatPercent ? PreheatPowerUpDown.Value : PreheatPowerUpDown.Value * 10);
            dataflash.ParamsBlock.PreheatTime           = (byte)(PreheatTimeUpDown.Value * 100);

            // General -> Coils Manager Tab
            dataflash.ParamsBlock.ResistanceNi       = (ushort)(ResistanceNiUpDown.Value * 100);
            dataflash.ParamsBlock.ResistanceNiLocked = ResistanceNiCheckBox.Checked;

            dataflash.ParamsBlock.ResistanceTi       = (ushort)(ResistanceTiUpDown.Value * 100);
            dataflash.ParamsBlock.ResistanceTiLocked = ResistanceTiCheckBox.Checked;

            dataflash.ParamsBlock.ResistanceSS       = (ushort)(ResistanceSSUpDown.Value * 100);
            dataflash.ParamsBlock.ResistanceSSLocked = ResistanceSSCheckBox.Checked;

            dataflash.ParamsBlock.ResistanceTCR       = (ushort)(ResistanceTCRUpDown.Value * 100);
            dataflash.ParamsBlock.ResistanceTCRLocked = ResistanceTCRCheckBox.Checked;

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

            // General -> Modes Tab
            dataflash.ParamsBlock.SelectedMode     = SelectedModeComboBox.GetSelectedItem <VapeMode>();
            dataflash.ParamsBlock.SelectedTCRIndex = (byte)SelectedTCRComboBox.SelectedIndex;
            dataflash.ParamsBlock.DisabledModes    = VapeModes.None;
            {
                if (!TempNiModeCheckBox.Checked)
                {
                    dataflash.ParamsBlock.DisabledModes |= VapeModes.TempNi;
                }
                if (!TempTiModeCheckBox.Checked)
                {
                    dataflash.ParamsBlock.DisabledModes |= VapeModes.TempTi;
                }
                if (!TempSSModeCheckBox.Checked)
                {
                    dataflash.ParamsBlock.DisabledModes |= VapeModes.TempSS;
                }
                if (!TCRModeCheckBox.Checked)
                {
                    dataflash.ParamsBlock.DisabledModes |= VapeModes.TCR;
                }
                if (!PowerModeCheckBox.Checked)
                {
                    dataflash.ParamsBlock.DisabledModes |= VapeModes.Power;
                }
                if (!BypassModeCheckBox.Checked)
                {
                    dataflash.ParamsBlock.DisabledModes |= VapeModes.Bypass;
                }
                if (!SmartModeCheckBox.Checked)
                {
                    dataflash.ParamsBlock.DisabledModes |= VapeModes.Start;
                }
            }

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

            // General -> Stats Tab
            dataflash.InfoBlock.PuffCount = (uint)PuffsUpDown.Value;
            dataflash.InfoBlock.TimeCount = (uint)PuffsTimeUpDown.Value * 10;

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

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

            var clockMode = ClockTypeComboBox.GetSelectedItem <ClockType>();

            switch (clockMode)
            {
            case ClockType.Disabled:
            {
                dataflash.ParamsBlock.Status.AnalogClock  = false;
                dataflash.ParamsBlock.Status.DigitalClock = false;
                break;
            }

            case ClockType.Analog:
            {
                dataflash.ParamsBlock.Status.AnalogClock  = true;
                dataflash.ParamsBlock.Status.DigitalClock = false;
                break;
            }

            case ClockType.Digital:
            {
                dataflash.ParamsBlock.Status.AnalogClock  = true;
                dataflash.ParamsBlock.Status.DigitalClock = true;
                break;
            }

            default:
                throw new ArgumentOutOfRangeException();
            }
            dataflash.ParamsBlock.Status.UseClassicMenu = UseClassicMenuCheckBox.Checked;

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

            // Developer -> Expert
            dataflash.ParamsBlock.ShuntCorrection      = (byte)ShuntCorrectionUpDown.Value;
            dataflash.ParamsBlock.SelectedBatteryModel = BatteryModelComboBox.GetSelectedItem <BatteryModel>();

            // Setup DateTime
            dataflash.InfoBlock.Year   = (ushort)DateTime.Now.Year;
            dataflash.InfoBlock.Month  = (byte)DateTime.Now.Month;
            dataflash.InfoBlock.Day    = (byte)DateTime.Now.Day;
            dataflash.InfoBlock.Hour   = (byte)DateTime.Now.Hour;
            dataflash.InfoBlock.Minute = (byte)DateTime.Now.Minute;
            dataflash.InfoBlock.Second = (byte)DateTime.Now.Second;
        }
        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);
        }