File tree 6 files changed +181
-0
lines changed
examples/Threading_Basics/Thermostat
6 files changed +181
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* Define a data sink named 'temperature' of type 'float'. */
2
+ SINK(temperature, float, 10);
3
+
4
+ void setup()
5
+ {
6
+ Serial.begin(9600);
7
+ while (!Serial) { }
8
+ }
9
+
10
+ bool is_ac_on = false;
11
+
12
+ void loop()
13
+ {
14
+ float current_temperature_deg = temperature.pop();
15
+
16
+ /* Check if the temperature reported by the thermostat is above
17
+ * or below 26.0 °C. If the temperature is above 26.0 °C, turn
18
+ * on the AC.
19
+ */
20
+ bool turn_ac_on = false,
21
+ turn_ac_off = false;
22
+
23
+ if (current_temperature_deg > 26.0f)
24
+ turn_ac_on = true;
25
+ else
26
+ turn_ac_off = true;
27
+
28
+ /* Only perform a simulated turning on of
29
+ * the air conditioner if the heating is
30
+ * air conditioner is currently turned off.
31
+ */
32
+ if (is_ac_on && turn_ac_off)
33
+ {
34
+ is_ac_on = false;
35
+
36
+ Serial.block();
37
+ Serial.print("AC OFF (");
38
+ Serial.print(current_temperature_deg);
39
+ Serial.println(" °C)");
40
+ Serial.unblock();
41
+ }
42
+
43
+ if (!is_ac_on && turn_ac_on)
44
+ {
45
+ is_ac_on = true;
46
+
47
+ Serial.block();
48
+ Serial.print("AC ON (");
49
+ Serial.print(current_temperature_deg);
50
+ Serial.println(" °C)");
51
+ Serial.unblock();
52
+ }
53
+ }
Original file line number Diff line number Diff line change
1
+ /* Define a data sink named 'temperature' of type 'float'. */
2
+ SINK(temperature, float, 10);
3
+
4
+ void setup()
5
+ {
6
+ Serial.begin(9600);
7
+ while (!Serial) { }
8
+ }
9
+
10
+ bool is_heating_on = false;
11
+
12
+ void loop()
13
+ {
14
+ float current_temperature_deg = temperature.pop();
15
+
16
+ /* Check if the temperature reported by the thermostat is above
17
+ * or below 22.0 °C. If the temperature is below 22.0 °C, turn
18
+ * on the heating.
19
+ */
20
+ bool turn_heating_on = false,
21
+ turn_heating_off = false;
22
+
23
+ if (current_temperature_deg < 22.0f)
24
+ turn_heating_on = true;
25
+ else
26
+ turn_heating_off = true;
27
+
28
+ /* Only perform a simulated turning on of
29
+ * the heating if the heating is currently
30
+ * turned off.
31
+ */
32
+ if (is_heating_on && turn_heating_off)
33
+ {
34
+ is_heating_on = false;
35
+
36
+ Serial.block();
37
+ Serial.print("Heating OFF (");
38
+ Serial.print(current_temperature_deg);
39
+ Serial.println(" °C)");
40
+ Serial.unblock();
41
+ }
42
+
43
+ if (!is_heating_on && turn_heating_on)
44
+ {
45
+ is_heating_on = true;
46
+
47
+ Serial.block();
48
+ Serial.print("Heating ON (");
49
+ Serial.print(current_temperature_deg);
50
+ Serial.println(" °C)");
51
+ Serial.unblock();
52
+ }
53
+ }
Original file line number Diff line number Diff line change
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 30 °C.
13
+ */
14
+ float const temperature_deg = (rand() % 16) + 15;
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 temperature sensor thread providing temperature readings
18
+ * with the various temperature control unit threads.
19
+ */
20
+ TemperatureSensor.temperature .connectTo (HeatingControl.temperature );
21
+ TemperatureSensor.temperature .connectTo (AirConditionerControl.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
+ HeatingControl.start ();
30
+ AirConditionerControl.start ();
31
+ TemperatureSensorReporter.start ();
32
+ }
33
+
34
+ void loop ()
35
+ {
36
+
37
+ }
You can’t perform that action at this time.
0 commit comments