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..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() { - Producer.counter.connectTo(Consumer.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 c0e94be..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_Thread.led.connectTo(Sink_Thread.led); + CONNECT(Source_Thread, led, Sink_Thread, led); Sink_Thread.start(); Source_Thread.start(); } 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 diff --git a/src/Arduino_Threads.h b/src/Arduino_Threads.h index 6a233ed..5078dfd 100644 --- a/src/Arduino_Threads.h +++ b/src/Arduino_Threads.h @@ -41,9 +41,7 @@ **************************************************************************************/ #define SOURCE(name, type) \ -public: \ - Source<type> name; \ -private: +Source<type> name; /* We need to call the SinkBlocking<T>(size_t const size) * non-default constructor using size as parameter. @@ -61,14 +59,10 @@ public: \ */ #define SINK_2_ARG(name, type) \ -public: \ - SinkBlocking<type> name{1}; \ -private: +SinkBlocking<type> name{1} #define SINK_3_ARG(name, type, size) \ -public: \ - SinkBlocking<type> name{size}; \ -private: +SinkBlocking<type> name{size} /* Black C macro magic enabling "macro overloading" * with same name macro, but multiple arguments. @@ -78,10 +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<type> name{}; \ -private: +SinkNonBlocking<type> name{} +#define CONNECT(source_thread, source_name, sink_thread, sink_name) \ +source_thread##Private::source_name.connectTo(sink_thread##Private::sink_name) #define SHARED_2_ARG(name, type) \ Shared<type> name; @@ -135,13 +129,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_ */