forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBasicMultiThreading.ino
122 lines (104 loc) · 4.49 KB
/
BasicMultiThreading.ino
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
/* Basic Multi Threading Arduino Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
// Please read file README.md in the folder containing this example.
#if CONFIG_FREERTOS_UNICORE
#define TASK_RUNNING_CORE 0
#else
#define TASK_RUNNING_CORE 1
#endif
#define ANALOG_INPUT_PIN A0
#ifndef LED_BUILTIN
#define LED_BUILTIN 13 // Specify the on which is your LED
#endif
// Define two tasks for Blink & AnalogRead.
void TaskBlink(void *pvParameters);
void TaskAnalogRead(void *pvParameters);
TaskHandle_t analog_read_task_handle; // You can (don't have to) use this to be able to manipulate a task from somewhere else.
// The setup function runs once when you press reset or power on the board.
void setup() {
// Initialize serial communication at 115200 bits per second:
Serial.begin(115200);
// Set up two tasks to run independently.
uint32_t blink_delay = 1000; // Delay between changing state on LED pin
xTaskCreate(
TaskBlink, "Task Blink" // A name just for humans
,
2048 // The stack size can be checked by calling `uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);`
,
(void *)&blink_delay // Task parameter which can modify the task behavior. This must be passed as pointer to void.
,
2 // Priority
,
NULL // Task handle is not used here - simply pass NULL
);
// This variant of task creation can also specify on which core it will be run (only relevant for multi-core ESPs)
xTaskCreatePinnedToCore(
TaskAnalogRead, "Analog Read", 2048 // Stack size
,
NULL // When no parameter is used, simply pass NULL
,
1 // Priority
,
&analog_read_task_handle // With task handle we will be able to manipulate with this task.
,
TASK_RUNNING_CORE // Core on which the task will run
);
Serial.printf("Basic Multi Threading Arduino Example\n");
// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}
void loop() {
if (analog_read_task_handle != NULL) { // Make sure that the task actually exists
delay(10000);
vTaskDelete(analog_read_task_handle); // Delete task
analog_read_task_handle = NULL; // prevent calling vTaskDelete on non-existing task
}
}
/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/
void TaskBlink(void *pvParameters) { // This is a task.
uint32_t blink_delay = *((uint32_t *)pvParameters);
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
If you want to know what pin the on-board LED is connected to on your ESP32 model, check
the Technical Specs of your board.
*/
// initialize digital LED_BUILTIN on pin 13 as an output.
pinMode(LED_BUILTIN, OUTPUT);
for (;;) { // A Task shall never return or exit.
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
// arduino-esp32 has FreeRTOS configured to have a tick-rate of 1000Hz and portTICK_PERIOD_MS
// refers to how many milliseconds the period between each ticks is, ie. 1ms.
delay(blink_delay);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(blink_delay);
}
}
void TaskAnalogRead(void *pvParameters) { // This is a task.
(void)pvParameters;
// Check if the given analog pin is usable - if not - delete this task
if (digitalPinToAnalogChannel(ANALOG_INPUT_PIN) == -1) {
Serial.printf("TaskAnalogRead cannot work because the given pin %d cannot be used for ADC - the task will delete itself.\n", ANALOG_INPUT_PIN);
analog_read_task_handle = NULL; // Prevent calling vTaskDelete on non-existing task
vTaskDelete(NULL); // Delete this task
}
/*
AnalogReadSerial
Reads an analog input on pin A3, prints the result to the serial monitor.
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
Attach the center pin of a potentiometer to pin A3, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
for (;;) {
// read the input on analog pin:
int sensorValue = analogRead(ANALOG_INPUT_PIN);
// print out the value you read:
Serial.println(sensorValue);
delay(100); // 100ms delay
}
}