Permalink
Browse files

Initial node lib commit

  • Loading branch information...
1 parent 5b53c14 commit fb8bb647ec0ff64187fb622a3b9c982a60c7623d @thammi thammi committed Apr 20, 2011
Showing with 57 additions and 0 deletions.
  1. +6 −0 node_lib/README.md
  2. +39 −0 node_lib/buzz.js
  3. +12 −0 node_lib/test.js
View
@@ -0,0 +1,6 @@
+# buzz node lib
+
+## Dependencies
+
+* https://github.com/voodootikigod/node-serialport
+
View
@@ -0,0 +1,39 @@
+var serialport = require('serialport');
+var events = require('events');
+
+function Buzz(device) {
+ var options = {
+ baudrate: 115200,
+ databits: 8,
+ parity: 0,
+ stopbits: 1,
+ parser: serialport.parsers.readline("\n\r"),
+ }
+
+ var that = this;
+
+ this.port = port = new serialport.SerialPort(device, options);
+
+ port.on("data", function(data) {
+ that.emit("button", data[1] - '0'.charCodeAt(0));
+ });
+}
+
+Buzz.prototype = new events.EventEmitter();
+
+Buzz.prototype.set_led = function (led, brightness) {
+ buf = new Buffer("L$$\n\r");
+
+ buf[1] = '0'.charCodeAt(0) + led;
+ buf[2] = brightness ? 0xff : 0x00;
+
+ // async?
+ this.port.write(buf);
+};
+
+Buzz.prototype.close = function () {
+ this.port.close();
+}
+
+exports.Buzz = Buzz
+
View
@@ -0,0 +1,12 @@
+var buzz = require('./buzz.js');
+
+b = new buzz.Buzz("/dev/ttyUSB0");
+
+b.set_led(1, 0);
+
+b.on("button", function(key) {
+ console.log(key);
+});
+
+//b.close();
+

0 comments on commit fb8bb64

Please sign in to comment.