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's Content

There have been 3 items by djcult (Search limited from 29-March 23)


By content type

See this member's

Sort by                Order  

#737 PwmGamma Sample

Posted by djcult on 18 August 2010 - 11:59 AM in Project Showcase

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)));
        }



#608 Blinking, fading and other useful functions

Posted by djcult on 16 August 2010 - 06:36 PM in Netduino 2 (and Netduino 1)

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



#606 Blinking, fading and other useful functions

Posted by djcult on 16 August 2010 - 06:30 PM in Netduino 2 (and Netduino 1)

I've written some wrappers around the OutputPort and PWM classes that provide some useful basic functionality. Blinking and fading can be done asyncronously.

Method signatures are:

DiscretePort.TurnOn()
DiscretePort.TurnOff()
DiscretePort.Toggle()
DiscretePort.IsOn
DiscretePort.Blink()
DiscretePort.Blink(int times)
DiscretePort.Blink(int times, bool async)
DiscretePort.Blink(int times, int periodInMilliseconds, bool async)

PwmPort.FadeIn()
PwmPort.FadeIn(int periodInMilliseconds)
PwmPort.FadeIn(int periodInMilliseconds, bool async)
PwmPort.FadeOut()
PwmPort.FadeOut(int periodInMilliseconds)
PwmPort.FadeOut(int periodInMilliseconds, bool async)
PwmPort.FadeInAndOut(int periodInMilliseconds)
PwmPort.FadeInAndOut(int periodInMilliseconds, bool async)

Forgive me if my terminology is all wrong - I'm new to the embedded thing. Code with examples is at here.




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.