cylon/lib/io/utils.js

38 lines
953 B
JavaScript

/**
* Cylon.js - I/O Utils
* cylonjs.com
*
* Copyright (c) 2013-2015 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
"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 };
}
};