/*********
Markus Pfeil
Added RTOS Part
/* Define the traceTASK_SWITCHED_IN() and *_OUT() macros become HIGH when the tasks is switched in, and low w
when switched out. This has to be done in the RTOS Library. */
//#define traceTASK_SWITCHED_IN() digitalWrite( (int)pxCurrentTCB->pxTaskTag, HIGH )
//#define traceTASK_SWITCHED_OUT() digitalWrite((int)pxCurrentTCB->pxTaskTag, LOW )
/* Define the pin on which the tasks is being monitored (to be put in each task) */ /*modified by Markus Pfeil*/
/* vTaskSetApplicationTaskTag( NULL, ( void * )data.debug_pin);
based on
Rui Santos
Complete project details at https://randomnerdtutorials.com
Project code: https://randomnerdtutorials.com/esp32-i2c-communication-arduino-ide/#2
*********/
#include <Wire.h>
#include <Arduino_FreeRTOS.h>
#include <semphr.h>
#include <queue.h>
#include <RTClib.h>
// Initialize DS1307RTC
RTC_DS1307 rtc;
//The use of Task Handles
TaskHandle_t TaskHandle_1;
TaskHandle_t TaskHandle_2;
void Task1(void *pvParameters);
void Task2(void *pvParameters);
void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("\nI2C Scanner");
//this will create the scanner Task
xTaskCreate(Task1, "Scanner", 128, NULL, 1, &TaskHandle_1);
// this will set the RTC
xTaskCreate(Task2, "Real_Time_Clock", 128, NULL, 3, &TaskHandle_2);
// where the real time clock initiates
rtc.begin();
}
//This Task is scanning the devices
void Task1(void *pvParameters) {
vTaskSetApplicationTaskTag( NULL, ( void * )12);
//waiting a bit so we can actually see it switch in once...otherwise the first activation is missed as the taskTag is not yet defined)
vTaskDelay(pdMS_TO_TICKS(10));
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print("Unknown error at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
}
else {
Serial.println("done\n");
}
vTaskDelete(NULL);
}
//This Tasks is display the real time clock
void Task2(void *pvParameters) {
vTaskSetApplicationTaskTag( NULL, ( void * )11);
for(;;) {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print("0");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print("0");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.println();
vTaskDelay(pdMS_TO_TICKS(500));
}
}
void loop() {
//Empty
}