Ruff 0.9.0 Documentation


PWM#

The pwm module allows you to create your own device driver with PWM output.Following line of code includes the PWM module:You can include this module with required('i2c');.

PWM driver developers may extend this module by adding more functions.

Create PWM driver#

You may easily build your PWM driver with Ruff framework, e.g.

var pwm = require('pwm');

PwmDriverClass = pwm.driver({
    attach: function(options) {
        ...
    },

    exports: {
        setFrequency: function(frequency) {
            // set frequency value
            ...
        },

        setDuty: function(duty) {
            // set duty value
            ...
        },
    }
});

As you can see here, setFrequency and setDuty are the only two functions you have to implement. The other functions are either built-in functions or will be provided by PWM driver developers.

Use PWM interface#

Driver developers who use PWM need to first declare the device as PWM type in driver.json.

{
    "inputs": {
        "pwm": {
            "type": "pwm"
        }
   }

(driver.json)

You may obtain an PWM instance in your driver code:

var driver = require('ruff-driver');

module.exports = driver({
    attach: function(options) {
        pwm = options.getRequired('pwm');
    }

    ...
});

API#

pwm.setFrequency(frequency)#

  • set frequency value.
  • unit: Hz

Example:

try {
    channel.setFrequency(500);
} catch(error) {
    console.log(error);
}

pwm.setDuty(duty)#

  • set duty value.
  • range: [0,1]