Advertisement
VladNitu

4_4_rup_asfaltu

Mar 7th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. // STRUCTURE of program: 2 processes: main process w/ 4 threads, 1 child process that reads input one line at a time form file
  2. // Create pipe before "fork"-ing, fork and then: child process will write to pipe (fd[1]), so close fd[0] for it ++ parent process will read from pipe (fd[0]), so close fd[1]
  3. // 4 queues, one for each LED => Queue qs[4];
  4.  
  5. // Accept command line input one line at a time in the form LED(0-3) Brightness(0-100) Duration(ms); Add this data to the correct queue of the corresponding led
  6. // (!!) Start the threads only after you added all data to the queues - after waitpid in main for child process
  7.  
  8. // Reuse the same functions for each LED. -> Create function parametrised by LedID in [0, 3]
  9.  
  10. //Ensure your program works correctly by piping output from the lightshow.txt into your own program -> DONE as above (2 processes w/ fork())
  11.  
  12. #include <wiringPi.h>
  13. #include <softPwm.h> // Notice the extra included library
  14. #include "queue.h"
  15. #include <pthread.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19.  
  20.  
  21. // define led output pins
  22. #define LED1 7
  23. #define LED2 0
  24. #define LED3 2
  25. #define LED4 3
  26. #define LEDS_COUNT 4
  27. #define QUEUES_COUNT 4
  28.  
  29. int LEDs[LEDS_COUNT] = {LED1, LED2, LED3, LED4};
  30. int tidsIds[LEDS_COUNT];
  31.  
  32. Queue qs[QUEUES_COUNT]; // 1. Create four queues, one for each LED
  33. pthread_t tid1;
  34. pthread_t tids[QUEUES_COUNT];
  35.  
  36. void setAllLeds() {
  37. for (int pin = 0; pin < QUEUES_COUNT; ++pin) { // Initialise pin for PWM output
  38.  
  39. pinMode(LEDs[pin], 0);
  40. softPwmCreate(LEDs[pin], 0, 100);
  41. softPwmWrite(LEDs[pin], 0);
  42. initQueue(&qs[pin]);
  43. tidsIds[pin] = pin;
  44. }
  45. }
  46.  
  47.  
  48. void* read_input_callback(void* arg) {
  49. int led, brightness, duration;
  50. while (scanf("%d %d %d", &led, &brightness, &duration) == 3) {
  51. pthread_mutex_lock(&qs[led].mutex);
  52. addToQueue(&qs[led], brightness, duration);
  53. pthread_mutex_unlock(&qs[led].mutex);
  54. }
  55. return NULL;
  56. }
  57.  
  58. void* process_data_callback(void* arg) {
  59.  
  60. int brightness, duration_ms;
  61. int led = * ((int*) arg);
  62.  
  63. while (1) {
  64.  
  65.  
  66. // printf("%d %d %d\n", i, brightness, duration_ms);
  67.  
  68.  
  69. if (queueSize(&qs[led]) > 0) // empty queue
  70. {
  71.  
  72. pthread_mutex_lock(&qs[led].mutex);
  73. removeFromQueue(&qs[led], &brightness, &duration_ms);
  74. pthread_mutex_unlock(&qs[led].mutex);
  75.  
  76. softPwmWrite(LEDs[led], brightness);
  77. usleep(duration_ms * 1000); // transform `ms` in `us`
  78. }
  79. else {
  80. //Turn of the 'i'_th led completely
  81. softPwmWrite(LEDs[led], 0);
  82. usleep(10 * 1000);
  83. return NULL;
  84. }
  85. }
  86.  
  87. return NULL;
  88. }
  89.  
  90.  
  91. int main() {
  92.  
  93. wiringPiSetup();
  94. setAllLeds();
  95.  
  96. for (int pin = 0; pin < 4; ++pin)
  97. softPwmWrite(LEDs[pin], 0);
  98.  
  99.  
  100. pthread_create(&tid1, NULL, read_input_callback, NULL);
  101.  
  102. pthread_join(tid1, NULL);
  103.  
  104. /*
  105. Create four threads, one for each LED, that fetch one by one the elements from their respective
  106. queues and set the LED to the indicated brightness for the specified duration. When the
  107. thread’s queue is empty, turn off the corresponding LED.
  108. */
  109. for (int i = 0; i < QUEUES_COUNT; ++i) {
  110. pthread_create(&tids[i], NULL, process_data_callback, &tidsIds[i]);
  111. }
  112.  
  113. for (int i = 0; i < QUEUES_COUNT; ++i) {
  114. pthread_join(tids[i], NULL);
  115. }
  116. // puts("DA");
  117.  
  118. for (int pin = 0; pin < 4; ++pin)
  119. softPwmWrite(LEDs[pin], 0);
  120.  
  121. return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement