UNPKG

rpio2

Version:

Control Raspberry Pi GPIO pins with node.js. Fast and easy to use.

529 lines (450 loc) 19.7 kB
'use strict'; global._$jscoverage = global._$jscoverage || {}; _$jscoverage['gpio.js'] = []; _$jscoverage['gpio.js'][3] = 0; _$jscoverage['gpio.js'][7] = 0; _$jscoverage['gpio.js'][8] = 0; _$jscoverage['gpio.js'][12] = 0; _$jscoverage['gpio.js'][14] = 0; _$jscoverage['gpio.js'][15] = 0; _$jscoverage['gpio.js'][16] = 0; _$jscoverage['gpio.js'][17] = 0; _$jscoverage['gpio.js'][20] = 0; _$jscoverage['gpio.js'][21] = 0; _$jscoverage['gpio.js'][22] = 0; _$jscoverage['gpio.js'][23] = 0; _$jscoverage['gpio.js'][24] = 0; _$jscoverage['gpio.js'][25] = 0; _$jscoverage['gpio.js'][28] = 0; _$jscoverage['gpio.js'][30] = 0; _$jscoverage['gpio.js'][31] = 0; _$jscoverage['gpio.js'][32] = 0; _$jscoverage['gpio.js'][33] = 0; _$jscoverage['gpio.js'][35] = 0; _$jscoverage['gpio.js'][37] = 0; _$jscoverage['gpio.js'][38] = 0; _$jscoverage['gpio.js'][39] = 0; _$jscoverage['gpio.js'][40] = 0; _$jscoverage['gpio.js'][41] = 0; _$jscoverage['gpio.js'][43] = 0; _$jscoverage['gpio.js'][45] = 0; _$jscoverage['gpio.js'][46] = 0; _$jscoverage['gpio.js'][47] = 0; _$jscoverage['gpio.js'][48] = 0; _$jscoverage['gpio.js'][49] = 0; _$jscoverage['gpio.js'][51] = 0; _$jscoverage['gpio.js'][54] = 0; _$jscoverage['gpio.js'][55] = 0; _$jscoverage['gpio.js'][57] = 0; _$jscoverage['gpio.js'][61] = 0; _$jscoverage['gpio.js'][62] = 0; _$jscoverage['gpio.js'][63] = 0; _$jscoverage['gpio.js'][64] = 0; _$jscoverage['gpio.js'][66] = 0; _$jscoverage['gpio.js'][67] = 0; _$jscoverage['gpio.js'][70] = 0; _$jscoverage['gpio.js'][71] = 0; _$jscoverage['gpio.js'][75] = 0; _$jscoverage['gpio.js'][76] = 0; _$jscoverage['gpio.js'][78] = 0; _$jscoverage['gpio.js'][81] = 0; _$jscoverage['gpio.js'][87] = 0; _$jscoverage['gpio.js'][88] = 0; _$jscoverage['gpio.js'][90] = 0; _$jscoverage['gpio.js'][93] = 0; _$jscoverage['gpio.js'][99] = 0; _$jscoverage['gpio.js'][101] = 0; _$jscoverage['gpio.js'][102] = 0; _$jscoverage['gpio.js'][104] = 0; _$jscoverage['gpio.js'][106] = 0; _$jscoverage['gpio.js'][107] = 0; _$jscoverage['gpio.js'][108] = 0; _$jscoverage['gpio.js'][110] = 0; _$jscoverage['gpio.js'][111] = 0; _$jscoverage['gpio.js'][113] = 0; _$jscoverage['gpio.js'][117] = 0; _$jscoverage['gpio.js'][118] = 0; _$jscoverage['gpio.js'][119] = 0; _$jscoverage['gpio.js'][122] = 0; _$jscoverage['gpio.js'][123] = 0; _$jscoverage['gpio.js'][125] = 0; _$jscoverage['gpio.js'][126] = 0; _$jscoverage['gpio.js'][128] = 0; _$jscoverage['gpio.js'][129] = 0; _$jscoverage['gpio.js'][130] = 0; _$jscoverage['gpio.js'][134] = 0; _$jscoverage['gpio.js'][139] = 0; _$jscoverage['gpio.js'][141] = 0; _$jscoverage['gpio.js'][142] = 0; _$jscoverage['gpio.js'][143] = 0; _$jscoverage['gpio.js'][144] = 0; _$jscoverage['gpio.js'][145] = 0; _$jscoverage['gpio.js'][147] = 0; _$jscoverage['gpio.js'][149] = 0; _$jscoverage['gpio.js'][153] = 0; _$jscoverage['gpio.js'][155] = 0; _$jscoverage['gpio.js'][156] = 0; _$jscoverage['gpio.js'][157] = 0; _$jscoverage['gpio.js'][159] = 0; _$jscoverage['gpio.js'][160] = 0; _$jscoverage['gpio.js'][161] = 0; _$jscoverage['gpio.js'][163] = 0; _$jscoverage['gpio.js'][164] = 0; _$jscoverage['gpio.js'][165] = 0; _$jscoverage['gpio.js'][167] = 0; _$jscoverage['gpio.js'][170] = 0; _$jscoverage['gpio.js'][175] = 0; _$jscoverage['gpio.js'][176] = 0; _$jscoverage['gpio.js'][178] = 0; _$jscoverage['gpio.js'][179] = 0; _$jscoverage['gpio.js'][183] = 0; _$jscoverage['gpio.js'][184] = 0; _$jscoverage['gpio.js'][188] = 0; _$jscoverage['gpio.js'][190] = 0; _$jscoverage['gpio.js'][193] = 0; _$jscoverage['gpio.js'][197] = 0; _$jscoverage['gpio.js'][199] = 0; _$jscoverage['gpio.js'][202] = 0; _$jscoverage['gpio.js'][203] = 0; _$jscoverage['gpio.js'][207] = 0; _$jscoverage['gpio.js'][209] = 0; _$jscoverage['gpio.js'][210] = 0; _$jscoverage['gpio.js'][212] = 0; _$jscoverage['gpio.js'][216] = 0; _$jscoverage['gpio.js'][217] = 0; _$jscoverage['gpio.js'][218] = 0; _$jscoverage['gpio.js'][220] = 0; _$jscoverage['gpio.js'][225] = 0; _$jscoverage['gpio.js'].source = ['\'use strict\';', '', 'const rpio = require(\'rpio\'),', ' util = require(\'util\'),', ' EventEmitter = require(\'events\').EventEmitter;', '', 'function updateEdge(gpio, type){', ' var changes = gpio.listeners(\'change\').length,', ' risings = gpio.listeners(\'rising\').length,', ' fallings = gpio.listeners(\'falling\').length;', '', ' var edge = gpio.edge;', '', ' function triggerEvent(pin){', ' gpio.emit(\'change\', gpio, pin);', ' if(gpio.value) gpio.emit(\'rising\', gpio, pin);', ' else gpio.emit(\'falling\', gpio, pin); ', ' }', '', ' if(type === \'change\'){', ' changes ++;', ' }else if(type === \'rising\'){', ' risings ++;', ' }else if(type === \'falling\'){', ' fallings ++;', ' }', '', ' if(changes > 0 || ', ' risings > 0 && fallings > 0){', ' if(edge !== rpio.POLL_BOTH){', ' gpio.edge = rpio.POLL_BOTH;', ' if(edge !== rpio.POLL_NONE){', ' rpio.poll(gpio.pin, null);', ' }', ' rpio.poll(gpio.pin, triggerEvent, rpio.POLL_BOTH);', ' }', ' }else if(risings > 0){', ' if(edge !== rpio.POLL_HIGH){', ' gpio.edge = rpio.POLL_HIGH;', ' if(edge !== rpio.POLL_NONE){', ' rpio.poll(gpio.pin, null);', ' }', ' rpio.poll(gpio.pin, triggerEvent, rpio.POLL_HIGH); ', ' }', ' }else if(fallings > 0){', ' if(edge !== rpio.POLL_LOW){', ' gpio.edge = rpio.POLL_LOW;', ' if(edge !== rpio.POLL_NONE){', ' rpio.poll(gpio.pin, null);', ' }', ' rpio.poll(gpio.pin, triggerEvent, rpio.POLL_LOW);', ' } ', ' }else{', ' if(edge !== rpio.POLL_NONE){', ' rpio.poll(gpio.pin, null);', ' }', ' gpio.edge = rpio.POLL_NONE;', ' }', '}', '', 'function Gpio(pin, activeLow){', ' this.pin = pin;', ' this.edge = rpio.POLL_NONE;', ' this.activeLow = !!activeLow;', ' ', ' this.on(\'removeListener\', function(type){', ' updateEdge(this);', ' });', '', ' this.on(\'newListener\', function(type){', ' updateEdge(this, type);', ' });', '', ' //private properties', ' var mode = -1;', ' Object.defineProperty(this, \'_mode\', {', ' get: function(){', ' return mode;', ' },', ' set: function(value){', ' mode = value;', ' },', ' enumerable: false', ' });', '', ' //input state POLL_HIGH POLL_ROW resistors', ' var state = -1;', ' Object.defineProperty(this, \'_state\', {', ' get: function(){', ' return state;', ' },', ' set: function(value){', ' state = value;', ' },', ' enumerable: false', ' });', '}', '', 'util.inherits(Gpio, EventEmitter);', '', 'Gpio.init = rpio.init;', 'require(\'./gpio_util.js\').defineStaticProperties(Gpio);', '', 'Object.assign(Gpio.prototype, {', ' open: function(mode, value){', ' this._mode = mode;', ' if(mode === rpio.INPUT && value !== undefined){', ' this._state = value;', ' }', ' if(mode === rpio.OUTPUT){', ' return rpio.open(this.pin, mode, this.activeLow ^ value);', ' }else{', ' return rpio.open(this.pin, mode, value);', ' }', ' },', ' close: function(){', ' this.removeAllListeners();', ' this._mode = this._state = -1;', ' return rpio.close(this.pin);', ' },', ' read: function(length){', ' if(length === undefined){', ' return this.value;', ' }else{', ' var buf = new Buffer(length);', ' rpio.readbuf(this.pin, buf, length);', '', ' if(this.activeLow){', ' for(var i = 0; i < buf.length; i++){', ' buf[i] ^= 1;', ' }', ' }', '', ' return buf;', ' }', ' },', ' write: function(buf, length){', ' ', ' var self = this;', '', ' function writeData(){', ' if(self.activeLow){', ' var newBuf = new Buffer(length || buf.length);', ' for(var i = 0; i < newBuf.length; i++){', ' newBuf[i] = 1 ^ buf[i];', ' }', ' return rpio.writebuf(self.pin, newBuf, length);', ' }else{ ', ' return rpio.writebuf(self.pin, buf, length);', ' } ', ' }', '', ' if(length === undefined && buf === 0 || buf === 1){', ' //gpio.write(1)', ' this.value = buf;', ' return;', ' }else if(typeof buf === \'string\'){', ' //gpio.write(\'1010101\')', ' buf = new Buffer(buf.trim().split(\'\').map(function(i){return 0|i}));', ' return writeData();', ' }else if(typeof buf === \'number\'){', ' //gpio.write(0b10101010);', ' return this.write(buf.toString(2), length);', ' }else if(buf instanceof Buffer){', ' if(buf[0] !== 0 && buf[0] !== 1){', ' //Buffer<\'0\',\'1\',\'0\',\'1\'...>', ' return this.write(buf.toString(), length);', ' }else{', ' //Buffer<00,01,00,01...>', ' return writeData();', ' }', ' }', ' },', ' sleep: function(ms, async){', ' if(!async){', ' return rpio.msleep(ms);', ' }', ' return new Promise(function(resolve){', ' setTimeout(resolve, ms); ', ' });', ' },', ' toggle: function(){', ' this.value ^= 1;', ' return this.value;', ' }', '});', '', 'Object.defineProperty(Gpio.prototype, \'value\', {', ' get: function(){', ' return this.activeLow ^ rpio.read(this.pin);', ' },', ' set: function(value){', ' rpio.write(this.pin, this.activeLow ^ value);', ' }', '});', '', 'Object.defineProperty(Gpio.prototype, \'mode\', {', ' get: function(){', ' return this._mode;', ' },', ' set: function(mode){', ' this._mode = mode;', ' rpio.mode(this.pin, mode);', ' }', '});', '', 'Object.defineProperty(Gpio.prototype, \'state\', {', ' get: function(){', ' if(this.mode === Gpio.INPUT){', ' return this._state;', ' }else{', ' return this.value;', ' }', ' },', ' set: function(state){', ' if(this.mode === Gpio.INPUT){', ' this._state = state;', ' rpio.pud(this.pin, state);', ' }else{', ' this.value = state;', ' }', ' }', '});', '', 'module.exports = Gpio;', '']; _$jscoverage['gpio.js'][3]++; const rpio = require('../rpio_sim.js'), util = require('util'), EventEmitter = require('events').EventEmitter; _$jscoverage['gpio.js'][7]++; function updateEdge(gpio, type) { _$jscoverage['gpio.js'][8]++; var changes = gpio.listeners('change').length, risings = gpio.listeners('rising').length, fallings = gpio.listeners('falling').length; _$jscoverage['gpio.js'][12]++; var edge = gpio.edge; _$jscoverage['gpio.js'][14]++; function triggerEvent(pin) { _$jscoverage['gpio.js'][15]++; gpio.emit('change', gpio, pin); if (gpio.value) { _$jscoverage['gpio.js'][16]++; gpio.emit('rising', gpio, pin); } else { _$jscoverage['gpio.js'][17]++; gpio.emit('falling', gpio, pin); } } _$jscoverage['gpio.js'][20]++; if (type === 'change') { _$jscoverage['gpio.js'][21]++; changes++; } else { _$jscoverage['gpio.js'][22]++; if (type === 'rising') { _$jscoverage['gpio.js'][23]++; risings++; } else { _$jscoverage['gpio.js'][24]++; if (type === 'falling') { _$jscoverage['gpio.js'][25]++; fallings++; } } }_$jscoverage['gpio.js'][28]++; if (changes > 0 || risings > 0 && fallings > 0) { _$jscoverage['gpio.js'][30]++; if (edge !== rpio.POLL_BOTH) { _$jscoverage['gpio.js'][31]++; gpio.edge = rpio.POLL_BOTH; _$jscoverage['gpio.js'][32]++; if (edge !== rpio.POLL_NONE) { _$jscoverage['gpio.js'][33]++; rpio.poll(gpio.pin, null); } _$jscoverage['gpio.js'][35]++; rpio.poll(gpio.pin, triggerEvent, rpio.POLL_BOTH); } } else { _$jscoverage['gpio.js'][37]++; if (risings > 0) { _$jscoverage['gpio.js'][38]++; if (edge !== rpio.POLL_HIGH) { _$jscoverage['gpio.js'][39]++; gpio.edge = rpio.POLL_HIGH; _$jscoverage['gpio.js'][40]++; if (edge !== rpio.POLL_NONE) { _$jscoverage['gpio.js'][41]++; rpio.poll(gpio.pin, null); } _$jscoverage['gpio.js'][43]++; rpio.poll(gpio.pin, triggerEvent, rpio.POLL_HIGH); } } else { _$jscoverage['gpio.js'][45]++; if (fallings > 0) { _$jscoverage['gpio.js'][46]++; if (edge !== rpio.POLL_LOW) { _$jscoverage['gpio.js'][47]++; gpio.edge = rpio.POLL_LOW; _$jscoverage['gpio.js'][48]++; if (edge !== rpio.POLL_NONE) { _$jscoverage['gpio.js'][49]++; rpio.poll(gpio.pin, null); } _$jscoverage['gpio.js'][51]++; rpio.poll(gpio.pin, triggerEvent, rpio.POLL_LOW); } } else { _$jscoverage['gpio.js'][54]++; if (edge !== rpio.POLL_NONE) { _$jscoverage['gpio.js'][55]++; rpio.poll(gpio.pin, null); } _$jscoverage['gpio.js'][57]++; gpio.edge = rpio.POLL_NONE; } } } } _$jscoverage['gpio.js'][61]++; function Gpio(pin, activeLow) { _$jscoverage['gpio.js'][62]++; this.pin = pin; _$jscoverage['gpio.js'][63]++; this.edge = rpio.POLL_NONE; _$jscoverage['gpio.js'][64]++; this.activeLow = !!activeLow; _$jscoverage['gpio.js'][66]++; this.on('removeListener', function (type) { _$jscoverage['gpio.js'][67]++; updateEdge(this); }); _$jscoverage['gpio.js'][70]++; this.on('newListener', function (type) { _$jscoverage['gpio.js'][71]++; updateEdge(this, type); }); //private properties _$jscoverage['gpio.js'][75]++; var mode = -1; _$jscoverage['gpio.js'][76]++; Object.defineProperty(this, '_mode', { get: function () { _$jscoverage['gpio.js'][78]++; return mode; }, set: function (value) { _$jscoverage['gpio.js'][81]++; mode = value; }, enumerable: false }); //input state POLL_HIGH POLL_ROW resistors _$jscoverage['gpio.js'][87]++; var state = -1; _$jscoverage['gpio.js'][88]++; Object.defineProperty(this, '_state', { get: function () { _$jscoverage['gpio.js'][90]++; return state; }, set: function (value) { _$jscoverage['gpio.js'][93]++; state = value; }, enumerable: false }); } _$jscoverage['gpio.js'][99]++; util.inherits(Gpio, EventEmitter); _$jscoverage['gpio.js'][101]++; Gpio.init = rpio.init; _$jscoverage['gpio.js'][102]++; require('./gpio_util.js').defineStaticProperties(Gpio); _$jscoverage['gpio.js'][104]++; Object.assign(Gpio.prototype, { open: function (mode, value) { _$jscoverage['gpio.js'][106]++; this._mode = mode; _$jscoverage['gpio.js'][107]++; if (mode === rpio.INPUT && value !== undefined) { _$jscoverage['gpio.js'][108]++; this._state = value; } _$jscoverage['gpio.js'][110]++; if (mode === rpio.OUTPUT) { _$jscoverage['gpio.js'][111]++; return rpio.open(this.pin, mode, this.activeLow ^ value); } else { _$jscoverage['gpio.js'][113]++; return rpio.open(this.pin, mode, value); } }, close: function () { _$jscoverage['gpio.js'][117]++; this.removeAllListeners(); _$jscoverage['gpio.js'][118]++; this._mode = this._state = -1; _$jscoverage['gpio.js'][119]++; return rpio.close(this.pin); }, read: function (length) { _$jscoverage['gpio.js'][122]++; if (length === undefined) { _$jscoverage['gpio.js'][123]++; return this.value; } else { _$jscoverage['gpio.js'][125]++; var buf = new Buffer(length); _$jscoverage['gpio.js'][126]++; rpio.readbuf(this.pin, buf, length); _$jscoverage['gpio.js'][128]++; if (this.activeLow) { _$jscoverage['gpio.js'][129]++; for (var i = 0; i < buf.length; i++) { _$jscoverage['gpio.js'][130]++; buf[i] ^= 1; } } _$jscoverage['gpio.js'][134]++; return buf; } }, write: function (buf, length) { _$jscoverage['gpio.js'][139]++; var self = this; _$jscoverage['gpio.js'][141]++; function writeData() { _$jscoverage['gpio.js'][142]++; if (self.activeLow) { _$jscoverage['gpio.js'][143]++; var newBuf = new Buffer(length || buf.length); _$jscoverage['gpio.js'][144]++; for (var i = 0; i < newBuf.length; i++) { _$jscoverage['gpio.js'][145]++; newBuf[i] = 1 ^ buf[i]; } _$jscoverage['gpio.js'][147]++; return rpio.writebuf(self.pin, newBuf, length); } else { _$jscoverage['gpio.js'][149]++; return rpio.writebuf(self.pin, buf, length); } } _$jscoverage['gpio.js'][153]++; if (length === undefined && buf === 0 || buf === 1) { _$jscoverage['gpio.js'][155]++; //gpio.write(1) this.value = buf; _$jscoverage['gpio.js'][156]++; return; } else { _$jscoverage['gpio.js'][157]++; if (typeof buf === 'string') { //gpio.write('1010101') buf = new Buffer(buf.trim().split('').map(function (i) { _$jscoverage['gpio.js'][159]++; return 0 | i; })); _$jscoverage['gpio.js'][160]++; return writeData(); } else { _$jscoverage['gpio.js'][161]++; if (typeof buf === 'number') { _$jscoverage['gpio.js'][163]++; //gpio.write(0b10101010); return this.write(buf.toString(2), length); } else { _$jscoverage['gpio.js'][164]++; if (buf instanceof Buffer) { _$jscoverage['gpio.js'][165]++; if (buf[0] !== 0 && buf[0] !== 1) { _$jscoverage['gpio.js'][167]++; //Buffer<'0','1','0','1'...> return this.write(buf.toString(), length); } else { _$jscoverage['gpio.js'][170]++; //Buffer<00,01,00,01...> return writeData(); } } } } } }, sleep: function (ms, async) { _$jscoverage['gpio.js'][175]++; if (!async) { _$jscoverage['gpio.js'][176]++; return rpio.msleep(ms); } _$jscoverage['gpio.js'][178]++; return new Promise(function (resolve) { _$jscoverage['gpio.js'][179]++; setTimeout(resolve, ms); }); }, toggle: function () { _$jscoverage['gpio.js'][183]++; this.value ^= 1; _$jscoverage['gpio.js'][184]++; return this.value; } }); _$jscoverage['gpio.js'][188]++; Object.defineProperty(Gpio.prototype, 'value', { get: function () { _$jscoverage['gpio.js'][190]++; return this.activeLow ^ rpio.read(this.pin); }, set: function (value) { _$jscoverage['gpio.js'][193]++; rpio.write(this.pin, this.activeLow ^ value); } }); _$jscoverage['gpio.js'][197]++; Object.defineProperty(Gpio.prototype, 'mode', { get: function () { _$jscoverage['gpio.js'][199]++; return this._mode; }, set: function (mode) { _$jscoverage['gpio.js'][202]++; this._mode = mode; _$jscoverage['gpio.js'][203]++; rpio.mode(this.pin, mode); } }); _$jscoverage['gpio.js'][207]++; Object.defineProperty(Gpio.prototype, 'state', { get: function () { _$jscoverage['gpio.js'][209]++; if (this.mode === Gpio.INPUT) { _$jscoverage['gpio.js'][210]++; return this._state; } else { _$jscoverage['gpio.js'][212]++; return this.value; } }, set: function (state) { _$jscoverage['gpio.js'][216]++; if (this.mode === Gpio.INPUT) { _$jscoverage['gpio.js'][217]++; this._state = state; _$jscoverage['gpio.js'][218]++; rpio.pud(this.pin, state); } else { _$jscoverage['gpio.js'][220]++; this.value = state; } } }); _$jscoverage['gpio.js'][225]++; module.exports = Gpio;