forked from feilipu/Arduino_FreeRTOS_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayQueue.ino
More file actions
160 lines (137 loc) · 3.81 KB
/
Copy pathArrayQueue.ino
File metadata and controls
160 lines (137 loc) · 3.81 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Example of a basic FreeRTOS queue
* https://www.freertos.org/Embedded-RTOS-Queues.html
*/
// Include Arduino FreeRTOS library
#include <Arduino_FreeRTOS.h>
// Include queue support
#include <queue.h>
// Define a Array
int pinReadArray[4]={0,0,0,0};
//Function Declaration
void TaskBlink(void *pvParameters);
void TaskAnalogReadPin0(void *pvParameters);
void TaskAnalogReadPin1(void *pvParameters);
void TaskSerial(void *pvParameters);
/*
* Declaring a global variable of type QueueHandle_t
*
*/
QueueHandle_t arrayQueue;
void setup() {
/**
* Create a queue.
* https://www.freertos.org/a00116.html
*/
arrayQueue=xQueueCreate(10, //Queue length
sizeof(int)); //Queue item size
if(arrayQueue!=NULL){
// Create task that consumes the queue if it was created.
xTaskCreate(TaskSerial,// Task function
"PrintSerial",// Task name
128,// Stack size
NULL,
2,// Priority
NULL);
// Create task that publish data in the queue if it was created.
xTaskCreate(TaskAnalogReadPin0, // Task function
"AnalogRead1",// Task name
128,// Stack size
NULL,
1,// Priority
NULL);
// Create other task that publish data in the queue if it was created.
xTaskCreate(TaskAnalogReadPin1,// Task function
"AnalogRead2",// Task name
128,// Stack size
NULL,
1,// Priority
NULL);
xTaskCreate(TaskBlink,// Task function
"Blink", // Task name
128,// Stack size
NULL,
0,// Priority
NULL);
}
}
void loop() {}
/**
* Analog read task for Pin A0
* Reads an analog input on pin 0 and send the readed value through the queue.
* See Blink_AnalogRead example.
*/
void TaskAnalogReadPin0(void *pvParameters){
(void) pvParameters;
for (;;){
pinReadArray[0]=0;
pinReadArray[1]=analogRead(A0);
/**
* Post an item on a queue.
* https://www.freertos.org/a00117.html
*/
xQueueSend(arrayQueue,&pinReadArray,portMAX_DELAY);
// One tick delay (15ms) in between reads for stability
vTaskDelay(1);
}
}
/**
* Analog read task for Pin A1
* Reads an analog input on pin 1 and send the readed value through the queue.
* See Blink_AnalogRead example.
*/
void TaskAnalogReadPin1(void *pvParameters){
(void) pvParameters;
for (;;){
pinReadArray[2]=1;
pinReadArray[3]=analogRead(A1);
/**
* Post an item on a queue.
* https://www.freertos.org/a00117.html
*/
xQueueSend(arrayQueue,&pinReadArray,portMAX_DELAY);
// One tick delay (15ms) in between reads for stability
vTaskDelay(1);
}
}
/**
* Serial task.
* Prints the received items from the queue to the serial monitor.
*/
void TaskSerial(void *pvParameters){
(void) pvParameters;
// Init Arduino serial
Serial.begin(9600);
// Wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
while (!Serial) {
vTaskDelay(1);
}
for (;;){
if(xQueueReceive(arrayQueue,&pinReadArray,portMAX_DELAY) == pdPASS ){
Serial.print("PIN:");
Serial.println(pinReadArray[0]);
Serial.print("value:");
Serial.println(pinReadArray[1]);
Serial.print("PIN:");
Serial.println(pinReadArray[2]);
Serial.print("value:");
Serial.println(pinReadArray[3]);
vTaskDelay(500/portTICK_PERIOD_MS);
}
}
}
/*
* Blink task.
* See Blink_AnalogRead example.
*/
void TaskBlink(void *pvParameters){
(void) pvParameters;
pinMode(LED_BUILTIN,OUTPUT);
digitalWrite(LED_BUILTIN,LOW);
for (;;){
digitalWrite(LED_BUILTIN,HIGH);
vTaskDelay(250/portTICK_PERIOD_MS);
digitalWrite(LED_BUILTIN,LOW);
vTaskDelay(250/portTICK_PERIOD_MS);
}
}