Skip to content

Commit 3f5abde

Browse files
committed
Thermostat example demonstrate advantages of source/sink vs shared in certain applications.
1 parent 082d78c commit 3f5abde

6 files changed

+114
-0
lines changed

examples/Threading_Basics/Thermostat/SharedVariables.h

Whitespace-only changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Define a data sink named 'temperature' of type 'float'. */
2+
SINK(temperature, float);
3+
4+
int const LIVING_ROOM_HEATING_RELAY_PIN = 3;
5+
6+
void setup()
7+
{
8+
pinMode(LIVING_ROOM_HEATING_RELAY_PIN, OUTPUT);
9+
}
10+
11+
void loop()
12+
{
13+
float current_temperature_deg = temperature.pop();
14+
/* Check if the temperature reported by the thermostat is above
15+
* or below 22.0 °C. If the temperature is below 22.0 °C, turn
16+
* on the heating.
17+
*/
18+
if (current_temperature_deg < 22.0f)
19+
digitalWrite(LIVING_ROOM_HEATING_RELAY_PIN, HIGH);
20+
else
21+
digitalWrite(LIVING_ROOM_HEATING_RELAY_PIN, LOW);
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Define a data sink named 'temperature' of type 'float'. */
2+
SINK(temperature, float);
3+
4+
void setup()
5+
{
6+
Serial.begin(9600);
7+
}
8+
9+
void loop()
10+
{
11+
float current_temperature_deg = temperature.pop();
12+
Serial.print("Temperature = ");
13+
Serial.print(current_temperature_deg);
14+
Serial.println(" °C");
15+
}
Lines changed: 19 additions & 0 deletions
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+
int const TEMPERATURE_SENSOR_ANALOG_IN = 0;
5+
6+
void setup()
7+
{
8+
9+
}
10+
11+
void loop()
12+
{
13+
/* Read temperature and convert to floating point. */
14+
float const temperature_deg = analogRead(TEMPERATURE_SENSOR_ANALOG_IN) / 256.0f;
15+
/* Store in temperature source variable. */
16+
temperature.push(temperature_deg);
17+
/* Do only one temperature sensore measurement per second. */
18+
delay(1000);
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Define a data sink named 'temperature' of type 'float'. */
2+
SINK(temperature, float);
3+
4+
int const SLEEPING_ROOM_AC_RELAY_PIN = 2;
5+
6+
void setup()
7+
{
8+
pinMode(SLEEPING_ROOM_AC_RELAY_PIN, OUTPUT);
9+
}
10+
11+
void loop()
12+
{
13+
float current_temperature_deg = temperature.pop();
14+
/* Check if the temperature reported by the thermostat is above
15+
* or below 20.0 °C. If the temperature is above 20.0 °C, turn
16+
* on the AC.
17+
*/
18+
if (current_temperature_deg > 20.0f)
19+
digitalWrite(SLEEPING_ROOM_AC_RELAY_PIN, HIGH);
20+
else
21+
digitalWrite(SLEEPING_ROOM_AC_RELAY_PIN, LOW);
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 contructed
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 modelled 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+
/* Start the individual threads for sensing the temperature
24+
* and controlling heating/air-conditioning based on the sensed
25+
* temperature on a per-room basis.
26+
*/
27+
TemperatureSensor.start();
28+
TemperatureControl_LivingRoom.start();
29+
TemperatureControl_SleepingRoom.start();
30+
TemperatureSensorReporter.start();
31+
}
32+
33+
void loop()
34+
{
35+
36+
}

0 commit comments

Comments
 (0)