### setup() Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineSchedulerTemplate.html Initializes the coroutine scheduler. This method should be called once during the global setup phase of the application to prepare the scheduler for managing coroutines. ```APIDOC ## setup() ### Description Set up the scheduler. Should be called from the global setup(). ### Method static void ### Parameters None ### Request Example ```cpp ace_routine::CoroutineSchedulerTemplate::setup(); ``` ### Response None ``` -------------------------------- ### Setup and Loop for CoroutineScheduler Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md Integrate CoroutineScheduler into your global setup() and loop() functions. Ensure CoroutineScheduler::setup() is called once in setup() and CoroutineScheduler::loop() is called repeatedly in loop(). ```C++ void setup() { ... CoroutineScheduler::setup(); } void loop() { CoroutineScheduler::loop(); } ``` -------------------------------- ### Setup and Loop for Coroutine Scheduler Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md Initializes the serial communication and the CoroutineScheduler in setup(), and runs the scheduler's loop in loop(). ```C++ Writer writer(channel); Reader reader(channel); void setup() { Serial.begin(115200); while (!Serial); // micro/leonardo ... CoroutineScheduler::setup(); ... } void loop() { CoroutineScheduler::loop(); } ``` -------------------------------- ### Directly Call setupCoroutine() in Global Setup Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md For a small number of coroutines, manually call their setupCoroutine() methods directly from the global setup() function. This approach is suitable when memory is a concern and automated setup is not necessary. ```C++ ManualCoroutine1 coroutine1; ManualCoroutine2 coroutine2; void setup() { coroutine1.setupCoroutine(); coroutine2.setupCoroutine(); CoroutineScheduler::setup(); ... ``` -------------------------------- ### setupCoroutine() Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate-members.html Initializes the coroutine's setup. ```APIDOC ## setupCoroutine() ### Description Performs the initial setup for the coroutine. This method should be called before the coroutine starts its execution. ### Method `setupCoroutine()` ### Parameters None ### Response None ``` -------------------------------- ### CoroutineSchedulerTemplate Internal Coroutine Setup Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/CoroutineScheduler_8h_source.html Iterates through all coroutines starting from the root and calls their individual setup methods. This is an internal method called by setupCoroutines(). ```cpp void setupCoroutinesInternal() { for (T_COROUTINE** p = T_COROUTINE::getRoot(); (*p) != nullptr; p = (*p)->getNext()) { (*p)->setupCoroutine(); } } ``` -------------------------------- ### Example LogBinTableRenderer Output Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md Example output from `LogBinTableRenderer::printTo(Serial, 2, 13)`, showing event counts for different time bins. ```text name <16us <32us <64us<128us<256us<512us <1ms <2ms <4ms <8ms >> 0x1DB 16898 52688 0 0 0 0 0 0 0 0 1 readPin 65535 1128 0 0 0 0 0 0 0 0 0 blinkLed 65535 800 0 0 0 0 0 0 0 0 0 ``` -------------------------------- ### CoroutineSchedulerTemplate Coroutine Setup Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/CoroutineScheduler_8h_source.html Sets up all coroutines managed by the scheduler. This is typically called after the scheduler setup. ```cpp static void setupCoroutines() { getScheduler()->setupCoroutinesInternal(); } ``` -------------------------------- ### CoroutineSchedulerTemplate Scheduler Setup Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/CoroutineScheduler_8h_source.html Initializes the scheduler by setting the current coroutine to the root coroutine. This is an internal method called by setup(). ```cpp void setupScheduler() { mCurrent = T_COROUTINE::getRoot(); } ``` -------------------------------- ### Coroutine Profiling Output Examples Source: https://github.com/bxparks/aceroutine/blob/develop/README.md Displays example output from coroutine profiling, showing both a formatted table and a JSON object. This output is generated by `LogBinTableRenderer` and `LogBinJsonRenderer` respectively. ```text name <16us <32us <64us<128us<256us<512us <1ms <2ms <4ms <8ms >> 0x1DB 16921 52650 0 0 0 0 0 0 0 0 1 readPin 65535 1189 0 0 0 0 0 0 0 0 0 blinkLed 65535 830 0 0 0 0 0 0 0 0 0 { "0x1DB":[16921,52650,0,0,0,0,0,0,0,0,1], "readPin":[65535,1189,0,0,0,0,0,0,0,0,0], "blinkLed":[65535,830,0,0,0,0,0,0,0,0,0] } ``` -------------------------------- ### Coroutine Setup and Profiling Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate.html Methods for setting up coroutine names and managing the coroutine profiler. ```APIDOC ## setupCoroutine(const char *) ### Description Deprecated method that does nothing. Use for C-string coroutine names. ### Method `void setupCoroutine(const char *name) ACE_ROUTINE_DEPRECATED` ### Parameters * `name` (const char *) - The name of the coroutine (deprecated). ### Returns None ``` ```APIDOC ## setupCoroutine(const __FlashStringHelper *) ### Description Deprecated method that does nothing. Use for Flash string coroutine names. ### Method `void setupCoroutine(const __FlashStringHelper *name) ACE_ROUTINE_DEPRECATED` ### Parameters * `name` (const __FlashStringHelper *) - The name of the coroutine (deprecated). ### Returns None ``` ```APIDOC ## setProfiler(CoroutineProfiler *) ### Description Sets the profiler for this coroutine. ### Method `void setProfiler(CoroutineProfiler *profiler)` ### Parameters * `profiler` (CoroutineProfiler *) - A pointer to the CoroutineProfiler object to set. ### Returns None ``` ```APIDOC ## getProfiler() ### Description Retrieves the currently set profiler for this coroutine. ### Method `CoroutineProfiler * getProfiler() const` ### Parameters None ### Returns A pointer to the CoroutineProfiler object, or nullptr if none is set. ``` -------------------------------- ### setupCoroutine (Deprecated) Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate.html Deprecated method that does nothing. The setup into the singly-linked list is automatically performed by the constructor. ```APIDOC ## setupCoroutine() [Deprecated] ### Description Deprecated method that does nothing. Starting v1.3, the setup into the singly-linked list is automatically performed by the constructor and this method no longer needs to be called manually. This method is retained for backwards compatibility. ### Method `void setupCoroutine(const __FlashStringHelper *)` ### Method `void setupCoroutine(const char *)` ``` -------------------------------- ### LogBinJsonRendererTemplate Example Output Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1LogBinJsonRendererTemplate.html This example shows the JSON output format generated by the LogBinJsonRendererTemplate class for profiling data. ```json { "soundManager":[1411,0,2,0,0,0,0,0,0,0], "soundRoutine":[1411,0,1,1,0,0,0,0,0,0] } ``` -------------------------------- ### Example LogBinJsonRenderer Output Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md Example output from `LogBinJsonRenderer::printTo(Serial, 2, 13)`, showing event counts as a JSON object. ```json { "0x1DB":[16898,52688,0,0,0,0,0,0,0,0,1], "readPin":[65535,1128,0,0,0,0,0,0,0,0,0], "blinkLed":[65535,800,0,0,0,0,0,0,0,0,0] } ``` -------------------------------- ### setupCoroutine Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate.html Performs coroutine initialization. This method is intended to be called from the global setup() function or via CoroutineScheduler::setupCoroutines(). ```APIDOC ## setupCoroutine() ### Description Perform coroutine initialization. This is intended to be called directly from the global `setup()` function, or through the `CoroutineScheduler::setupCoroutines()` method which should also be called from the global `setup()` function. If your coroutines do not override this method, hence do not need to perform any setup, then you should _not_ call `CoroutineScheduler::setupCoroutines()` to avoid consuming unnecessary flash memory. On AVR processors, each `Coroutine::setupCoroutine()` seems to consume at least 50-60 bytes of flash memory overhead per coroutine. On 32-bit processors, the overhead seems to be only about 30-40 bytes per coroutine. ### Method `virtual void setupCoroutine()` ``` -------------------------------- ### Complex Inheritance Example Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/graph_legend.html A comprehensive example demonstrating multiple inheritance types (public, protected, private) and class usage, resulting in a detailed graph. ```cpp /*! Super class that inherits a number of other classes */ class Inherited : public PublicBase, protected ProtectedBase, private PrivateBase, public Undocumented, public Templ { private: Used *m_usedClass; }; ``` -------------------------------- ### CoroutineSchedulerTemplate Setup Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/CoroutineScheduler_8h_source.html Initializes the coroutine scheduler. This should be called once before any coroutines are run. ```cpp static void setup() { getScheduler()->setupScheduler(); } ``` -------------------------------- ### Basic Coroutine Structure in C++ Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md Defines a simple one-shot coroutine and a looping coroutine using AceRoutine macros. Includes setup for CoroutineScheduler in `setup()` and its execution in `loop()`. ```C++ #include using namespace ace_routine; COROUTINE(oneShotRoutine) { COROUTINE_BEGIN(); ... COROUTINE_YIELD(); ... COROUTINE_AWAIT(condition); ... COROUTINE_DELAY(100); ... COROUTINE_END(); } COROUTINE(loopingRoutine) { COROUTINE_LOOP() { ... COROUTINE_YIELD(); ... } } void setup() { // Set up Serial port if needed by app, not needed by AceRoutine Serial.begin(115200); while (!Serial); // Leonardo/Micro ... CoroutineScheduler::setup(); ... } void loop() { CoroutineScheduler::loop(); } ``` -------------------------------- ### Deprecated Coroutine Setup Methods Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/Coroutine_8h_source.html These are deprecated methods for setting up a coroutine with a name. Use newer methods if available. They currently do nothing. ```cpp void setupCoroutine(const char* /*name*/) ACE_ROUTINE_DEPRECATED {} ``` ```cpp void setupCoroutine(const __FlashStringHelper* /*name*/) ACE_ROUTINE_DEPRECATED {} ``` -------------------------------- ### getRoot Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate-members.html Gets the root coroutine. ```APIDOC ## getRoot ### Description Gets the root coroutine. ### Method `static` ### Signature `getRoot()` ``` -------------------------------- ### Coroutine Communication Using Instance Variables Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md Demonstrates how to share state between coroutines by passing references and using public methods. Ensure CoroutineScheduler is set up and looped in setup() and loop() respectively. ```C++ class RoutineA: public Coroutine { public: int runCoroutine() override { COROUTINE_LOOP() { ... } } void setState(bool s) { state = s; } private: bool state; }; class RoutineB: public Coroutine { public: RoutineB(RoutineA& routineACoroutine): routineA(routineACoroutine) {} int runCoroutine() override { COROUTINE_LOOP() { ... routineA.setState(state); COROUTINE_YIELD(); } } private: RoutineA& routineA; }; RoutineA routineA; RoutineB routineB(routineA); void setup() { ... CoroutineScheduler::setup(); ... } void loop() { CoroutineScheduler::loop(); } ``` -------------------------------- ### Coroutine Profiling with LogBinTableRenderer and LogBinJsonRenderer Source: https://github.com/bxparks/aceroutine/blob/develop/README.md Shows how to set up coroutine profilers and extract profiling information using `Coroutine::runCoroutineWithProfiler()`. This example utilizes `LogBinTableRenderer` for formatted output and `LogBinJsonRenderer` for JSON output. ```C++ #include using namespace ace_routine; const int PIN = 2; const int LED = LED_BUILTIN; const int LED_ON = HIGH; const int LED_OFF = LOW; COROUTINE(blinkLed) { COROUTINE_LOOP() { digitalWrite(LED, LED_ON); COROUTINE_DELAY(100); digitalWrite(LED, LED_OFF); COROUTINE_DELAY(500); } } COROUTINE(printHelloWorld) { COROUTINE_LOOP() { Serial.print(F("Hello, ")); Serial.flush(); COROUTINE_DELAY(1000); Serial.println(F("World")); COROUTINE_DELAY(4000); } } COROUTINE(printProfiling) { COROUTINE_LOOP() { LogBinTableRenderer::printTo( Serial, 3 /*startBin*/, 14 /*endBin*/, false /*clear*/); LogBinJsonRenderer::printTo( Serial, 3 /*startBin*/, 14 /*endBin*/); COROUTINE_DELAY(5000); } } LogBinProfiler profiler1; LogBinProfiler profiler2; LogBinProfiler profiler3; void setup() { delay(1000); Serial.begin(115200); while (!Serial); // Leonardo/Micro pinMode(LED, OUTPUT); pinMode(PIN, INPUT); // Coroutine names can be either C-string or F-string. blinkLed.setName("blinkLed"); readPin.setName(F("readPin")); // Manually attach the profilers to the coroutines. blinkLed.setProfiler(&profiler1); readPin.setProfiler(&profiler2); printProfiling.setProfiler(&profiler3); } void loop() { blinkLed.runCoroutineWithProfiler(); printHelloWorld.runCoroutineWithProfiler(); printProfiling.runCoroutineWithProfiler(); } ``` -------------------------------- ### Automated Coroutine Setup with CoroutineScheduler Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md Use CoroutineScheduler::setupCoroutines() to automatically call setupCoroutine() for all registered coroutines. This is efficient for a large number of coroutines when memory consumption is not a primary constraint. ```C++ ManualCoroutine1 coroutine1; ManualCoroutine2 coroutine2; void setup() { CoroutineScheduler::setupCoroutines(); CoroutineScheduler::setup(); ... ``` -------------------------------- ### Define and Run a Simple Coroutine with COROUTINE() Source: https://github.com/bxparks/aceroutine/blob/develop/README.md Use the COROUTINE() macro for simple coroutines. It handles boilerplate and internal bookkeeping. This example blinks an LED and prints 'Hello, World!' concurrently. ```C++ #include using namespace ace_routine; const int LED = LED_BUILTIN; const int LED_ON = HIGH; const int LED_OFF = LOW; COROUTINE(blinkLed) { COROUTINE_LOOP() { digitalWrite(LED, LED_ON); COROUTINE_DELAY(100); digitalWrite(LED, LED_OFF); COROUTINE_DELAY(500); } } COROUTINE(printHelloWorld) { COROUTINE_LOOP() { Serial.print(F("Hello, ")); Serial.flush(); COROUTINE_DELAY(1000); Serial.println(F("World")); COROUTINE_DELAY(4000); } } void setup() { delay(1000); Serial.begin(115200); while (!Serial); // Leonardo/Micro pinMode(LED, OUTPUT); } void loop() { blinkLed.runCoroutine(); printHelloWorld.runCoroutine(); } ``` -------------------------------- ### Public Inheritance Example Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/graph_legend.html Demonstrates a class inheriting from another using public inheritance, shown with a dark blue arrow in Doxygen graphs. ```cpp /*! Class that is inherited using public inheritance */ class PublicBase : public Truncated { }; ``` -------------------------------- ### Coroutine Blink Example Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md Demonstrates an asymmetric LED blink using the COROUTINE macro for non-blocking behavior. Use this for simpler asynchronous tasks. ```C++ COROUTINE(blink) { COROUTINE_LOOP() { digitalWrite(LED_BUILTIN, HIGH); COROUTINE_DELAY(100); digitalWrite(LED_BUILTIN, LOW); COROUTINE_DELAY(500); } } void loop() { blink.runCoroutine(); } ``` -------------------------------- ### Private Inheritance Example Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/graph_legend.html Illustrates a class inheriting via private inheritance, depicted by a dark red arrow. ```cpp /*! Class that is inherited using private inheritance */ class PrivateBase { }; ``` -------------------------------- ### seconds() Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/ClockInterface_8h_source.html Calculates and returns the current number of seconds elapsed since the Arduino system started. ```APIDOC ## static unsigned long seconds() ### Description Get the current seconds. ### Definition ClockInterface.h:63 ``` -------------------------------- ### Template Class Example Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/graph_legend.html Defines a template class, which can be instantiated and will show a yellow dashed arrow relation to its template. ```cpp /*! A template class */ template class Templ { }; ``` -------------------------------- ### Auto-create Profilers for All Coroutines Source: https://context7.com/bxparks/aceroutine/llms.txt Uses `LogBinProfiler::createProfilers()` to automatically allocate heap-based profilers for all registered coroutines, simplifying setup when many coroutines require profiling. ```cpp #include using namespace ace_routine; COROUTINE(taskA) { COROUTINE_LOOP() { COROUTINE_DELAY(100); } } COROUTINE(taskB) { COROUTINE_LOOP() { COROUTINE_DELAY(200); } } COROUTINE(taskC) { COROUTINE_LOOP() { COROUTINE_DELAY(50); } } COROUTINE(printStats) { COROUTINE_LOOP() { COROUTINE_DELAY(5000); LogBinTableRenderer::printTo(Serial, 2, 13); // Bins are automatically cleared after printing (clear=true by default). } } void setup() { Serial.begin(115200); while (!Serial); taskA.setName(F("taskA")); taskB.setName(F("taskB")); taskC.setName(F("taskC")); printStats.setName(F("printStats")); LogBinProfiler::createProfilers(); // one profiler per coroutine, heap-allocated CoroutineScheduler::setup(); } void loop() { CoroutineScheduler::loopWithProfiler(); } ``` -------------------------------- ### Class Usage Example Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/graph_legend.html Represents a class that is used or contained by another class, indicated by a purple dashed arrow. ```cpp /*! Class that is used by the Inherited class */ class Used { }; ``` -------------------------------- ### Non-Blocking Delay Blink Example Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md Shows an equivalent LED blink using standard non-blocking delay functions with static variables. This is an alternative to coroutines for managing state across delays. ```C++ void blink() { static uint16_t prevMillis; static uint8_t blinkState; const uint8_t kBlinkStateLow = 0; const uint8_t kBlinkStateHigh = 1; if (blinkState == kBlinkStateHigh) { uint16_t nowMillis = millis(); if ((uint16_t) (nowMillis - prevMillis) >= 100) { prevMillis = nowMillis; digitalWrite(LED_BUILTIN, LOW); blinkState = kBlinkStateLow; } } else { uint16_t nowMillis = millis(); if ((uint16_t) (nowMillis - prevMillis) >= 500) { prevMillis = nowMillis; digitalWrite(LED_BUILTIN, HIGH); blinkState = kBlinkStateHigh; } } } void loop() { blink(); } ``` -------------------------------- ### Coroutine Lifecycle Methods Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate.html These methods define the core lifecycle and behavior of a coroutine, including setup, execution, and state management. ```APIDOC ## runCoroutine ### Description This is a pure virtual function that must be implemented by derived classes to define the main logic of the coroutine. ### Method `virtual int runCoroutine() = 0;` ### Parameters None ### Returns An integer representing the coroutine's exit status or result. ``` ```APIDOC ## setupCoroutine ### Description This method is called once when the coroutine is initialized. It is used to perform any necessary setup operations before the coroutine starts executing. ### Method `virtual void setupCoroutine()` ### Parameters None ### Returns None ``` ```APIDOC ## runCoroutineWithProfiler ### Description A variant of `runCoroutine()` that includes profiling to measure execution time and update an attached profiler if available. ### Method `int runCoroutineWithProfiler()` ### Parameters None ### Returns An integer representing the coroutine's exit status or result. ``` ```APIDOC ## suspend ### Description Suspends the coroutine's execution at the next scheduler iteration. The coroutine will not run until explicitly resumed. ### Method `void suspend()` ### Parameters None ### Returns None ``` ```APIDOC ## resume ### Description Resumes a suspended coroutine by adding it to the head of the scheduler's linked list and changing its state to Yielding. This allows the coroutine to continue its execution. ### Method `void resume()` ### Parameters None ### Returns None ``` ```APIDOC ## reset ### Description Resets the coroutine to its initial state, allowing it to be executed again from the beginning. ### Method `void reset()` ### Parameters None ### Returns None ``` -------------------------------- ### Example Writer Coroutine Sending Messages Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md A sketch of a Writer coroutine that sends 10 integer messages to a channel. It uses COROUTINE_BEGIN/END and COROUTINE_CHANNEL_WRITER. ```C++ class Writer: public Coroutine { public: Writer(...) {...} int runCoroutine() override { static int i; COROUTINE_BEGIN(); for (i = 0; i < 9; i++) { Message message = { Message::kStatusOk, i }; COROUTINE_CHANNEL_WRITER(mChannel, message); } COROUTINE_END(); } private: Channel& mChannel; }; ``` -------------------------------- ### setupCoroutines Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/CoroutineScheduler_8h_source.html Sets up the coroutines by calling their setupCoroutine() methods. This is a static method that initializes the coroutine system. ```APIDOC ## setupCoroutines ### Description Set up the coroutines by calling their setupCoroutine() methods. ### Method static void setupCoroutines() ### Endpoint N/A (Static method, not an endpoint) ``` -------------------------------- ### Print Coroutine Profiler Data to a Table Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1LogBinTableRendererTemplate.html Use this static method to loop over all coroutines and print an ASCII version of their frequency distribution. It assumes all coroutines use the same profiler class. Customize the output by specifying the start and end bins, and whether to clear the bins or roll up exterior bins. ```cpp static void printTo( Print &_printer_, uint8_t _startBin_, uint8_t _endBin_, bool _clear_ = true, bool _rollup_ = true ) ``` -------------------------------- ### CoroutineSchedulerTemplate::setup Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineSchedulerTemplate-members.html Initializes the coroutine scheduler. ```APIDOC ## setup ### Description Initializes the coroutine scheduler, preparing it for operation. ### Method `setup()` ``` -------------------------------- ### Run Benchmarks with Make Source: https://github.com/bxparks/aceroutine/blob/develop/examples/MemoryBenchmark/README.md Execute the benchmark generation process using the Makefile. This command will produce various .txt files containing benchmark data. ```bash make benchmarks ``` -------------------------------- ### setupCoroutine(const char *name) [DEPRECATED] Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate-members.html Deprecated method to set up the coroutine with a string name. ```APIDOC ## setupCoroutine(const char *name) [DEPRECATED] ### Description This method is deprecated. It was used to set up the coroutine and provide a string name. Use `setName()` and `setupCoroutine()` separately. ### Method `setupCoroutine(const char *name)` ### Parameters * **name** (const char *) - The name to assign to the coroutine (deprecated). ### Response None ``` -------------------------------- ### setupCoroutine (deprecated) Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/Coroutine_8h_source.html Deprecated methods for setting up a coroutine with a name. Overloads exist for C-style strings and FlashStringHelper. ```APIDOC ## setupCoroutine (deprecated) ### Description Deprecated methods for setting up a coroutine with a name. These methods are marked as deprecated and should not be used in new code. ### Method `void setupCoroutine(const char* name)` `void setupCoroutine(const __FlashStringHelper* name)` ### Parameters - `name` (const char* or const __FlashStringHelper*) - The name of the coroutine (deprecated). ``` -------------------------------- ### Automate README Generation Source: https://github.com/bxparks/aceroutine/blob/develop/examples/AutoBenchmark/README.md Execute this command to automate the generation of the README.md file, including benchmark tables. This simplifies the documentation update process. ```bash make README.md ``` -------------------------------- ### getProfiler() Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/functions_func.html Gets the coroutine's profiler. ```APIDOC ## getProfiler() ### Description Retrieves the profiler object associated with this coroutine. ### Method N/A (Member Function) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response - **LogBinProfilerTemplate***: A pointer to the coroutine's profiler. ``` -------------------------------- ### getNext() Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/functions_func.html Gets the next coroutine in a sequence. ```APIDOC ## getNext() ### Description Retrieves a pointer to the next coroutine in a linked list or sequence. ### Method N/A (Member Function) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response - **CoroutineTemplate***: A pointer to the next coroutine. ``` -------------------------------- ### setupCoroutine(const __FlashStringHelper *name) [DEPRECATED] Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate-members.html Deprecated method to set up the coroutine with a FlashStringHelper name. ```APIDOC ## setupCoroutine(const __FlashStringHelper *name) [DEPRECATED] ### Description This method is deprecated. It was used to set up the coroutine and provide a name stored in program memory. Use `setName()` and `setupCoroutine()` separately. ### Method `setupCoroutine(const __FlashStringHelper *name)` ### Parameters * **name** (const __FlashStringHelper *) - The Flash string name to assign to the coroutine (deprecated). ### Response None ``` -------------------------------- ### getJump() Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/functions_func.html Gets the jump target of the coroutine. ```APIDOC ## getJump() ### Description Retrieves the jump target address or identifier for the coroutine. ### Method N/A (Member Function) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response - **void***: A pointer to the jump target. ``` -------------------------------- ### getFName() Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/functions_func.html Gets the function name of the coroutine. ```APIDOC ## getFName() ### Description Retrieves the function name associated with the coroutine. ### Method N/A (Member Function) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response - **const char***: A pointer to the function name of the coroutine. ``` -------------------------------- ### getStatus Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate-members.html Gets the current status of the coroutine. ```APIDOC ## getStatus ### Description Gets the current status of the coroutine. ### Method `inline protected` ### Signature `getStatus() const` ``` -------------------------------- ### getNext Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate-members.html Gets the next coroutine in the scheduler. ```APIDOC ## getNext ### Description Gets the next coroutine in the scheduler. ### Method `inline` ### Signature `getNext()` ``` -------------------------------- ### CoroutineSchedulerTemplate::setupCoroutines Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineSchedulerTemplate.html Sets up all registered coroutines by calling their respective setupCoroutine() methods. This ensures coroutines are ready to run. ```APIDOC ## setupCoroutines ### Description Set up the coroutines by calling their setupCoroutine() methods. ### Method static void ### Signature `void setupCoroutines()` ``` -------------------------------- ### getNameType() Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/functions_func.html Gets the type of name used for the coroutine. ```APIDOC ## getNameType() ### Description Retrieves the type of name (e.g., C-style, function name) used for the coroutine. ### Method N/A (Member Function) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response - **int**: An integer representing the name type. ``` -------------------------------- ### getProfiler Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate-members.html Gets the coroutine's profiler information. ```APIDOC ## getProfiler ### Description Gets the coroutine's profiler information. ### Method `inline` ### Signature `getProfiler() const` ``` -------------------------------- ### getNameType Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate-members.html Gets the type of the coroutine's name. ```APIDOC ## getNameType ### Description Gets the type of the coroutine's name. ### Method `inline` ### Signature `getNameType() const` ``` -------------------------------- ### getJump Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate-members.html Gets the coroutine's jump target. ```APIDOC ## getJump ### Description Gets the coroutine's jump target. ### Method `inline protected` ### Signature `getJump() const` ``` -------------------------------- ### getFName Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate-members.html Gets the coroutine's C++-style name. ```APIDOC ## getFName ### Description Gets the coroutine's C++-style name. ### Method `inline` ### Signature `getFName() const` ``` -------------------------------- ### getCName Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate-members.html Gets the coroutine's C-style name. ```APIDOC ## getCName ### Description Gets the coroutine's C-style name. ### Method `inline` ### Signature `getCName() const` ``` -------------------------------- ### AutoBenchmark CPU Performance Comparison Source: https://github.com/bxparks/aceroutine/blob/develop/examples/AutoBenchmark/README.md Compares the execution time of different scheduling methods, including direct scheduling and coroutine scheduling, with and without profiling. Results are shown in microseconds per iteration. ```text CPU: +---------------------------------+--------+-------------+--------+ | Functionality | iters | micros/iter | diff | |---------------------------------+--------+-------------+--------| | EmptyLoop | 10000 | 1.900 | 0.000 | |---------------------------------+--------+-------------+--------| | DirectScheduling | 10000 | 2.800 | 0.900 | | DirectSchedulingWithProfiler | 10000 | 5.800 | 3.900 | |---------------------------------+--------+-------------+--------| | CoroutineScheduling | 10000 | 7.000 | 5.100 | | CoroutineSchedulingWithProfiler | 10000 | 9.300 | 7.400 | +---------------------------------+--------+-------------+--------+ ``` -------------------------------- ### CoroutineSchedulerTemplate::setupCoroutines Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineSchedulerTemplate-members.html Sets up the coroutines within the scheduler. ```APIDOC ## setupCoroutines ### Description Configures and prepares the coroutines to be managed by the scheduler. ### Method `setupCoroutines()` ``` -------------------------------- ### coroutineSeconds() Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/functions_func.html Returns the current time in seconds since the coroutine started. ```APIDOC ## coroutineSeconds() ### Description Returns the current time in seconds relative to the coroutine's start. ### Method N/A (Member Function) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response - **unsigned long long**: The current time in seconds. ``` -------------------------------- ### COROUTINE_BEGIN Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/Coroutine_8h.html Marks the beginning of a coroutine. ```APIDOC ## COROUTINE_BEGIN() ### Description Marks the beginning of a coroutine. ### Definition Coroutine.h:141 ``` -------------------------------- ### coroutineMillis() Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/functions_func.html Returns the current time in milliseconds since the coroutine started. ```APIDOC ## coroutineMillis() ### Description Returns the current time in milliseconds relative to the coroutine's start. ### Method N/A (Member Function) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response - **unsigned long long**: The current time in milliseconds. ``` -------------------------------- ### HelloSchedulerWithProfiler Arduino Sketch Source: https://github.com/bxparks/aceroutine/blob/develop/README.md This sketch demonstrates how to use `CoroutineScheduler::loopWithProfiler()` and `LogBinProfiler::createProfilers()` to manage multiple coroutines and collect performance data. It sets up three coroutines: one for blinking an LED, another for printing 'Hello, World!', and a third for periodically printing profiling information. ```C++ #include using namespace ace_routine; const int PIN = 2; const int LED = LED_BUILTIN; const int LED_ON = HIGH; const int LED_OFF = LOW; COROUTINE(blinkLed) { COROUTINE_LOOP() { digitalWrite(LED, LED_ON); COROUTINE_DELAY(100); digitalWrite(LED, LED_OFF); COROUTINE_DELAY(500); } } COROUTINE(printHelloWorld) { COROUTINE_LOOP() { Serial.print(F("Hello, ")); Serial.flush(); COROUTINE_DELAY(1000); Serial.println(F("World")); COROUTINE_DELAY(4000); } } COROUTINE(printProfiling) { COROUTINE_LOOP() { LogBinTableRenderer::printTo( Serial, 3 /*startBin*/, 14 /*endBin*/, false /*clear*/); LogBinJsonRenderer::printTo( Serial, 3 /*startBin*/, 14 /*endBin*/); COROUTINE_DELAY(5000); } } void setup() { delay(1000); Serial.begin(115200); while (!Serial); // Leonardo/Micro pinMode(LED, OUTPUT); pinMode(PIN, INPUT); // Coroutine names can be either C-string or F-string. blinkLed.setName("blinkLed"); readPin.setName(F("readPin")); // Create profilers on the heap and attach them to all coroutines. LogBinProfiler::createProfilers(); CoroutineScheduler::setup(); } void loop() { CoroutineScheduler::loopWithProfiler(); } ``` -------------------------------- ### getFName() Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate.html Get the name of the coroutine assuming it's an f-string. This value can be null. ```APIDOC ## getFName() ### Description Get name of the coroutine assuming it's an f-string. Nullable. ### Method const __FlashStringHelper* ### Parameters None ### Return Value const __FlashStringHelper* - The F-string name of the coroutine, or nullptr if not set. ``` -------------------------------- ### Create and Inject Channel Instance Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md Demonstrates how to create a Channel instance templated with a specific message type and inject it into Coroutine subclasses (Writer and Reader). ```C++ Channel channel; class Writer: public Coroutine { public: Writer(Channel& channel, ...): mChannel(channel), ... {...} private: Channel& mChannel; }; class Reader: public Coroutine { public: Reader(Channel& channel, ...): mChannel(channel), ... {...} private: Channel& mChannel; }; ``` -------------------------------- ### getCName() Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1CoroutineTemplate.html Get the name of the coroutine assuming it's a c-string. This value can be null. ```APIDOC ## getCName() ### Description Get name of the coroutine assuming it's a c-string. Nullable. ### Method const char* ### Parameters None ### Return Value const char* - The C-string name of the coroutine, or nullptr if not set. ``` -------------------------------- ### printTo Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1LogBinTableRendererTemplate-members.html Renders the log bin table to a given printer. ```APIDOC ## printTo ### Description Renders the log bin table to a given printer. ### Parameters - **printer** (Print &) - The printer object to write to. - **startBin** (uint8_t) - The starting bin index. - **endBin** (uint8_t) - The ending bin index. - **clear** (bool) - Optional. Whether to clear the table before rendering. Defaults to true. - **rollup** (bool) - Optional. Whether to roll up bins. Defaults to true. ``` -------------------------------- ### Invisible Class Example Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/graph_legend.html Illustrates a class that is not visible in Doxygen graphs due to truncation. ```cpp /*! Invisible class because of truncation */ class Invisible { }; ``` -------------------------------- ### Generate README Table Source: https://github.com/bxparks/aceroutine/blob/develop/examples/ChannelBenchmark/README.md Generates an ASCII table from benchmark results stored in a .txt file. This script automates the process of creating tables for the README. ```bash $ ./generate_table.awk < nano.txt ``` ```bash $ make README.md ``` -------------------------------- ### ace_routine::ClockInterface::seconds Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1ClockInterface-members.html Returns the current time in seconds since the system started. ```APIDOC ## seconds() ### Description Returns the current time in seconds. ### Method static ### Parameters None ### Returns unsigned long - The current time in seconds. ``` -------------------------------- ### Channel Constructor Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1Channel.html Initializes a new instance of the Channel class. ```APIDOC ## Channel() ### Description Constructs an instance of the Channel class. ### Method Constructor ### Parameters None ``` -------------------------------- ### ace_routine::ClockInterface::millis Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1ClockInterface-members.html Returns the current time in milliseconds since the system started. ```APIDOC ## millis() ### Description Returns the current time in milliseconds. ### Method static ### Parameters None ### Returns unsigned long - The current time in milliseconds. ``` -------------------------------- ### ace_routine::ClockInterface::micros Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/classace__routine_1_1ClockInterface-members.html Returns the current time in microseconds since the system started. ```APIDOC ## micros() ### Description Returns the current time in microseconds. ### Method static ### Parameters None ### Returns unsigned long - The current time in microseconds. ``` -------------------------------- ### COROUTINE_BEGIN Macro Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/Coroutine_8h.html Marks the beginning of a coroutine's execution context. ```APIDOC ## COROUTINE_BEGIN ### Description Mark the beginning of a coroutine. ### Value ```cpp void* p = this->getJump(); if (p != nullptr) { goto *p; } ``` ### Definition Coroutine.h:141 ``` -------------------------------- ### Generate Benchmark Table Source: https://github.com/bxparks/aceroutine/blob/develop/examples/AutoBenchmark/README.md Use this command to generate an ASCII table from a .txt results file. This helps in visualizing benchmark data. ```bash ./generate_table.awk < nano.txt ``` -------------------------------- ### Truncated Class Example Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/graph_legend.html Shows a class where inheritance relations might be hidden because the graph is truncated. ```cpp /*! Truncated class, inheritance relation is hidden */ class Truncated : public Invisible { }; ``` -------------------------------- ### Undocumented Class Example Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/graph_legend.html Represents a class that is not documented with Doxygen comments and will appear with a gray border in graphs. ```cpp /* Class not documented with doxygen comments */ class Undocumented { }; ``` -------------------------------- ### Manual Coroutine Subclassing Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md Illustrates creating custom coroutine instances by manually subclassing the Coroutine class and defining the runCoroutine method. ```C++ class MyCoroutine : public Coroutine { public: int runCoroutine() override { COROUTINE_BEGIN(); ... COROUTINE_END(); } }; MyCoroutine routine1; MyCoroutine routine2; ``` -------------------------------- ### Get Coroutine Profiler Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/Coroutine_8h_source.html Retrieves the CoroutineProfiler object associated with this coroutine. Returns nullptr if no profiler is set. ```cpp CoroutineProfiler* getProfiler() const { return mProfiler; } ``` -------------------------------- ### LogBinTableRenderer PrintTo Method Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md Static method to print profiler results in a human-readable formatted table with fixed column widths. The `printer` is typically `Serial`. ```C++ class LogBinTableRenderer { public: static void printTo( Print& printer, uint8_t startBin, uint8_t endBin, bool clear = true, bool rollup = true ); }; ``` -------------------------------- ### Coroutine Name Management Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/Coroutine_8h_source.html Methods for getting and setting the name of a coroutine. The name can be a C-style string or a FlashStringHelper. ```APIDOC ## Coroutine Name Management ### `uint8_t getNameType() const` #### Description Retrieves the type of the name stored for the coroutine. ### `void setName(const char* name)` #### Description Sets the coroutine's name using a C-style string. ### `void setName(const __FlashStringHelper* name)` #### Description Sets the coroutine's name using a FlashStringHelper. ### `const char* getCName() const` #### Description Retrieves the coroutine's name as a C-style string. Returns nullptr if the name is not a C-style string. ### `const __FlashStringHelper* getFName() const` #### Description Retrieves the coroutine's name as a FlashStringHelper. Returns nullptr if the name is not a FlashStringHelper. ### `void printNameTo(Print& printer, uint8_t maxLen = 0) const` #### Description Prints the coroutine's name to a given Print object, with optional truncation or padding to a specified maximum length. Handles both C-style strings and FlashStringHelpers, and provides a default representation if the name is null. ``` -------------------------------- ### Coroutine Profiler Integration Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md Methods to set and get a CoroutineProfiler for a Coroutine. Use runCoroutineWithProfiler() to enable profiling. ```C++ class Coroutine { ... public: void setProfiler(CoroutineProfiler* profiler); CoroutineProfiler* getProfiler() const; int runCoroutineWithProfiler(); ... }; ``` -------------------------------- ### Coroutine Profiling with LogBinProfiler Source: https://context7.com/bxparks/aceroutine/llms.txt Enables execution-time profiling for coroutines by attaching LogBinProfiler instances and using `runCoroutineWithProfiler()` or `CoroutineScheduler::loopWithProfiler()`. Results can be rendered as tables or JSON. ```cpp #include using namespace ace_routine; COROUTINE(fastTask) { COROUTINE_LOOP() { // simulate fast work for (volatile int i = 0; i < 100; i++) {} COROUTINE_YIELD(); } } COROUTINE(slowTask) { COROUTINE_LOOP() { COROUTINE_DELAY(500); } } COROUTINE(reporter) { COROUTINE_LOOP() { COROUTINE_DELAY(5000); // Print histogram table: bins 3..14 (~8 µs to ~8 ms range) LogBinTableRenderer::printTo(Serial, 3, 14, /*clear=*/false); // Print same data as JSON LogBinJsonRenderer::printTo(Serial, 3, 14); } } LogBinProfiler p1, p2, p3; void setup() { Serial.begin(115200); while (!Serial); fastTask.setName("fastTask"); slowTask.setName(F("slowTask")); reporter.setName(F("reporter")); fastTask.setProfiler(&p1); slowTask.setProfiler(&p2); reporter.setProfiler(&p3); CoroutineScheduler::setup(); } void loop() { CoroutineScheduler::loopWithProfiler(); // measures elapsed µs per coroutine } // Example table output: // name <16us <32us <64us<128us<256us<512us <1ms <2ms <4ms <8ms >> // fastTask 65535 1234 0 0 0 0 0 0 0 0 0 // slowTask 0 0 0 0 0 0 65535 0 0 0 0 // reporter 0 0 0 0 0 0 0 0 0 0 10 ``` -------------------------------- ### Define Coroutine Loop Source: https://github.com/bxparks/aceroutine/blob/develop/docs/html/Coroutine_8h.html Marks the beginning of a coroutine loop. Can be used instead of COROUTINE_BEGIN() at the start of a Coroutine. ```c++ #define COROUTINE_LOOP ( ) **Value:** [COROUTINE_BEGIN](Coroutine_8h.html#a7b7dc05639a37cb3f51bf348c68db283)(); \ while (true) \ [COROUTINE_BEGIN](Coroutine_8h.html#a7b7dc05639a37cb3f51bf348c68db283) ``` -------------------------------- ### Arduino Nano CPU Benchmark Results Source: https://github.com/bxparks/aceroutine/blob/develop/README.md Benchmark results for Arduino Nano showing performance differences between empty loops, direct scheduling, and coroutine scheduling, with and without profiling. ```text +---------------------------------+--------+-------------+--------+ | Functionality | iters | micros/iter | diff | |---------------------------------+--------+-------------+--------| | EmptyLoop | 10000 | 1.700 | 0.000 | |---------------------------------+--------+-------------+--------| | DirectScheduling | 10000 | 2.900 | 1.200 | | DirectSchedulingWithProfiler | 10000 | 5.700 | 4.000 | |---------------------------------+--------+-------------+--------| | CoroutineScheduling | 10000 | 7.100 | 5.400 | | CoroutineSchedulingWithProfiler | 10000 | 9.300 | 7.600 | +---------------------------------+--------+-------------+--------+ ``` -------------------------------- ### COROUTINE() Macro Expansion Source: https://github.com/bxparks/aceroutine/blob/develop/USER_GUIDE.md Shows the equivalent manual implementation of a coroutine created using the COROUTINE() macro, illustrating the generated class structure. ```C++ class Coroutine_doSomething : public Coroutine { public: Coroutine_doSomething() {} int runCoroutine() override { COROUTINE_BEGIN(); ... COROUTINE_END(); } }; Coroutine_doSomething doSomething; ``` -------------------------------- ### Benchmark Results Table Source: https://github.com/bxparks/aceroutine/blob/develop/examples/ChannelBenchmark/README.md Displays benchmark results for CPU overhead of Channel operations across different microcontrollers. The 'diff' column approximates the overhead per iteration. ```text +---------------+--------+-------------+--------+ | Functionality | count | micros/iter | diff | +---------------+--------+-------------+--------+ | Baseline | 10000 | 16.400 | 0.000 | | Channels | 10000 | 28.100 | 11.700 | +---------------+--------+-------------+--------+ ``` ```text +---------------+--------+-------------+--------+ | Functionality | count | micros/iter | diff | +---------------+--------+-------------+--------+ | Baseline | 10000 | 16.500 | 0.000 | | Channels | 10000 | 28.300 | 11.800 | +---------------+--------+-------------+--------+ ``` ```text +---------------+--------+-------------+--------+ | Functionality | count | micros/iter | diff | +---------------+--------+-------------+--------+ | Baseline | 50000 | 3.040 | 0.000 | | Channels | 50000 | 4.460 | 1.420 | +---------------+--------+-------------+--------+ ``` ```text +---------------+--------+-------------+--------+ | Functionality | count | micros/iter | diff | +---------------+--------+-------------+--------+ | Baseline | 10000 | 2.700 | 0.000 | | Channels | 10000 | 4.200 | 1.500 | +---------------+--------+-------------+--------+ ``` ```text +---------------+--------+-------------+--------+ | Functionality | count | micros/iter | diff | +---------------+--------+-------------+--------+ | Baseline | 100000 | 0.880 | 0.000 | | Channels | 100000 | 1.340 | 0.460 | +---------------+--------+-------------+--------+ ```