/// <summary> /// Start a worker thread and return - so that the original thread is free to tweak settings. /// </summary> public void StartInBackground() { lock (_startStopLocker) { if (_thread?.IsAlive == true) { throw new InvalidOperationException("Already started."); } if (DataToSend == null) { DataToSend = new PulseSpaceDurationList() { 500, 500, 500 }; } _thread = new Thread(new ThreadStart(Loop)) { Name = "RaspberryIRDotNet - TestTransmitter - " + TransmissionDevice, IsBackground = true }; _keepGoing = true; _thread.Start(); } }
public void Send(IReadOnlyPulseSpaceDurationList buffer) { using (var irDevice = OpenDevice()) { WriteToDevice(irDevice, buffer); } }
/// <summary> /// Get bytes that must be sent to an IR device in order to transmit a buffer. /// </summary> private byte[] GetTxBytes(IReadOnlyPulseSpaceDurationList buffer) { const int bytesIn32BitInt = 4; if (buffer.Count % 2 == 0) { throw new InvalidPulseSpaceDataException("The buffer starts with a PULSE and must end with a PULSE, so there must be an odd number of items in the buffer. But there is an even number, so it ends with a SPACE."); } byte[] result = new byte[buffer.Count * bytesIn32BitInt]; int i = 0; foreach (int packet in buffer) { if (packet <= 0) { throw new InvalidPulseSpaceDataException("TX buffer contains invalid data, duration cannot be zero or less."); } Array.Copy(BitConverter.GetBytes(packet), 0, result, i, bytesIn32BitInt); i += bytesIn32BitInt; } return(result); }
public ButtonInfo(IReadOnlyPulseSpaceDurationList irData, int frequency, int dutyCycle, TimeSpan interButtonSleep) { IRData = irData ?? throw new ArgumentNullException(nameof(irData)); Frequency = frequency; DutyCycle = dutyCycle; InterButtonSleep = interButtonSleep; }
public PulseSpaceUnitList(int unitDuration, IReadOnlyPulseSpaceDurationList pulseSpaceDurations) : this(pulseSpaceDurations.Count) { foreach (var duration in pulseSpaceDurations) { int rounded = Utility.RoundMicrosecs(duration, unitDuration); Add(Convert.ToByte(rounded / unitDuration)); } }
public void SetLeadInPatternAsMicrosecs(IReadOnlyPulseSpaceDurationList microsecs) { if (UnitDurationMicrosecs < 1) { throw new InvalidOperationException($"Must define {nameof(UnitDurationMicrosecs)} first."); } LeadInPattern = new PulseSpaceUnitList(UnitDurationMicrosecs, microsecs); }
protected void WriteToDevice(FileSystem.IOpenFile file, IReadOnlyPulseSpaceDurationList buffer) { byte[] txBytesBuffer = GetTxBytes(buffer); try { _fileSystem.WriteToDevice(file, txBytesBuffer); } catch (System.ComponentModel.Win32Exception err) when(err.NativeErrorCode == LinuxErrorCodes.EINVAL) { throw new InvalidPulseSpaceDataException("The IR device rejected the IR data.", err); } }
/// <param name="pulsesAndSpacesAsDurations">The pulse/space durations. These will be rounded to multiples of the <paramref name="unitDuration"/>.</param> /// <param name="unitDuration">How long each unit lasts, in microseconds.</param> public IRPulseMessage(IReadOnlyPulseSpaceDurationList pulsesAndSpacesAsDurations, int unitDuration) { if (pulsesAndSpacesAsDurations == null) { throw new ArgumentNullException(nameof(pulsesAndSpacesAsDurations)); } if (unitDuration < Utility.UnitDurationMinimum || unitDuration > Utility.UnitDurationMaximum) { throw new ArgumentOutOfRangeException(nameof(unitDuration), unitDuration, "Unit duration is invalid."); } UnitDuration = unitDuration; _durations = pulsesAndSpacesAsDurations.Copy(unitDuration); // Create a copy because the original buffer might be modified by the caller after we return. _units = new PulseSpaceUnitList(unitDuration, pulsesAndSpacesAsDurations); UnitCount = _units.UnitCount; }
public void Send(IReadOnlyPulseSpaceDurationList buffer) { AssertNotDisposed(); AssertOpen(); WriteToDevice(_openDeviceHandle, buffer); }
public void Constructor_FromDuration_NullDurations() { IReadOnlyPulseSpaceDurationList list = null; Assert.That(() => new IRPulseMessage(list, 100), Throws.ArgumentNullException); }