1 | #include <Arduino.h> |
---|
2 | |
---|
3 | /** |
---|
4 | * Solution to 02 - Blinky Challenge |
---|
5 | * |
---|
6 | * Toggles LED at different rates using separate tasks. |
---|
7 | * |
---|
8 | * Date: December 3, 2020 |
---|
9 | * Author: Shawn Hymel |
---|
10 | * License: 0BSD |
---|
11 | */ |
---|
12 | |
---|
13 | // Use only core 1 for demo purposes |
---|
14 | #if CONFIG_FREERTOS_UNICORE |
---|
15 | static const BaseType_t app_cpu = 0; |
---|
16 | #else |
---|
17 | static const BaseType_t app_cpu = 1; |
---|
18 | #endif |
---|
19 | |
---|
20 | // LED rates |
---|
21 | static const int rate_1 = 3000; // ms |
---|
22 | static const int rate_2 = 1000; // ms |
---|
23 | |
---|
24 | // Pins |
---|
25 | //#define LED_BUILTIN 17 // BLUE LED kidbright |
---|
26 | //#define LED_BUILTIN 15 // YELLOW LED kidbright |
---|
27 | //#define LED_BUILTIN 12 // GREEN LED kidbright |
---|
28 | //#define LED_BUILTIN 2 // RED LED kidbright |
---|
29 | //#define LED_BUILTIN 14 // PIN 14 Buzzer kidbright |
---|
30 | #define LED_BUILTIN 12 |
---|
31 | |
---|
32 | static const int led_pin = LED_BUILTIN; |
---|
33 | |
---|
34 | // Our task: blink an LED at one rate |
---|
35 | void toggleLED_1(void *parameter) { |
---|
36 | while(1) { |
---|
37 | digitalWrite(led_pin, HIGH); |
---|
38 | vTaskDelay(rate_1 / portTICK_PERIOD_MS); |
---|
39 | digitalWrite(led_pin, LOW); |
---|
40 | vTaskDelay(rate_1 / portTICK_PERIOD_MS); |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | // Our task: blink an LED at another rate |
---|
45 | void toggleLED_2(void *parameter) { |
---|
46 | while(1) { |
---|
47 | digitalWrite(led_pin, HIGH); |
---|
48 | vTaskDelay(rate_2 / portTICK_PERIOD_MS); |
---|
49 | digitalWrite(led_pin, LOW); |
---|
50 | vTaskDelay(rate_2 / portTICK_PERIOD_MS); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | void setup() { |
---|
55 | |
---|
56 | // Configure pin |
---|
57 | pinMode(led_pin, OUTPUT); |
---|
58 | |
---|
59 | // Task to run forever |
---|
60 | xTaskCreatePinnedToCore( // Use xTaskCreate() in vanilla FreeRTOS |
---|
61 | toggleLED_1, // Function to be called |
---|
62 | "Toggle 1", // Name of task |
---|
63 | 1024, // Stack size (bytes in ESP32, words in FreeRTOS) |
---|
64 | NULL, // Parameter to pass to function |
---|
65 | 1, // Task priority (0 to configMAX_PRIORITIES - 1) |
---|
66 | NULL, // Task handle |
---|
67 | app_cpu); // Run on one core for demo purposes (ESP32 only) |
---|
68 | |
---|
69 | // Task to run forever |
---|
70 | xTaskCreatePinnedToCore( // Use xTaskCreate() in vanilla FreeRTOS |
---|
71 | toggleLED_2, // Function to be called |
---|
72 | "Toggle 2", // Name of task |
---|
73 | 1024, // Stack size (bytes in ESP32, words in FreeRTOS) |
---|
74 | NULL, // Parameter to pass to function |
---|
75 | 1, // Task priority (0 to configMAX_PRIORITIES - 1) |
---|
76 | NULL, // Task handle |
---|
77 | app_cpu); // Run on one core for demo purposes (ESP32 only) |
---|
78 | |
---|
79 | // If this was vanilla FreeRTOS, you'd want to call vTaskStartScheduler() in |
---|
80 | // main after setting up your tasks. |
---|
81 | } |
---|
82 | |
---|
83 | void loop() { |
---|
84 | // Do nothing |
---|
85 | // setup() and loop() run in their own task with priority 1 in core 1 |
---|
86 | // on ESP32 |
---|
87 | } |
---|