Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // STRUCTURE of program: 2 processes: main process w/ 4 threads, 1 child process that reads input one line at a time form file
- // 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]
- // 4 queues, one for each LED => Queue qs[4];
- // 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
- // (!!) Start the threads only after you added all data to the queues - after waitpid in main for child process
- // Reuse the same functions for each LED. -> Create function parametrised by LedID in [0, 3]
- //Ensure your program works correctly by piping output from the lightshow.txt into your own program -> DONE as above (2 processes w/ fork())
- #include <wiringPi.h>
- #include <softPwm.h> // Notice the extra included library
- #include "queue.h"
- #include <pthread.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- // define led output pins
- #define LED1 7
- #define LED2 0
- #define LED3 2
- #define LED4 3
- #define LEDS_COUNT 4
- #define QUEUES_COUNT 4
- int LEDs[LEDS_COUNT] = {LED1, LED2, LED3, LED4};
- int tidsIds[LEDS_COUNT];
- Queue qs[QUEUES_COUNT]; // 1. Create four queues, one for each LED
- pthread_t tid1;
- pthread_t tids[QUEUES_COUNT];
- void setAllLeds() {
- for (int pin = 0; pin < QUEUES_COUNT; ++pin) { // Initialise pin for PWM output
- pinMode(LEDs[pin], 0);
- softPwmCreate(LEDs[pin], 0, 100);
- softPwmWrite(LEDs[pin], 0);
- initQueue(&qs[pin]);
- tidsIds[pin] = pin;
- }
- }
- void* read_input_callback(void* arg) {
- int led, brightness, duration;
- while (scanf("%d %d %d", &led, &brightness, &duration) == 3) {
- pthread_mutex_lock(&qs[led].mutex);
- addToQueue(&qs[led], brightness, duration);
- pthread_mutex_unlock(&qs[led].mutex);
- }
- return NULL;
- }
- void* process_data_callback(void* arg) {
- int brightness, duration_ms;
- int led = * ((int*) arg);
- while (1) {
- // printf("%d %d %d\n", i, brightness, duration_ms);
- if (queueSize(&qs[led]) > 0) // empty queue
- {
- pthread_mutex_lock(&qs[led].mutex);
- removeFromQueue(&qs[led], &brightness, &duration_ms);
- pthread_mutex_unlock(&qs[led].mutex);
- softPwmWrite(LEDs[led], brightness);
- usleep(duration_ms * 1000); // transform `ms` in `us`
- }
- else {
- //Turn of the 'i'_th led completely
- softPwmWrite(LEDs[led], 0);
- usleep(10 * 1000);
- return NULL;
- }
- }
- return NULL;
- }
- int main() {
- wiringPiSetup();
- setAllLeds();
- for (int pin = 0; pin < 4; ++pin)
- softPwmWrite(LEDs[pin], 0);
- pthread_create(&tid1, NULL, read_input_callback, NULL);
- pthread_join(tid1, NULL);
- /*
- Create four threads, one for each LED, that fetch one by one the elements from their respective
- queues and set the LED to the indicated brightness for the specified duration. When the
- thread’s queue is empty, turn off the corresponding LED.
- */
- for (int i = 0; i < QUEUES_COUNT; ++i) {
- pthread_create(&tids[i], NULL, process_data_callback, &tidsIds[i]);
- }
- for (int i = 0; i < QUEUES_COUNT; ++i) {
- pthread_join(tids[i], NULL);
- }
- // puts("DA");
- for (int pin = 0; pin < 4; ++pin)
- softPwmWrite(LEDs[pin], 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement