0

I am making an assignment for for school and the goal of the assignment is to use Pthread Barriers, to make the LEDs on the raspberry pi slowly fades from unlit to lit and vice versa. I am a beginner to C and I think I udnerstand the cocept of pthread sna dn pthread barriers.

the following is the code that I have made, it should make the LED in raspPI to fade from unlit to lit:

// Setup wiringPi
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

// define led output PINS
#define LED1 7
#define LED2 0
#define LED3 2
#define LED4 3

pthread_barrier_t* mythread;

void ledInit(void){
    // Initialize pin for PMW output
    softPwmCreate(LED1, 0, 100);
    softPwmCreate(LED2, 0, 100);
    softPwmCreate(LED3, 0, 100);
    softPwmCreate(LED4, 0, 100);
}

// a thread that slowly fades LEDs from unlit to lit. 
void* unlitToLit(void){
    ledInit();
    softPwmWrite(LED1, 50);
    softPwmWrite(LED2, 50);
    softPwmWrite(LED3, 50);
    softPwmWrite(LED4, 50);
}

void main(void){
    wiringPiSetup();
    //set LED1 pin to output
    pinMode(LED1, OUTPUT);
    pinMode(LED2, OUTPUT);
    pinMode(LED3, OUTPUT);
    pinMode(LED4, OUTPUT);

    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);

    pthread_barrier_init(&mythread, NULL, unlitToLit);

    if (pthread_create(&mythread, NULL, unlitToLit, )) {
        fprintf(stderr, "Error creating thread\n");
        return 1;
    }

    printf("threads finished");
    return 0;

      // used for synchronization
      // pthread_barrier_wait(&mythread);
}

In my view the code is almost correct, but its not working, could you please help me to find out and fix the problem.

list of errors/warnings:

3_1.c:15:5: warning: implicit declaration of function ‘softPwmCreate’ [-Wimplicit-function-declaration]
     softPwmCreate(LED1, 0, 100);
     ^
3_1.c: At top level:
3_1.c:21:1: warning: return type defaults to ‘int’
 unlitToLit(void){
 ^
3_1.c: In function ‘unlitToLit’:
3_1.c:23:5: warning: implicit declaration of function ‘softPwmWrite’ [-Wimplicit-function-declaration]
     softPwmWrite(LED1, 50);
     ^
3_1.c: At top level:
3_1.c:28:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main(void){
      ^
3_1.c: In function ‘main’:
3_1.c:40:43: warning: passing argument 3 of ‘pthread_barrier_init’ makes integer from pointer without a cast
     pthread_barrier_init(&mythread, NULL, unlitToLit);
                                           ^
In file included from 3_1.c:5:0:
/usr/include/pthread.h:1079:12: note: expected ‘unsigned int’ but argument is of type ‘int (*)(void)’
 extern int pthread_barrier_init (pthread_barrier_t *__restrict __barrier,
            ^
3_1.c:42:53: error: expected expression before ‘)’ token
     if (pthread_create(&mythread, NULL, unlitToLit, )) {
                                                     ^
3_1.c:42:24: warning: passing argument 1 of ‘pthread_create’ from incompatible pointer type
     if (pthread_create(&mythread, NULL, unlitToLit, )) {
                        ^
In file included from 3_1.c:5:0:
/usr/include/pthread.h:244:12: note: expected ‘pthread_t * restrict’ but argument is of type ‘union pthread_barrier_t *’
 extern int pthread_create (pthread_t *__restrict __newthread,
            ^
3_1.c:42:41: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
     if (pthread_create(&mythread, NULL, unlitToLit, )) {
                                         ^
In file included from 3_1.c:5:0:
/usr/include/pthread.h:244:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘int (*)(void)’
 extern int pthread_create (pthread_t *__restrict __newthread,
            ^
3_1.c:44:9: warning: ‘return’ with a value, in function returning void
         return 1;
         ^
3_1.c:48:5: warning: ‘return’ with a value, in function returning void
     return 0;
     ^
3_1.c: In function ‘unlitToLit’:
3_1.c:27:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
makefile:12: recipe for target '3_1' failed
S. N
  • 3,456
  • 12
  • 42
  • 65
  • You need to change the pulse width if you actually want the LEDs to fade... softPwmWrite(LED1, 50); will set the pulse width to 50 % which will make the LED be "half" lit. You need to repeatedly change the PWM from 0 to 100 and vice versa... – Pethead Apr 06 '18 at 13:50
  • "but its not working" What is not working? What is the problem? – Lundin Apr 06 '18 at 13:53

2 Answers2

1
   pthread_barrier_init(&mythread, NULL, unlitToLit);

mythread variable is defined as a pointer so you are passing a memory address of a pointer here. You should define your mythread variable as follows:

pthread_barrier_t mythread;

Also the purpose of using barriers (and IMHO the assignment) is to synchronize threads, so that they continue execution from some stabilized shared state. In your case that would be all LEDS lit or unlit. You need to add pthread_barrier_wait to synchronize them.

There is yet another problem in:

pthread_barrier_init(&mythread, NULL, unlitToLit);

As the third argument is count which is used to specify how many threads should call pthread_barrier_wait for barrier to unlock. In your call you pass a function address which is wrong.

pbn
  • 2,406
  • 2
  • 26
  • 39
  • do I need to define the number of threads using "#define THREAD_COUNT 2" or something like that and later pass this to my pthread_barrier_init? – S. N Apr 06 '18 at 14:05
0

By reading the documentation of the library (Software PWM Library reference) that you are using I can see that you need to include:

#include <softPwm.h>

And I can also read this for the function softPwmWrite():

void softPwmWrite (int pin, int value) ;

This updates the PWM value on the given pin. The value is checked to be in-range and pins that haven’t previously been initialised via softPwmCreate will be silently ignored. I can't see that you have initialised the pins as PWM pins.

You also need to change the pulse width if you actually want the LEDs to fade... softPwmWrite(LED1, 50); will set the pulse width to 50 % which will make the LED be "half" lit. You need to repeatedly change the PWM from 0 to 100 and vice versa...

Pethead
  • 178
  • 2
  • 6
  • 15
  • thank you, so the way that I can think of to do this is to make a loop that cahnegs the values for each led from low to high or viceverca!!, however I am still getting many warnings and I cant not compile te code! – S. N Apr 06 '18 at 14:00