I'm doing a program, written in C for my Raspberry Pi, to turn on and off some lights depending on whether it's connected or not to the Internet.
The code i finished and works as i want it to is this:
int doPing() {
int stat = system("ping -c 1 google.com");
return stat;
}
int main() {
int status;
int prev_status, isdif;
status = doPing();
if(status==0)
system("sudo /home/pi/goingWellBlink &");
else
system("sudo /home/pi/errorBlink &");
prev_status = status;
for(;;) {
status=doPing();
if(status!=prev_status)
isdif=1;
else
isdif=0;
if(isdif && status==0) {
system("sudo pkill errorBlink &");
system("sudo /home/pi/goingWellBlink &");
}
if(isdif && status!=0) {
system("sudo pkill goingWellBlink &");
system("sudo /home/pi/errorBlink &");
}
prev_status=status;
sleep(10);
}
return 0;
}
Where errorBlink
and goingWellBlink
are programs I have already compiled that run in an infinite loop, doing some different light sequence each.
Basically, the main starts asking for the return status of ping, and starts one of the light sequences based on that. That light sequence won't change unless a change in the return status of ping is detected.
For portability purposes, and not depending on two files being exactly there, I want to insert in the code, errorBlink
and goingWellBlink
as void functions and do this same code but with threads. Instead of starting an external program, starting a thread with the light sequence, how could I achieve this?