var serialport = require("serialport");
var colors = require('colors');
var SerialPort = serialport.SerialPort;
var EventEmitter = require('events').EventEmitter;

/**
 * constructor function
 */
var DeskLamp = function() {

	var self = this;

	this.device = "/dev/tty.usbmodem1421";
	this.baudrate = 9600;
	this.serialPort;

	this.r;
	this.g;
	this.b;
};

/**
 * Event emmitter goodness!
 */
DeskLamp.prototype.__proto__ = EventEmitter.prototype;

/**
 * Initilize the DeskLamp, connects to the serial port.
 */
DeskLamp.prototype.connect = function (callback) {
	var self = this;

	this.serialPort = new SerialPort(this.device, {
		baudrate: this.baudrate,
		parser: serialport.parsers.readline("\n")
	});

	this.serialPort.on("open", function() {
		// need to wait for a second, who knows why.
		setTimeout(function() {
			self.connected();
			callback();
		}, 1000);
	});
};

/**
 * connected handler for when the serial connection is open.
 */
DeskLamp.prototype.connected = function() {
	console.log('DeskLamp '.green + this.device.yellow + ' @ ' + this.baudrate.toString().blue + ' bps'.blue);

	this.serialPort.on('data', function(data) {
		console.info('∞ '.magenta + data);
	});
};

/**
 * Sets the color of the leds
 */
DeskLamp.prototype.setColor = function(r, g, b) {
	this.serialPort.write(r + ',' + g + ',' + b + '\n');
};

module.exports = new DeskLamp();

//function pulse() {
	//if(pulseInhale) {
		//r+=5;
		//g+=5;
		//b+=5;
		//if(r >= 255) pulseInhale = false;
	//} else {
		//r-=5;
		//g-=5;
		//b-=5;
		//if(r <= 50) pulseInhale = true;
	//}

	//setColor(r, g, b);
//}

@@ -345,16 +345,86 @@
 %09this.b;
+%0A%0A%09this.on('color', function(r, g, b) %7B%0A%09%09this.setColor(r, g, b);%0A%09%7D);
 %0A%7D;%0A%0A/**
@@ -928,17 +928,17 @@
 ;%0A%09%09%7D, 1
-0
+3
 00);%0A%09%7D)
@@ -1290,24 +1290,24 @@
 he leds%0A */%0A
-
 DeskLamp.pro
@@ -1344,16 +1344,103 @@
 g, b) %7B%0A
+%09//console.log(r.toString().red + ',' + g.toString().green + ',' + b.toString().blue);%0A
 %09this.se

add color event handler