yapcduino
Version:
Yet another nodejs library for pcduino (extends jheising's node.pcduino but provide simple API and some extra function).
108 lines (94 loc) • 2.83 kB
JavaScript
var Native = require('../build/Release/yapcduino');
var C = require('./constant');
function SoftPWM(pin) {
this.pin = pin;
Native.pinMode(pin, C.OUTPUT);
}
/**
* Attach the SoftPWM instance to a new pin
*
*/
SoftPWM.prototype.attach = function(pin) {
if (this.pin != null) {
this.detach();
}
this.pin = pin;
return this.dutyCycle;
};
/**
* Detach the SoftPWM instance from original pin (will unset output of original pin and set this.pin to null)
*
*/
SoftPWM.prototype.detach = function() {
if (this.pin != null) {
Native.unsetSoftPWM(this.pin);
}
this.pin = null;
};
/**
* Get count of loops of the pin since last write
*
*/
SoftPWM.prototype.getLoopCount = function() {
var originalLoopsToLive = typeof this.originalLoopsToLive == "undefined" ? -1 : this.originalLoopsToLive;
return Native.getSoftPWMLoopCount(this.pin, originalLoopsToLive);
};
/**
* Sync wait until this soft pwm thread finish all its work
*
*/
SoftPWM.prototype.sync = function() {
Native.syncWaitPin(this.pin);
};
/**
* Writes a PWM wave to a p in (using digitalWrite).
* To stop, simply call pwm.write(0).
*
* @see http://www.arduino.cc/en/Tutorial/PWM
* @see http://www.arduino.cc/en/Reference/analogWrite
* @param {Float} dutyCycle - Duty Cycle [0, 1]
* @param {Object} options - Options for PWM
* @param {Int} options.period - Period (in us), defaults to 20 * 1000
* @param {Int} options.frequency - Frequency, period will be ignored if frequency set
* @param {Number} options.loops - (loops to live) How many loops should it run, defaults to Infinity (actually is 2147483647), note that -1 will be converted to 2147483647
* @param {Bool} options.sync - Whether to run it in sync mode, defaults to false
*/
SoftPWM.prototype.write = function(dutyCycle, options) {
var pin = this.pin;
options = options || {};
this.originalLoopsToLive = options.loops;
this.dutyCycle = dutyCycle;
// process period
var period = options.period;
if (options.frequency) {
period = 1000 * 1000 / options.frequency;
}
if (typeof period === "undefined") {
period = 20 * 1000;
}
if (dutyCycle === 0) {
Native.unsetSoftPWM(pin);
return;
}
// process duty cycle
var highus = parseInt(period * dutyCycle);
var lowus = period - highus;
// process loops to live
var loops = options.loops || Infinity;
if (loops === Infinity) {
loops = -1;
}
if (options.sync) {
Native.setSoftPWMSync(pin, highus, lowus, loops);
} else {
Native.setSoftPWM(pin, highus, lowus, loops);
}
};
/**
* Read last set dutyCycle (will return undefined if not set)
*
*/
SoftPWM.prototype.read = function() {
return this.dutyCycle;
};
module.exports = SoftPWM;