Hi there,
I've got something in my mind and need your advice :)
Let's imagine we've got 8 LED's and 8 buttons attached to MSP430. (actually, it doesn't matter how many LED's or buttons, or which port they're attached to)
We want a solution where a press of button one causes the first LED to light up for 1 second. After that time it'll switch off automatically.
For now that's nothing special. Now, let's say every LED lights up when the corresponding button has been pressed, but every one of these LEDs switches off
after a different amount of time. Moreover this whole mechanism should be independent of when which button has been pressed. It should be somehow interrupt-driven
and low power (using low power modes between the interrupts). OR: use the spare processing power inbetween status changes for something else.
My first thoughts were:
1. functions for each LED like "LED1_on();", "LED1_off();" etc.
2. a timer function like "millis();" in Arduino IDE
3. a array or struct that will hold a function pointer and the amount of time (millis) it should take until the function will be executed
4. a function to allocate i.e. "LED1_off();" to this array with the desired amount of time
5. timer-ISR checks every ms if there exists a function in the array that needs to be serviced and if so, executes it
my program could look something like this:
if(button1 pressed)
{
LED1_on();
AddTimerEvent(LED1_off, 500); // after 500ms it should "fire"
}
// move on doing other stuff
Adding this to a port ISR makes it completely independet of my main program code.
What do you think about my approach? Any advice? Does it already exist and I missed it?
Regards,
Leo
PS: I have to admit I never used function pointers. I only read "The C programming language" (Kernighan & Richie) some days ago and thought this would be a good match.