0

I'm not a C developer but i need to write simple program and i have problems with delay. Here is my program:

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

#include <wiringPi.h>
#include <softPwm.h>

int main (int argc, char *argv[])
{
  int val = 10;
  if (argc > 1) {
    val = atoi(argv[1]);
  }
  wiringPiSetup ()  ;

  pinMode(1, OUTPUT);
  softPwmCreate (1, 0, 100) ;
  printf ("Soft Pwm created: %s!\n", argv[1]) ;

  softPwmWrite (1, val) ;

  delay (200);

  return 0;
}

And it works pretty well until i delete row with delay (200). How can i wait until function softPwmWrite complete without delay() before program has done? Im' using Linux and wiringPi library. Thanks.

Quentin
  • 62,093
  • 7
  • 131
  • 191
Footniko
  • 2,682
  • 2
  • 27
  • 36
  • If you have any side-effects of `softPwmWrite ()`, you can check that in a loop before finishing, but that's pretty course way, anyway. – Sourav Ghosh Jul 16 '15 at 07:27
  • 2
    I think that's not a question about language, but about a particular library, `softPwm`, I thinik. – Petr Jul 16 '15 at 07:32
  • `delay` is not a standard C library, and C is not the same as C++ (so choose one!). You probably are using some operating system, name it! – Basile Starynkevitch Jul 16 '15 at 07:34
  • @BasileStarynkevitch, yeah, sorry, just edited my question. When i said i'm not a C/C++ developer i meant i've never write code for such level. – Footniko Jul 16 '15 at 07:53
  • @Petr, yes. You are right. Sorry, i just need to solve this problem and i don't have enough reputation to put wiringPi in the tag section. – Footniko Jul 16 '15 at 07:55
  • Why so many downvotes? Pls leave some comments. I've noticed i'm not C/C++ developer and never code on such level before. What's wrong? – Footniko Jul 16 '15 at 07:58
  • 1
    Probably people didn't get that you couldn't create the missing tags. – Quentin Jul 16 '15 at 08:34

1 Answers1

1

Include pthread.h and call pthread_exit:

#include <pthread.h>

....

softPwmWrite (1, val) ;
pthread_exit(0);
}

When the softPwmWrite returns, it'll exit the program. softPwmWrite uses threads and you just need ensure your program doesn't die before the threads complete. When all threads complete, the process will exit at the end.

P.P
  • 117,907
  • 20
  • 175
  • 238