-1

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?

Jay Kominek
  • 8,674
  • 1
  • 34
  • 51
CrossNox
  • 53
  • 1
  • 7
  • 2
    Why don't rewrite this code as bash script and then do your multi-threading in bash? IMO it would be much more simpler. Besides, the whole task (triggering GPIO lines) by definition can be solved much more elegant in scripting language (i.e. bash) rather than C. Or I am missing something? – Sam Protsenko May 04 '15 at 22:52
  • No, you are actually right. It is just i have been using the wiringPi library and we are studying C in my university classes, so i just preferred to do it with it. – CrossNox May 06 '15 at 00:01

1 Answers1

0

does this have to be in c? because it seems the task could be much more easily implemented otherwise. for instance you could use a bash script that runs every minute to check the status of the internet and then use a python script to interact with the gpio pins to turn on the leds.

jacoballenwood
  • 2,787
  • 2
  • 24
  • 39