private void StopUpdates() { // Stop updater if started if (updater.IsStarted) { updater.Stop(); } }
private void Update() { var enabledPins = pins.Values.Where(p => p.Enabled && p.DutyCycle != 0).ToList(); // If there are no enabled pins, stop updates if (enabledPins.Count == 0) { updater.Stop(); return; } for (int i = 0; i < enabledPins.Count; i++) { var softPin = enabledPins[i]; var value = (softPin.InvertPolarity) ? GpioPinValue.Low : GpioPinValue.High; softPin.Pin.Write(value); } if (!stopwatch.IsRunning) { stopwatch.Start(); } else { stopwatch.Restart(); } long startTicks = stopwatch.ElapsedTicks; long currentTicks = 0; double period = 1000.0 / actualFrequency; // Calculate target ticks for (int i = 0; i < enabledPins.Count; i++) { var softPin = enabledPins[i]; softPin.targetTicks = startTicks + softPin.DutyCycle * period * ticksPerSecond / 1000.0; } int processedPins = 0; while (processedPins < enabledPins.Count) { currentTicks = stopwatch.ElapsedTicks; for (int i = 0; i < enabledPins.Count; i++) { var softPin = enabledPins[i]; if ((softPin.targetTicks > 0) && (currentTicks > softPin.targetTicks)) { softPin.targetTicks = 0; processedPins++; var pinValue = (softPin.InvertPolarity) ? GpioPinValue.High : GpioPinValue.Low; softPin.Pin.Write(pinValue); } } } double endCycleTicks = startTicks + period * ticksPerSecond / 1000.0; currentTicks = stopwatch.ElapsedTicks; while (currentTicks < endCycleTicks) { // TODO: Better looping strategy currentTicks = stopwatch.ElapsedTicks; } }
private void Update() { var enabledPins = m_Pins.Values.Where(p => p.Enabled && Math.Abs(p.DutyCycle) > double.Epsilon).ToList(); // If there are no enabled pins, stop updates if (enabledPins.Count == 0) { m_Updater.Stop(); return; } foreach (var softPin in enabledPins) { var value = (softPin.InvertPolarity) ? GpioPinValueEnum.Low : GpioPinValueEnum.High; softPin.Pin.Write(value); } if (!m_Stopwatch.IsRunning) { m_Stopwatch.Start(); } else { m_Stopwatch.Restart(); } long startTicks = m_Stopwatch.ElapsedTicks; long currentTicks; double period = 1000.0 / m_ActualFrequency; // Calculate target ticks foreach (var softPin in enabledPins) { softPin.TargetTicks = startTicks + softPin.DutyCycle * period * m_TicksPerSecond / 1000.0; } int processedPins = 0; while (processedPins < enabledPins.Count) { currentTicks = m_Stopwatch.ElapsedTicks; foreach (var softPin in enabledPins) { if ((softPin.TargetTicks > 0) && (currentTicks > softPin.TargetTicks)) { softPin.TargetTicks = 0; processedPins++; var pinValue = (softPin.InvertPolarity) ? GpioPinValueEnum.High : GpioPinValueEnum.Low; softPin.Pin.Write(pinValue); } } } double endCycleTicks = startTicks + period * m_TicksPerSecond / 1000.0; currentTicks = m_Stopwatch.ElapsedTicks; while (currentTicks < endCycleTicks) { currentTicks = m_Stopwatch.ElapsedTicks; } }