I have searched the possible solution on many forums for several days but have no luck; ( I post my question here and your reply is greatly appreciated.
IDEA: Use a script to control lights (in C under Linux)
APPLICATION SCENARIO I have three lights: red, blue and green. The script has the schedules to control them. For example, From now in 10 seconds, turn on the red light for 2 seconds; From now in 15 seconds, turn on the blue light for 10 seconds; From now in 21 seconds, turn on the red light for 5 seconds; From now in 5 seconds, turn on the green light for 7 seconds; From now in 103 seconds, turn on the green light for 11 seconds; ….. When and how long to turn on the light are totally arbitrary. And this program should be able to expand to hundred lights and thousands schedules.
HOW TO CODE IT My idea is to have two processes and one mailbox : The first process reads the script file and parse the schedules into many timers. Once the timer expires, it send a message (including light ID and action – ON or OFF --) to the mailbox. The second process is to turn on or off the specified light based on the message from the mailbox.
Each schedule will be parsed into two timers: Schedule: From now in 10 seconds, turn on the red light for 2 seconds; Parsed to: Timer 1: timer will expire in 10 seconds; once expire, it passes the light ID (red light) and action (ON) as a message to the mailbox; Timer 2: timer will expire in (10+2) seconds; once expire, it passes the light ID (red light) and action (OFF) as a message to the mailbox;
The second process continually checks the mailbox and takes the proper action on proper light based on the received message.
MY QUESTION The timer in Linux () only issues the same SIGALRM signal once expired. It’s not possible for me to pass the light ID and action to the mailbox. Do I have any other way to do it? Thanks a lot.