2017-01-21 127 views
0

我正在尝试为Raspberry Pi的GPIO引脚创建自己的模块,但是如果事情发生了变化(如按下按钮/释放按钮),我想收到.on('something', function() { })事件。node.js模块 - EventEmitter

python脚本每隔0.01秒发送一个值,如true或false。

module.js

var PythonShell = require('python-shell'); 

module.exports = { 
    switch_live: function(pin) { 

     var status = false; 

     this.get = (function(){ 
      return status; 
     }); 

     var options = { 
      mode: 'text', 
      args: [pin], 
      pythonOptions: ['-u'] 
     }; 

     var pyshell = new PythonShell('./data/switch_loop.py', options); 

     pyshell.on('message', function(message) { 

      if (message == 'false') { 

       if (status) { 
        // EMIT: button released 

       } 

       status = false; 
      } else { 

       if (!status) { 
        // EMIT: button pressed 

       } 

       status = true; 
      } 



     }); 

     return this.status;  

    }, 
    some_other_funcs: function(pin) { 
     // ... 
    } 
} 

app.js

var module = require('./module.js'); 

var button = new module.switch_live(10); 

该模块可以告诉你该按钮的状态:

// get status of button 
var status = button.get(); // returns "true" or "false" 

但我想是这样的:

button.on('pressed', function() { 
    // call this function every time when button is pressed 
}); 

button.on('released', function() { 
    // call this function every time when button is released 
}); 

谢谢:)

编辑:我没有找到解决方案,因为我需要“module.exports”内的功能。

回答

0

我的解决方案

它是Linkerkit-Board的模块。

app.js

var linkerkit = require('./linkerkit.js'); 

var button = new linkerkit.switch_live(20); 

button.on('pressed', function() { 
    // call this function every time when button is pressed 

    console.log('press'); 

}); 

button.on('released', function() { 
    // call this function every time when button is released 

    console.log('release'); 

}); 

linkerkit。JS

var PythonShell = require('python-shell'); 
var EventEmitter = require('events'); 
var util = require('util'); 

var linkerkit = { 

    switch_live: function(pin) { 

     var self = this; 

     this.status = false; 
     this.pin = pin; 
     EventEmitter.call(this); 

     this.get = function() { 
      return this.status; 
     } 

     this._pyshell = new PythonShell('./data/switch_loop.py', { 
      mode: 'text', 
      args: [this.pin], 
      pythonOptions: ['-u'] 
     }); 

     this._pyshell.on("message", function(message) { 

      if (message === "false") { 
       if (this.status === true) { 
        // EMIT: button released 
        self.emit("released"); 
       } 
       this.status = false; 
      } 
      else { 
       if (this.status === false) { 
        // EMIT: button pressed 
        self.emit("pressed"); 
       } 
       this.status = true; 
      } 

     }); 

    } 

} 


for (var key in linkerkit) { 
    util.inherits(linkerkit[key], EventEmitter); 
} 

module.exports = linkerkit; 

我编辑的代码,因为它应该能够做到这样的事情:

var button = linkerkit.switch(20); 
button.get() // get status of button once, has no python loop 

var button_live = linkerkit.switch_live(20); 
button.get() // get status of button once, has a python loop 
button.on('pressed', callback); 
button.on('released', callback); 

var led = linkerkit.led(10); 
led.on(); 
led.off(); 
led.brightness(255); // digitalOutput - brightness 
led.blink(200); // 100ms on, 100ms off 

var ledcolor = linkerkit.led_rgb(12); 
ledcolor.on(); // on -> color: white 
ledcolor.off(); 
ledcolor.rgb([255, 0, 0]); // on -> color: red 

还有更多的功能...

-1

您应该能够使用节点的内置EventEmitter类来创建自己的 “按钮,发射器”:

module.js

var PythonShell = require('python-shell'); 

var util = require('util'); 
var EventEmitter = require('events'); 

function ButtonEmitter(pinNumber) { 
    this.status = false; 
    this.pinNumber = pinNumber; 
    EventEmitter.call(this); 
} 

util.inherits(ButtonEmitter, EventEmitter); 

ButtonEmitter.prototype.get = function() { 
    return this.status; 
}; 

ButtonEmitter.prototype.switch_live = function() { 
    var options = { 
    mode: 'text', 
    args: [this.pinNumber], 
    pythonOptions: ['-u'] 
    }; 

    this._pyshell = new PythonShell('./data/switch_loop.py', options); 

    this._pyshell.on("message", this._handleMessage.bind(this)); 

    return this.status; 
}; 

ButtonEmitter.prototype._handleMessage = function(message) { 
    if (message === "false") { 
    if (this.status === true) { 
     // EMIT: button released 
     this.emit("pressed"); 
    } 
    this.status = false; 
    } else { 
    if (this.status === false) { 
    // EMIT: button pressed 
    this.emit("released"); 
    } 
    this.status = true; 
    } 
}; 

module.exports = ButtonEmitter; 

app.js

var ButtonTrigger = require('./module.js'); 

var button = new ButtonTrigger(10); 

button.on('pressed', function() { 
    // call this function every time when button is pressed 
}); 

button.on('released', function() { 
    // call this function every time when button is released 
}); 

button.swith_live(); 

说明

上面的代码利用节点的util.inherits为“继承” EventEmitter,让你使用任何类访问.on().off()和其他事件的方法。 (注:你可以使用ES6 classextends语法也一样,如果你的节点版本支持)

然后,用你的“继承”,添加您自己switch_live方法启动“倾听”的过程,它传递_handleMessage回调。

_handleMessage回调运行时,它将比较当前状态和传入回调,并使用您在问题中发布的事件名称触发.emit()。同样,.emit()可用,因为您是“子类”EventEmitter

+0

谢谢帮助我:) – user2850375

+0

@ user2850375当然。不要忘记将答案标记为“已接受”! –

+0

我写了另一个答案来分享我的解决方案。我投了你的答案:) – user2850375