Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option for TriState Signal #11

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
132 changes: 66 additions & 66 deletions Emitter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var _ = require('underscore'),
Q = require('q'),
path = require('path'),
exec = require('child_process').exec,
util = require('util');
const _ = require('underscore');
const Q = require('q');
const path = require('path');
const exec = require('child_process').exec;
const util = require('util');



Expand All @@ -13,8 +13,8 @@ module.exports = Emitter;
Emitter.SCRIPT = 'build/codesend';

function Emitter(options) {
this.options = options;

this.options = options;

}

Expand All @@ -28,87 +28,87 @@ function Emitter(options) {
* @param [callback] Callback(error, stdout)
* @return Promise
*/
Emitter.prototype.sendCode = function (code, options, callback) {

var deferred = Q.defer();

//NoOp as default callback
if(!_.isFunction(callback)) {
callback = _.noop;
}

//Check arguments length
if(arguments.length === 0 || arguments.length > 3) {
return deferred.reject(new Error('Invalid parameters. sendCode(code, [options, callback])'));
}
Emitter.prototype.sendCode = function(code, options, callback) {

var deferred = Q.defer();

//Check if code is a number (and parse it)
code = parseInt(code);
if(!_.isNumber(code)) {
return deferred.reject(new Error('First parameter must be a integer'));
}
//NoOp as default callback
if (!_.isFunction(callback)) {
callback = _.noop;
}

//Tidy up
switch(arguments.length) {
//Check arguments length
if (arguments.length === 0 || arguments.length > 3) {
return deferred.reject(new Error('Invalid parameters. sendCode(code, [options, callback])'));
}

//function(code)
case 1:
//Check if code is a number (and parse it)
code = parseInt(code);
if (!_.isNumber(code)) {
return deferred.reject(new Error('First parameter must be a integer'));
}

options = this.options;
//Tidy up
switch (arguments.length) {

break;
//function(code)
case 1:

//function(code, options || callback)
case 2:
options = this.options;

//function(code, callback)
if(_.isFunction(options)) {
break;

callback = options;
options = this.options;
//function(code, options || callback)
case 2:

//function(code, options)
} else if (_.isObject(options)) {
//function(code, callback)
if (_.isFunction(options)) {

_.defaults(options, this.options);
callback = options;
options = this.options;

//function(code, ???)
} else {
//function(code, options)
} else if (_.isObject(options)) {

return deferred.reject(new Error('Second parameter must be a function (callback) or an object (options)'));
_.defaults(options, this.options);

}
//function(code, ???)
} else {

break;
return deferred.reject(new Error('Second parameter must be a function (callback) or an object (options)'));

//function(code, options, callback)
default:
}

_.defaults(options, this.options);
break;

break;
//function(code, options, callback)
default:

}

//Send the code
exec([path.join(__dirname, Emitter.SCRIPT),
'--code', code,
'--pin', options.pin,
'--pulse-length', options.pulseLength
].join(' '), function (error, stdout, stderr) {
_.defaults(options, this.options);

error = error || stderr;
break;

if(error) {
deferred.reject(error);
} else {
deferred.resolve(stdout);
}

callback(error, stdout);
//Send the code
exec([path.join(__dirname, Emitter.SCRIPT),
'--code', code,
'--pin', options.pin,
'--pulse-length', options.pulseLength
].join(' '), function(error, stdout, stderr) {

error = error || stderr;

if (error) {
deferred.reject(error);
} else {
deferred.resolve(stdout);
}

callback(error, stdout);

});
});

return deferred.promise;
return deferred.promise;

};
};
106 changes: 106 additions & 0 deletions EmitterTriState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const _ = require('underscore');
const Q = require('q');
const path = require('path');
const exec = require('child_process').exec;
const util = require('util');


module.exports = EmitterTriState;



EmitterTriState.SCRIPT = 'build/codesend';

function EmitterTriState(options) {
this.options = options;
}

/**
* Send a decimal code through 433Mhz (and return a promise).
*
* @param code TriState code
* @param [options] Options to configure pin or pulseLength
* options.pin Pin on which send the code
* options.pulseLength Pulse length
* @param [callback] Callback(error, stdout)
* @return Promise
*/
EmitterTriState.prototype.sendCode = function(code, options, callback) {

var deferred = Q.defer();

//NoOp as default callback
if (!_.isFunction(callback)) {
callback = _.noop;
}

//Check arguments length
if (arguments.length === 0 || arguments.length > 3) {
return deferred.reject(new Error('Invalid parameters. sendCode(code, [options, callback])'));
}


//Tidy up
switch (arguments.length) {

//function(code)
case 1:

options = this.options;

break;

//function(code, options || callback)
case 2:

//function(code, callback)
if (_.isFunction(options)) {

callback = options;
options = this.options;

//function(code, options)
} else if (_.isObject(options)) {

_.defaults(options, this.options);

//function(code, ???)
} else {

return deferred.reject(new Error('Second parameter must be a function (callback) or an object (options)'));

}

break;

//function(code, options, callback)
default:

_.defaults(options, this.options);

break;

}

//Send the code
exec([path.join(__dirname, EmitterTriState.SCRIPT),
'--pin', options.pin,
'--pulse-length', options.pulseLength,
'--tri-state', code
].join(' '), function(error, stdout, stderr) {

error = error || stderr;

if (error) {
deferred.reject(error);
} else {
deferred.resolve(stdout);
}

callback(error, stdout);

});

return deferred.promise;

};
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# rpi-433
[![npm version](https://badge.fury.io/js/rpi-433.svg)](http://badge.fury.io/js/rpi-433)
# rpi-433-tristate
[![npm version](https://badge.fury.io/js/rpi-433-tristate.svg)](http://badge.fury.io/js/rpi-433-tristate)

[![NPM](https://nodei.co/npm/rpi-433.png?downloads=true)](https://nodei.co/npm/rpi-433/)
[![NPM](https://nodei.co/npm/rpi-433-tristate.png?downloads=true)](https://nodei.co/npm/rpi-433-tristate/)

Simple NodeJS module to send and receive decimal codes through 433Mhz device on RaspberryPI 2
Simple NodeJS module receive decimal codes and send tristate signals through 433Mhz device on RaspberryPI 2

## Disclaimer
It's a fork from eroak but my PR got no attention. So i made my own package.

### Dependencies
* wiringPi : https://projects.drogon.net/raspberry-pi/wiringpi/
Expand All @@ -21,7 +24,7 @@ root@raspberrypi:/home/pi/wiringPi/wiringPi# ./build
### Installation

```bash
npm install rpi-433
npm install rpi-433-tristate
```

### Usage
Expand All @@ -40,16 +43,19 @@ gpio readall
### Example

```js
var rpi433 = require('rpi-433'),
var rpi433 = require('rpi-433-tristate'),
rfSniffer = rpi433.sniffer({
pin: 2, //Snif on GPIO 2 (or Physical PIN 13)
debounceDelay: 500 //Wait 500ms before reading another code
}),
});
rfEmitter = rpi433.emitter({
pin: 0, //Send through GPIO 0 (or Physical PIN 11)
pulseLength: 350 //Send the code with a 350 pulse length
});

rfEmitterTri = rpi433.emitterTriState({
pin: 0, //Equal to above
pulseLength: 300
})
// Receive (data is like {code: xxx, pulseLength: xxx})
rfSniffer.on('data', function (data) {
console.log('Code received: '+data.code+' pulse length : '+data.pulseLength);
Expand All @@ -60,6 +66,11 @@ rfEmitter.sendCode(1234, function(error, stdout) { //Send 1234
if(!error) console.log(stdout); //Should display 1234
});

// Send TriState signal
rfEmitterTri.sendCode("FF0FFFFF00F0", function(error, stdout) {
if(!error) console.log(stdout);
});

/* Or :

rfEmitter.sendCode(code);
Expand Down
Loading