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

Add RC livolo switch #58

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/rc.js
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ var rc = new arduino.RC({

board.on('ready', function(){
setTimeout(function() {
// alternative: rc.decimal("123456")
rc.triState("0FFF0FFFFF0F");
}, 1000);
setTimeout(function() {
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ module.exports = {
Sensor: require('./lib/sensor'),
Ping: require('./lib/ping'),
PIR: require('./lib/pir'),
LCD: require('./lib/lcd'),
RC: require('./lib/rc'),
IR: require('./lib/ir')
};
14 changes: 7 additions & 7 deletions lib/board.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
var events = require('events'),
child = require('child_process'),
util = require('util'),
colors = require('colors'),
chalk = require('chalk'),
serial = require('serialport');

/*
@@ -29,7 +29,7 @@ var Board = function (options) {

self.log('info', 'binding serial events');
self.serial.on('data', function(data){
self.log('receive', data.toString().red);
self.log('receive', chalk.red(data.toString()));
self.emit('data', data);
});

@@ -73,7 +73,7 @@ util.inherits(Board, events.EventEmitter);
Board.prototype.detect = function (callback) {
this.log('info', 'attempting to find Arduino board');
var self = this;
child.exec('ls /dev | grep ACM', function(err, stdout, stderr){
child.exec('ls /dev | grep -E "'+ self.device +'"', function(err, stdout, stderr){
var usb = stdout.slice(0, -1).split('\n'),
found = false,
err = null,
@@ -132,7 +132,7 @@ Board.prototype.write = function (m) {
this.log('write', m);
this.serial.write('!' + m + '.');
} else {
this.log('info', 'serial not ready, buffering message: ' + m.red);
this.log('info', 'serial not ready, buffering message: ' + chalk.red(m));
this.writeBuffer.push(m);
}
}
@@ -181,7 +181,7 @@ Board.prototype.pinMode = function (pin, val) {
Board.prototype.digitalWrite = function (pin, val) {
pin = this.normalizePin(pin);
val = this.normalizeVal(val);
this.log('info', 'digitalWrite to pin ' + pin + ': ' + val.green);
this.log('info', 'digitalWrite to pin ' + pin + ': ' + chalk.green(val));
this.write('01' + pin + val);
}

@@ -197,7 +197,7 @@ Board.prototype.digitalRead = function (pin) {
Board.prototype.analogWrite = function (pin, val) {
pin = this.normalizePin(pin);
val = this.normalizeVal(val);
this.log('info', 'analogWrite to pin ' + pin + ': ' + val.green);
this.log('info', 'analogWrite to pin ' + pin + ': ' + chalk.green(val));
this.write('03' + pin + val);
}
Board.prototype.analogRead = function (pin) {
@@ -220,7 +220,7 @@ Board.prototype.delay = function (ms) {
Board.prototype.log = function (/*level, message*/) {
var args = [].slice.call(arguments);
if (this.debug) {
console.log(String(+new Date()).grey + ' duino '.blue + args.shift().magenta + ' ' + args.join(', '));
console.log(chalk.gray(Date.now()) + chalk.blue(' duino ') + chalk.magenta(args.shift()) + ' ' + args.join(', '));
}
}

11 changes: 11 additions & 0 deletions lib/rc.js
Original file line number Diff line number Diff line change
@@ -17,4 +17,15 @@ RC.prototype.triState = function (val) {
this.board.write(msg);
}

/*
* Send decimal code
* val must be 12 chars or less
*/
RC.prototype.decimal = function (val) {
// at most twelve, null-term if shorter
var terminatedval = (val + '\0').slice(0,12)
var msg = '94' + this.pin + terminatedval;
this.board.write(msg);
}

module.exports = RC;
38 changes: 11 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -5,26 +5,18 @@
"url": "http://campedersen.com/"
},
"contributors": [
{
"name": "Rick Waldron",
"email": "waldron.rick@gmail.com"
},
{
"name": "Leonhardt Wille",
"email": "wille@riverlabs.de"
},
{
"name": "Seiya Konno",
"email": "nulltask@gmail.com"
},
{
"name": "Willi Thiel",
"email": "ni-c@ni-c.de"
}
{ "name": "Rick Waldron", "email": "waldron.rick@gmail.com" },
{ "name": "Leonhardt Wille", "email": "wille@riverlabs.de" },
{ "name": "Seiya Konno", "email": "nulltask@gmail.com" },
{ "name": "Nathan Vander Wilt", "email": "nate@calftrail.com" },
{ "name": "Adam Brault (&yet)", "email": "contact@andyet.com" },
{ "name": "Emanuele Tessore", "email": "setola@gmail.com" },
{ "name": "Willi Thiel", "email": "ni-c@ni-c.de" },
{ "name": "Tom Janson", "email": "priv.tom.janson+duino@gmail.com" }
],
"name": "duino",
"description": "Arduino framework for mad scientists",
"version": "0.0.8",
"version": "0.0.11",
"keywords": [
"arduino",
"serial",
@@ -40,15 +32,7 @@
},
"dependencies": {
"serialport": "*",
"colors": "*"
"chalk": "*"
},
"devDependencies": {},
"readme": "# duino\n\nA framework for working with Arduinos in node.js\n\n![arduino](http://i.imgur.com/eFq84.jpg)\n\n# install\n\n npm install duino\n\n# usage\n\n````javascript\nvar arduino = require('duino'),\n board = new arduino.Board();\n\nvar led = new arduino.Led({\n board: board,\n pin: 13\n});\n\nled.blink();\n````\n\n# what ಠ_ಠ\n\nThe way this works is simple (in theory, not in practice). The Arduino listens for low-level signals over a serial port, while we abstract all of the logic on the Node side.\n\n1. Plug in your Arduino\n2. Upload the C code at `./src/du.ino` to it\n3. Write a simple **duino** script\n4. ?????\n5. Profit!\n\n# libraries\n\n##board\n\nRight now, the board library will attempt to autodiscover the Arduino. I'm going to make it configurable, don't worry.\n\n````javascript\nvar board = new arduino.Board({\n debug: true\n});\n````\n\nDebug mode is off by default. Turning it on will enable verbose logging in your terminal, and tell the Arduino board to echo everthing back to you. You will get something like this:\n\n![debug](http://i.imgur.com/gBYZA.png)\n\nThe **board** object is an EventEmitter. You can listen for the following events:\n\n* `data` messages from the serial port, delimited by newlines\n* `connected` when the serial port has connected\n* `ready` when all internal post-connection logic has finished and the board is ready to use\n\n````javascript\nboard.on('ready', function(){\n // do stuff\n});\n\nboard.on('data', function(m){\n console.log(m);\n}\n````\n\n###board.serial\n\nLow-level access to the serial connection to the board\n\n###board.write(msg)\n\nWrite a message to the board, wrapped in predefined delimiters (! and .)\n\n###board.pinMode(pin, mode)\n\nSet the mode for a pin. `mode` is either `'in'` or `'out'`\n\n###board.digitalWrite(pin, val)\n\nWrite one of the following to a pin:\n\n####board.HIGH and board.LOW\n\nConstants for use in low-level digital writes\n\n###board.analogWrite(pin,val)\n\nWrite a value between 0-255 to a pin\n\n##led\n\n````javascript\nvar led = new arduino.Led({\n board: board,\n pin: 13\n});\n````\n\nPin will default to 13.\n\n###led.on()\n\nTurn the LED on\n\n###led.off()\n\nTurn the LED off\n\n###led.blink(interval)\n\nBlink the LED at `interval` ms. Defaults to 1000\n\n###led.fade(interval)\n\nFade the to full brightness then back to minimal brightness in `interval` ms. Defaults to 2000\n\n###led.bright\n\nCurrent brightness of the LED\n\n##piezo\n\n````javascript\nvar led = new arduino.Piezo({\n board: board,\n pin: 13\n});\n````\nPin will default to 13.\n\n###piezo.note(note, duration)\n\nPlay a pre-calculated note for a given duration (in milliseconds).\n\n`note` must be a string, one of `d`, `e`, `f`, `g`, `a`, `b`, or `c` (must be lowercase)\n\n###piezo.tone(tone, duration)\n\nWrite a square wave to the piezo element.\n\n`tone` and `duration` must be integers. See code comments for math on `tone` generation.\n\n##button\n\n````javascript\nvar button = new arduino.Button({\n board: board,\n pin: 13\n});\n````\nPin will default to 13.\n\nButtons are simply EventEmitters. They will emit the events `up` and `down`. You may also access their `down` property.\n\n````javascript\nbutton.on('down', function(){\n // delete the database!\n console.log('BOOM');\n});\n\nsetInterval(function(){\n console.log(button.down);\n}, 1000);\n````\n\n##servo\n\n````javascript\nvar servo = new arduino.Servo({\n board: board\n});\n\nservo.write(0);\nservo.write(180);\n````\nPin will default to 9. (Arduino PWM default)\n\n###servo.sweep()\n\nIncrement position from 0 to 180.\n\n###servo.write(pos)\n\nInstruct the servo to immediately go to a position from 0 to 180.\n\n##motor\n\n##potentiometer\n\n# protocol\n\nEach message sent to the Arduino board by the **board** class has 8 bytes.\n\nA full message looks like this:\n\n !0113001.\n\n`!` Start\n`01` Command (digitalWrite)\n`13` Pin number\n`001` Value (high)\n`.` Stop\n\nI was drunk. It works.\n\n##command\n\nWhat is implemented right now:\n\n* `00` pinMode\n* `01` digitalWrite\n* `02` digitalRead\n* `03` analogWrite\n* `04` analogRead\n* `99` debug\n\n##pin\n\nPins can be sent as an integer or a string(`1`, `2`, `\"3\"`, `\"A0\"`)\n\n##value\n\n* `board.LOW`(`0`)\n* `board.HIGH`(`255`)\n* integer/string from `0`-`255` for PWM pins\n\n# license\n\n(The MIT License)\n\nCopyright (c) 2011 Cam Pedersen <cam@onswipe.com>\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n",
"readmeFilename": "README.md",
"_id": "duino@0.0.8",
"dist": {
"shasum": "3ebb97ebd5baa063021ff007c31fe28f20a02367"
},
"_from": "https://github.com/ni-c/duino/tarball/master",
"_resolved": "https://github.com/ni-c/duino/tarball/master"
"devDependencies": {}
}
56 changes: 40 additions & 16 deletions src/du.ino
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#include <Servo.h>
#include "RCSwitch.h"
#include <RCSwitch.h>

bool debug = false;

int index = 0;

char messageBuffer[18];
char messageBuffer[24];
char cmd[3];
char pin[3];
char val[13];
char aux[4];
char type[2];
char addr[5];

Servo servo;

@@ -42,19 +44,26 @@ void process() {
}
int cmdid = atoi(cmd);

if (cmdid == 96) {
if (cmdid == 95) {
strncpy(type, messageBuffer + 4, 1);
type[1] = '\0';
strncpy(val, messageBuffer + 5, 9);
val[8] = '\0';
strncpy(addr, messageBuffer + 14, 4);
addr[4] = '\0';
} else if (cmdid == 96 || cmdid == 94) {
strncpy(val, messageBuffer + 4, 12);
val[12] = '\0';
} else if (cmdid > 90) {
} else if (cmdid > 96) {
strncpy(val, messageBuffer + 4, 2);
val[2] = '\0';
strncpy(aux, messageBuffer + 6, 3);
aux[3] = '\0';
} else {
strncpy(val, messageBuffer + 4, 3);
val[3] = '\0';
val[4] = '\0';
strncpy(aux, messageBuffer + 7, 3);
aux[3] = '\0';
aux[4] = '\0';
}

// Serial.println(cmd);
@@ -63,16 +72,17 @@ void process() {
// Serial.println(aux);

switch(cmdid) {
case 0: sm(pin,val); break;
case 1: dw(pin,val); break;
case 2: dr(pin,val); break;
case 3: aw(pin,val); break;
case 4: ar(pin,val); break;
case 96: handleRCTriState(pin, val); break;
case 97: handlePing(pin,val,aux); break;
case 98: handleServo(pin,val,aux); break;
case 99: toggleDebug(val); break;
default: break;
case 0: sm(pin,val); break;
case 1: dw(pin,val); break;
case 2: dr(pin,val); break;
case 3: aw(pin,val); break;
case 4: ar(pin,val); break;
case 94: handleRCDecimal(pin, val); break;
case 96: handleRCTriState(pin, val); break;
case 97: handlePing(pin,val,aux); break;
case 98: handleServo(pin,val,aux); break;
case 99: toggleDebug(val); break;
default: break;
}
}

@@ -262,3 +272,17 @@ void handleRCTriState(char *pin, char *val) {
rc.enableTransmit(p);
rc.sendTriState(val);
}

/*
* Handle RC commands via decimal code
* For those sockets that don't use tri-state.
* handleRCDecimal("10", "5522351")
*/
void handleRCDecimal(char *pin, char *val) {
int p = getPin(pin);
if (p == -1) { if (debug) Serial.println("badpin"); return; }
if (debug) Serial.println("RCdec" + atol(val));
RCSwitch rc = RCSwitch();
rc.enableTransmit(p);
rc.send(atol(val), 24);
}