Esempio n. 1
0
        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);
            }));
        }
Esempio n. 2
0
        public PwmController(IPwmPin forwardPwmPin, IPwmPin backwardPwmPin, int stepsPerChange)
        {
            _forwardPwmPin  = forwardPwmPin;
            _backwardPwmPin = backwardPwmPin;

            _activePin = forwardPwmPin;

            _stepsPerChange = stepsPerChange;

            Direction = Direction.Forward;
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        private async Task SetActivePinAsync(IPwmPin value)
        {
            var inactivePin = value == _forwardPwmPin ? _backwardPwmPin : _forwardPwmPin;

            if (value == _activePin)
            {
                return;
            }

            await inactivePin.ChangeDutyCycleInStepsAsync(0);

            _activePin = value;
        }
Esempio n. 5
0
 public DimmableLed(IPwmController controller, GpioEnum gpio, int initialValue)
 {
     Pin = controller.OpenPin(gpio);
     Pin.Start();
     Dim(initialValue, this);
 }
Esempio n. 6
0
 public Servo(IPwmPin pin)
 {
     _pin = pin;
     RegisterPin(pin);
     _maxAngle = 180;
 }
Esempio n. 7
0
 public static Task Stop(this IPwmPin pwmPin, int stepsPerChange = 3, int delayPerStepMs = 40)
 {
     return(pwmPin.ChangeDutyCycleInStepsAsync(0, stepsPerChange, delayPerStepMs));
 }