]> git.piffa.net Git - sketchbook_andrea/blob - multitasking/blink_10_freeRTOS/blink_10_freeRTOS.ino
rtos
[sketchbook_andrea] / multitasking / blink_10_freeRTOS / blink_10_freeRTOS.ino
1 /* Blink without Delay: RTOS
2
3    Implementazione di due processi indipendenti:
4    TaskFirstBlink e  TaskSecondBlink
5    per far lampeggiare due LED tramite il framework FreeRTOS
6
7  * E' necessario installare la libreria: Arduino_FreeRTOS
8 - https://github.com/feilipu/Arduino_FreeRTOS_Library
9 - https://www.hackster.io/feilipu/using-freertos-multi-tasking-in-arduino-ebc3cc
10  */
11 #include <Arduino_FreeRTOS.h>
12
13 // define two tasks for Blink & AnalogRead
14 void TaskFirstBlink( void *pvParameters );
15 void TaskSecondBlink( void *pvParameters );
16
17 // the setup function runs once when you press reset or power the board
18 void setup() {
19
20   // Now set up two tasks to run independently.
21   xTaskCreate(
22     TaskFirstBlink
23     ,  (const portCHAR *)"First Blink"   // A name just for humans
24     ,  128  // This stack size can be checked & adjusted by reading the Stack Highwater
25     ,  NULL
26     ,  2  // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
27     ,  NULL );
28
29   xTaskCreate(
30     TaskSecondBlink
31     ,  (const portCHAR *) "Sec Blink"
32     ,  128  // Stack size
33     ,  NULL
34     ,  1  // Priority
35     ,  NULL );
36
37   // Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
38 }
39
40 void loop()
41 {
42   // Empty. Things are done in Tasks.
43 }
44
45 /*
46   This example code is in the public domain.
47
48   modified 8 May 2014
49   by Scott Fitzgerald
50   
51   modified 2 Sep 2016
52   by Arturo Guadalupi
53
54   modified 9 Gen 2017 
55   by Andrea Manni
56 */
57
58 /*--------------------------------------------------*/
59 /*---------------------- Tasks ---------------------*/
60 /*--------------------------------------------------*/
61
62 // First Task
63 void TaskFirstBlink(void *pvParameters)  // This is a task.
64 {
65   (void) pvParameters;
66
67 // initialize digital LED_BUILTIN on pin 13 as an output.
68 byte led = 13;
69 const int pausa = 500;
70 pinMode(led, OUTPUT);
71
72   for (;;) // Equivalent to the classic loop() function
73   {
74     digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
75     vTaskDelay( pausa / portTICK_PERIOD_MS ); // wait for one second
76     digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
77     vTaskDelay( pausa / portTICK_PERIOD_MS ); // wait for one second
78   }
79 }
80
81 // Second Task
82 void TaskSecondBlink(void *pvParameters)  // This is a task.
83 {
84   (void) pvParameters;
85   
86 byte led = 3;
87 const int pausa = 1000;
88 pinMode(led, OUTPUT);
89
90   for (;;) 
91   {
92     digitalWrite(3, HIGH);   // turn the LED on (HIGH is the voltage level)
93     vTaskDelay( pausa / portTICK_PERIOD_MS ); // wait for one second
94     digitalWrite(3, LOW);    // turn the LED off by making the voltage LOW
95     vTaskDelay( pausa / portTICK_PERIOD_MS ); // wait for one second
96   }
97 }