Skip to content

Examples Observers

Luis Llamas edited this page Aug 26, 2019 · 11 revisions

★ = important

Do ★

#include "ReactiveArduinoLib.h"

void setup()
{
	Serial.begin(115200);
	while (!Serial) delay(1);

	Reactive::FromRange(1, 10, 2)
	.Do([](int x) { Serial.println(2 * x); });
}

void loop()
{
}

DoAndFinally ★

#include "ReactiveArduinoLib.h"

void setup()
{
	Serial.begin(115200);
	while (!Serial) delay(1);

	Reactive::FromRange(1, 10, 2)
	.DoAndFinally( 	[](int x) { Serial.println(2 * x); },
					[]() { Serial.println("No more items"); });
}

void loop()
{
}

Finally

#include "ReactiveArduinoLib.h"

void setup()
{
	Serial.begin(115200);
	while (!Serial) delay(1);

	Reactive::FromRange(1, 10, 2)
	.Finally([]() { Serial.println("No more items"); });
}

void loop()
{
}

DoNothing ★

#include "ReactiveArduinoLib.h"

int values[] = { 0, 1, 1, 2, 1, 5, 4 };
size_t valuesLength = sizeof(values) / sizeof(values[0]);

void setup()
{
	Serial.begin(115200);
	while (!Serial) delay(1);

	Reactive::FromArray(values, valuesLength)
		.If([](int x) { return x > 2; },
			[](int x) {Serial.println(10 * x); })
		.If([](int x) { return x <= 2; },
			[](int x) {Serial.println(5 * x); })
		.DoNothing();
}

void loop()
{
}

Property

#include "ReactiveArduinoLib.h"

int myProperty;

void setup()
{
	Serial.begin(115200);
	while (!Serial) delay(1);

	Reactive::FromRange(1, 10, 2)
	.ToProperty(myProperty);
}

void loop()
{
}

ToArray

#include "ReactiveArduinoLib.h"

int values[] = { 0, 1, 1, 2, 1, 5, 4 };
const size_t valuesLength = sizeof(values) / sizeof(values[0]);

float finalValues[valuesLength];

void setup()
{
	Serial.begin(115200);
	while (!Serial) delay(1);

	Reactive::FromArray(values, valuesLength)
		.Select<float>([](int x) -> float {return 2.0 * x; })
		.ToArray(finalValues, valuesLength);

	for (size_t i = 0; i < valuesLength; i++)
		Serial.println(finalValues[i]);
}

void loop()
{
}

ToCircularBuffer

#include "ReactiveArduinoLib.h"

int values[] = { 0, 1, 1, 2, 1, 5, 4 };
const size_t valuesLength = sizeof(values) / sizeof(values[0]);

const size_t finalValuesLength = 5;
float finalValues[finalValuesLength];

void setup()
{
	Serial.begin(115200);
	while (!Serial) delay(1);

	Reactive::FromArray(values, valuesLength)
		.Select<float>([](int x) -> float {return 2 * x; })
		.ToCircularBuffer(finalValues, finalValuesLength);

	for (size_t i = 0; i < finalValuesLength; i++)
		Serial.println(finalValues[i]);
}

void loop()
{
}

ToDigitalOutput

#include "ReactiveArduinoLib.h"

auto observableInt = Reactive::Property<int>();

void setup()
{
	Serial.begin(115200);
	while (!Serial) delay(1);

	observableInt
	.Select([](int x) {return x >= 3 ? HIGH : LOW; })
	.ToDigitalOutput(10);
}

void loop()
{
	delay(1000);
	observableInt = 1;
	delay(1000);
	observableInt = 5;
	delay(1000);
	observableInt = 2;
	delay(1000);
	observableInt = 3;
}

ToAnalogOutput

#include "ReactiveArduinoLib.h"

auto observableInt = Reactive::Property<int>();

void setup()
{
	Serial.begin(115200);
	while (!Serial) delay(1);

	observableInt
	.Select([](int x) {return x * 50; })
	.ToAnalogOutput(11);
}

void loop()
{
	delay(1000);
	observableInt = 1;
	delay(1000);
	observableInt = 5;
	delay(1000);
	observableInt = 2;
	delay(1000);
	observableInt = 3;
}

ToSerial

#include "ReactiveArduinoLib.h"

int values[] = { 0, 1, 1, 2, 1, 5, 4 };
const size_t valuesLength = sizeof(values) / sizeof(values[0]);

void setup()
{
	Serial.begin(115200);
	while (!Serial) delay(1);

	Reactive::FromArray(values, valuesLength)
	.ToSerial();
}

void loop()
{
}