Skip to content

Commit

Permalink
Merge pull request #62 from TcMenu/main-logging
Browse files Browse the repository at this point in the history
#61 Move to new logging framework, fix platform determination.
  • Loading branch information
davetcc authored Oct 19, 2024
2 parents df3f36f + 4f6ff7e commit dbac1d5
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 103 deletions.
4 changes: 2 additions & 2 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 0 additions & 11 deletions examples/mbedRtos/mbedExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
#include <TaskManagerIO.h>
#include <TmLongSchedule.h>

#define LOG_TASK_MANGER_DEBUG 0

// Here we create a serial object to write log statements to.
BufferedSerial console(USBTX, USBRX, 115200);

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 0 additions & 8 deletions examples/taskManagement/taskManagement.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 6 additions & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
Expand Down
3 changes: 2 additions & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name=TaskManagerIO
version=1.4.3
version=1.5.0
maintainer=https://www.thecoderscorner.com
author=davetcc
category=Other
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
2 changes: 1 addition & 1 deletion platformio-test.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ board = esp32dev
extra_scripts = post:merge-bin.py

lib_deps =
davetcc/IoAbstraction@^4.0.2
tcmenu/IoAbstraction@^4.0.2
TaskManagerIO

test_testing_command =
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
framework = arduino

lib_deps =
davetcc/IoAbstraction@^4.0.2
TcMenu/IoAbstraction@^4.0.2
TaskManagerIO

[env:megaatmega2560]
Expand Down
58 changes: 11 additions & 47 deletions src/TaskManagerIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "TaskPlatformDeps.h"
#include "TaskManagerIO.h"
#include "ExecWithParameter.h"
#include <IoLogging.h>

#ifdef BUILD_FOR_PICO_CMAKE
critical_section_t* tm_internal::tmLock;
Expand All @@ -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)
Expand All @@ -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;
Expand All @@ -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");
}
}
};
Expand Down Expand Up @@ -129,14 +93,14 @@ taskid_t TaskManager::findFreeTask() {
for (taskid_t i=0; i<numberOfBlocks;i++) {
auto taskId = taskBlocks[i]->allocateTask();
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;
}

Expand All @@ -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.
}
}
Expand Down Expand Up @@ -229,7 +193,7 @@ void TaskManager::cancelTask(taskid_t taskId) {
if (task) {
taskManager.execute(new ExecWith2Parameters<TimerTask*, TaskManager*>([](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);
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -485,7 +449,7 @@ void TaskManager::setInterruptCallback(InterruptFn handler) {
}

char* TaskManager::checkAvailableSlots(char* data, size_t dataSize) const {
auto maxLen = min(taskid_t(dataSize - 1), taskBlocks[numberOfBlocks - 1]->lastSlot());
auto maxLen = internal_min(taskid_t(dataSize - 1), taskBlocks[numberOfBlocks - 1]->lastSlot());
size_t position = 0;

for(taskid_t i=0; i<numberOfBlocks;i++) {
Expand Down
39 changes: 8 additions & 31 deletions src/TaskPlatformDeps.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ class TimerTask;
#if defined(BUILD_FOR_PICO_CMAKE)
#include <pico/stdlib.h>
#include <valarray>
#ifndef min
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))
#endif
#elif !defined(__MBED__)
#include <Arduino.h>
#endif
Expand Down Expand Up @@ -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.
Expand All @@ -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

0 comments on commit dbac1d5

Please sign in to comment.