servo

xod/common-hardware/servo

Drives a standard hobby servo. Possible errors: — Invalid port
servo
@/servo
Deprecated: Use `xod-dev/servo/servo` instead
Drives a standard hobby servo. Possible errors: — Invalid port
PORTport
Board port number the servo is connected to.
VALnumber
Desired servo angle or value in unit range [0.0, 1.0]. For standard servo 0.0 would be mapped to 0° and 1.0 would be 180°.
UPDpulse
Triggers new write
servo
DONE
PORT
VAL
UPD
DONEpulse
Fires on writing complete

C++ implementation

#pragma XOD evaluate_on_pin disable
#pragma XOD evaluate_on_pin enable input_UPD

{{#global}}
#include <Servo.h>
{{/global}}

struct State {
    Servo servo;
    int configuredPort = -1;
};

{{ GENERATED_CODE }}

void evaluate(Context ctx) {
    static_assert(isValidDigitalPort(constant_input_PORT), "must be a valid digital port");

    if (!isInputDirty<input_UPD>(ctx))
        return;

    State* state = getState(ctx);
    // TODO
    auto port = (int)getValue<input_PORT>(ctx);

    if (port != state->configuredPort) {
        state->servo.attach(port);
        state->configuredPort = port;
    }

    state->servo.write(getValue<input_VAL>(ctx) * 180);
    emitValue<output_DONE>(ctx, 1);
}