Create a bridge / callback unit to easily transform anything into a Node
sofian opened this issue · comments
Sofian Audry commented
Could look like this:
MyAnalogSensor photo;
MyAnalogActuator motor;
MyDigitalSensor button;
MyDigitalActuator relay;
AnalogCallback photoCallback(readPhoto);
AnalogCallback motorCallback(readMotor, writeMotor);
DigitalCallback buttonCallback(readButton);
DigitalCallback relayCallback(readRelay, writeRelay);
float readPhoto() {
return mapTo01(sensor.read(), 0, 1023);
}
float readMotor() {
return motor.currentValue();
}
float writeMotor(float value) {
motor.writeRaw((int)mapFrom01(value, 0, 255));
return motor.currentValue(); // *** this seems superfluous ... perhaps there is a better way
}
bool readButton() {
return button.isPressed();
}
...
void step() {
if (buttonCallback) {
photoCallback >> motorCallback;
relayCallback.off();
}
else
relayCallback.on();
}