diff --git a/examples/Threading_Basics/Thermostat/AirConditionerControl.inot b/examples/Threading_Basics/Thermostat/AirConditionerControl.inot new file mode 100644 index 0000000..59dc16e --- /dev/null +++ b/examples/Threading_Basics/Thermostat/AirConditionerControl.inot @@ -0,0 +1,53 @@ +/* Define a data sink named 'temperature' of type 'float'. */ +SINK(temperature, float, 10); + +void setup() +{ + Serial.begin(9600); + while (!Serial) { } +} + +bool is_ac_on = false; + +void loop() +{ + float current_temperature_deg = temperature.pop(); + + /* Check if the temperature reported by the thermostat is above + * or below 26.0 °C. If the temperature is above 26.0 °C, turn + * on the AC. + */ + bool turn_ac_on = false, + turn_ac_off = false; + + if (current_temperature_deg > 26.0f) + turn_ac_on = true; + else + turn_ac_off = true; + + /* Only perform a simulated turning on of + * the air conditioner if the heating is + * air conditioner is currently turned off. + */ + if (is_ac_on && turn_ac_off) + { + is_ac_on = false; + + Serial.block(); + Serial.print("AC OFF ("); + Serial.print(current_temperature_deg); + Serial.println(" °C)"); + Serial.unblock(); + } + + if (!is_ac_on && turn_ac_on) + { + is_ac_on = true; + + Serial.block(); + Serial.print("AC ON ("); + Serial.print(current_temperature_deg); + Serial.println(" °C)"); + Serial.unblock(); + } +} diff --git a/examples/Threading_Basics/Thermostat/HeatingControl.inot b/examples/Threading_Basics/Thermostat/HeatingControl.inot new file mode 100644 index 0000000..5fcaf82 --- /dev/null +++ b/examples/Threading_Basics/Thermostat/HeatingControl.inot @@ -0,0 +1,53 @@ +/* Define a data sink named 'temperature' of type 'float'. */ +SINK(temperature, float, 10); + +void setup() +{ + Serial.begin(9600); + while (!Serial) { } +} + +bool is_heating_on = false; + +void loop() +{ + float current_temperature_deg = temperature.pop(); + + /* Check if the temperature reported by the thermostat is above + * or below 22.0 °C. If the temperature is below 22.0 °C, turn + * on the heating. + */ + bool turn_heating_on = false, + turn_heating_off = false; + + if (current_temperature_deg < 22.0f) + turn_heating_on = true; + else + turn_heating_off = true; + + /* Only perform a simulated turning on of + * the heating if the heating is currently + * turned off. + */ + if (is_heating_on && turn_heating_off) + { + is_heating_on = false; + + Serial.block(); + Serial.print("Heating OFF ("); + Serial.print(current_temperature_deg); + Serial.println(" °C)"); + Serial.unblock(); + } + + if (!is_heating_on && turn_heating_on) + { + is_heating_on = true; + + Serial.block(); + Serial.print("Heating ON ("); + Serial.print(current_temperature_deg); + Serial.println(" °C)"); + Serial.unblock(); + } +} diff --git a/examples/Threading_Basics/Thermostat/SharedVariables.h b/examples/Threading_Basics/Thermostat/SharedVariables.h new file mode 100644 index 0000000..e69de29 diff --git a/examples/Threading_Basics/Thermostat/TemperatureSensor.inot b/examples/Threading_Basics/Thermostat/TemperatureSensor.inot new file mode 100644 index 0000000..d763ca4 --- /dev/null +++ b/examples/Threading_Basics/Thermostat/TemperatureSensor.inot @@ -0,0 +1,19 @@ +/* Define a data source named 'temperature' of type 'float'. */ +SOURCE(temperature, float); + +void setup() +{ + +} + +void loop() +{ + /* Read temperature - since this is just a simulation + * so we take a random value between 15 and 30 °C. + */ + float const temperature_deg = (rand() % 16) + 15; + /* Store in temperature source variable. */ + temperature.push(temperature_deg); + /* Do only one temperature sensore measurement per second. */ + delay(5000); +} diff --git a/examples/Threading_Basics/Thermostat/TemperatureSensorReporter.inot b/examples/Threading_Basics/Thermostat/TemperatureSensorReporter.inot new file mode 100644 index 0000000..c6ceb10 --- /dev/null +++ b/examples/Threading_Basics/Thermostat/TemperatureSensorReporter.inot @@ -0,0 +1,19 @@ +/* Define a data sink named 'temperature' of type 'float'. */ +SINK(temperature, float); + +void setup() +{ + Serial.begin(9600); + while (!Serial) { } +} + +void loop() +{ + float const current_temperature_deg = temperature.pop(); + + Serial.block(); + Serial.print("Temperature = "); + Serial.print(current_temperature_deg); + Serial.println(" °C"); + Serial.unblock(); +} diff --git a/examples/Threading_Basics/Thermostat/Thermostat.ino b/examples/Threading_Basics/Thermostat/Thermostat.ino new file mode 100644 index 0000000..7fdfd17 --- /dev/null +++ b/examples/Threading_Basics/Thermostat/Thermostat.ino @@ -0,0 +1,37 @@ +/* This example emulates a thermostat system which consists of + * a single temperature sensore and multiple heating devices + * or air-conditioners. The temperature sensor provides periodic + * temperature sensor measurements and acts as a temperature source. + * This temperature is consumed by various TemperatureControl_ threads + * which perform the act of actual room temperature control. + * + * Note: While there is only a single temperature "source" there are + * multiple temperature "sinks". The source/sink paradigm is constructed + * in such a way, that each sink is guaranteed to see every value provided + * by a source. This is something that can not be modeled using the shared + * variable abstraction. + */ + +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); + + /* Start the individual threads for sensing the temperature + * and controlling heating/air-conditioning based on the sensed + * temperature on a per-room basis. + */ + TemperatureSensor.start(); + HeatingControl.start(); + AirConditionerControl.start(); + TemperatureSensorReporter.start(); +} + +void loop() +{ + +}