/* --------------------------------------------------------------
Application: 01 - Rev1
Release Type: Baseline Multitask Skeleton Starter Code
Class: Real Time Systems - Su 2025
AI Use: Commented inline
Caden Kohlmeier
Real-Time Systems
Application 1
Due: 5/25/1015
Thematic Context
Healthcare: The LED simulates a heartbeat indicator on a patient monitoring device, blinking regularly in sync with heartbeats, while the console prints patient vitals. The system must not miss a beat or a log message.
Analysis
• If I increase the delay within the print task, it functions as expected and will print the message at the desired interval. When I increase the LED flash delay from 250ms to 2500ms, the led fails to blink altogether. When increasing the number of characters for the print message, I did not notice any additional delay or undesirable output.
• I removed the vTaskDelay and did see that it flooded the console with print messages. It has been reset to include vTaskDelay.
• The health care example made the most sense to me practically. I included an arbitrary BPM every 10s. Displaying long messages in a practical application would likely be undesirable as information is transmitted every 10 seconds. In a healthcare setting, long messages related to heart rate could cause delays in the operator reading the message. Correct functionality of this system at predictable times is important for calculating averages or information across a ten second period. Heart rate, blood pressure, blood sugar or other quantitative medical measurements may rely on accuracy and predictability.
• The period for the blink task is 2Hz, defined by T=1/f. LED stays on for 250ms, then is off 250ms for a total time of 500ms
• The period for the print task is 1Hz, defined by T=1/f
• The system does meet the timing requirements. I have added a tick log in my print statement that is active and has also included one for the LED but have it commented out for clarity. I was able to verify it by printing the tick of each message.
• I modified the vTaskDelay as stated previously and could get the system to fail and not flash the LED at all.
AI USE: I used AI in this Application to get easily familiar with some of the functionality of the ESP32. ChatGPT was used to verify original output and provided header file and logic for random BPM
---------------------------------------------------------------*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_random.h"
#define LED_PIN GPIO_NUM_15 // Using GPIO15 for the LED
// Task to blink an LED at 2 Hz (500 ms period: 250 ms ON, 250 ms OFF)
void blink_task(void *pvParameters) {
bool led_on = false;
TickType_t last_tick = xTaskGetTickCount();
while (1) {
// TODO: Set LED pin high or low based on led_on flag; right now it's always on... boring; hint in the commented out print statement
gpio_set_level(LED_PIN, led_on); //state
led_on = !led_on; // toggle state for next time
//printf("LED %s\n", led_on ? "ON" : "OFF"); //debug message
/*called to wait 250ms*/ vTaskDelay(pdMS_TO_TICKS(250)); // Delay for 250 ms using MS to Ticks Function vs alternative which is MS / ticks per ms
//printf("LED tick: %lu\n", xTaskGetTickCount());
}
vTaskDelete(NULL); // We'll never get here; tasks run forever
}
// Task to print a message every 10000 ms (10 seconds)
void print_task(void *pvParameters) {
while (1) {
// TODO: Print a periodic message based on thematic area. Could be a counter or timestamp.
uint32_t number = (esp_random() % 51)+100;
printf("BPM: %lu @ Tick %lu\n" , number, xTaskGetTickCount());
vTaskDelay(pdMS_TO_TICKS(10000)); // Delay for 10000 ms (10s)
}
vTaskDelete(NULL); // We'll never get here; tasks run forever
}
void app_main() {
// Initialize LED GPIO
gpio_reset_pin(LED_PIN);
gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
// Instantiate/ Create tasks:
// . pointer to task function,
// . descriptive name, [has a max length; located in the FREERTOS_CONFIG.H]
// . stack depth,
// . parameters [optional] = NULL
// . priority [0 = low],
// . pointer referencing this created task [optional] = NULL
// Learn more here https://www.freertos.org/Documentation/02-Kernel/04-API-references/01-Task-creation/01-xTaskCreate
xTaskCreate(blink_task, "Blink Task", 2048, NULL, 1, NULL);
xTaskCreate(print_task, "Print Task", 2048, NULL, 1, NULL); // print, stack depth 2048, no parameters, both are priority 1
}