WIP on sero utils

This commit is contained in:
Edgar Silva 2014-06-02 15:45:42 -05:00
parent 92fea6225b
commit 293594355c
2 changed files with 31 additions and 1 deletions

View File

@ -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,

29
lib/io/utils.js Normal file
View File

@ -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;