forked from feilipu/Arduino_FreeRTOS_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructArray.ino
More file actions
150 lines (124 loc) · 3.46 KB
/
Copy pathStructArray.ino
File metadata and controls
150 lines (124 loc) · 3.46 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
/*
* 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 Structure Array
struct Arduino{
int pin[2];
int ReadValue[2];
};
//Function Declaration
void Blink(void *pvParameters);
void POT(void *pvParameters);
void TaskSerial(void *pvParameters);
/*
* Declaring a global variable of type QueueHandle_t
*
*/
QueueHandle_t structArrayQueue;
void setup() {
/**
* Create a queue.
* https://www.freertos.org/a00116.html
*/
structArrayQueue=xQueueCreate(10, //Queue length
sizeof(struct Arduino)); //Queue item size
if(structArrayQueue!=NULL){
xTaskCreate(TaskBlink, // Task function
"Blink",// Task name
128,// Stack size
NULL,
0,// Priority
NULL);
// Create other task that publish data in the queue if it was created.
xTaskCreate(POT,// Task function
"AnalogRead",// Task name
128, // Stack size
NULL,
2,// Priority
NULL);
// Create task that consumes the queue if it was created.
xTaskCreate(TaskSerial,// Task function
"PrintSerial",// A name just for humans
128,// This stack size can be checked & adjusted by reading the Stack Highwater
NULL,
1, // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
NULL);
}
}
void loop() {}
/*
* Blink task.
* See Blink_AnalogRead example.
*/
void TaskBlink(void *pvParameters){
(void) pvParameters;
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
for(;;)
{
digitalWrite(13,HIGH);
vTaskDelay(250/portTICK_PERIOD_MS);
digitalWrite(13,LOW);
vTaskDelay(250/portTICK_PERIOD_MS);
}
}
/**
* Analog read task for Pin A0 and A1
* Reads an analog input on pin 0 and pin 1
* Send the readed value through the queue.
* See Blink_AnalogRead example.
*/
void POT(void *pvParameters){
(void) pvParameters;
pinMode(A0,INPUT);
pinMode(A1,INPUT);
for (;;){
// Read the input on analog pin 0:
struct Arduino currentVariable;
currentVariable.pin[0]=0;
currentVariable.pin[1]=1;
currentVariable.ReadValue[0]=analogRead(A0);
currentVariable.ReadValue[1]=analogRead(A1);
/**
* Post an item on a queue.
* https://www.freertos.org/a00117.html
*/
xQueueSend(structArrayQueue,¤tVariable,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 (;;){
struct Arduino currentVariable;
/**
* Read an item from a queue.
* https://www.freertos.org/a00118.html
*/
if(xQueueReceive(structArrayQueue,¤tVariable,portMAX_DELAY) == pdPASS ){
for(int i=0;i<2;i++){
Serial.print("PIN:");
Serial.println(currentVariable.pin[i]);
Serial.print("value:");
Serial.println(currentVariable.ReadValue[i]);
}
}
vTaskDelay(500/portTICK_PERIOD_MS);
}
}