Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

djcult

Member Since 16 Aug 2010
Offline Last Active Sep 01 2010 12:05 PM
-----

Posts I've Made

In Topic: PwmGamma Sample

18 August 2010 - 11:59 AM

I acheived something similar by using an "easing function" (which I stole some from WPF). You can set an acceleration and/or deceleration ratio. I have an acceleration ratio of 100% which means it starts off slowly and then speeds up; thus the LED appears to fade in linearly.

Code is here if you're interested.

private const int AccelerationRatio = 1; // Start off slowly and speed up
private const int DecelerationRatio = 0;

private void EasingFade(FadeMode fadeMode, int periodInMilliseconds)
        {
            var fadeIn = fadeMode == FadeMode.FadeIn;

            float progressRatio = fadeIn ? 0 : 1;

            while (fadeIn ? progressRatio <= 1 : progressRatio >= 0)
            {
                _pwnPort.SetDutyCycle(ComputeEasedProgressRatio(AccelerationRatio, DecelerationRatio, progressRatio));
                progressRatio = fadeIn ? progressRatio+0.01f : progressRatio-0.01f; // Increment or decrement by 1%

                Thread.Sleep(periodInMilliseconds / 100);
            }
        }

        private static float ComputeEasedProgressRatio(float accelerationRatio, float decelerationRatio, float progressRatio)
        {
            var combinedRatio = accelerationRatio + decelerationRatio;
            var num = progressRatio;
            if (combinedRatio == 0.0)
                return num;

            var num2 = (float)(2.0 / (2.0 - combinedRatio));
            if (num < accelerationRatio)
                return (float)(((num2 * num) * num) / (2.0 * accelerationRatio));

            if (num <= (1.0 - decelerationRatio))
                return (float)(num2 * (num - (accelerationRatio / 2.0)));

            var num5 = (float)(1.0 - num);
            return (float)(1.0 - (((num2 * num5) * num5) / (2.0 * decelerationRatio)));
        }

In Topic: Blinking, fading and other useful functions

16 August 2010 - 06:36 PM

Oh, I forgot: PwmPort.SetDutyCycle(float percent) !

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.