From b00e04cc4c1c25c4271898eb15aaed07c29999d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Pereira?= Date: Wed, 24 Jan 2018 18:03:36 +0000 Subject: [PATCH 1/5] Added sub-instancing example. --- .../Ex_03_SubProcesses/Ex_03_SubProcesses.ino | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 examples/Ex_03_SubProcesses/Ex_03_SubProcesses.ino diff --git a/examples/Ex_03_SubProcesses/Ex_03_SubProcesses.ino b/examples/Ex_03_SubProcesses/Ex_03_SubProcesses.ino new file mode 100644 index 0000000..66c360d --- /dev/null +++ b/examples/Ex_03_SubProcesses/Ex_03_SubProcesses.ino @@ -0,0 +1,144 @@ +/* +* Example 03: Ex_03_SubProcesses.ino +* By: GitModu +* +* This example expands the Ex_02_MultiBlink.ino example, and demonstrates sub-instancing of Processes inside a class. +* This allows class definition with a custom Process without needing extra static declarations, sharing the same scheduler. +* BlinkProcess has 3 BlinkProcess objects that will blink at a diff. period and add them to the scheduler: +* - blink250 (pin 13) +* - blink500 (pin 12) +* - blink1000 (pin 11) +* Connect an LED to each of these pins and watch them blink. +* Also added a simple LogProcess to demonstrate the usage of the same scheduler. +*/ + +#include + +// Create my custom Blink Process +class BlinkProcess : public Process +{ +public: + // Call the Process constructor + BlinkProcess(Scheduler &manager, ProcPriority pr, unsigned int period, int pin) + : Process(manager, pr, period) + { + _pinState = LOW; // Set the default state + _pin = pin; // Store the pin number + } + +protected: + //setup the pins + virtual void setup() + { + pinMode(_pin, OUTPUT); + _pinState = LOW; + digitalWrite(_pin, _pinState); + } + + // Undo setup() + virtual void cleanup() + { + pinMode(_pin, INPUT); + _pinState = LOW; + } + + //LEDs should be off when disabled + virtual void onDisable() + { + _pinState = LOW; + digitalWrite(_pin, _pinState); + } + + //Start the LEDs on + virtual void onEnable() + { + _pinState = HIGH; + digitalWrite(_pin, _pinState); + } + + // Create our service routine + virtual void service() + { + // If pin is on turn it off, otherwise turn it on + _pinState = !_pinState; + digitalWrite(_pin, _pinState); + } + +private: + bool _pinState; //the Current state of the pin + int _pin; // The pin the LED is on +}; + +class BlinkerProcesses +{ +public: + // Call the Processes constructor's + BlinkerProcesses(Scheduler &manager) : + blink250(manager, HIGH_PRIORITY, 250, 13) + , blink500(manager, HIGH_PRIORITY, 500, 12) + , blink1000(manager, HIGH_PRIORITY, 1000, 11) + { + + } + + void Start() + { + // Add and enable our blink processes + blink250.add(true); // Same as calling blink250.add() and blink250.enable(); + blink500.add(true); + blink1000.add(true); + } + + void Stop() + { + blink250.disable(); + blink500.disable(); + blink1000.disable(); + } + + +private: + // Create our blink processes + BlinkProcess blink250;// Blink 13 every 250 ms + BlinkProcess blink500; // Blink 12 every 500 ms + BlinkProcess blink1000; // Blink 11 every 1000 ms +}; + +class LogProcess : public Process +{ +public: + LogProcess(Scheduler &manager, ProcPriority pr, unsigned int period) + : Process(manager, pr, period) + { + + } +protected: + //setup the pins + virtual void setup() + { + Serial.begin(57600); + } + + // Create our service routine + virtual void service() + { + Serial.println(F("Log Message.")); + } +}; + +Scheduler sched; // Create a global Scheduler object + +BlinkerProcesses Blinkers(sched); // Has 3 independent BlinkProcesses. +LogProcess LogService(sched, LOW_PRIORITY, 5000); // Show a log message every 5000 ms + +void setup() +{ + // Add and enable our blink processes and custom process, sharing the scheduler. + LogService.add(true); + Blinkers.Start(); +} + +void loop() +{ + sched.run(); +} From 29aeaedc0fdab7ba230b42078357db07fc098631 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Pereira?= Date: Fri, 26 Jan 2018 11:38:53 +0000 Subject: [PATCH 2/5] Example is now a Process as well, managing it's sub-processes. --- .../Ex_03_SubProcesses/Ex_03_SubProcesses.ino | 80 ++++++++++++++++--- 1 file changed, 67 insertions(+), 13 deletions(-) diff --git a/examples/Ex_03_SubProcesses/Ex_03_SubProcesses.ino b/examples/Ex_03_SubProcesses/Ex_03_SubProcesses.ino index 66c360d..324402b 100644 --- a/examples/Ex_03_SubProcesses/Ex_03_SubProcesses.ino +++ b/examples/Ex_03_SubProcesses/Ex_03_SubProcesses.ino @@ -69,33 +69,87 @@ private: int _pin; // The pin the LED is on }; -class BlinkerProcesses +class BlinkProcessManager :public Process { +private: + enum ProcessManagerStates + { + Step1, + Step2, + Step3 + } ProcessManagerState = Step1; public: // Call the Processes constructor's - BlinkerProcesses(Scheduler &manager) : - blink250(manager, HIGH_PRIORITY, 250, 13) + BlinkProcessManager(Scheduler &manager) : + Process(manager, LOW_PRIORITY, 1000) + , blink250(manager, HIGH_PRIORITY, 250, 13) , blink500(manager, HIGH_PRIORITY, 500, 12) - , blink1000(manager, HIGH_PRIORITY, 1000, 11) + , blink1000(manager, MEDIUM_PRIORITY, 1000, 11) { } - void Start() + // Add our blink processes, without enabling them just yet + virtual void setup() + { + blink250.add(); + blink500.add(); + blink1000.add(); + } + + void start() { - // Add and enable our blink processes - blink250.add(true); // Same as calling blink250.add() and blink250.enable(); - blink500.add(true); - blink1000.add(true); + blink250.enable(); + blink500.enable(); + blink1000.enable(); } - void Stop() + void stop() { blink250.disable(); blink500.disable(); blink1000.disable(); } + //Disable sub-processes when disabled + virtual void onDisable() + { + stop(); + } + + //Enable sub-processes when enabled + virtual void onEnable() + { + start(); + } + + // Create our service routine + virtual void service() + { + switch (ProcessManagerState) + { + case BlinkProcessManager::Step1://Enable blinkers for 5 seconds + Serial.println(F("Blinkers enabled.")); + start(); + ProcessManagerState = BlinkProcessManager::Step2; + setPeriod(5000); + break; + case BlinkProcessManager::Step2://Disable blinkers for 5 seconds + Serial.println(F("Blinkers disabled.")); + stop(); + ProcessManagerState = BlinkProcessManager::Step3; + setPeriod(5000); + break; + case BlinkProcessManager::Step3://Sleep for 2 seconds + Serial.println(F("Blinker manager sleeping.")); + ProcessManagerState = BlinkProcessManager::Step1; + setPeriod(2000); + break; + default: + break; + } + } + private: // Create our blink processes @@ -128,14 +182,14 @@ protected: Scheduler sched; // Create a global Scheduler object -BlinkerProcesses Blinkers(sched); // Has 3 independent BlinkProcesses. -LogProcess LogService(sched, LOW_PRIORITY, 5000); // Show a log message every 5000 ms +BlinkProcessManager BlinkManager(sched); // Has 3 independent BlinkProcesses. +LogProcess LogService(sched, LOW_PRIORITY, 10000); // Show a log message every 10000 ms void setup() { // Add and enable our blink processes and custom process, sharing the scheduler. LogService.add(true); - Blinkers.Start(); + BlinkManager.add(true); } void loop() From 7b0d033f5790ce3c37f0045fe558df1fa5c8653d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Pereira?= Date: Tue, 27 Feb 2018 14:43:57 +0000 Subject: [PATCH 3/5] Added deconstructor to the BlinkerManager that deletes all the BlinkProcesses from the scheduler. --- examples/Ex_03_SubProcesses/Ex_03_SubProcesses.ino | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/examples/Ex_03_SubProcesses/Ex_03_SubProcesses.ino b/examples/Ex_03_SubProcesses/Ex_03_SubProcesses.ino index 324402b..5b5b8c8 100644 --- a/examples/Ex_03_SubProcesses/Ex_03_SubProcesses.ino +++ b/examples/Ex_03_SubProcesses/Ex_03_SubProcesses.ino @@ -123,6 +123,13 @@ public: start(); } + virtual void cleanup() + { + blink250.destroy(); + blink500.destroy(); + blink1000.destroy(); + } + // Create our service routine virtual void service() { From 17b41e93d6a456a141129b2637d22d1d73770453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Thu, 26 Apr 2018 17:19:38 +0100 Subject: [PATCH 4/5] Update readme with fork description. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 58c7439..133f236 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # ArduinoProcessScheduler A cooperative Arduino object oriented, cooperative process scheduler to replace them all. +This fork is adds a wrapper for STM32 (Arduino_STM32) to implement the same basic Process interface, on top of object-oriented FreeRTOS TaskCpp. + ## What is this? As your Arduino projects get more complicated, you will begin to see the need for multitasking, or at least appear to multitask. Perhaps you want to check if a button was pressed as often as you can, but you only want to update a display once every second. Trying to do this on your own can quickly turn into overwhelming spagetti code involving `millis()`. `ArduinoProcessScheduler` seeks to simplify this. Simply create your custom Process that needs to be serviced at certain times, and let the scheduler handle the rest. From 58d247bdaaef6f7efae6a74736cdb68451a42e7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Thu, 26 Apr 2018 17:20:52 +0100 Subject: [PATCH 5/5] Added TaskCpp dependency description. --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 133f236..adc2dc8 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ # ArduinoProcessScheduler A cooperative Arduino object oriented, cooperative process scheduler to replace them all. -This fork is adds a wrapper for STM32 (Arduino_STM32) to implement the same basic Process interface, on top of object-oriented FreeRTOS TaskCpp. +This fork is adds a wrapper for STM32 (Arduino_STM32) to implement the same basic Process interface, on top of object-oriented [FreeRTOS TaskCpp](https://github.com/GitMoDu/FreeRTOScpp). + +Depends on https://github.com/GitMoDu/FreeRTOScpp . ## What is this? As your Arduino projects get more complicated, you will begin to see the need for multitasking, or at least appear to multitask. Perhaps you want to check if a button was pressed as often as you can, but you only want to update a display once every second. Trying to do this on your own can quickly turn into overwhelming spagetti code involving `millis()`. `ArduinoProcessScheduler` seeks to simplify this. Simply create your custom Process that needs to be serviced at certain times, and let the scheduler handle the rest.