Jump to content
with the lemons!

Breathing LED

Recommended Posts

Yes.

If you just want to do a pulsing LED, an easy way to start would be to use the Stellarisware
RGB library.

Here is a very quick, rough port - regard this as pseudocode.
(I don't have a launchpad yet, so while this compiles it may not work. In particular I haven't checked that the adjustments to the math are all correct, and I left the math code as it was, rather than
cleaning it up.)

You might look at qs-rgb.c for ideas on handling the timers.

 

[Edit: See later post for updated code which works].

#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/rom.h"
#include "math.h"
#include "rgb.h"
unsigned long RGBWhite[3] = { RED_WHITE_BALANCE, GREEN_WHITE_BALANCE, BLUE_WHITE_BALANCE };
// Delay for given number of milliseconds
void delay(unsigned uDelayMs)
{
SysCtlDelay(SysCtlClockGet() / 3000 * uDelayMs);
}

#define PI 3.14159265f
// Use to adjust max and min brightness of LED
#define peak 0.5f
#define offset 0.5f

main()
{
float fIntensity;
float buf_len = 500; // Number of steps
float span = PI ; // PI = whole period
float w0 = -PI / 2;
int i;
ROM_SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
SYSCTL_OSC_MAIN);
RGBInit(0);
RGBIntensitySet(0.0f);
RGBColorSet(RGBWhite);
RGBEnable();
while(1)
for (i = 0 ;i<buf_len;i++){
fIntensity = offset + (peak * sin(w0 + span * i / buf_len));
RGBIntensitySet(fIntensity);
delay(5); //Quick and dirty - interrupt would be cleaner
}
RGBDisable(); // Never actually gets here, but be tidy anyway
}

Share this post


Link to post
Share on other sites

It is part of the Stellarisware library. It is located in wherever you installed stellarisware/boards/ek-lm4f120xl/drivers

You have to tell whatever compiler you are using where to find it.

(e.g. Code Composer is not well set up for finding libraries, you have to tell it for each new project where the libraries are, and telling it is a little complicated. See for instance this thread.

http://forum.stellarisiti.com/topic/303-how-to-make-a-new-project-that-uses-stellarisware-in-ccs/

 

(I imported rgb.c into my Code Composer project, and added the above mentioned directory

to the list of places to look for include files).

 

A good place to start might be to open the qs-rgb project (stellarisware/boards/ek-lm4f120xl/qs-rgb) in whatever environment you are using. Since it uses the rgb library, it should have everything set up. Make a copy of that project, create breathe.c with the above code as a starting point. (May need to remove qs-rgb.c and rgb_commands.c).

 

If that doesn't work for you, there are a bunch of knowledgeable folks here, so I expect if you indicate which environment you are using, somebody will know how to tell it where the samples/libraries are.

Share this post


Link to post
Share on other sites

As I noted when I posted the code - that is just a draft/outline.

Though I ordered my launchpad back in September, delivery isn't slated until first part of December

(i.e. I don't have hardware to test the RGB functions on).

 

I would have been surprised (and pleased) if it did work right off. That it doesn't work and needs some debugging is par for the course. I tossed it out there as a quick sketch of an approach, not as a finished solution.

Share this post


Link to post
Share on other sites
My launchpads finally came.  :)
 
Main problem with the code was definition of the color (turned out the constants in the library were floats, but the color is supposed to be ulongs).
 
I played with the equation a little (tweaking parameters).  Graphing the intensity value came in CCS came out looking similar to one in linked article.
 
// Breathing LED - see stelarisiti forum post
// For Stellaris launchpad (could try to port to other Stellaris)


#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/rom.h"
#include "math.h"

#include "rgb.h"


#define RGB_MAX 0xFFFF


unsigned long RGBWhite[3] = { (unsigned long) (RED_WHITE_BALANCE * (float) RGB_MAX), (unsigned long) (GREEN_WHITE_BALANCE * (float) RGB_MAX), (unsigned long) (BLUE_WHITE_BALANCE * (float) RGB_MAX) };

// Delay for given number of milliseconds
void delay(unsigned uDelayMs)
{
SysCtlDelay(SysCtlClockGet() / 3000 * uDelayMs);
}


#define PI 3.14159265f

// Use to adjust max and min brightness of LED
#define peak 0.4f
#define offset 0.4f

float fIntensity;

main()
{
// float fIntensity;
float buf_len = 500.0f; // Number of steps
float span = PI ;   // PI = whole period
float w0 = -PI / 2.0f;
int i;

    ROM_SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
                       SYSCTL_OSC_MAIN);

    RGBInit(0);
    RGBIntensitySet(0.0f);
    RGBColorSet(RGBWhite);
    RGBEnable();

while(1)
for (i = 0 ;i<buf_len;i++){
fIntensity = offset - fabsf(peak * sinf(w0 + span * ((float) i) / buf_len));
RGBIntensitySet(fIntensity);
delay(10); //Quick and dirty - interrupt would be cleaner
}
RGBDisable(); // Never actually gets here, but be tidy anyway
}

 

 

 
 

Share this post


Link to post
Share on other sites

 

My launchpads finally came.  :)
 
Main problem with the code was definition of the color (turned out the constants in the library were floats, but the color is supposed to be ulongs).
 
I played with the equation a little (tweaking parameters).  Graphing the intensity value came in CCS came out looking similar to one in linked article.
 

Hi Igor.. please use code tags next time. I've edited your post above.

Share this post


Link to post
Share on other sites

Hi Igor.. please use code tags next time. I've edited your post above.

 

Thanks.  I looked for a code option, but missed it.  (Buried among too many other inscrutable little pictures -

wish the editor had an "I hate icons" option which would spell out the buttons in text).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×