public void StartAsync()
{
// ***
// *** Check if disposed and throw an exception
// *** if this instance has been disposed.
// ***
this.CheckDisposed();
Task.Factory.StartNew(async () =>
{
while (!_cancellationTokenSource.IsCancellationRequested)
{
// ***
// *** Pulse High (unless the value is 0 in which case
// *** the output will stay low.
// ***
if (this.Value != this.MinimumValue)
{
this.Pin.Write(GpioPinValue.High);
}
// ***
// *** Delay the for the time specified by HighPulseWidth
// ***
await this.DelayMicroSeconds(this.HighPulseWidth);
// ***
// *** Pulse Low unless the value is at maximum in which
// *** case the pulse will remain high.
// ***
if ((this.MaximumValue - this.Value) != this.MinimumValue)
{
this.Pin.Write(GpioPinValue.Low);
}
// ***
// *** Delay the for the time specified by LowPulseWidth
// ***
await this.DelayMicroSeconds(this.LowPulseWidth);
// ***
// *** Check if the PulseWidthChanged event needs to be fired.
// ***
if (this.LowPulseWidth != _previousLowPulseWidth || this.HighPulseWidth != _previousHighPulseWidth)
{
_previousLowPulseWidth = this.LowPulseWidth;
_previousHighPulseWidth = this.HighPulseWidth;
this.OnPulseWidthChanged(this.HighPulseWidth, this.LowPulseWidth);
}
// ***
// *** Fire the Pulsed event (monitoring of this event
// *** can impact the performance of the application and
// *** the ability of this code to keep the timing
// *** correct.
// ***
this.OnPwmPulsed();
}
});
}