ShadowNode

Use Node.js in your end devices(QQ: 796448809)

View the Project on GitHub yodaos-project/ShadowNode

Platform Support

The following shows PWM module APIs available for each platform.

  Linux
(Ubuntu)
Raspbian
(Raspberry Pi)
NuttX
(STM32F4-Discovery)
TizenRT
(Artik053)
pwm.open O O O O
pwmpin.setPeriod O O O O
pwmpin.setPeriodSync O O O O
pwmpin.setFrequency O O O O
pwmpin.setFrequencySync O O O O
pwmpin.setDutyCycle O O O O
pwmpin.setDutyCycleSync O O O O
pwmpin.setEnable O O O O
pwmpin.setEnableSync O O O O
pwmpin.close O O O O
pwmpin.closeSync O O O O

Class: PWM

new PWM()

Returns a new PWM object which can open a PWM pin.

This object allows the developer to specify a pin and generate a pulse-width modulatated (PWM) signal through that.

pwm.open(configuration[, callback])

Opens PWM pin with the specified configuration.

To correctly open a PWM pin one must know the correct pin number:

Example

var Pwm = require('pwm');
var pwm = new Pwm();
var config = {
  pin: 0,
  period: 0.1,
  dutyCycle: 0.5
}
var pwm0 = pwm.open(config, function(err) {
  if (err) {
    throw err;
  }
});

Class: PWMPin

pwmpin.setPeriod(period[, callback])

The setPeriod method allows the configuration of the period of the PWM signal in seconds. The period argument must specified and it should be a positive number.

Configuration is done asynchronously and the callback method is invoked after the period is configured to the new value or if an error occured.

Example

pwm0.setPeriod(1, function(err) {
  if (err) {
    throw err;
  }
  console.log('done');
});
// prints: do
console.log('do');
// prints: done

pwmpin.setPeriodSync(period)

The setPeriodSync method allows the configuration of the period of the PWM signal in seconds. The period argument must specified and it should be a positive number.

Configuration is done synchronously and will block till the period is configured.

Example

pwm0.setPeriodSync(1);
// prints: done
console.log('done');

pwmpin.setFrequency(frequency[, callback])

The setFequency method congifures the frequency of the PWM signal. frequency is the measurement of how often the signal is repeated in a single period.

Configuration is done asynchronously and the callback method is invoked after the frequency is configured to the new value or if an error occured.

Example

pwm0.setFrequency(1, function(err) {
  if (err) {
    throw err;
  }
  console.log('done');
});
// prints: do
console.log('do');
// prints: done

pwmpin.setFrequencySync(frequency)

The setFequencySync method congifures the frequency of the PWM signal. frequency is the measurement of how often the signal is repeated in a single period.

Configuration is done synchronously and will block till the frequency is configured.

Example

pwm0.setFrequencySync(1);
// prints: done
console.log('done');

pwmpin.setDutyCycle(dutyCycle[, callback])

The setDutyCycle method allows the configuration of the duty cycle of the PWM signal. The duty cycle is the ratio of the pulse width in one period.

Configuration is done asynchronously and the callback method is invoked after the duty cycle is configured to the new value or if an error occured.

Example

pwm0.setDutyCycle(1, function(err) {
  if (err) {
    throw err;
  }
  console.log('done');
});
// prints: do
console.log('do');
// prints: done

pwmpin.setDutyCycleSync(dutyCycle)

The setDutyCycleSync method allows the configuration of the duty cycle of the PWM signal. The duty cycle is the ratio of the pulse width in one period.

Configuration is done synchronously and will block till the duty cycle is configured.

Example

pwm0.setDutyCycleSync(1);
// prints: done
console.log('done');

pwmpin.setEnable(enable[, callback])

The setEnable method can turn on/off the generation of the PWM signal. If the enable argument is true then the PWN signal generation is turned on. Otherwise the signal generation is turned off.

After enabling/disabling the signal generation the callback is invoked.

Example

pwm0.setEnable(true, function(err) {
  if (err) {
    throw err;
  }
  console.log('done');
});
// prints: do
console.log('do');
// prints: done

pwmpin.setEnableSync(enable)

The setEnableSync method can turn on/off the generation of the PWM signal. If the enable argument is true then the PWN signal generation is turned on. Otherwise the signal generation is turned off.

Example

pwm0.setEnableSync(false);
// prints: done
console.log('done');

pwmpin.close([callback])

The close method closes the PWM pin asynchronously.

The callback method will be invoked after the PWM device is released.

Example

pwm0.close(function(err) {
  if (err) {
    throw err;
  }
  console.log('done');
});
// prints: do
console.log('do');
// prints: done

pwmpin.closeSync()

The closeSync method closes the PWM pin synchronously.

Example

pwm0.closeSync();
// prints: done
console.log('done');