/**
* Solution to 02 - Blinky Challenge
*
* Toggles LED at different rates using separate tasks.
*
* Date: December 3, 2020
* Author: Shawn Hymel
* License: 0BSD
*/
// Use only core 1 for demo purposes
#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0;
#else
static const BaseType_t app_cpu = 1;
#endif
// LED rates
static const int rate_1 = 500; // ms
static const int rate_2 = 323; // ms
// Pins
static const int led_pin_1 = 22; // G2
static const int led_pin_2 = 18; // G4
// Our task: blink an LED at one rate
void toggleLED_1(void *parameter) {
while(1) {
Serial.print("LED 1 ON\n");
digitalWrite(led_pin_1, HIGH);
vTaskDelay(rate_1 / portTICK_PERIOD_MS);
Serial.print("LED 1 OFF\n");
digitalWrite(led_pin_1, LOW);
vTaskDelay(rate_1 / portTICK_PERIOD_MS);
}
}
// Our task: blink an LED at another rate
void toggleLED_2(void *parameter) {
while(1) {
Serial.print("LED 2 ON\n");
digitalWrite(led_pin_2, HIGH);
vTaskDelay(rate_2 / portTICK_PERIOD_MS);
Serial.print("LED 2 OFF\n");
digitalWrite(led_pin_2, LOW);
vTaskDelay(rate_2 / portTICK_PERIOD_MS);
}
}
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Configure pins
pinMode(led_pin_1, OUTPUT);
pinMode(led_pin_2, OUTPUT);
// Task to run forever
xTaskCreatePinnedToCore( // Use xTaskCreate() in vanilla FreeRTOS
toggleLED_1, // Function to be called
"Toggle 1", // Name of task
1024, // Stack size (bytes in ESP32, words in FreeRTOS)
NULL, // Parameter to pass to function
1, // Task priority (0 to configMAX_PRIORITIES - 1)
NULL, // Task handle
app_cpu); // Run on one core for demo purposes (ESP32 only)
// Task to run forever
xTaskCreatePinnedToCore( // Use xTaskCreate() in vanilla FreeRTOS
toggleLED_2, // Function to be called
"Toggle 2", // Name of task
1024, // Stack size (bytes in ESP32, words in FreeRTOS)
NULL, // Parameter to pass to function
1, // Task priority (0 to configMAX_PRIORITIES - 1)
NULL, // Task handle
app_cpu); // Run on one core for demo purposes (ESP32 only)
// If this was vanilla FreeRTOS, you'd want to call vTaskStartScheduler() in
// main after setting up your tasks.
}
void loop() {
// Do nothing
// setup() and loop() run in their own task with priority 1 in core 1
// on ESP32
}