Skip to content

Thermostat example demonstrate advantages of source/sink vs shared in certain applications. #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions examples/Threading_Basics/Thermostat/AirConditionerControl.inot
Original file line number Diff line number Diff line change
@@ -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();
}
}
53 changes: 53 additions & 0 deletions examples/Threading_Basics/Thermostat/HeatingControl.inot
Original file line number Diff line number Diff line change
@@ -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();
}
}
Empty file.
19 changes: 19 additions & 0 deletions examples/Threading_Basics/Thermostat/TemperatureSensor.inot
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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();
}
37 changes: 37 additions & 0 deletions examples/Threading_Basics/Thermostat/Thermostat.ino
Original file line number Diff line number Diff line change
@@ -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()
{

}