Skip to content

Commit

Permalink
EV3: Proximity
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Sep 5, 2015
1 parent db24f28 commit ef5c3da
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 88 deletions.
14 changes: 14 additions & 0 deletions eg/proximity-EV3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var five = require("../lib/johnny-five.js");
var board = new five.Board();

board.on("ready", function() {
var proximity = new five.Proximity({
controller: "EV3",
pin: "BBS1"
});

proximity.on("change", function() {
console.log("The obstruction has moved.");
console.log(this.cm + "cm", this.in + "in");
});
});
8 changes: 1 addition & 7 deletions lib/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,8 @@ var Controllers = {
state.shield = EV3.shieldPort(opts.pin);
state.register = EV3.Touch;

state.ev3 = new EV3(
Object.assign({}, opts, {
io: this.io
})
);

state.ev3 = new EV3(Object.assign(opts, { io: this.io }));
state.ev3.setup(state.shield, EV3.Type_EV3_TOUCH);

state.ev3.read(state.shield, EV3.Touch, EV3.Touch_Bytes, function(data) {
var value = data[0];
// Since i2cRead is continuous regardless of the reading,
Expand Down
11 changes: 8 additions & 3 deletions lib/ev3.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Bank.prototype.write = function(register, bytes) {
this.io.i2cWrite(this.address, register, bytes);
};

// http://www.nr.edu/csc200/labs-ev3/ev3-user-guide-EN.pdf

function EV3(options) {
if (shared) {
return shared;
Expand Down Expand Up @@ -77,7 +79,6 @@ EV3.shieldPort = function(pin) {
};
};


EV3.prototype = Object.create(Emitter.prototype, {
constructor: {
value: EV3
Expand Down Expand Up @@ -159,15 +160,19 @@ EV3.S2_OFFSET = 52;
/*
* Sensor Read Registers
*/
EV3.Touch = 0x83;
EV3.Bump = 0x84;
EV3.Proximity = 0x83;
EV3.Touch = 0x83;




/*
* Sensor Read Byte Counts
*/
EV3.Touch_Bytes = 1;
EV3.Bump_Bytes = 1;
EV3.Proximity_Bytes = 2;
EV3.Touch_Bytes = 1;


/*
Expand Down
46 changes: 35 additions & 11 deletions lib/proximity.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
var Board = require("../lib/board.js"),
within = require("./mixins/within"),
__ = require("./fn"),
events = require("events"),
util = require("util");
var Board = require("../lib/board");
var EV3 = require("../lib/ev3");
var within = require("./mixins/within");
var __ = require("./fn");
var Emitter = require("events").EventEmitter;
var util = require("util");
var priv = new Map();

function analogHandler(opts, dataHandler) {

this.io.pinMode(this.pin, this.io.MODES.ANALOG);

this.io.analogRead(this.pin, function(data) {
dataHandler.call(this, data);
}.bind(this));

}

var Controllers = {
Expand Down Expand Up @@ -220,7 +219,29 @@ var Controllers = {
return raw;
}
}
}
},
EV3: {
initialize: {
value: function(opts, dataHandler) {
var state = priv.get(this);

state.shield = EV3.shieldPort(opts.pin);

state.ev3 = new EV3(Object.assign(opts, { io: this.io }));
state.ev3.setup(state.shield, EV3.Type_EV3);
state.ev3.read(state.shield, EV3.Proximity, EV3.Proximity_Bytes, function(data) {
var value = data[0] | (data[1] << 8);

dataHandler(value);
});
}
},
toCm: {
value: function(raw) {
return raw;
}
}
},
};

// Sensor aliases
Expand Down Expand Up @@ -282,7 +303,8 @@ function Proximity(opts) {
return new Proximity(opts);
}

var controller;
var controller = null;
var state = {};
var raw = 0;
var freq = opts.freq || 25;
var last = 0;
Expand All @@ -305,6 +327,8 @@ function Proximity(opts) {
};
}

priv.set(this, state);

Object.defineProperties(this, {
/**
* [read-only] Calculated centimeter value
Expand Down Expand Up @@ -374,7 +398,7 @@ Proximity.Controllers = [
"0A02", "GP2Y0A02YK0F",
];

util.inherits(Proximity, events.EventEmitter);
util.inherits(Proximity, Emitter);

__.mixin(Proximity.prototype, within);

Expand Down
3 changes: 1 addition & 2 deletions test/button.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
var MockFirmata = require("./util/mock-firmata");
var sinon = require("sinon");
var MockFirmata = require("./util/mock-firmata");
var EV3 = require("../lib/ev3");
var five = require("../lib/johnny-five");
var Button = five.Button;
var Board = five.Board;


function newBoard() {
var io = new MockFirmata();
var board = new Board({
Expand Down
Loading

0 comments on commit ef5c3da

Please sign in to comment.