From 293594355cb75f4dc2f02a8f92be9a81ea3443a1 Mon Sep 17 00:00:00 2001 From: Edgar Silva Date: Mon, 2 Jun 2014 15:45:42 -0500 Subject: [PATCH] WIP on sero utils --- lib/cylon.js | 3 ++- lib/io/utils.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 lib/io/utils.js diff --git a/lib/cylon.js b/lib/cylon.js index 9eeae15..98e66a4 100644 --- a/lib/cylon.js +++ b/lib/cylon.js @@ -20,7 +20,8 @@ var Cylon = module.exports = { Adaptor: require('./adaptor'), IO: { - DigitalPin: require('./io/digital-pin') + DigitalPin: require('./io/digital-pin'), + Utils: requre('./io/utils') }, api_instance: null, diff --git a/lib/io/utils.js b/lib/io/utils.js new file mode 100644 index 0000000..eb7d457 --- /dev/null +++ b/lib/io/utils.js @@ -0,0 +1,29 @@ +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;