2014-12-16 03:15:29 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
module.exports = {
|
2015-06-30 01:21:25 +08:00
|
|
|
/**
|
|
|
|
* Calculates PWM Period and Duty based on provided params.
|
|
|
|
*
|
|
|
|
* @param {Number} scaledDuty the scaled duty value
|
|
|
|
* @param {Number} freq frequency to use
|
|
|
|
* @param {Number} pulseWidth pulse width
|
|
|
|
* @param {String} [polarity=high] polarity value (high or low)
|
|
|
|
* @return {Object} calculated period and duty encapsulated in an object
|
|
|
|
*/
|
2014-06-06 03:47:48 +08:00
|
|
|
periodAndDuty: function(scaledDuty, freq, pulseWidth, polarity) {
|
2014-06-03 04:45:42 +08:00
|
|
|
var period, duty, maxDuty;
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
polarity = polarity || "high";
|
2014-06-03 04:45:42 +08:00
|
|
|
period = Math.round(1.0e9 / freq);
|
|
|
|
|
|
|
|
if (pulseWidth != null) {
|
|
|
|
var pulseWidthMin = pulseWidth.min * 1000,
|
2014-06-10 02:34:49 +08:00
|
|
|
pulseWidthMax = pulseWidth.max * 1000;
|
2014-06-03 04:45:42 +08:00
|
|
|
|
2015-04-15 12:49:12 +08:00
|
|
|
maxDuty = pulseWidthMax - pulseWidthMin;
|
2014-06-03 04:45:42 +08:00
|
|
|
duty = Math.round(pulseWidthMin + (maxDuty * scaledDuty));
|
|
|
|
} else {
|
|
|
|
duty = Math.round(period * scaledDuty);
|
|
|
|
}
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
if (polarity === "low") {
|
2014-06-03 04:45:42 +08:00
|
|
|
duty = period - duty;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { period: period, duty: duty };
|
2014-06-03 07:02:02 +08:00
|
|
|
}
|
2014-06-03 04:45:42 +08:00
|
|
|
};
|