fork(1) download
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <time.h>
  4. //#include <wiringPi.h>
  5. #define HIGH 1
  6. #define LOW 0
  7.  
  8. int led_freq = 100; // Hz
  9.  
  10.  
  11. int global_set_first = HIGH;
  12. int global_set_second = LOW;
  13. long period_us = 5000;
  14. int cycle_ish = 200;
  15.  
  16. void set_led_brightness(int i){
  17.  
  18. if (i > led_freq || i < 1) {
  19. i = led_freq / 2;
  20. }
  21.  
  22. if (i > led_freq/2) {
  23. i = led_freq - i;
  24. global_set_first = LOW;
  25. global_set_second = HIGH;
  26. } else {
  27. global_set_first = HIGH;
  28. global_set_second = LOW;
  29. }
  30. cycle_ish = led_freq/i;
  31.  
  32. }
  33.  
  34. void timespec_add_us(struct timespec *t, long us)
  35. {
  36. // add microseconds to timespecs nanosecond counter
  37. t->tv_nsec += us*1000;
  38.  
  39. // if wrapping nanosecond counter, increment second counter
  40. if (t->tv_nsec > 1000000000)
  41. {
  42. t->tv_nsec -= 1000000000;
  43. t->tv_sec += 1;
  44. }
  45. }
  46.  
  47. void* task(void* v){
  48.  
  49. int counter = 0;
  50. struct timespec wait_time_v;
  51. clock_gettime(CLOCK_REALTIME, &wait_time_v);
  52.  
  53. while (1) {
  54.  
  55. if (counter >= cycle_ish) {
  56. //digitalWrite(0, global_set_first);
  57. counter = 0;
  58. } else {
  59. //digitalWrite(0,global_set_second);
  60. }
  61.  
  62. timespec_add_us(&wait_time_v, period_us);
  63. clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &wait_time_v, NULL);
  64.  
  65. counter++;
  66. }
  67. return 0;
  68. }
  69.  
  70. int main(int argc, char *argv[]){
  71. period_us = 1000000/led_freq;
  72.  
  73. int i=100;
  74. if (argc == 2) {
  75. i = atoi(argv[1]);
  76. }
  77.  
  78. // wiringPiSetup();
  79. // pinMode(0, OUTPUT);
  80.  
  81. pthread_t p;
  82.  
  83. pthread_create(&p, NULL, task, (void*) &i);
  84. int input;
  85. while (1) {
  86.  
  87. scanf("%d", &input);
  88. //set_led_brightness(input);
  89. }
  90. /*
  91. int a;
  92. for (a = 199; a > 1; a-=10) {
  93. set_led_brightness(a);
  94. printf("i = %d\n", a);
  95. usleep(1000000);
  96. }
  97. for (a = 1; a < 200; a+=10) {
  98. set_led_brightness(a);
  99. printf("i = %d\n", a);
  100. usleep(1000000);
  101. }
  102. */
  103. pthread_join(p, NULL);
  104.  
  105.  
  106. return 0;
  107. }
  108.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘main’:
prog.c:75:3: warning: implicit declaration of function ‘atoi’ [-Wimplicit-function-declaration]
   i = atoi(argv[1]);
   ^
/home/9O5uq1/cc64YdI0.o: In function `main':
prog.c:(.text.startup+0x4d): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty