30 lines
818 B
JavaScript
30 lines
818 B
JavaScript
"use strict";
|
|
|
|
module.exports = {
|
|
// Returns { period: int, duty: int }
|
|
// Calculated based on params value, freq, pulseWidth = { min: int, max: int }
|
|
// pulseWidth min and max need to be specified in microseconds
|
|
periodAndDuty: function(scaledDuty, freq, pulseWidth, polarity) {
|
|
var period, duty, maxDuty;
|
|
|
|
polarity = polarity || "high";
|
|
period = Math.round(1.0e9 / freq);
|
|
|
|
if (pulseWidth != null) {
|
|
var pulseWidthMin = pulseWidth.min * 1000,
|
|
pulseWidthMax = pulseWidth.max * 1000;
|
|
|
|
maxDuty = pulseWidthMax - pulseWidthMin;
|
|
duty = Math.round(pulseWidthMin + (maxDuty * scaledDuty));
|
|
} else {
|
|
duty = Math.round(period * scaledDuty);
|
|
}
|
|
|
|
if (polarity === "low") {
|
|
duty = period - duty;
|
|
}
|
|
|
|
return { period: period, duty: duty };
|
|
}
|
|
};
|