WIP on sero utils
This commit is contained in:
parent
92fea6225b
commit
293594355c
|
@ -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,
|
||||
|
|
|
@ -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;
|
Loading…
Reference in New Issue