cylon/lib/io/utils.js

30 lines
818 B
JavaScript
Raw Normal View History

2014-12-16 03:15:29 +08:00
"use strict";
module.exports = {
2014-06-03 04:45:42 +08:00
// 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
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
maxDuty = pulseWidthMax - pulseWidthMin;
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
};