-
Notifications
You must be signed in to change notification settings - Fork 8
Examples Transformations
Luis Llamas edited this page Apr 25, 2019
·
10 revisions
★ = important
#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()
{
}
#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;
}
#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()
{
}
#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()
{
}
#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()
{
}
#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()
{
}
#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;
}
#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;
}
#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;
}
#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;
}
#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()
{
}
#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()
{
}
#include "ReactiveArduinoLib.h"
auto reactiveInput = Reactive::FromAnalogInput<int>(A0, INPUT_PULLUP);
void setup()
{
reactiveInput
>> Reactive::AdcToVoltage<float>()
>> Reactive::ToSerial<float>();
}
void loop()
{
reactiveInput.Next();
}