From 268f0cec51cd4101547df1d9e16d19975049ac16 Mon Sep 17 00:00:00 2001 From: PSVM-J <39494832+PSVM-J@users.noreply.github.com> Date: Mon, 25 Jun 2018 12:51:32 +0300 Subject: [PATCH 1/4] New module for N-fet and P-fet --- modules/@amperka/X-fet.js | 183 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 modules/@amperka/X-fet.js diff --git a/modules/@amperka/X-fet.js b/modules/@amperka/X-fet.js new file mode 100644 index 0000000..d2e1ac8 --- /dev/null +++ b/modules/@amperka/X-fet.js @@ -0,0 +1,183 @@ +var X_fet = function(opts) { + if (typeof opts === 'number') { + SPI2.setup(); + this._spi = SPI2; + this._qtyMod = 1; + this._cs = opts; + } else { + opts = opts || {}; + this._spi = opts.spi; + this._cs = opts.cs; + this._qtyMod = opts.qtyMod || 1; + } + this._valuePins = []; + this._blinkOnTime = []; + this._blinkOffTime = []; + this._blinkTimeoutID = []; + for (var i = 0; i < this._qtyMod; i++) { + this._valuePins[i] = 0; + this._blinkOnTime[i] = [0, 0, 0, 0, 0, 0, 0, 0]; + this._blinkOffTime[i] = [0, 0, 0, 0, 0, 0, 0, 0]; + this._blinkTimeoutID[i] = [0, 0, 0, 0, 0, 0, 0, 0]; + } + this._spi.send(this._valuePins, this._cs); +}; + +X_fet.prototype.turnOn = function(pin, numMod) { + this._numMod = numMod || 0; + this._mask = this._getMask(pin); + this._clearBlink(pin, numMod); + this._valuePins[this._valuePins.length - this._numMod - 1] |= this._mask; + this._spi.send(this._valuePins, this._cs); +}; + +X_fet.prototype.turnOff = function(pin, numMod) { + this._numMod = numMod || 0; + this._mask = this._getMask(pin); + this._clearBlink(pin, numMod); + this._valuePins[this._valuePins.length - this._numMod - 1] &= ~this._mask; + this._spi.send(this._valuePins, this._cs); +}; + +X_fet.prototype.toggle = function(pin, numMod) { + t0his._numMod = numMod || 0; + this._mask = this._getMask(pin); + this._clearBlink(pin, numMod); + this._valuePins[this._valuePins.length - this._numMod - 1] ^= this._mask; + this._spi.send(this._valuePins, this._cs); +}; + +X_fet.prototype.turnAllOff = function(numMod) { + this._setAllpins(0, numMod); +}; + +X_fet.prototype.turnAllOn = function(numMod) { + this._setAllpins(255, numMod); +}; + +X_fet.prototype._setAllpins = function(value, numMod) { + + if (Array.isArray(numMod)) { + for (var i = 0; i < numMod.length; i++) { + this._clearAllBlink(i); + this._valuePins[this._valuePins.length - this._numMod[i] - 1] = value; + } + } else if (typeof numMod === 'number') { + this._numMod = numMod || 0; + this._clearAllBlink(numMod); + this._valuePins[this._valuePins.length - this._numMod - 1] = value; + } else { + for (var i = 0; i < this._qtyMod; i++) { + this._clearAllBlink(i); + this._valuePins[this._valuePins.length - i - 1] = value; + } + } + this._spi.send(this._valuePins, this._cs); +}; + +X_fet.prototype._getMask = function(pin) { + if (Array.isArray(pin)) { + var mask = 0; + for (var i = 0; i < pin.length; i++) { + mask |= 1 << pin[i]; + } + } else { + mask = 1 << pin; + } + return mask; +}; + + + +X_fet.prototype.blink = function(pin, numMod, onTime, offTime) { + if (Array.isArray(pin)) { + for (var i = 0; i < pin.length; i++) { + this._blinkPin(pin[i], numMod, onTime, offTime); + } + } else { + this._blinkPin(pin, numMod, onTime, offTime); + }; +}; + +X_fet.prototype._blinkPin = function(pin, numMod, onTime, offTime) { + if (this._blinkOnTime[numMod][pin] === onTime && + this._blinkOffTime[numMod][pin] && + this._blinkOffTime[numMod][pin] === offTime) { + return; + } + this._clearBlinkPin(pin, numMod); + this._blinkOnTime[numMod][pin] = onTime; + this._blinkOffTime[numMod][pin] = offTime; + + if (this._isOn(pin, numMod)) { + this._blinkOff(pin, numMod); + } else { + this._blinkOn(pin, numMod); + } +}; + +X_fet.prototype._isOn = function(pin, numMod) { + var mask = 1 << pin; + this._numMod = numMod || 0; + if ((this._valuePins[this._valuePins.length - this._numMod - 1] & mask) > 0) { + return true; + } else { + return false; + } +}; + +X_fet.prototype._clearAllBlink = function(numMod) { + for (var i = 0; i < 8; i++) { + this._clearBlinkPin(i, numMod); + } +}; + +X_fet.prototype._clearBlink = function(pin, numMod) { + if (Array.isArray(pin)) { + + for (var i = 0; i < pin.length; i++) { + this._clearBlinkPin(pin[i], numMod); + } + } else { + this._clearBlinkPin(pin, numMod); + } +}; + +X_fet.prototype._clearBlinkPin = function(pin, numMod) { + if (this._blinkTimeoutID[numMod][pin]) { + clearTimeout(this._blinkTimeoutID[numMod][pin]); + this._blinkTimeoutID[numMod][pin] = null; + this._blinkOnTime[numMod][pin] = 0; + this._blinkOffTime[numMod][pin] = 0; + } +}; + +X_fet.prototype._blinkOn = function(pin, numMod) { + this._update(pin, numMod); + this._blinkTimeoutID[numMod][pin] = setTimeout( + this._blinkOff.bind(this, pin, numMod), + this._blinkOnTime[numMod][pin] * 1000); +}; + +X_fet.prototype._blinkOff = function(pin, numMod) { + this._update(pin, numMod); + + if (this._blinkOffTime[numMod][pin]) { + this._blinkTimeoutID[numMod][pin] = setTimeout( + this._blinkOn.bind(this, pin, numMod), + this._blinkOffTime[numMod][pin] * 1000); + } else { + this._blinkTimeoutID[numMod][pin] = null; + } +}; + +X_fet.prototype._update = function(pin, numMod) { + this._numMod = numMod || 0; + this._mask = this._getMask(pin); + this._valuePins[this._valuePins.length - this._numMod - 1] ^= this._mask; + this._spi.send(this._valuePins, this._cs); +}; + +exports.connect = function(opts) { + return new X_fet(opts); +}; From 5218d37325841001390229df0a9b65397cd7a75a Mon Sep 17 00:00:00 2001 From: PSVM-J <39494832+PSVM-J@users.noreply.github.com> Date: Mon, 25 Jun 2018 18:35:04 +0300 Subject: [PATCH 2/4] fixed numMod in turnOn --- modules/@amperka/X-fet.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/modules/@amperka/X-fet.js b/modules/@amperka/X-fet.js index d2e1ac8..04b2b2f 100644 --- a/modules/@amperka/X-fet.js +++ b/modules/@amperka/X-fet.js @@ -24,26 +24,26 @@ var X_fet = function(opts) { }; X_fet.prototype.turnOn = function(pin, numMod) { - this._numMod = numMod || 0; + numMod |= 0; this._mask = this._getMask(pin); this._clearBlink(pin, numMod); - this._valuePins[this._valuePins.length - this._numMod - 1] |= this._mask; + this._valuePins[this._valuePins.length - numMod - 1] |= this._mask; this._spi.send(this._valuePins, this._cs); }; X_fet.prototype.turnOff = function(pin, numMod) { - this._numMod = numMod || 0; + numMod |= 0; this._mask = this._getMask(pin); - this._clearBlink(pin, numMod); - this._valuePins[this._valuePins.length - this._numMod - 1] &= ~this._mask; + this._clearBlink(pin, this._numMod); + this._valuePins[this._valuePins.length - numMod - 1] &= ~this._mask; this._spi.send(this._valuePins, this._cs); }; X_fet.prototype.toggle = function(pin, numMod) { - t0his._numMod = numMod || 0; + numMod |= 0; this._mask = this._getMask(pin); this._clearBlink(pin, numMod); - this._valuePins[this._valuePins.length - this._numMod - 1] ^= this._mask; + this._valuePins[this._valuePins.length - numMod - 1] ^= this._mask; this._spi.send(this._valuePins, this._cs); }; @@ -60,12 +60,12 @@ X_fet.prototype._setAllpins = function(value, numMod) { if (Array.isArray(numMod)) { for (var i = 0; i < numMod.length; i++) { this._clearAllBlink(i); - this._valuePins[this._valuePins.length - this._numMod[i] - 1] = value; + this._valuePins[this._valuePins.length - numMod[i] - 1] = value; } } else if (typeof numMod === 'number') { - this._numMod = numMod || 0; + numMod |= 0; this._clearAllBlink(numMod); - this._valuePins[this._valuePins.length - this._numMod - 1] = value; + this._valuePins[this._valuePins.length - numMod - 1] = value; } else { for (var i = 0; i < this._qtyMod; i++) { this._clearAllBlink(i); @@ -118,8 +118,8 @@ X_fet.prototype._blinkPin = function(pin, numMod, onTime, offTime) { X_fet.prototype._isOn = function(pin, numMod) { var mask = 1 << pin; - this._numMod = numMod || 0; - if ((this._valuePins[this._valuePins.length - this._numMod - 1] & mask) > 0) { + numMod |= 0; + if ((this._valuePins[this._valuePins.length - numMod - 1] & mask) > 0) { return true; } else { return false; @@ -172,9 +172,9 @@ X_fet.prototype._blinkOff = function(pin, numMod) { }; X_fet.prototype._update = function(pin, numMod) { - this._numMod = numMod || 0; + numMod |= 0; this._mask = this._getMask(pin); - this._valuePins[this._valuePins.length - this._numMod - 1] ^= this._mask; + this._valuePins[this._valuePins.length - numMod - 1] ^= this._mask; this._spi.send(this._valuePins, this._cs); }; From 4fb8d36cb001688a5dbf976d9f34a90f6eeaf3e0 Mon Sep 17 00:00:00 2001 From: PSVM-J <39494832+PSVM-J@users.noreply.github.com> Date: Wed, 27 Jun 2018 13:48:02 +0300 Subject: [PATCH 3/4] deleted X-fet --- modules/@amperka/X-fet.js | 183 -------------------------------------- 1 file changed, 183 deletions(-) delete mode 100644 modules/@amperka/X-fet.js diff --git a/modules/@amperka/X-fet.js b/modules/@amperka/X-fet.js deleted file mode 100644 index 04b2b2f..0000000 --- a/modules/@amperka/X-fet.js +++ /dev/null @@ -1,183 +0,0 @@ -var X_fet = function(opts) { - if (typeof opts === 'number') { - SPI2.setup(); - this._spi = SPI2; - this._qtyMod = 1; - this._cs = opts; - } else { - opts = opts || {}; - this._spi = opts.spi; - this._cs = opts.cs; - this._qtyMod = opts.qtyMod || 1; - } - this._valuePins = []; - this._blinkOnTime = []; - this._blinkOffTime = []; - this._blinkTimeoutID = []; - for (var i = 0; i < this._qtyMod; i++) { - this._valuePins[i] = 0; - this._blinkOnTime[i] = [0, 0, 0, 0, 0, 0, 0, 0]; - this._blinkOffTime[i] = [0, 0, 0, 0, 0, 0, 0, 0]; - this._blinkTimeoutID[i] = [0, 0, 0, 0, 0, 0, 0, 0]; - } - this._spi.send(this._valuePins, this._cs); -}; - -X_fet.prototype.turnOn = function(pin, numMod) { - numMod |= 0; - this._mask = this._getMask(pin); - this._clearBlink(pin, numMod); - this._valuePins[this._valuePins.length - numMod - 1] |= this._mask; - this._spi.send(this._valuePins, this._cs); -}; - -X_fet.prototype.turnOff = function(pin, numMod) { - numMod |= 0; - this._mask = this._getMask(pin); - this._clearBlink(pin, this._numMod); - this._valuePins[this._valuePins.length - numMod - 1] &= ~this._mask; - this._spi.send(this._valuePins, this._cs); -}; - -X_fet.prototype.toggle = function(pin, numMod) { - numMod |= 0; - this._mask = this._getMask(pin); - this._clearBlink(pin, numMod); - this._valuePins[this._valuePins.length - numMod - 1] ^= this._mask; - this._spi.send(this._valuePins, this._cs); -}; - -X_fet.prototype.turnAllOff = function(numMod) { - this._setAllpins(0, numMod); -}; - -X_fet.prototype.turnAllOn = function(numMod) { - this._setAllpins(255, numMod); -}; - -X_fet.prototype._setAllpins = function(value, numMod) { - - if (Array.isArray(numMod)) { - for (var i = 0; i < numMod.length; i++) { - this._clearAllBlink(i); - this._valuePins[this._valuePins.length - numMod[i] - 1] = value; - } - } else if (typeof numMod === 'number') { - numMod |= 0; - this._clearAllBlink(numMod); - this._valuePins[this._valuePins.length - numMod - 1] = value; - } else { - for (var i = 0; i < this._qtyMod; i++) { - this._clearAllBlink(i); - this._valuePins[this._valuePins.length - i - 1] = value; - } - } - this._spi.send(this._valuePins, this._cs); -}; - -X_fet.prototype._getMask = function(pin) { - if (Array.isArray(pin)) { - var mask = 0; - for (var i = 0; i < pin.length; i++) { - mask |= 1 << pin[i]; - } - } else { - mask = 1 << pin; - } - return mask; -}; - - - -X_fet.prototype.blink = function(pin, numMod, onTime, offTime) { - if (Array.isArray(pin)) { - for (var i = 0; i < pin.length; i++) { - this._blinkPin(pin[i], numMod, onTime, offTime); - } - } else { - this._blinkPin(pin, numMod, onTime, offTime); - }; -}; - -X_fet.prototype._blinkPin = function(pin, numMod, onTime, offTime) { - if (this._blinkOnTime[numMod][pin] === onTime && - this._blinkOffTime[numMod][pin] && - this._blinkOffTime[numMod][pin] === offTime) { - return; - } - this._clearBlinkPin(pin, numMod); - this._blinkOnTime[numMod][pin] = onTime; - this._blinkOffTime[numMod][pin] = offTime; - - if (this._isOn(pin, numMod)) { - this._blinkOff(pin, numMod); - } else { - this._blinkOn(pin, numMod); - } -}; - -X_fet.prototype._isOn = function(pin, numMod) { - var mask = 1 << pin; - numMod |= 0; - if ((this._valuePins[this._valuePins.length - numMod - 1] & mask) > 0) { - return true; - } else { - return false; - } -}; - -X_fet.prototype._clearAllBlink = function(numMod) { - for (var i = 0; i < 8; i++) { - this._clearBlinkPin(i, numMod); - } -}; - -X_fet.prototype._clearBlink = function(pin, numMod) { - if (Array.isArray(pin)) { - - for (var i = 0; i < pin.length; i++) { - this._clearBlinkPin(pin[i], numMod); - } - } else { - this._clearBlinkPin(pin, numMod); - } -}; - -X_fet.prototype._clearBlinkPin = function(pin, numMod) { - if (this._blinkTimeoutID[numMod][pin]) { - clearTimeout(this._blinkTimeoutID[numMod][pin]); - this._blinkTimeoutID[numMod][pin] = null; - this._blinkOnTime[numMod][pin] = 0; - this._blinkOffTime[numMod][pin] = 0; - } -}; - -X_fet.prototype._blinkOn = function(pin, numMod) { - this._update(pin, numMod); - this._blinkTimeoutID[numMod][pin] = setTimeout( - this._blinkOff.bind(this, pin, numMod), - this._blinkOnTime[numMod][pin] * 1000); -}; - -X_fet.prototype._blinkOff = function(pin, numMod) { - this._update(pin, numMod); - - if (this._blinkOffTime[numMod][pin]) { - this._blinkTimeoutID[numMod][pin] = setTimeout( - this._blinkOn.bind(this, pin, numMod), - this._blinkOffTime[numMod][pin] * 1000); - } else { - this._blinkTimeoutID[numMod][pin] = null; - } -}; - -X_fet.prototype._update = function(pin, numMod) { - numMod |= 0; - this._mask = this._getMask(pin); - this._valuePins[this._valuePins.length - numMod - 1] ^= this._mask; - this._spi.send(this._valuePins, this._cs); -}; - -exports.connect = function(opts) { - return new X_fet(opts); -}; From d0d7e9d5eeedcda4e9a30ea8a51ea67bc2ed70c4 Mon Sep 17 00:00:00 2001 From: PSVM-J <39494832+PSVM-J@users.noreply.github.com> Date: Wed, 27 Jun 2018 13:51:27 +0300 Subject: [PATCH 4/4] changed name x-fet and connect param --- modules/@amperka/x-fet.js | 200 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 modules/@amperka/x-fet.js diff --git a/modules/@amperka/x-fet.js b/modules/@amperka/x-fet.js new file mode 100644 index 0000000..5a3c2b1 --- /dev/null +++ b/modules/@amperka/x-fet.js @@ -0,0 +1,200 @@ +var defaultOpts = { + cs: P8, + spi: SPI2, + qtyMod: 1 +}; +// +var X_fet = function(opts) { + if (typeof opts === 'number') { + defaultOpts.cs = opts; + } + opts = this.extend(defaultOpts, opts || {}); + this._spi = opts.spi; + this._cs = opts.cs; + this._qtyMod = opts.qtyMod; + this._valuePins = []; + this._blinkOnTime = []; + this._blinkOffTime = []; + this._blinkTimeoutID = []; + for (var i = 0; i < this._qtyMod; i++) { + this._valuePins[i] = 0; + this._blinkOnTime[i] = [0, 0, 0, 0, 0, 0, 0, 0]; + this._blinkOffTime[i] = [0, 0, 0, 0, 0, 0, 0, 0]; + this._blinkTimeoutID[i] = [0, 0, 0, 0, 0, 0, 0, 0]; + } + this._spi.send(this._valuePins, this._cs); +}; + +X_fet.prototype.extend = function(obj) { + var length = arguments.length; + for (var index = 1; index < length; index++) { + var source = arguments[index], + keys = Object.keys(source), + l = keys.length; + for (var i = 0; i < l; i++) { + var key = keys[i]; + obj[key] = source[key]; + } + } + return obj; +}; + + +X_fet.prototype.turnOn = function(pin, numMod) { + numMod |= 0; + this._mask = this._getMask(pin); + this._clearBlink(pin, numMod); + this._valuePins[this._valuePins.length - numMod - 1] |= this._mask; + this._spi.send(this._valuePins, this._cs); +}; + +X_fet.prototype.turnOff = function(pin, numMod) { + numMod |= 0; + this._mask = this._getMask(pin); + this._clearBlink(pin, this._numMod); + this._valuePins[this._valuePins.length - numMod - 1] &= ~this._mask; + this._spi.send(this._valuePins, this._cs); +}; + +X_fet.prototype.toggle = function(pin, numMod) { + numMod |= 0; + this._mask = this._getMask(pin); + this._clearBlink(pin, numMod); + this._valuePins[this._valuePins.length - numMod - 1] ^= this._mask; + this._spi.send(this._valuePins, this._cs); +}; + +X_fet.prototype.turnAllOff = function(numMod) { + this._setAllpins(0, numMod); +}; + +X_fet.prototype.turnAllOn = function(numMod) { + this._setAllpins(255, numMod); +}; + +X_fet.prototype._setAllpins = function(value, numMod) { + + if (Array.isArray(numMod)) { + for (var i = 0; i < numMod.length; i++) { + this._clearAllBlink(i); + this._valuePins[this._valuePins.length - numMod[i] - 1] = value; + } + } else if (typeof numMod === 'number') { + numMod |= 0; + this._clearAllBlink(numMod); + this._valuePins[this._valuePins.length - numMod - 1] = value; + } else { + for (var i = 0; i < this._qtyMod; i++) { + this._clearAllBlink(i); + this._valuePins[this._valuePins.length - i - 1] = value; + } + } + this._spi.send(this._valuePins, this._cs); +}; + +X_fet.prototype._getMask = function(pin) { + if (Array.isArray(pin)) { + var mask = 0; + for (var i = 0; i < pin.length; i++) { + mask |= 1 << pin[i]; + } + } else { + mask = 1 << pin; + } + return mask; +}; + + + +X_fet.prototype.blink = function(pin, numMod, onTime, offTime) { + if (Array.isArray(pin)) { + for (var i = 0; i < pin.length; i++) { + this._blinkPin(pin[i], numMod, onTime, offTime); + } + } else { + this._blinkPin(pin, numMod, onTime, offTime); + }; +}; + +X_fet.prototype._blinkPin = function(pin, numMod, onTime, offTime) { + if (this._blinkOnTime[numMod][pin] === onTime && + this._blinkOffTime[numMod][pin] && + this._blinkOffTime[numMod][pin] === offTime) { + return; + } + this._clearBlinkPin(pin, numMod); + this._blinkOnTime[numMod][pin] = onTime; + this._blinkOffTime[numMod][pin] = offTime; + + if (this._isOn(pin, numMod)) { + this._blinkOff(pin, numMod); + } else { + this._blinkOn(pin, numMod); + } +}; + +X_fet.prototype._isOn = function(pin, numMod) { + var mask = 1 << pin; + numMod |= 0; + if ((this._valuePins[this._valuePins.length - numMod - 1] & mask) > 0) { + return true; + } else { + return false; + } +}; + +X_fet.prototype._clearAllBlink = function(numMod) { + for (var i = 0; i < 8; i++) { + this._clearBlinkPin(i, numMod); + } +}; + +X_fet.prototype._clearBlink = function(pin, numMod) { + if (Array.isArray(pin)) { + + for (var i = 0; i < pin.length; i++) { + this._clearBlinkPin(pin[i], numMod); + } + } else { + this._clearBlinkPin(pin, numMod); + } +}; + +X_fet.prototype._clearBlinkPin = function(pin, numMod) { + if (this._blinkTimeoutID[numMod][pin]) { + clearTimeout(this._blinkTimeoutID[numMod][pin]); + this._blinkTimeoutID[numMod][pin] = null; + this._blinkOnTime[numMod][pin] = 0; + this._blinkOffTime[numMod][pin] = 0; + } +}; + +X_fet.prototype._blinkOn = function(pin, numMod) { + this._update(pin, numMod); + this._blinkTimeoutID[numMod][pin] = setTimeout( + this._blinkOff.bind(this, pin, numMod), + this._blinkOnTime[numMod][pin] * 1000); +}; + +X_fet.prototype._blinkOff = function(pin, numMod) { + this._update(pin, numMod); + + if (this._blinkOffTime[numMod][pin]) { + this._blinkTimeoutID[numMod][pin] = setTimeout( + this._blinkOn.bind(this, pin, numMod), + this._blinkOffTime[numMod][pin] * 1000); + } else { + this._blinkTimeoutID[numMod][pin] = null; + } +}; + +X_fet.prototype._update = function(pin, numMod) { + numMod |= 0; + this._mask = this._getMask(pin); + this._valuePins[this._valuePins.length - numMod - 1] ^= this._mask; + this._spi.send(this._valuePins, this._cs); +}; + +exports.connect = function(opts) { + return new X_fet(opts); +};