#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#define HEARTBEAT_LED_PIN GPIO_NUM_4 // use GPIO4 for the LED
// blink task function
void heartbeat_task(void *pvParameters){
int heartbeatState = 0; //start with LED off
TickType_t lastTime = 0;
while(1){
TickType_t now = xTaskGetTickCount();
//prints the period of the function
printf("LED toggled at %lu ms (Δ = %lu ms)\n",
(unsigned long)(now * portTICK_PERIOD_MS),
(unsigned long)((now - lastTime) * portTICK_PERIOD_MS));
lastTime = now;
gpio_set_level(HEARTBEAT_LED_PIN, heartbeatState);
heartbeatState = !heartbeatState; // toggle on/off
//delays for 250 ms
vTaskDelay(pdMS_TO_TICKS(250));
vTaskDelay(pdMS_TO_TICKS(50)); // experimentation section
}
vTaskDelete(NULL);
}
//prints a message every 10s
void vitals_task(void * pvParameters){
TickType_t lastTime = 0;
while(1){
TickType_t now = xTaskGetTickCount();
//generate psuedo heart rate of 60-100 bpm
int heartRate = (rand() % 41 + 60);
//prints the period of the function
printf("Vitals printed at %lu ms (Δ = %lu ms) | Heart Rate: %d bpm\n",
(unsigned long)(now * portTICK_PERIOD_MS),
(unsigned long)((now - lastTime) * portTICK_PERIOD_MS),
heartRate);
lastTime = now;
printf("Patient's heart is beating, Time = %lu ms | Heart Rate: %d bpm \n", (unsigned long)(xTaskGetTickCount()*portTICK_PERIOD_MS), heartRate);
vTaskDelay(pdMS_TO_TICKS(15000));
}
vTaskDelete(NULL);
}
void app_main() {
//LED GPIO pin set up
gpio_reset_pin(HEARTBEAT_LED_PIN);
gpio_set_direction(HEARTBEAT_LED_PIN, GPIO_MODE_OUTPUT);
xTaskCreate(heartbeat_task, "HeartbeatTask", 2048, NULL, 1, NULL);
xTaskCreate(vitals_task, "VitalsTask", 2048, NULL, 2, NULL);
}