From 923dceeeaec03c3a3efbe6a4d971dfe41a6f3b69 Mon Sep 17 00:00:00 2001 From: dave Date: Sun, 15 Sep 2024 09:41:32 +0100 Subject: [PATCH] #61 Move to new logging framework, fix platform determination. --- cmake/CMakeLists.txt | 4 +- examples/mbedRtos/mbedExample.cpp | 11 ----- examples/taskManagement/taskManagement.ino | 8 ---- library.json | 7 ++- library.properties | 3 +- src/TaskManagerIO.cpp | 56 ++++------------------ src/TaskPlatformDeps.h | 39 ++++----------- 7 files changed, 28 insertions(+), 100 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 9a28ca3..b3aa1b5 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -10,7 +10,7 @@ target_compile_definitions(TaskManagerIO ) target_include_directories(TaskManagerIO PUBLIC - ${PROJECT_SOURCE_DIR}/TaskManagerIO/src + ${PROJECT_SOURCE_DIR}/lib/TaskManagerIO/src ) -target_link_libraries(TaskManagerIO PUBLIC pico_stdlib pico_sync) +target_link_libraries(TaskManagerIO PUBLIC TcMenuLog pico_stdlib pico_sync) diff --git a/examples/mbedRtos/mbedExample.cpp b/examples/mbedRtos/mbedExample.cpp index 0437e76..6f4867e 100644 --- a/examples/mbedRtos/mbedExample.cpp +++ b/examples/mbedRtos/mbedExample.cpp @@ -13,8 +13,6 @@ #include #include -#define LOG_TASK_MANGER_DEBUG 0 - // Here we create a serial object to write log statements to. BufferedSerial console(USBTX, USBRX, 115200); @@ -207,15 +205,6 @@ void anotherProc() { int main() { log("starting up taskmanager example"); -#if LOG_TASK_MANGER_DEBUG != 0 - // this is how we get diagnostic information from task manager - // it will notify of significant events to the loggingDelegate. - tm_internal::setLoggingDelegate([](tm_internal::TmErrorCode code, int task) { - log("Taskmgr notification code: ", code); - log(" -> Task num: ", task); - }); -#endif //LOG_TASK_MANGER_DEBUG - setupTasks(); threadEventMgr.start(taskPump); diff --git a/examples/taskManagement/taskManagement.ino b/examples/taskManagement/taskManagement.ino index 2f81698..b84e7a6 100644 --- a/examples/taskManagement/taskManagement.ino +++ b/examples/taskManagement/taskManagement.ino @@ -104,14 +104,6 @@ void setup() { Serial.print(", blocks = "); Serial.println(DEFAULT_TASK_BLOCKS); - // if you want to receive notifications from task manager, provide a loggingDelegate as below. - tm_internal::setLoggingDelegate([] (tm_internal::TmErrorCode code, int id) { - Serial.print("TM Notification code="); - Serial.print(code); - Serial.print(", id="); - Serial.println(id); - }); - // connect a switch to interruptPin, so you can raise interrupts. pinMode(interruptPin, INPUT); diff --git a/library.json b/library.json index 30c30fe..67534d4 100644 --- a/library.json +++ b/library.json @@ -13,7 +13,12 @@ "maintainer": true } ], - "version": "1.4.3", + "dependencies": [ + { + "name": "TcMenuLog" + } + ], + "version": "1.5.0", "license": "Apache-2.0", "frameworks": "arduino, mbed", "platforms": "*" diff --git a/library.properties b/library.properties index 8fc75ab..4cc1ae2 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=TaskManagerIO -version=1.4.3 +version=1.5.0 maintainer=https://www.thecoderscorner.com author=davetcc category=Other @@ -7,3 +7,4 @@ url=https://github.com/TcMenu/TaskManagerIO sentence=Task manager for Arduino and mbed with marshalled interrupts, first class support for events and timed execution. Thread safe for RTOS use. paragraph=Simple efficient task management with interrupt marshalling. Provides the ability to schedule things to be done either at certain times or on event triggers. This library can also marshall interrupts into task manager includes=TaskManager.h +depends=TcMenuLog diff --git a/src/TaskManagerIO.cpp b/src/TaskManagerIO.cpp index f4f34ad..a0e7ef2 100644 --- a/src/TaskManagerIO.cpp +++ b/src/TaskManagerIO.cpp @@ -6,6 +6,7 @@ #include "TaskPlatformDeps.h" #include "TaskManagerIO.h" #include "ExecWithParameter.h" +#include #ifdef BUILD_FOR_PICO_CMAKE critical_section_t* tm_internal::tmLock; @@ -20,22 +21,10 @@ void tm_internal::initPicoTmLock() { void yield() { sleep_us(1); } - -unsigned long millis() { - return to_ms_since_boot(get_absolute_time()); -} - -unsigned long micros() { - return to_us_since_boot(get_absolute_time()); -} - #endif #ifdef IOA_USE_MBED -volatile bool timingStarted = false; -Timer ioaTimer; - void yield() { # if !defined(PIO_NEEDS_RTOS_WORKAROUND) @@ -44,36 +33,11 @@ void yield() { wait(0.0000001); #endif } - -unsigned long millis() { - if(!timingStarted) { - timingStarted = true; - ioaTimer.start(); - } - return ioaTimer.read_ms(); -} - -unsigned long micros() { - if(!timingStarted) { - timingStarted = true; - ioaTimer.start(); - } - return (unsigned long) ioaTimer.read_high_resolution_us(); -} - #endif TaskManager taskManager; -namespace tm_internal { - LoggingDelegate loggingDelegate = nullptr; - - void setLoggingDelegate(LoggingDelegate delegate) { - loggingDelegate = delegate; - } -} - class TmSpinLock { private: tm_internal::TmAtomicBool* lockObject; @@ -85,14 +49,14 @@ class TmSpinLock { locked = tm_internal::atomicSwapBool(lockObject, false, true); if(!locked) { yield(); // something else has the lock, let it get control to finish up! - if(++count == 1000) tm_internal::tmNotification(tm_internal::TM_WARN_HIGH_SPINCOUNT, TASKMGR_INVALIDID); + if(++count == 1000) serlogF(SER_WARNING, "TM spin>1000"); } } while(!locked); } ~TmSpinLock() { if(!tm_internal::atomicSwapBool(lockObject, true, false)) { - tm_internal::tmNotification(tm_internal::TM_ERROR_LOCK_FAILURE, TASKMGR_INVALIDID); + serlogF(SER_ERROR, "TM lock fail"); } } }; @@ -129,14 +93,14 @@ taskid_t TaskManager::findFreeTask() { for (taskid_t i=0; iallocateTask(); if(taskId != TASKMGR_INVALIDID) { - tm_internal::tmNotification(tm_internal::TM_INFO_TASK_ALLOC, taskId); + serlogF2(SER_IOA_DEBUG, "TM alloc", taskId); return taskId; } } // already full, cannot allocate further. if(numberOfBlocks == DEFAULT_TASK_BLOCKS) { - tm_internal::tmNotification(tm_internal::TM_ERROR_FULL, TASKMGR_INVALIDID); + serlogF(SER_ERROR, "TM full"); return TASKMGR_INVALIDID; } @@ -148,11 +112,11 @@ taskid_t TaskManager::findFreeTask() { auto nextIdSpace = taskBlocks[numberOfBlocks - 1]->lastSlot() + 1; taskBlocks[numberOfBlocks] = new TaskBlock(nextIdSpace); if(taskBlocks[numberOfBlocks] != nullptr) { - tm_internal::tmNotification(tm_internal::TM_INFO_REALLOC, numberOfBlocks); + serlogF2(SER_IOA_DEBUG, "TM alloc: ", numberOfBlocks); numberOfBlocks++; } else { - tm_internal::tmNotification(tm_internal::TM_ERROR_FULL, numberOfBlocks); + serlogF(SER_ERROR, "TM full"); break; // no point to continue here, new has failed. } } @@ -229,7 +193,7 @@ void TaskManager::cancelTask(taskid_t taskId) { if (task) { taskManager.execute(new ExecWith2Parameters([](TimerTask* task, TaskManager* tm) { tm->removeFromQueue(task); - tm_internal::tmNotification(tm_internal::TM_INFO_TASK_FREE, TASKMGR_INVALIDID); + serlogF(SER_IOA_DEBUG, "TM free"); task->clear(); }, task, this), true); } @@ -278,7 +242,7 @@ void TaskManager::dealWithInterrupt() { } else { task->clear(); - tm_internal::tmNotification(tm_internal::TM_INFO_TASK_FREE, TASKMGR_INVALIDID); + serlogF(SER_IOA_DEBUG, "TM free int"); } } else { @@ -309,7 +273,7 @@ void TaskManager::runLoop() { putItemIntoQueue(tm); } else { tm->clear(); - tm_internal::tmNotification(tm_internal::TM_INFO_TASK_FREE, TASKMGR_INVALIDID); + serlogF(SER_IOA_DEBUG, "TM free loop"); } } tm = tm->getNext(); diff --git a/src/TaskPlatformDeps.h b/src/TaskPlatformDeps.h index 9c97b3f..75c2bbf 100644 --- a/src/TaskPlatformDeps.h +++ b/src/TaskPlatformDeps.h @@ -27,10 +27,6 @@ class TimerTask; #if defined(BUILD_FOR_PICO_CMAKE) #include #include -#ifndef min -#define min(a, b) (((a) < (b)) ? (a) : (b)) -#define max(a, b) (((a) > (b)) ? (a) : (b)) -#endif #elif !defined(__MBED__) #include #endif @@ -393,33 +389,6 @@ typedef uint32_t sched_t; #endif // DEFAULT_TASK_BLOCKS not defined when task size is #endif // DEFAULT_TASK_SIZE defined already -namespace tm_internal { - enum TmErrorCode { - /** reallocating memory by adding another block, number of blocks in the second parameter */ - TM_INFO_REALLOC = 1, - /** A task slot has been allocated, taskid in second parameter */ - TM_INFO_TASK_ALLOC = 2, - /** A task slot has been freed, taskid in second parameter */ - TM_INFO_TASK_FREE = 3, - - // warnings and errors are over 100, info level 0..99 - - /** probable bug, the lock status was not as expected, please report along with sketch to reproduce */ - TM_ERROR_LOCK_FAILURE = 100, - /** high concurrency is resulting in very high spin counts, performance may be affected */ - TM_WARN_HIGH_SPINCOUNT, - /** task manager is full, consider settings the default task settings higher. */ - TM_ERROR_FULL - }; - typedef void (*LoggingDelegate)(TmErrorCode code, int task); - extern LoggingDelegate loggingDelegate; - void setLoggingDelegate(LoggingDelegate delegate); - - inline void tmNotification(TmErrorCode code, int task) { - if(loggingDelegate) loggingDelegate(code, task); - } -} - // // Here we define an attribute needed for interrupt support on ESP8266 and ESP32 boards, any interrupt code that is // going to run on these boards should be marked with this attribute. @@ -441,4 +410,12 @@ namespace tm_internal { #endif // _has_include #endif // GCC>=5 and !TM_ALLOW_CAPTURED_LAMBDA +#ifndef internal_min +#define internal_min(a, b) ((a) > (b) ? (b) : (a)); +#endif // internal_min + +#ifndef internal_max +#define internal_max(a, b) ((a) < (b) ? (b) : (a)); +#endif // internal_max + #endif //TASKMANGERIO_PLATFORMDETERMINATION_H