public static Task ChangeDutyCycleInStepsAsync(this IPwmPin pwmPin, int targetDutyCycle, int stepsPerChange = 6, int delayPerStepMs = 80) { var dutyCycleChange = targetDutyCycle - pwmPin.CurrentDutyCyclePercent; // Adjust stepsPerChange if each step would be less than 1 stepsPerChange = Math.Min(stepsPerChange, Math.Abs(dutyCycleChange)); if (stepsPerChange == 0) { return(Task.CompletedTask); } var fullStep = dutyCycleChange / stepsPerChange; return(Task.Run( async() => { int currentDutyCycle; do { currentDutyCycle = pwmPin.CurrentDutyCyclePercent; pwmPin.SetActiveDutyCyclePercent(currentDutyCycle + fullStep); await Task.Delay(delayPerStepMs); } while (Math.Abs(pwmPin.CurrentDutyCyclePercent - targetDutyCycle) > Math.Abs(fullStep)); pwmPin.SetActiveDutyCyclePercent(targetDutyCycle); })); }
public PwmController(IPwmPin forwardPwmPin, IPwmPin backwardPwmPin, int stepsPerChange) { _forwardPwmPin = forwardPwmPin; _backwardPwmPin = backwardPwmPin; _activePin = forwardPwmPin; _stepsPerChange = stepsPerChange; Direction = Direction.Forward; }
public async Task <PwmController> CreateDutAsync() { ForwardPwmPin = NewForwardMockPin().Object; BackwardPwmPin = NewBackwardMockPin().Object; var result = new PwmController(ForwardPwmPin, BackwardPwmPin, _stepsPerChange); await result.SetDirectionAsync(_direction); await result.ChangeDutyCyclePercentAsync(_dutyCyclePercent); return(result); }
private async Task SetActivePinAsync(IPwmPin value) { var inactivePin = value == _forwardPwmPin ? _backwardPwmPin : _forwardPwmPin; if (value == _activePin) { return; } await inactivePin.ChangeDutyCycleInStepsAsync(0); _activePin = value; }
public DimmableLed(IPwmController controller, GpioEnum gpio, int initialValue) { Pin = controller.OpenPin(gpio); Pin.Start(); Dim(initialValue, this); }
public Servo(IPwmPin pin) { _pin = pin; RegisterPin(pin); _maxAngle = 180; }
public static Task Stop(this IPwmPin pwmPin, int stepsPerChange = 3, int delayPerStepMs = 40) { return(pwmPin.ChangeDutyCycleInStepsAsync(0, stepsPerChange, delayPerStepMs)); }