Skip to content

Examples Transformations

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

★ = important

Select ★

#include "ReactiveArduinoLib.h"

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

	Reactive::FromRange(0, 10)
	.Select([](int x) {return x * 2; })
	.ToSerial();
}

void loop()
{
}

Cast ★

#include "ReactiveArduinoLib.h"

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

	Reactive::FromRange(0, 10)
	.Cast<float>()   // also ToInt() and ToFloat()
	.ToSerial();
}

void loop()
{
}

Map ★ (alias for Select)

#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::FromRange(0, 10)
	.Map<double>([](int x) -> double { return 2.0 * x; })
	.ToSerial();
}

void loop()
{
}

Reduce

#include "ReactiveArduinoLib.h"

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

	Reactive::FromRange(0, 10)
	.Reduce([](int acum, int current) { return acum + current; })
	.ToSerial();
}

void loop()
{
}

Limit

#include "ReactiveArduinoLib.h"

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

	Reactive::FromRange(0, 10)
	.Limit(3, 7)
	.ToSerial();
}

void loop()
{
}

Scale

#include "ReactiveArduinoLib.h"

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

	Reactive::FromRange(0, 10)
	.Scale(0, 10, 100, 200)
	.ToSerial();
}

void loop()
{
}
#include "ReactiveArduinoLib.h"

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

	Reactive::FromRange(0, 10)
	.Scale(100)
	.ToSerial();
}

void loop()
{
}

Timestamp (millis or micros)

#include "ReactiveArduinoLib.h"

auto observableManual = Reactive::ManualDefer<int>();

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

	observableManual
	.Millis()  // Millis() or Micros()
	.ToSerial();
}

void loop()
{
	delay(2000);
	observableManual.Next();

Elapsed (millis or micros)

#include "ReactiveArduinoLib.h"

auto observableManual = Reactive::ManualDefer<int>();

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

	observableManual
	.ElapsedMillis()  // ElapsedMillis() or ElapsedMicros()
	.ToSerial();
}

void loop()
{
	delay(2000);
	observableManual.Next();
}

Frequency

#include "ReactiveArduinoLib.h"

auto observableManual = Reactive::ManualDefer<int>();

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

	observableManual
	.Frequency()
	.ToSerial();
}

void loop()
{
	delay(2000);
	observableManual.Next();
}

Toggle

#include "ReactiveArduinoLib.h"

auto observableManual = Reactive::ManualDefer<int>();

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

	observableManual
	.Toggle()
	.ToSerial();
}

void loop()
{
	delay(2000);
	observableManual.Next();
}

Threshold

Single Threshold

#include "ReactiveArduinoLib.h"
// Emits ON when (OFF && x > 3)
// Emits OFF when (ON && x < 3)
// Results:
// Ignore 0, 1, 1, 2, 1
// EMITS SHOW ON at 5
// Ignore 4
// EMITS OFF at 2
// EMITS ON at 6
// EMITS OFF at 2
// EMITS ON at 4

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

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

	Reactive::FromArray(values, valuesLength)
	.Threshold(3)
	.ToSerial();


}

void loop()
{
}

Double Threshold (Histeresis)

#include "ReactiveArduinoLib.h"
// Emits ON when (OFF && x > 4)
// Emits OFF when (ON && x < 3)
// Results:
// Ignore 0, 1, 1, 2, 1
// EMITS SHOW ON at 5
// Ignore 4
// EMITS OFF at 2
// EMITS ON at 6
// EMITS OFF at 2
// Ignore 4

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

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

	Reactive::FromArray(values, valuesLength)
	.Threshold(3, 4) 
	.ToSerial();


}

void loop()
{
}

AdcToVoltaje

#include "ReactiveArduinoLib.h"

auto reactiveInput = Reactive::FromAnalogInput(A0, INPUT_PULLUP);

void setup()
{
	reactiveInput
	.AdcToVoltage()
	.ToSerial();
}

void loop()
{
	reactiveInput.Next();
}