Skip to content

[Alex/inot-as-namespace] Provide workarounds for typical sketch idioms not working when using inot-as-class model. #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 26, 2022
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

void setup()
{
Producer.counter.connectTo(Consumer.counter);
CONNECT(Producer, counter, Consumer, counter);
Producer.start();
Consumer.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions examples/Threading_Basics/Thermostat/Thermostat.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 27 additions & 18 deletions src/Arduino_Threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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_ */