Skip to content

Commit 3172b51

Browse files
authored
Merge pull request #69 from Derekduke/master
Add an example of task state switching
2 parents 03acee1 + 7865597 commit 3172b51

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

examples/TaskStatus/TaskStatus.ino

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <Arduino_FreeRTOS.h>
2+
3+
//define task handles
4+
TaskHandle_t TaskBlink_Handler;
5+
TaskHandle_t TaskSerial_Handler;
6+
7+
// define two tasks for Blink & Serial
8+
void TaskBlink( void *pvParameters );
9+
void TaskSerial(void* pvParameters);
10+
11+
// the setup function runs once when you press reset or power the board
12+
void setup() {
13+
// initialize serial communication at 9600 bits per second:
14+
Serial.begin(9600);
15+
16+
while (!Serial) {
17+
; // wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
18+
}
19+
20+
// Now set up two tasks to run independently.
21+
xTaskCreate(
22+
TaskBlink
23+
, "Blink" // A name just for humans
24+
, 128 // This stack size can be checked & adjusted by reading the Stack Highwater
25+
, NULL //Parameters passed to the task function
26+
, 2 // Priority, with 2 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
27+
, &TaskBlink_Handler );//Task handle
28+
29+
xTaskCreate(
30+
TaskSerial
31+
, "Serial"
32+
, 128 // Stack size
33+
, NULL //Parameters passed to the task function
34+
, 1 // Priority
35+
, &TaskSerial_Handler ); //Task handle
36+
}
37+
38+
39+
void loop()
40+
{
41+
// Empty. Things are done in Tasks.
42+
}
43+
44+
/*--------------------------------------------------*/
45+
/*---------------------- Tasks ---------------------*/
46+
/*--------------------------------------------------*/
47+
48+
void TaskSerial(void* pvParameters){
49+
/*
50+
Serial
51+
Send "s" or "r" through the serial port to control the suspend and resume of the LED light task.
52+
This example code is in the public domain.
53+
*/
54+
(void) pvParameters;
55+
for (;;) // A Task shall never return or exit.
56+
{
57+
while(Serial.available()>0){
58+
switch(Serial.read()){
59+
case 's':
60+
vTaskSuspend(TaskBlink_Handler);
61+
Serial.println("Suspend!");
62+
break;
63+
case 'r':
64+
vTaskResume(TaskBlink_Handler);
65+
Serial.println("Resume!");
66+
break;
67+
}
68+
vTaskDelay(1);
69+
}
70+
}
71+
}
72+
73+
void TaskBlink(void *pvParameters) // This is a task.
74+
{
75+
(void) pvParameters;
76+
pinMode(LED_BUILTIN, OUTPUT);
77+
for (;;) // A Task shall never return or exit.
78+
{
79+
//Serial.println(11);
80+
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
81+
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
82+
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
83+
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
84+
}
85+
}

0 commit comments

Comments
 (0)