Thursday, July 19, 2007

LED driver design

This is about Light Emitting Diode (LED) driver function design. I guess this is quite an embedded article, but I think the design questions also are the types you ask in PC programs as well.

You might have the following functions to manipulation the LED:
void ledInit();
void flash_led(int number_of_times);
void led(bool state);

Whilst this is fine, you might instead code this up as a class:
class Led {
Led() { /* code to init direction to out for pin */ }
void on() { /* turn on LED code */ }
void off() { /* turn off LED code */ }
void set(bool state);
void flash();

And I guess there are several ways to structure that.

However, I now have more LEDs on different ports ... so what do I do? Seems like the ideal candidate for multiple instances of one class.

One option in procedural design is....

void ledInit(int led_number);
void flash_led(int led_number, int number_of_times);
void led(int led_number, bool state);

But that just looks plain wrong! Mostly because it's procedural I think... but also because those horrid selector parameters. Also how do the different functions know how to select the right led port?

if(led_number==0) { port = 23; }
else if(led_number==1) { port = 24; }
else if(led_number==2) { port = 24; }
else { /* break? */ }

In each one? There is probably a better way of course, even in procedural code.

So what's the best OO way? And I'm guessing there isn't one specific way... just what do you think are the good options?

Here are some ideas for those who don't know where to start...
  • Factory class (for instance production)
  • Template class (pass in port as template parameter? Or specific type?)
  • Write multiple sets of code
  • Use constructor to set up RAM variables that point to specific LED port.
  • static const LEDs objects in ROM/Flash


Post a Comment

<< Home

Newer›  ‹Older