diff --git a/HW7_Mutex/led.h b/HW7_Mutex/led.h new file mode 100644 index 0000000000000000000000000000000000000000..293cb68adf0d61fabba49f9714becf07016f5f8f --- /dev/null +++ b/HW7_Mutex/led.h @@ -0,0 +1,12 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <wiringPi.h> +#include <pthread.h> + +#define RED 1 +#define GREEN 4 +#define YELLOW 5 + +void led_green_task(void *args); +void led_yellow_task(void *args); \ No newline at end of file diff --git a/HW7_Mutex/led_green.c b/HW7_Mutex/led_green.c new file mode 100644 index 0000000000000000000000000000000000000000..a62721fc7371a2eabda26fac14c60bc38dbb447b --- /dev/null +++ b/HW7_Mutex/led_green.c @@ -0,0 +1,14 @@ +#include "led.h" + +void led_green_task(void* args) { + pinMode(GREEN, OUTPUT); + + for (int i = 0; i < (int)args; i++) { + digitalWrite(GREEN, HIGH); + printf("GREEN TURN ON\n"); + sleep(2); + digitalWrite(GREEN, LOW); + printf("GREEN TURN OFF\n"); + sleep(2); + } +} \ No newline at end of file diff --git a/HW7_Mutex/led_main.c b/HW7_Mutex/led_main.c new file mode 100644 index 0000000000000000000000000000000000000000..6f941097b16a335feb8336d7d8f0adb8c9d54209 --- /dev/null +++ b/HW7_Mutex/led_main.c @@ -0,0 +1,62 @@ +#include "led.h" + +void led_red_task(void) { + pinMode(RED, OUTPUT); + + for (int i = 0; i < 10; i++) { + digitalWrite(RED, HIGH); + printf("RED TURN ON\n"); + sleep(1); + digitalWrite(RED, LOW); + printf("RED TURN OFF\n"); + sleep(1); + } +} + +int main(int argc, char* argv[]) { + int gno; + int time; + + + wiringPiSetup(); + + if (argc < 2) { + printf("Insert Correct value, try again\n"); + exit(-1); + } + gno = atoi(argv[1]); + time = atoi(argv[2]); + + + pthread_t G; + pthread_t Y; + + if (gno == 1) { + if ((pthread_create(&G, NULL, led_green_task, (void*)time)) < 0) { + perror("thread create error:"); + exit(0); + } + } + else if (gno == 2) { + if ((pthread_create(&Y, NULL, led_yellow_task, (void*)time)) < 0){ + perror("thread create error:"); + exit(0); + } + } + else if (gno == 3) { + if ((pthread_create(&G, NULL, led_green_task, (void*)time)) < 0) { + perror("thread create error:"); + exit(0); + } + if ((pthread_create(&Y, NULL, led_yellow_task, (void*)time)) < 0){ + perror("thread create error:"); + exit(0); + } + } + led_red_task(); + + + + pthread_exit(NULL); + return 0; +} \ No newline at end of file diff --git a/HW7_Mutex/led_yellow.c b/HW7_Mutex/led_yellow.c new file mode 100644 index 0000000000000000000000000000000000000000..b1d352a9e1c64b4d39d163bb89150640899d62c6 --- /dev/null +++ b/HW7_Mutex/led_yellow.c @@ -0,0 +1,14 @@ +#include "led.h" + +void led_yellow_task(void* args) { + pinMode(YELLOW, OUTPUT); + + for (int i = 0; i < (int)args; i++) { + digitalWrite(YELLOW, HIGH); + printf("YELLOW TURN ON\n"); + sleep(3); + digitalWrite(YELLOW, LOW); + printf("YELLOW TURN OFF\n"); + sleep(3); + } +} \ No newline at end of file