pwm-write

xod/gpio/pwm-write

Outputs hardware-supported PWM signal on a board port. Possible errors: — Invalid port
pwm-write
@/pwm-write
Outputs hardware-supported PWM signal on a board port. Possible errors: — Invalid port
PORTport
Board port to write to. If the port supports hardware PWM it will be used. If not, it will be set high for values greater than 0.5 and set low otherwise.
DUTYnumber
Duty cycle value in range 0.0 … 1.0
UPDpulse
Triggers new write
pwm-write
PORT
DUTY
UPD
DONE
DONEpulse
Fires on writing complete

Previously known as xod/core/pwm-output.

C++ implementation

#pragma XOD evaluate_on_pin disable
#pragma XOD evaluate_on_pin enable input_UPD

#ifdef ESP32
#include <analogWrite.h>
#endif

node {
    #ifdef PWMRANGE
    static constexpr Number pwmRange = PWMRANGE;
    #else
    static constexpr Number pwmRange = 255.0;
    #endif

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

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

        auto duty = getValue<input_DUTY>(ctx);
        duty = duty > 1 ? 1 : (duty < 0 ? 0 : duty);
        int val = (int)(duty * pwmRange);

        pinMode(constant_input_PORT, OUTPUT);
        analogWrite(constant_input_PORT, val);

        emitValue<output_DONE>(ctx, 1);
    }
}