// my solution to lesson 3
// Learned so far
// - writing task
// - task scheduling
// Challenge
// Write 2 tasks.
// On to display a led and one where a user can enter a number which delays the blinking
#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0 ;
#else
static const BaseType_t app_cpu = 1;
#endif
// Pins
static const int led_pin = 2;
int delayTime = 500;
//Our task blink a led
void toggleLed(void * parameter) {
while (1) {
digitalWrite(led_pin, HIGH);
vTaskDelay(delayTime / portTICK_PERIOD_MS );
digitalWrite(led_pin, LOW);
vTaskDelay(delayTime / portTICK_PERIOD_MS);
}
}
// Our task is to read in a number
void changeBlinkRate(void * parameter) {
char ch;
char buff[20];
int idx = 0;
memset(buff, 0, 20);
while (1) {
//clear the buffer
if (Serial.available() > 0) {
ch = Serial.read();
if (ch == '\n') {
delayTime = atoi(buff);
Serial.print("Delay time : ");
Serial.println(delayTime);
// clear buffer
memset(buff, 0, 20);
idx = 0 ;
} else {
if (idx < 19) {
buff[idx] = ch;
idx++;
}
}
}
}
}
void setup() {
pinMode(led_pin, OUTPUT);
Serial.begin(9600);
Serial.println("RW");
// Task to run forever
xTaskCreatePinnedToCore(
toggleLed, // Function to be called
"ToggleLed", // name of the function
1024, // stack size
NULL, // parameter to pass to the function
1, // priority
NULL, // task handle
app_cpu
);
// Task to run forever
xTaskCreatePinnedToCore(
changeBlinkRate, // Function to be called
"ChangeBlinkRate", // name of the function
1024, // stack size
NULL, // parameter to pass to the function
1, // priority
NULL, // task handle
app_cpu
);
}
void loop() {
vTaskDelay(delayTime / portTICK_PERIOD_MS);
}