-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
102 lines (86 loc) · 3.33 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
/**
* Brief:
* This code shows a basic logic to perform inter-task communication using multiple queues
*
* GPIO status:
* GPIO2 : output ( built-in LED on Devkit V1 )
*
*/
#define builtin 2 //++ Define the built-in LED on Devkit V1
QueueHandle_t q=NULL; //++ Create a handle for Queue
QueueHandle_t p=NULL; //++ Create a handle for Queue
void led_task(void *pvParameter) //++ Queue recieve task
{
unsigned long counter;
bool flag = false;
if(q == NULL){
printf("Counter Queue is not ready \n");
return;
}
if(p == NULL){
printf("Flag Queue is not ready \n");
return;
}
while(1){
xQueueReceive(q,&counter,(TickType_t )(250/portTICK_PERIOD_MS)); //++ Receive the queue counter value
xQueueReceive(p,&flag,(TickType_t )(250/portTICK_PERIOD_MS)); //++ Receive the queue flag status
if(counter%10 == 0){ //++ For every count number divisible by 10, enter the condition
printf("value received on queue: %lu \n",counter);
gpio_set_level(builtin,1); //++ Turn on built-in LED
vTaskDelay(500/portTICK_PERIOD_MS);
}
if(flag==true)
{
printf("value received on queue was multiple of 5\n");
flag = false; //++ Reset the flag for next queue
}
vTaskDelay(100/portTICK_PERIOD_MS);
gpio_set_level(builtin,0); //++ Turn off built-in LED
}
}
void count_task(void *pvParameter){ //++ Queue sending task
unsigned long counter=1;
bool flag = false;
if(q == NULL){
printf("Counter Queue is not ready \n");
return;
}
if(p == NULL){
printf("Flag Queue is not ready \n");
return;
}
while(1){
printf("value sent on queue: %lu \n",counter);
xQueueSend(q,(void *)&counter,(TickType_t )0); //++ Add counter value on the Queue
counter++;
if(counter%5 == 0)
{
flag = true;
xQueueSend(p,(void *)&flag,(TickType_t )0); //++ Add Flag status on the Queue
}
vTaskDelay(1000/portTICK_PERIOD_MS);
}
}
void app_main()
{
gpio_reset_pin(builtin);
gpio_set_direction(builtin, GPIO_MODE_OUTPUT); //++ Set built-in LED direction
q=xQueueCreate(20,sizeof(unsigned long)); //++ Create first queue
p=xQueueCreate(5,sizeof(bool)); //++ Create second queue
if(q != NULL && p != NULL){
printf("Queue is created\n");
vTaskDelay(1000/portTICK_PERIOD_MS);
xTaskCreate(&led_task,"led_task",2048,NULL,5,NULL);
printf("led task started\n");
xTaskCreate(&count_task,"count_task",2048,NULL,5,NULL);
printf("counter task started\n");
}else{
printf("Queue creation failed");
}
}