Skip to content

Commit f2ce390

Browse files
authored
Merge pull request #47 from arduino-libraries/experiment-threading
[Alex/inot-as-namespace] Provide workarounds for typical sketch idioms not working when using inot-as-class model.
2 parents da1f84f + 067458e commit f2ce390

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

Diff for: examples/Threading_Basics/Source_Sink_Counter/Source_Sink_Counter.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
void setup()
77
{
8-
Producer.counter.connectTo(Consumer.counter);
8+
CONNECT(Producer, counter, Consumer, counter);
99
Producer.start();
1010
Consumer.start();
1111
}

Diff for: examples/Threading_Basics/Source_Sink_LED/Source_Sink_LED.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
void setup()
77
{
8-
Source_Thread.led.connectTo(Sink_Thread.led);
8+
CONNECT(Source_Thread, led, Sink_Thread, led);
99
Sink_Thread.start();
1010
Source_Thread.start();
1111
}

Diff for: examples/Threading_Basics/Thermostat/Thermostat.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ void setup()
1717
/* Connect the temperature sensor thread providing temperature readings
1818
* with the various temperature control unit threads.
1919
*/
20-
TemperatureSensor.temperature.connectTo(HeatingControl.temperature);
21-
TemperatureSensor.temperature.connectTo(AirConditionerControl.temperature);
22-
TemperatureSensor.temperature.connectTo(TemperatureSensorReporter.temperature);
20+
CONNECT(TemperatureSensor, temperature, HeatingControl, temperature);
21+
CONNECT(TemperatureSensor, temperature, AirConditionerControl, temperature);
22+
CONNECT(TemperatureSensor, temperature, TemperatureSensorReporter, temperature);
2323

2424
/* Start the individual threads for sensing the temperature
2525
* and controlling heating/air-conditioning based on the sensed

Diff for: src/Arduino_Threads.h

+27-18
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@
4141
**************************************************************************************/
4242

4343
#define SOURCE(name, type) \
44-
public: \
45-
Source<type> name; \
46-
private:
44+
Source<type> name;
4745

4846
/* We need to call the SinkBlocking<T>(size_t const size)
4947
* non-default constructor using size as parameter.
@@ -61,14 +59,10 @@ public: \
6159
*/
6260

6361
#define SINK_2_ARG(name, type) \
64-
public: \
65-
SinkBlocking<type> name{1}; \
66-
private:
62+
SinkBlocking<type> name{1}
6763

6864
#define SINK_3_ARG(name, type, size) \
69-
public: \
70-
SinkBlocking<type> name{size}; \
71-
private:
65+
SinkBlocking<type> name{size}
7266

7367
/* Black C macro magic enabling "macro overloading"
7468
* with same name macro, but multiple arguments.
@@ -78,10 +72,10 @@ public: \
7872
#define SINK(...) GET_SINK_MACRO(__VA_ARGS__, SINK_3_ARG, SINK_2_ARG)(__VA_ARGS__)
7973

8074
#define SINK_NON_BLOCKING(name, type) \
81-
public: \
82-
SinkNonBlocking<type> name{}; \
83-
private:
75+
SinkNonBlocking<type> name{}
8476

77+
#define CONNECT(source_thread, source_name, sink_thread, sink_name) \
78+
source_thread##Private::source_name.connectTo(sink_thread##Private::sink_name)
8579

8680
#define SHARED_2_ARG(name, type) \
8781
Shared<type> name;
@@ -135,13 +129,28 @@ class Arduino_Threads
135129
void threadFunc();
136130
};
137131

138-
#define THD_ENTER(tabname) class ARDUINO_THREADS_CONCAT(tabname, Class) : public Arduino_Threads { \
139-
public: \
140-
ARDUINO_THREADS_CONCAT(tabname, Class)() { _tabname = ARDUINO_THREADS_TO_STRING(tabname); } \
141-
private: \
132+
#define THD_SETUP(ns) ns::setup()
133+
#define THD_LOOP(ns) ns::loop()
134+
135+
#define THD_ENTER(tabname) \
136+
namespace ARDUINO_THREADS_CONCAT(tabname,Private)\
137+
{\
138+
void setup();\
139+
void loop();\
140+
}\
141+
class ARDUINO_THREADS_CONCAT(tabname, Class) : public Arduino_Threads\
142+
{\
143+
public:\
144+
ARDUINO_THREADS_CONCAT(tabname, Class)() { _tabname = ARDUINO_THREADS_TO_STRING(tabname); }\
145+
protected:\
146+
virtual void setup() override { THD_SETUP(ARDUINO_THREADS_CONCAT(tabname,Private)); }\
147+
virtual void loop() override { THD_LOOP(ARDUINO_THREADS_CONCAT(tabname,Private)); }\
148+
};\
149+
namespace ARDUINO_THREADS_CONCAT(tabname,Private)\
150+
{
142151

143-
#define THD_DONE(tabname) \
144-
}; \
152+
#define THD_DONE(tabname)\
153+
};\
145154
ARDUINO_THREADS_CONCAT(tabname,Class) tabname;
146155

147156
#endif /* ARDUINO_THREADS_H_ */

0 commit comments

Comments
 (0)