30 lines
822 B
JavaScript
30 lines
822 B
JavaScript
|
Utils = {
|
||
|
// 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(scaledValue, 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 = puhlseWidth.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 };
|
||
|
};
|
||
|
};
|
||
|
|
||
|
module.exports = Utils;
|