You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An example of ESPressio Threads' Garbage Collection system in action.
3
+
*/
4
+
5
+
#defineESPRESSIO_THREAD_DEFAULT_STACK_SIZE = 1600// This sets the default Stack Size for all Threads in the system.
6
+
7
+
#include<Arduino.h>
8
+
9
+
#include"ESPressio_IThread.hpp"// This gives us access to the `IThread` interface.
10
+
#include"ESPressio_Thread.hpp"// This gives us access to our `Thread` base class.
11
+
#include"ESPressio_ThreadManager.hpp"// This gives us access to the `ThreadManager` class.
12
+
13
+
usingnamespaceESPressio::Threads;
14
+
15
+
classDemoThread : publicThread {
16
+
private:
17
+
uint32_t _counter = 0;
18
+
protected:
19
+
voidOnLoop() {
20
+
Serial.printf("DemoThread: %u\n", _counter++);
21
+
delay(1000);
22
+
if (counter == 10) { // When the counter reaches 10...
23
+
Terminate(); // ... terminate the thread.
24
+
}
25
+
}
26
+
};
27
+
28
+
DemoThread* thread;
29
+
30
+
// This function will be called when the thread is destroyed.
31
+
voidonThreadDestroyed(IThread* thread) {
32
+
Serial.printf("Thread %u has been destroyed!\n", thread->GetThreadID()); // Print a message to the Serial Monitor.
33
+
}
34
+
35
+
voidsetup() {
36
+
Serial.begin(115200); // Start the Serial Monitor.
37
+
38
+
thread = newDemoThread(true); // Create a new instance of our `DemoThread` class. The `true` parameter tells the Thread to use Garbage Collection when it's Terminated!
39
+
40
+
thread.SetStartOnInitialize(true); // This will start the thread as soon as it's initialized. (true is the default, but we're setting it here for clarity.)
41
+
thread.SetOnDestroy(onThreadDestroyed); // This will set the `onThreadDestroyed` function as the callback for when the thread is destroyed.
42
+
43
+
ThreadManager::Initialize(); // This will initialize ALL Thread instances in your code!
44
+
}
45
+
46
+
voidloop() {
47
+
// You can still use your main loop as normal, if you want to!
Copy file name to clipboardExpand all lines: examples/SimpleForeverThread/SimpleForeverThread.ino
+9-4Lines changed: 9 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -6,8 +6,9 @@
6
6
7
7
#include<Arduino.h>
8
8
9
-
#include"ESPressio_Thread.hpp"
10
-
#include"ESPressio_ThreadManager.hpp"
9
+
#include"ESPressio_IThread.hpp"// This gives us access to the `IThread` interface.
10
+
#include"ESPressio_Thread.hpp"// This gives us access to our `Thread` base class.
11
+
#include"ESPressio_ThreadManager.hpp"// This gives us access to the `ThreadManager` class.
11
12
12
13
usingnamespaceESPressio::Threads;
13
14
@@ -24,9 +25,13 @@ class DemoThread : public Thread {
24
25
DemoThread thread;
25
26
26
27
voidsetup() {
27
-
Serial.begin(115200);
28
+
Serial.begin(115200);// Start the Serial Monitor.
28
29
29
-
thread.SetStartOnInitialize(true);
30
+
thread.SetStartOnInitialize(true);// This will start the thread as soon as it's initialized. (true is the default, but we're setting it here for clarity.)
30
31
31
32
ThreadManager::Initialize(); // This will initialize ALL Thread instances in your code!
33
+
}
34
+
35
+
voidloop() {
36
+
// You can still use your main loop as normal, if you want to!
Copy file name to clipboardExpand all lines: src/ESPressio_IThread.hpp
+46-8Lines changed: 46 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,7 @@
3
3
// #define CORE_THREADING_DEBUG // Uncomment this to explicitly enable debugging for the threading module
4
4
#include<Arduino.h>
5
5
#include<cstdint>
6
+
#include<functional>
6
7
7
8
namespaceESPressio {
8
9
@@ -29,16 +30,12 @@ namespace ESPressio {
29
30
/// `Initialize` is invoked automatically for all Threads when the `ThreadManager` is initialized in your `main()` (or `setup()` for MCU projects) function.
30
31
virtualvoidInitialize() = 0;
31
32
32
-
/*
33
-
`Terminate` is invoked automatically for all Threads when the `ThreadManager` is terminated in your `main()` (or `loop()` for MCU projects) function.
34
-
You can, however, invoke it manually to terminate a Thread at any time!
35
-
*/
33
+
/// `Terminate` is invoked automatically for all Threads when the `ThreadManager` is terminated in your `main()` (or `loop()` for MCU projects) function.
34
+
/// You can, however, invoke it manually to terminate a Thread at any time!
36
35
virtualvoidTerminate() = 0;
37
36
38
-
/*
39
-
`Start` will start the Thread loop if it is not already running.
40
-
It will also Resume the thread if it is `Paused`.
41
-
*/
37
+
/// `Start` will start the Thread loop if it is not already running.
38
+
/// It will also Resume the thread if it is `Paused`.
42
39
virtualvoidStart() = 0;
43
40
44
41
/// `Pause` will pause the Thread loop if it is running.
0 commit comments