private IEnumerable <HarpMessage> SetTriggerMode(FrameFlags[] pattern, int triggerPeriod, int dwellTime)
        {
            var triggerState = TriggerHelper.FromFrameFlags(pattern);

            yield return(HarpCommand.WriteByte(ConfigurationRegisters.TriggerState, triggerState));

            yield return(HarpCommand.WriteByte(ConfigurationRegisters.TriggerStateLength, (byte)pattern.Length));

            yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.TriggerPeriod, (ushort)triggerPeriod));

            yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.TriggerTimeUpdateOutputs, (ushort)dwellTime));
        }
        IEnumerable <HarpMessage> WriteRegisters(bool savePersistent)
        {
            var triggerState = configuration.TriggerState;

            yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.Config, (ushort)configuration.Config));

            yield return(HarpCommand.WriteByte(ConfigurationRegisters.ScreenBrightness, (byte)configuration.ScreenBrightness));

            yield return(HarpCommand.WriteByte(ConfigurationRegisters.TriggerState, TriggerHelper.FromFrameFlags(triggerState)));

            yield return(HarpCommand.WriteByte(ConfigurationRegisters.TriggerStateLength, (byte)triggerState.Length));

            yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.TriggerPeriod, (ushort)configuration.TriggerPeriod));

            yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.TriggerTimeUpdateOutputs, (ushort)configuration.DwellTime));

            yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.TriggerLaserOn, (ushort)configuration.TriggerLaserOn));

            yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.TriggerLaserOff, (ushort)configuration.TriggerLaserOff));

            yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.DacL415, LedPowerConverter.ClampLedPower((ushort)configuration.L415)));

            yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.DacL470, LedPowerConverter.ClampLedPower((ushort)configuration.L470)));

            yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.DacL560, LedPowerConverter.ClampLedPower((ushort)configuration.L560)));

            yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.DacLaser, (ushort)configuration.LaserAmplitude));

            yield return(HarpCommand.WriteByte(ConfigurationRegisters.Out0Conf, (byte)configuration.DigitalOutput0));

            yield return(HarpCommand.WriteByte(ConfigurationRegisters.Out1Conf, (byte)configuration.DigitalOutput1));

            yield return(HarpCommand.WriteByte(ConfigurationRegisters.In0Conf, (byte)configuration.DigitalInput0));

            yield return(HarpCommand.WriteByte(ConfigurationRegisters.In1Conf, (byte)configuration.DigitalInput1));

            yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.StimPeriod, (ushort)configuration.PulsePeriod));

            yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.StimOn, (ushort)configuration.PulseWidth));

            yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.StimReps, (ushort)configuration.PulseCount));

            if (savePersistent)
            {
                yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.StimWavelength, 0));

                yield return(HarpCommand.Reset(ResetMode.Save));
            }
        }
        IEnumerable <HarpMessage> WritePropertyRegister(string propertyName)
        {
            switch (propertyName)
            {
            case nameof(configuration.ClockSynchronizer):
            case nameof(configuration.Output1Routing):
                yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.Config, (ushort)configuration.Config));

                yield return(HarpCommand.WriteByte(ConfigurationRegisters.Out1Conf, (byte)configuration.DigitalOutput1));

                break;

            case nameof(configuration.ScreenBrightness):
                yield return(HarpCommand.WriteByte(ConfigurationRegisters.ScreenBrightness, (byte)configuration.ScreenBrightness));

                break;

            case nameof(configuration.FrameRate):
            case nameof(configuration.DwellTime):
            case nameof(configuration.InterleaveWidth):
                yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.TriggerPeriod, (ushort)configuration.TriggerPeriod));

                yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.TriggerTimeUpdateOutputs, (ushort)configuration.DwellTime));

                yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.TriggerLaserOn, (ushort)configuration.TriggerLaserOn));

                yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.TriggerLaserOff, (ushort)configuration.TriggerLaserOff));

                break;

            case nameof(configuration.TriggerState):
                var triggerState = configuration.TriggerState;
                yield return(HarpCommand.WriteByte(ConfigurationRegisters.TriggerState, TriggerHelper.FromFrameFlags(triggerState)));

                yield return(HarpCommand.WriteByte(ConfigurationRegisters.TriggerStateLength, (byte)triggerState.Length));

                break;

            case nameof(configuration.L415):
                yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.DacL415, LedPowerConverter.ClampLedPower((ushort)configuration.L415)));

                break;

            case nameof(configuration.L470):
                yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.DacL470, LedPowerConverter.ClampLedPower((ushort)configuration.L470)));

                break;

            case nameof(configuration.L560):
                yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.DacL560, LedPowerConverter.ClampLedPower((ushort)configuration.L560)));

                break;

            case nameof(configuration.LaserAmplitude):
                yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.DacLaser, (ushort)configuration.LaserAmplitude));

                break;

            case nameof(configuration.DigitalOutput0):
                yield return(HarpCommand.WriteByte(ConfigurationRegisters.Out0Conf, (byte)configuration.DigitalOutput0));

                break;

            case nameof(configuration.DigitalOutput1):
                yield return(HarpCommand.WriteByte(ConfigurationRegisters.Out1Conf, (byte)configuration.DigitalOutput1));

                break;

            case nameof(configuration.DigitalInput0):
                yield return(HarpCommand.WriteByte(ConfigurationRegisters.In0Conf, (byte)configuration.DigitalInput0));

                break;

            case nameof(configuration.DigitalInput1):
                yield return(HarpCommand.WriteByte(ConfigurationRegisters.In1Conf, (byte)configuration.DigitalInput1));

                break;

            case nameof(configuration.LaserWavelength):
                yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.StimWavelength, (ushort)configuration.LaserWavelength));

                if (configuration.LaserWavelength != LaserWavelength.None)
                {
                    yield return(HarpCommand.WriteByte(ConfigurationRegisters.ScreenBrightness, (byte)configuration.ScreenBrightness));
                }
                break;

            case nameof(configuration.PulseFrequency):
                yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.StimPeriod, (ushort)configuration.PulsePeriod));

                break;

            case nameof(configuration.PulseWidth):
                yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.StimOn, (ushort)configuration.PulseWidth));

                yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.DacLaser, (ushort)configuration.LaserAmplitude));

                break;

            case nameof(configuration.PulseCount):
                yield return(HarpCommand.WriteUInt16(ConfigurationRegisters.StimReps, (ushort)configuration.PulseCount));

                break;

            default: yield break;
            }
        }
        private void ReadRegister(HarpMessage message)
        {
            switch (message.Address)
            {
            case ConfigurationRegisters.Config:
                configuration.Config = message.GetPayloadUInt16();
                break;

            case ConfigurationRegisters.WhoAmI:
                configuration.WhoAmI = message.GetPayloadUInt16();
                break;

            case ConfigurationRegisters.HardwareVersionHigh:
                configuration.HardwareVersionHigh = message.GetPayloadByte();
                break;

            case ConfigurationRegisters.HardwareVersionLow:
                configuration.HardwareVersionLow = message.GetPayloadByte();
                break;

            case ConfigurationRegisters.FirmwareVersionHigh:
                configuration.FirmwareVersionHigh = message.GetPayloadByte();
                break;

            case ConfigurationRegisters.FirmwareVersionLow:
                configuration.FirmwareVersionLow = message.GetPayloadByte();
                break;

            case ConfigurationRegisters.SerialNumber:
                configuration.SerialNumber = message.GetPayloadUInt16();
                break;

            case ConfigurationRegisters.ScreenBrightness:
                configuration.ScreenBrightness = message.GetPayloadByte();
                break;

            case ConfigurationRegisters.TriggerState:
                var triggerState = message.GetPayloadArray <byte>();
                configuration.TriggerState = TriggerHelper.ToFrameFlags(triggerState);
                break;

            case ConfigurationRegisters.TriggerStateLength:
                var triggerStateLength = message.GetPayloadByte();
                if (configuration.TriggerState?.Length != triggerStateLength)
                {
                    var array = configuration.TriggerState;
                    Array.Resize(ref array, triggerStateLength);
                    configuration.TriggerState = array;
                }
                triggerStateView.BeginInvoke((Action)SetTriggerState);
                break;

            case ConfigurationRegisters.TriggerPeriod:
                configuration.TriggerPeriod = message.GetPayloadUInt16();
                break;

            case ConfigurationRegisters.TriggerTimeUpdateOutputs:
                configuration.DwellTime = message.GetPayloadUInt16();
                break;

            case ConfigurationRegisters.DacL415:
                configuration.L415 = message.GetPayloadUInt16();
                break;

            case ConfigurationRegisters.DacL470:
                configuration.L470 = message.GetPayloadUInt16();
                break;

            case ConfigurationRegisters.DacL560:
                configuration.L560 = message.GetPayloadUInt16();
                break;

            case ConfigurationRegisters.DacLaser:
                configuration.LaserAmplitude = message.GetPayloadUInt16();
                break;

            case ConfigurationRegisters.Out0Conf:
                configuration.DigitalOutput0 = (DigitalOutputConfiguration)message.GetPayloadByte();
                break;

            case ConfigurationRegisters.Out1Conf:
                configuration.DigitalOutput1 = (DigitalOutputConfiguration)message.GetPayloadByte();
                break;

            case ConfigurationRegisters.In0Conf:
                configuration.DigitalInput0 = (DigitalInputConfiguration)message.GetPayloadByte();
                break;

            case ConfigurationRegisters.In1Conf:
                configuration.DigitalInput1 = (DigitalInputConfiguration)message.GetPayloadByte();
                break;

            case ConfigurationRegisters.StimWavelength:
                configuration.LaserWavelength = message.GetPayloadUInt16();
                setupLaserButton.BeginInvoke((Action)SetLaserCalibrationState);
                break;

            case ConfigurationRegisters.StimPeriod:
                configuration.PulsePeriod = message.GetPayloadUInt16();
                break;

            case ConfigurationRegisters.StimOn:
                configuration.PulseWidth = message.GetPayloadUInt16();
                break;

            case ConfigurationRegisters.StimReps:
                configuration.PulseCount = message.GetPayloadUInt16();
                break;

            case ConfigurationRegisters.TriggerLaserOn:
                configuration.TriggerLaserOn = message.GetPayloadUInt16();
                break;

            case ConfigurationRegisters.TriggerLaserOff:
                configuration.TriggerLaserOff = message.GetPayloadUInt16();
                break;

            case ConfigurationRegisters.CameraSerialNumber:
                if (message.PayloadType == PayloadType.TimestampedU64)
                {
                    configuration.CameraSerialNumber = message.GetPayloadUInt64();
                }
                break;

            default:
                break;
            }
        }