Skip to content

Examples Observables

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

★ = important

FromProperty ★

#include "ReactiveArduinoLib.h"

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

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

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

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

Range

#include "ReactiveArduinoLib.h"

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

	Reactive::FromRange<int>(1, 10, 2)
		>> Reactive::ToSerial<int>();
}

void loop()
{
}

RangeDefer

#include "ReactiveArduinoLib.h"

auto observableRange = Reactive::FromRangeDefer<int>(1, 5, 1);

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

	observableRange
		>> Reactive::ToSerial<int>();
}

void loop()
{
	observableRange.Next();
	delay(1000);
}

Array

#include "ReactiveArduinoLib.h"

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

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

	Reactive::FromArray(values, valuesLength)
		>> Reactive::ToSerial<int>();
}

void loop()
{
}

ArrayDefer

#include "ReactiveArduinoLib.h"

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

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

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

	observableArray
		>> Reactive::ToSerial<int>();
}

void loop()
{
	observableArray.Next();
	delay(1000);
}

Interval (millis or micros)

#include "ReactiveArduinoLib.h"

auto interval= Reactive::IntervalMillis<unsigned long>(1000);

void setup()
{
	interval
		>> Reactive::ToSerial<unsigned long>();
}

void loop()
{
	interval.Update();
}

Timer (millis or micros)

#include "ReactiveArduinoLib.h"

auto timer = Reactive::TimerMillis<unsigned long>(1000);

void setup()
{
	timer
		>> Reactive::ToSerial<unsigned long>();
}

void loop()
{
	timer.Update();
}

Digital Input

#include "ReactiveArduinoLib.h"

auto reactiveInput = Reactive::FromDigitalInput<int>(10, INPUT_PULLUP);

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

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

Analog Input

#include "ReactiveArduinoLib.h"

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

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

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