/*
https://provisional.binefa.com/index.php/Recull_de_pr%C3%A0ctiques_de_2n_de_DAM._Curs_2023-2024#fr-mw-1.29_Presa_de_contacte_amb_MicroWorkers_i_FreeRTOS
Jordi Binefa - 20240216
*/
#if CONFIG_FREERTOS_UNICORE
#define ARDUINO_RUNNING_CORE 0
#else
#define ARDUINO_RUNNING_CORE 1
#endif
#define MINIMUM_DELAY_ALLOWING_MULTITASKING 50
#define BOTO 35
#define LED 2
bool bBotoPremut(int nQuin){
const TickType_t xDelay = 50 / portTICK_PERIOD_MS; // 0.05 seconds
vTaskDelay(50);
return !digitalRead(nQuin);
}
void TaskJobLeds(void *pvParameters) // This is a task.
{
(void) pvParameters;
const TickType_t xDelay = 500 / portTICK_PERIOD_MS; // 0.5 seconds
bool bEstatLed = HIGH;
// pinMode(BOTO, INPUT);
pinMode(LED, OUTPUT);
for (;;) {
digitalWrite(LED,bEstatLed);
vTaskDelay(xDelay);
bEstatLed = !bEstatLed;
//vTaskDelay(MINIMUM_DELAY_ALLOWING_MULTITASKING);
}
}
void TaskJobBoto(void *pvParameters) // This is a task.
{
(void) pvParameters;
long int liCmpt = 0;
bool bBotoAra = bBotoPremut(BOTO);
bool bBotoAbans = bBotoAra;
pinMode(BOTO, INPUT);
// pinMode(LED, OUTPUT);
for (;;) {
bBotoAra = bBotoPremut(BOTO);
if(bBotoAbans != bBotoAra){
bBotoAbans = bBotoAra;
liCmpt++;
Serial.print(liCmpt);
if(bBotoAra){
Serial.println(") Botó premut.");
}else{
Serial.println(") Botó no premut.");
}
}
//vTaskDelay(MINIMUM_DELAY_ALLOWING_MULTITASKING);
}
}
void setup() {
Serial.begin(115200);
xTaskCreatePinnedToCore(
TaskJobLeds
, "TaskJobLeds" // A name just for humans
, 2 * 1024 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL
, ARDUINO_RUNNING_CORE);
xTaskCreatePinnedToCore(
TaskJobBoto
, "TaskJobBoto" // A name just for humans
, 2 * 1024 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL
, ARDUINO_RUNNING_CORE);
}
void loop() {
delay(10);
}