arkhipenko / TaskScheduler

Cooperative multitasking for Arduino, ESPx, STM32, nRF and other microcontrollers
http://playground.arduino.cc/Code/TaskScheduler
BSD 3-Clause "New" or "Revised" License
1.21k stars 221 forks source link

Move-defititions-to-isolated--.cpp #130

Closed crabel99 closed 2 years ago

crabel99 commented 2 years ago

Including the class method definitions in the header file limits the extensibility of the library, by only allowing one *.cpp file to have access to the TaskScheduler.h file. This prevents breaking the tasks up into smaller, isolated, and more manageable code blocks.

My current project has three main libraries, one for data acquisition, one for a TFT display, and the other for the capacitive input. By breaking the code up this way I can create a singleton class wrapper for each of the three main functions with all of the tasks for the respective main functions neatly bundled together. To demonstrate the needed class structure, I posted a simple example of the blink sketch.

I tested this library on a run of the mill TaskScheduler based sketch and it wasn't impacted.

I thought the include guards would have isolated the class function definitions better, but my compiler did not seem to agree with me.

arkhipenko commented 2 years ago

Unfortunately, this prevents the use of # define compilation options in Arduino IDE, so will not be merged. Please see example 16 for the way to use TS in PlatformIO.

crabel99 commented 2 years ago

Now I understand how you have it set up. Instead of using the TaskScheduler.h header, you use TaskSchedulerDeclarations.has the replacement header, and then create a single *.cpp and #include TaskScheduler.h as the only line. Well played.

GitMoDu commented 2 years ago

you use TaskSchedulerDeclarations.has the replacement header

Exactly, once you do that, you can extend your tasks cleanly and do it the OO way, including templating and pseudo-interfaces if needed. Here's a real-world example: https://github.com/GitMoDu/IValueReader/blob/master/src/Task/TemplateReaderTask.h