Skip to content

Instantly share code, notes, and snippets.

@ajfisher
Created June 8, 2015 10:12
Show Gist options
  • Save ajfisher/dec40f484ffcf6f1fb4c to your computer and use it in GitHub Desktop.
Save ajfisher/dec40f484ffcf6f1fb4c to your computer and use it in GitHub Desktop.
Beginning version colour sensor module support
var five = require("johnny-five");
var colour_sensor = require("./coloursensor.js")
var board = five.Board();
var led;
board.on('ready', function() {
var cs = new colour_sensor({
address: 0x29,
freq: 1000,
board: this,
});
led = new five.Led({
pin: 8,
});
cs.on("ready", function() {
console.log("Colour Sensor Ready");
//led.on();
cs.enable();
led.on();
cs.raw_data(function() {
console.log("Got raw data back");
console.log(cs.state);
led.off();
console.log(cs.decimal_rgb);
console.log(cs.rgb);
}.bind(this) );
});
cs.on("enabled", function() {
console.log("Colour sensor enabled");
});
cs.on("error", function(err) {
console.log("error");
});
cs.on("data", function() {
console.log(this.state);
});
});
board.on('error', function(err) {
console.log(err);
process.exit();
});
var Emitter = require("events").EventEmitter,
util = require("util");
// command set
var BASE_ADDRESS = 0x29; // i2c address of the module
var SENSOR_ID = 0x12;
var COMMAND_BIT = 0x80; // General command value
var ATIME = 0x01; // integration time (50ms)
var GAIN_CONTROL = 0x0F; // Srt gain level
var ENABLE = 0x00; // enable reg
var ENABLE_ADC = 0x02; // enable ADC
var ENABLE_POWER_ON = 0x01; // enable power on
var RED_DATA_L = 0x16; // red register
var TCS34725_ID = 0x44; // ID of the module type
var INTEGRATION_TIME = 0xEB; // 50ms
var GAIN_TYPES = {
G_1X: 0x00,
G_4X: 0x01,
G_16X: 0x02,
G_60X: 0x03
};
var GAIN = GAIN_TYPES.G_1X; // 4x gain default
/** DO THE INTEGRATION TIME STUFF HERE and gain set them up as objects to
define the sizes.
typedef enum
{
TCS34725_INTEGRATIONTIME_2_4MS = 0xFF, //< 2.4ms - 1 cycle - Max Count: 1024 /
TCS34725_INTEGRATIONTIME_24MS = 0xF6, //< 24ms - 10 cycles - Max Count: 10240 /
TCS34725_INTEGRATIONTIME_50MS = 0xEB, //< 50ms - 20 cycles - Max Count: 20480 /
TCS34725_INTEGRATIONTIME_101MS = 0xD5, //< 101ms - 42 cycles - Max Count: 43008 /
TCS34725_INTEGRATIONTIME_154MS = 0xC0, //< 154ms - 64 cycles - Max Count: 65535 /
TCS34725_INTEGRATIONTIME_700MS = 0x00 //< 700ms - 256 cycles - Max Count: 65535 /
}
**/
var priv = new Map();
function ColourSensor(opts) {
if (!(this instanceof ColourSensor)) {
return new ColourSensor(opts);
}
this.addr = opts && opts.addr || 0x29;
this.freq = opts.freq || 100;
this.board = opts.board || null;
// FIXME Need to look at the below and the right way to set it.
this.state = {
initialised: false,
raw: {r: 0, g: 0, b: 0, c: 0, },
};
this.board.io.setMaxListeners(100);
this.board.io.i2cConfig();
// check configuration
// Send config request and then wait for the data to come back.
this.board.io.i2cWrite(this.addr, COMMAND_BIT | SENSOR_ID);
this.board.io.i2cReadOnce(this.addr, 1, function(data) {
var err = null;
// test to make sure the right module is working and send back
// status if it is or isn't
if (data[0] == TCS34725_ID) {
this.state.initialised = true;
this.emit("initialised", err);
} else {
err = { message: "Wrong module type", };
this.emit("error", err);
}
}.bind(this) );
this.on("initialised", function() {
// once module is initialised, set the integration time and the gain
console.log("Initialised");
this.set_integration_time(opts.integration_time || INTEGRATION_TIME, function() {
this.set_gain(opts.gain || GAIN, function() {
// we should be ready to enable and start reading the module
this.emit("configured", null);
}.bind(this) );
}.bind(this) );
});
this.on("configured", function() {
// module is configured, we now need to enable the colour detector for use
console.log("Configured");
this.emit("ready");
});
Object.defineProperty(this, "decimal_rgb", {
// used for converting to 0..1 RGB values
get: function() {
// returns {r, g, b} as decimal ranges 0..1
var rgb = {};
var c = this.state.raw.c;
rgb.r = this.state.raw.r / c;
rgb.g = this.state.raw.g / c;
rgb.b = this.state.raw.b / c;
return (rgb);
},
});
Object.defineProperty(this, "rgb", {
// convert to values 0..255 RGB values
get: function() {
// returns {r, g, b} as integers ranges 0..255
var rgb = {};
var dec_rgb = this.decimal_rgb;
rgb.r = Math.floor(dec_rgb.r * 256);
rgb.g = Math.floor(dec_rgb.g * 256);
rgb.b = Math.floor(dec_rgb.b * 256);
return(rgb);
},
});
}
util.inherits(ColourSensor, Emitter);
ColourSensor.prototype.raw_data = function(cb) {
// gets the raw data from the sensor
// set the state.
this.state.raw = {c: 0, r: 0, g: 0, b: 0};
// read the data off the colour registerss in one big slab and then
// split it up into each channel
this.board.io.i2cWrite(this.addr, COMMAND_BIT | 0x14);
this.board.io.i2cReadOnce(this.addr, 8, function(data) {
console.log("GOT DATA BACK");
console.log(data);
// data comes in low first for each channel
this.state.raw.c = (data[1] << 8) | data[0];
this.state.raw.r = (data[3] << 8) | data[2];
this.state.raw.g = (data[5] << 8) | data[4];
this.state.raw.b = (data[7] << 8) | data[6];
// call the callback
if (typeof(cb) === "function") {
setTimeout(function() {
cb();
}.bind(this), this.integration_time);
} else {
return;
}
}.bind(this));
};
ColourSensor.prototype.set_integration_time = function(time, cb) {
// sets the specific integration time and then calls the cb provided.
var err = null;
if (this.state.initialised) {
// write the integration time to the module
console.log("Setting Integration Time");
this.integration_time = time;
this.board.io.i2cWrite(this.addr, COMMAND_BIT | ATIME);
this.board.io.i2cWrite(this.addr, time & 0xFF);
//now call the callback if needed.
if (typeof(cb) === "function") {
cb();
} else {
return;
}
} else {
err = { message: "Set integration time - module not initialised", };
this.emit("error", err);
}
};
ColourSensor.prototype.set_gain = function(gain, cb) {
// sets the appropriate gain for the module
var err = null;
if (this.state.initialised) {
// write the integration time to the module
console.log("Setting gain");
this.gain = gain;
this.board.io.i2cWrite(this.addr, COMMAND_BIT | GAIN_CONTROL);
this.board.io.i2cWrite(this.addr, gain & 0xFF);
//now call the callback
if (typeof(cb) === "function") {
cb();
} else {
return;
}
} else {
err = { message: "Set gain - module not initialised", };
this.emit("error", err);
}
};
ColourSensor.prototype.enable = function() {
// turn on the colour sensor
var err = null;
if (this.state.initialised) {
console.log("Enabling module");
console.log("Enable Part 1");
this.board.io.i2cWrite(this.addr, COMMAND_BIT | ENABLE);
this.board.io.i2cWrite(this.addr, ENABLE_POWER_ON & 0xFF);
// have to wait 3ms before sending second message
setTimeout(function() {
console.log("Part 2");
this.board.io.i2cWrite(this.addr, COMMAND_BIT | ENABLE);
this.board.io.i2cWrite(this.addr, (ENABLE_POWER_ON | ENABLE_ADC) & 0xFF);
this.emit("enabled");
}.bind(this), 3);
} else {
err = { message: "Enable - module not initialised", };
this.emit("error", err);
}
};
ColourSensor.prototype.disable = function() {
// turn off the colour sensor
//
};
module.exports = ColourSensor;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment