From 5f4a0c0f38e26e00f5384edee0d1c533ab3ba876 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 9 Nov 2021 12:17:56 +0100 Subject: [PATCH 1/4] experiment ... --- src/Arduino_Threads.h | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Arduino_Threads.h b/src/Arduino_Threads.h index 1750f95..460f8b5 100644 --- a/src/Arduino_Threads.h +++ b/src/Arduino_Threads.h @@ -127,13 +127,28 @@ class Arduino_Threads void threadFunc(); }; -#define THD_ENTER(tabname) class ARDUINO_THREADS_CONCAT(tabname, Class) : public Arduino_Threads { \ -public: \ - ARDUINO_THREADS_CONCAT(tabname, Class)() { _tabname = ARDUINO_THREADS_TO_STRING(tabname); } \ -private: \ +#define THD_SETUP(ns) ns::setup() +#define THD_LOOP(ns) ns::loop() + +#define THD_ENTER(tabname) \ +namespace ARDUINO_THREADS_CONCAT(tabname,Private)\ +{\ + void setup();\ + void loop();\ +}\ +class ARDUINO_THREADS_CONCAT(tabname, Class) : public Arduino_Threads\ +{\ +public:\ + ARDUINO_THREADS_CONCAT(tabname, Class)() { _tabname = ARDUINO_THREADS_TO_STRING(tabname); }\ +protected:\ + virtual void setup() override { THD_SETUP(ARDUINO_THREADS_CONCAT(tabname,Private)); }\ + virtual void loop() override { THD_LOOP(ARDUINO_THREADS_CONCAT(tabname,Private)); }\ +};\ +namespace ARDUINO_THREADS_CONCAT(tabname,Private)\ +{ -#define THD_DONE(tabname) \ -}; \ +#define THD_DONE(tabname)\ +};\ ARDUINO_THREADS_CONCAT(tabname,Class) tabname; #endif /* ARDUINO_THREADS_H_ */ From 19fbfe9a7102adb2a1e820e74161440181ec55a1 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 10 Nov 2021 06:37:05 +0100 Subject: [PATCH 2/4] Fix compilation error and access source/sinks via scope-resolution operator. --- .../Source_Sink_Counter.ino | 2 +- .../Source_Sink_LED/Source_Sink_LED.ino | 2 +- src/Arduino_Threads.h | 18 +++++------------- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/examples/Threading_Basics/Source_Sink_Counter/Source_Sink_Counter.ino b/examples/Threading_Basics/Source_Sink_Counter/Source_Sink_Counter.ino index f237bb2..5cde7dc 100644 --- a/examples/Threading_Basics/Source_Sink_Counter/Source_Sink_Counter.ino +++ b/examples/Threading_Basics/Source_Sink_Counter/Source_Sink_Counter.ino @@ -5,7 +5,7 @@ void setup() { - Producer.counter.connectTo(Consumer.counter); + ProducerPrivate::counter.connectTo(ConsumerPrivate::counter); Producer.start(); Consumer.start(); } diff --git a/examples/Threading_Basics/Source_Sink_LED/Source_Sink_LED.ino b/examples/Threading_Basics/Source_Sink_LED/Source_Sink_LED.ino index c0e94be..1c278ec 100644 --- a/examples/Threading_Basics/Source_Sink_LED/Source_Sink_LED.ino +++ b/examples/Threading_Basics/Source_Sink_LED/Source_Sink_LED.ino @@ -5,7 +5,7 @@ void setup() { - Source_Thread.led.connectTo(Sink_Thread.led); + Source_ThreadPrivate::led.connectTo(Sink_ThreadPrivate::led); Sink_Thread.start(); Source_Thread.start(); } diff --git a/src/Arduino_Threads.h b/src/Arduino_Threads.h index 460f8b5..729b73e 100644 --- a/src/Arduino_Threads.h +++ b/src/Arduino_Threads.h @@ -41,9 +41,7 @@ **************************************************************************************/ #define SOURCE(name, type) \ -public: \ - Source name; \ -private: +Source name; /* We need to call the SinkBlocking(size_t const size) * non-default constructor using size as parameter. @@ -61,14 +59,10 @@ public: \ */ #define SINK_2_ARG(name, type) \ -public: \ - SinkBlocking name{1}; \ -private: +SinkBlocking name{1} #define SINK_3_ARG(name, type, size) \ -public: \ - SinkBlocking name{size}; \ -private: +SinkBlocking name{size} /* Black C macro magic enabling "macro overloading" * with same name macro, but multiple arguments. @@ -78,12 +72,10 @@ public: \ #define SINK(...) GET_SINK_MACRO(__VA_ARGS__, SINK_3_ARG, SINK_2_ARG)(__VA_ARGS__) #define SINK_NON_BLOCKING(name, type) \ -public: \ - SinkNonBlocking name{}; \ -private: +SinkNonBlocking name{} #define SHARED(name, type) \ - Shared name; + Shared name #define ARDUINO_THREADS_CONCAT_(x,y) x##y #define ARDUINO_THREADS_CONCAT(x,y) ARDUINO_THREADS_CONCAT_(x,y) From c7251b8ecfb57fe915adb843f63a45a872181310 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 10 Nov 2021 06:40:26 +0100 Subject: [PATCH 3/4] Hiding clunky implementation details within neat CONNECT API. --- .../Source_Sink_Counter/Source_Sink_Counter.ino | 2 +- examples/Threading_Basics/Source_Sink_LED/Source_Sink_LED.ino | 2 +- src/Arduino_Threads.h | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/Threading_Basics/Source_Sink_Counter/Source_Sink_Counter.ino b/examples/Threading_Basics/Source_Sink_Counter/Source_Sink_Counter.ino index 5cde7dc..7f528a1 100644 --- a/examples/Threading_Basics/Source_Sink_Counter/Source_Sink_Counter.ino +++ b/examples/Threading_Basics/Source_Sink_Counter/Source_Sink_Counter.ino @@ -5,7 +5,7 @@ void setup() { - ProducerPrivate::counter.connectTo(ConsumerPrivate::counter); + CONNECT(Producer, counter, Consumer, counter); Producer.start(); Consumer.start(); } diff --git a/examples/Threading_Basics/Source_Sink_LED/Source_Sink_LED.ino b/examples/Threading_Basics/Source_Sink_LED/Source_Sink_LED.ino index 1c278ec..d781476 100644 --- a/examples/Threading_Basics/Source_Sink_LED/Source_Sink_LED.ino +++ b/examples/Threading_Basics/Source_Sink_LED/Source_Sink_LED.ino @@ -5,7 +5,7 @@ void setup() { - Source_ThreadPrivate::led.connectTo(Sink_ThreadPrivate::led); + CONNECT(Source_Thread, led, Sink_Thread, led); Sink_Thread.start(); Source_Thread.start(); } diff --git a/src/Arduino_Threads.h b/src/Arduino_Threads.h index 729b73e..d2b7989 100644 --- a/src/Arduino_Threads.h +++ b/src/Arduino_Threads.h @@ -74,6 +74,9 @@ SinkBlocking name{size} #define SINK_NON_BLOCKING(name, type) \ SinkNonBlocking name{} +#define CONNECT(source_thread, source_name, sink_thread, sink_name) \ +source_thread##Private::source_name.connectTo(sink_thread##Private::sink_name) + #define SHARED(name, type) \ Shared name From 067458ebabeea4933237a84b8e0c744f0208c0e9 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 26 Jul 2022 07:32:28 +0200 Subject: [PATCH 4/4] Utilize CONNECT macro. --- examples/Threading_Basics/Thermostat/Thermostat.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/Threading_Basics/Thermostat/Thermostat.ino b/examples/Threading_Basics/Thermostat/Thermostat.ino index 7fdfd17..f033e07 100644 --- a/examples/Threading_Basics/Thermostat/Thermostat.ino +++ b/examples/Threading_Basics/Thermostat/Thermostat.ino @@ -17,9 +17,9 @@ void setup() /* Connect the temperature sensor thread providing temperature readings * with the various temperature control unit threads. */ - TemperatureSensor.temperature.connectTo(HeatingControl.temperature); - TemperatureSensor.temperature.connectTo(AirConditionerControl.temperature); - TemperatureSensor.temperature.connectTo(TemperatureSensorReporter.temperature); + CONNECT(TemperatureSensor, temperature, HeatingControl, temperature); + CONNECT(TemperatureSensor, temperature, AirConditionerControl, temperature); + CONNECT(TemperatureSensor, temperature, TemperatureSensorReporter, temperature); /* Start the individual threads for sensing the temperature * and controlling heating/air-conditioning based on the sensed