Skip to content

Examples Transformations

Luis Llamas edited this page Apr 25, 2019 · 10 revisions

★ = important

Select ★

#include "ReactiveArduinoLib.h"

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

auto observableArray = Reactive::FromArray<int>(values, valuesLength);

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

	observableArray
	>> Reactive::Select<int>([](int x) {return x * 2; })
	>> Reactive::ToSerial<int>();
}

void loop()
{
}

Cast ★

#include "ReactiveArduinoLib.h"

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

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

	observableInt
	>> Reactive::Cast<int, float>()
	>> Reactive::ToSerial<float>();
}

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

Map ★

#include "ReactiveArduinoLib.h"

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

auto observableArray = Reactive::FromArray<int>(values, valuesLength);

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

	observableArray
	>> Reactive::Map<int, double>([](int x) { return 2.0 * x; })
	>> Reactive::ToSerial<double>();
}

void loop()
{
}

Reduce

#include "ReactiveArduinoLib.h"

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

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

void loop()
{
}

Limit

#include "ReactiveArduinoLib.h"

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

	Reactive::FromRange<int>(0, 10)
	>> Reactive::Limit<int>(2, 5)
	>> Reactive::ToSerial<int>();
}

void loop()
{
}

Scale

#include "ReactiveArduinoLib.h"

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

	Reactive::FromRange<int>(0, 10)
	>> Reactive::Scale<int>(0, 10, 100, 200)
	>> Reactive::ToSerial<int>();
}

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

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

	Reactive::FromRange<int>(0, 10)
	>> Reactive::Scale<int>(100)
	>> Reactive::ToSerial<int>();
}

void loop()
{
}

Timestamp (millis or micros)

#include "ReactiveArduinoLib.h"

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

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

	observableInt
	>> Reactive::Millis<int>()  // Millis() or Micros()
	>> Reactive::ToSerial<unsigned long>();
}

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

Elapsed (millis or micros)

#include "ReactiveArduinoLib.h"

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

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

	observableInt
	>> Reactive::ElapsedMillis<int>()  // ElapsedMillis() or ElapsedMicros()
	>> Reactive::ToSerial<unsigned long>();
}

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

Frequency

#include "ReactiveArduinoLib.h"

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

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

	observableInt
	>> Reactive::Frequency<int>()
	>> Reactive::ToSerial<float>();
}

void loop() 
{
	delay(2000);
	observableInt = 1;
}

Toggle

#include "ReactiveArduinoLib.h"

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

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

	observableInt
	>> Reactive::Toggle<int>()
	>> Reactive::ToSerial<bool>();
}

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

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(9600);
	while (!Serial) delay(1);

	Reactive::FromArray<int>(values, valuesLength)
	>> Reactive::Threshold<int>(3) 
	>> Reactive::ToSerial<bool>();


}

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(9600);
	while (!Serial) delay(1);

	Reactive::FromArray<int>(values, valuesLength)
	>> Reactive::Threshold<int>(3, 4) 
	>> Reactive::ToSerial<bool>();


}

void loop()
{
}

AdcToVoltaje

#include "ReactiveArduinoLib.h"

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

void setup()
{
	reactiveInput
	>> Reactive::AdcToVoltage<float>()
	>> Reactive::ToSerial<float>();
}

void loop()
{
	reactiveInput.Next();
}
Clone this wiki locally