Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8564a6b

Browse files
committedJul 20, 2022
Thermostat example demonstrate advantages of source/sink vs shared in certain applications.
1 parent 082d78c commit 8564a6b

6 files changed

+193
-0
lines changed
 

‎examples/Threading_Basics/Thermostat/SharedVariables.h

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Define a data sink named 'temperature' of type 'float'. */
2+
SINK(temperature, float, 10);
3+
4+
static String living_room_message_prefix(String const & /* msg */)
5+
{
6+
return String("[Living Room] ");
7+
}
8+
9+
void setup()
10+
{
11+
Serial.begin(9600);
12+
Serial.prefix(living_room_message_prefix);
13+
while (!Serial) { }
14+
}
15+
16+
bool is_heating_on = false;
17+
18+
void loop()
19+
{
20+
float current_temperature_deg = temperature.pop();
21+
22+
/* Check if the temperature reported by the thermostat is above
23+
* or below 22.0 °C. If the temperature is below 22.0 °C, turn
24+
* on the heating.
25+
*/
26+
bool turn_heating_on = false,
27+
turn_heating_off = false;
28+
29+
if (current_temperature_deg < 22.0f)
30+
turn_heating_on = true;
31+
else
32+
turn_heating_off = true;
33+
34+
/* Only perform a simulated turning on of
35+
* the heating if the heating is currently
36+
* turned off.
37+
*/
38+
if (is_heating_on && turn_heating_off)
39+
{
40+
is_heating_on = false;
41+
42+
Serial.block();
43+
Serial.print("Heating OFF (");
44+
Serial.print(current_temperature_deg);
45+
Serial.println(" °C)");
46+
Serial.unblock();
47+
}
48+
49+
if (!is_heating_on && turn_heating_on)
50+
{
51+
is_heating_on = true;
52+
53+
Serial.block();
54+
Serial.print("Heating ON (");
55+
Serial.print(current_temperature_deg);
56+
Serial.println(" °C)");
57+
Serial.unblock();
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Define a data sink named 'temperature' of type 'float'. */
2+
SINK(temperature, float, 10);
3+
4+
static String sleeping_room_message_prefix(String const & /* msg */)
5+
{
6+
return String("[Sleeping Room] ");
7+
}
8+
9+
void setup()
10+
{
11+
Serial.begin(9600);
12+
Serial.prefix(sleeping_room_message_prefix);
13+
while (!Serial) { }
14+
}
15+
16+
bool is_ac_on = false;
17+
18+
void loop()
19+
{
20+
float current_temperature_deg = temperature.pop();
21+
22+
/* Check if the temperature reported by the thermostat is above
23+
* or below 20.0 °C. If the temperature is above 20.0 °C, turn
24+
* on the AC.
25+
*/
26+
bool turn_ac_on = false,
27+
turn_ac_off = false;
28+
29+
if (current_temperature_deg > 20.0f)
30+
turn_ac_on = true;
31+
else
32+
turn_ac_off = true;
33+
34+
/* Only perform a simulated turning on of
35+
* the air conditioner if the heating is
36+
* air conditioner is currently turned off.
37+
*/
38+
if (is_ac_on && turn_ac_off)
39+
{
40+
is_ac_on = false;
41+
42+
Serial.block();
43+
Serial.print("AC OFF (");
44+
Serial.print(current_temperature_deg);
45+
Serial.println(" °C)");
46+
Serial.unblock();
47+
}
48+
49+
if (!is_ac_on && turn_ac_on)
50+
{
51+
is_ac_on = true;
52+
53+
Serial.block();
54+
Serial.print("AC ON (");
55+
Serial.print(current_temperature_deg);
56+
Serial.println(" °C)");
57+
Serial.unblock();
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Define a data source named 'temperature' of type 'float'. */
2+
SOURCE(temperature, float);
3+
4+
void setup()
5+
{
6+
7+
}
8+
9+
void loop()
10+
{
11+
/* Read temperature - since this is just a simulation
12+
* so we take a random value between 15 and 25 °C.
13+
*/
14+
float const temperature_deg = (rand() % 16) + 10;
15+
/* Store in temperature source variable. */
16+
temperature.push(temperature_deg);
17+
/* Do only one temperature sensore measurement per second. */
18+
delay(5000);
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Define a data sink named 'temperature' of type 'float'. */
2+
SINK(temperature, float);
3+
4+
void setup()
5+
{
6+
Serial.begin(9600);
7+
while (!Serial) { }
8+
}
9+
10+
void loop()
11+
{
12+
float const current_temperature_deg = temperature.pop();
13+
14+
Serial.block();
15+
Serial.print("Temperature = ");
16+
Serial.print(current_temperature_deg);
17+
Serial.println(" °C");
18+
Serial.unblock();
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* This example emulates a thermostat system which consists of
2+
* a single temperature sensore and multiple heating devices
3+
* or air-conditioners. The temperature sensor provides periodic
4+
* temperature sensor measurements and acts as a temperature source.
5+
* This temperature is consumed by various TemperatureControl_ threads
6+
* which perform the act of actual room temperature control.
7+
*
8+
* Note: While there is only a single temperature "source" there are
9+
* multiple temperature "sinks". The source/sink paradigm is constructed
10+
* in such a way, that each sink is guaranteed to see every value provided
11+
* by a source. This is something that can not be modeled using the shared
12+
* variable abstraction.
13+
*/
14+
15+
void setup()
16+
{
17+
/* Connect the TemperatureSensor thread providing temperature readings
18+
* with the various TemperatureControl_* threads.
19+
*/
20+
TemperatureSensor.temperature.connectTo(TemperatureControl_LivingRoom.temperature);
21+
TemperatureSensor.temperature.connectTo(TemperatureControl_SleepingRoom.temperature);
22+
TemperatureSensor.temperature.connectTo(TemperatureSensorReporter.temperature);
23+
24+
/* Start the individual threads for sensing the temperature
25+
* and controlling heating/air-conditioning based on the sensed
26+
* temperature on a per-room basis.
27+
*/
28+
TemperatureSensor.start();
29+
TemperatureControl_LivingRoom.start();
30+
TemperatureControl_SleepingRoom.start();
31+
TemperatureSensorReporter.start();
32+
}
33+
34+
void loop()
35+
{
36+
37+
}

0 commit comments

Comments
 (0)
Please sign in to comment.