My question now, is that how to set the size of the struct array from Arduino sketch ?
TASK_MANAGER task[SYSTEM_TASKS] = {0};
In this case I must set the size in the header file, but I want to set the size from the Arduino sketch.
Another question I want to ask about the struct scope to other libraries, is my setting for the struct good ? It's working for me with this setting and I ran a function from another libraries with this setting.
Simple answer, and this is asked a thousand times, you don't. Your task_manager.cpp is compiled without ANY knowledge of your main sketch in which you include it.
So you have to set it in task_manager.h/cpp or define the sctruct in you main sketch and point the library to it.
Last option is to rewrite the whole library to be object oriented and define 'SYSTEM_TASKS'-number of objects in your main sketch.
That's how I manage background tasks. Each is a link for a linked list. Each time (typically through the loop() when the user calls idle() ) The processor just runs down the list calling everyone's doAction() method. Its super simple.
I set up mine for doing simple things in the background like blinking LEDs. Pretty quickly I found that about 95% of my code now runs on that background list. It is the best "do many things without delay()" method I've ever seen.
septillion:
Simple answer, and this is asked a thousand times, you don't. Your task_manager.cpp is compiled without ANY knowledge of your main sketch in which you include it.
So you have to set it in task_manager.h/cpp or define the struct in you main sketch and point the library to it.
Last option is to rewrite the whole library to be object oriented and define 'SYSTEM_TASKS'-number of objects in your main sketch.
Yes, I learned stuff in C++ which are setters and getters.
Another wonderful way is to use the constructor of the library, that's brilliant ! Just pass the initialize values to the private variables and the code is good to go.
But I want to know how this is available in C. My intention actually is to experience as much approaches as I can in C, then getting into C++ would be meaningful.
That's how I manage background tasks. Each is a link for a linked list. Each time (typically through the loop() when the user calls idle() ) The processor just runs down the list calling everyone's doAction() method. Its super simple.
-jim lee
Is there a library for this method, or you prefer I get into the principal of linked lists and try to build that in my task manager ?
I want you to know that I want this struct to be shared with other libraries too.
So in this way I have to declare the struct in the header file of task_manager.h
Then I defined it with setting the size of struct array.
I don't understand the question. But, I think my answer remains the same. If you want to set an array size at run time, you must use dynamic allocation.
wolfrose:
Is there a library for this method, or you prefer I get into the principal of linked lists and try to build that in my task manager ?
LC_baseTools from your friendly IDE library manager has a link list set of classes in there. link list, double linked list, queue, stack. Link list base classes. It also has the idler class you can see how that was put together.
gfvalvo:
I don't understand the question. But, I think my answer remains the same. If you want to set an array size at run time, you must use dynamic allocation.
Another question now, would this method use high RAM; e.g. cause a system high overhead ? Is my method of using a struct good ? are there better more simpler methods and use less RAM ?
jimLee:
LC_baseTools from your friendly IDE library manager has a link list set of classes in there. link list, double linked list, queue, stack. Link list base classes. It also has the idler class you can see how that was put together.
-jim lee
Thanks
The one thing I'm thinking about now is that using the task_manager library would cause me a lot of work putting the struct elements in each function of each library; for example, a normal function like:
I have to check that the function isn't already locked for delay period
If it isn't, then function executes
locking the function
setting the delay period
Taking time snap
It's ok I'm not so much complaining about all the steps I have to do, but what I want to be sure of is this method I'm following is practical and accepted in programming community, so I know I'm moving in the right path as everyone else.
////////////////////////////////////////////////////////////////////////////////////////////////
// allocating multitasking nodes for system tasks for dynamic with calloc and static with normal
// struct object initialize
# First declare task_manager struct
typedef struct {
uint32_t task_st;
uint32_t task_pr;
bool task_lock;
}TASK_MANAGER;
# Then, you have two ways of initializing struct objects:
////////////////////////////////////////
// 1: use typedef stuct object array
// global
TASK_MANAGER task[SYSTEM_TASKS] = {0};
TASK_MANAGER *task_ptr;
// in init function
task_ptr = &task[0];
// in other source files we usue external variables
extern TASK_MANAGER *task_ptr;
////////////////////////////////////////
// 2: use calloc
// global
TASK_MANAGER *task_ptr;
// in init function
task_ptr = (TASK_MANAGER*)calloc(tasks,sizeof(TASK_MANAGER));
// in other source files we usue external variables
extern TASK_MANAGER *task_ptr;