Our embedded target board needs to support delayed/egress lighting. If the target board is sleeping, the wakeup switch input (when "closed") will be used to wake up the target board and turn ON the light. Once ON, the light would stay ON until either the switch input "opens" OR a 2 minute timer expires.
This will have to function via the wakeup switch input, as well as when the target board is powered up via the traditional means.
Please see Code below. The function I wrote below will be called every
40ms.How can it be improved?
When switch is "closed", light will turn ON and then may turn OFF. Is it possible to do one or the other in "if' body.
When switch is "open", timer will reset and light will turn OFF. Doing this every 40ms seems to be waste of CPU.
Thank you!
void light_update(void) { static unsigned on_timer = 3000;
/* Is switch closed? */ if ( closed ) { /* Turn ON light */ output = 1; if (on_timer != 0) { on_timer--; } else { /* Turn OFF light */ output = 0; } }
else { /* Reset Timer */ on_timer = 3000;
/* Turn OFF light */ output = 0; }
}task(40ms) { light_update(); }
--------------------------------------- Posted through