-
-
Save rwaldron/5f54854fc998b5227e71 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var five = require('../lib/johnny-five.js'), | |
board, button; | |
board = new five.Board(); | |
board.on("ready", function() { | |
this.firmata.sendI2CConfig(); | |
var LSM303_CTRL_REG1_A = 0x20 | |
, LSM303_CTRL_REG2_A = 0x21 | |
, LSM303_CTRL_REG3_A = 0x22 | |
, LSM303_CTRL_REG4_A = 0x23 | |
, LSM303_CTRL_REG5_A = 0x24 | |
, LSM303_CTRL_REG6_A = 0x25 // DLHC only | |
, LSM303_HP_FILTER_RESET_A = 0x25 // DLH, DLM only | |
, LSM303_REFERENCE_A = 0x26 | |
, LSM303_STATUS_REG_A = 0x27 | |
, LSM303_OUT_X_L_A = 0x28 | |
, LSM303_OUT_X_H_A = 0x29 | |
, LSM303_OUT_Y_L_A = 0x2A | |
, LSM303_OUT_Y_H_A = 0x2B | |
, LSM303_OUT_Z_L_A = 0x2C | |
, LSM303_OUT_Z_H_A = 0x2D | |
, LSM303_FIFO_CTRL_REG_A = 0x2E // DLHC only | |
, LSM303_FIFO_SRC_REG_A = 0x2F // DLHC only | |
, LSM303_INT1_CFG_A = 0x30 | |
, LSM303_INT1_SRC_A = 0x31 | |
, LSM303_INT1_THS_A = 0x32 | |
, LSM303_INT1_DURATION_A = 0x33 | |
, LSM303_INT2_CFG_A = 0x34 | |
, LSM303_INT2_SRC_A = 0x35 | |
, LSM303_INT2_THS_A = 0x36 | |
, LSM303_INT2_DURATION_A = 0x37 | |
, LSM303_CLICK_CFG_A = 0x38 // DLHC only | |
, LSM303_CLICK_SRC_A = 0x39 // DLHC only | |
, LSM303_CLICK_THS_A = 0x3A // DLHC only | |
, LSM303_TIME_LIMIT_A = 0x3B // DLHC only | |
, LSM303_TIME_LATENCY_A = 0x3C // DLHC only | |
, LSM303_TIME_WINDOW_A = 0x3D // DLHC only | |
, LSM303_CRA_REG_M = 0x00 | |
, LSM303_CRB_REG_M = 0x01 | |
, LSM303_MR_REG_M = 0x02 | |
, LSM303_OUT_X_H_M = 0x03 | |
, LSM303_OUT_X_L_M = 0x04 | |
, LSM303_OUT_Y_H_M = -1 // The addresses of the Y and Z magnetometer output registers | |
, LSM303_OUT_Y_L_M = -2 // are reversed on the DLM and DLHC relative to the DLH. | |
, LSM303_OUT_Z_H_M = -3 // These four defines have dummy values so the library can | |
, LSM303_OUT_Z_L_M = -4 // determine the correct address based on the device type. | |
, LSM303_SR_REG_M = 0x09 | |
, LSM303_IRA_REG_M = 0x0A | |
, LSM303_IRB_REG_M = 0x0B | |
, LSM303_IRC_REG_M = 0x0C | |
, LSM303_WHO_AM_I_M = 0x0F // DLM only | |
, LSM303_TEMP_OUT_H_M = 0x31 // DLHC only | |
, LSM303_TEMP_OUT_L_M = 0x32 // DLHC only | |
, LSM303DLH_OUT_Y_H_M = 0x05 | |
, LSM303DLH_OUT_Y_L_M = 0x06 | |
, LSM303DLH_OUT_Z_H_M = 0x07 | |
, LSM303DLH_OUT_Z_L_M = 0x08 | |
, LSM303DLM_OUT_Z_H_M = 0x05 | |
, LSM303DLM_OUT_Z_L_M = 0x06 | |
, LSM303DLM_OUT_Y_H_M = 0x07 | |
, LSM303DLM_OUT_Y_L_M = 0x08 | |
, LSM303DLHC_OUT_Z_H_M = 0x05 | |
, LSM303DLHC_OUT_Z_L_M = 0x06 | |
, LSM303DLHC_OUT_Y_H_M = 0x07 | |
, LSM303DLHC_OUT_Y_L_M = 0x08; | |
var MAG_ADDRESS = 0x3C | |
, ACC_ADDRESS_SA0_A_LOW = 0x30 | |
, ACC_ADDRESS_SA0_A_HIGH = 0x32; | |
// chip: | |
// LSM303DLHC; | |
function LSM303() { | |
this.acc_address = ACC_ADDRESS_SA0_A_HIGH; | |
} | |
LSM303.prototype.enableDefault = function() { | |
var self = this; | |
console.log('enableDefault'); | |
self.writeAccReg(LSM303_CTRL_REG1_A, 0x27); | |
self.writeAccReg(LSM303_CTRL_REG4_A, 0x08); | |
self.writeMagReg(LSM303_MR_REG_M, 0x00); | |
}; | |
LSM303.prototype.writeMagReg = function(reg, value) { | |
console.log('writeMagReg' + reg + value); | |
board.firmata.sendI2CWriteRequest(MAG_ADDRESS, [reg, value]); | |
}; | |
LSM303.prototype.readMagReg = function() { | |
var self = this; | |
console.log('readMag'); | |
board.firmata.sendI2CWriteRequest(MAG_ADDRESS, [LSM303_OUT_X_H_M]); | |
board.firmata.sendI2CReadRequest(MAG_ADDRESS, 6, function(data) { | |
console.log('DATA: '); | |
console.dir(data); | |
}); | |
}; | |
LSM303.prototype.writeAccReg = function(reg, value) { | |
var self = this; | |
console.log('writeAccReg' + reg + value); | |
board.firmata.sendI2CWriteRequest(self.acc_address, [reg, value]); | |
}; | |
LSM303.prototype.readAccReg = function() { | |
var self = this; | |
console.log('readAccReg'); | |
//board.firmata.sendI2CWriteRequest(self.acc_address, [LSM303_OUT_X_L_A]); | |
board.firmata.sendI2CWriteRequest(self.acc_address, [(1 << 7)]); | |
//board.firmata.sendI2CReadRequest(self.acc_address, 6, function(data) { | |
// console.log(data); | |
//}); | |
board.firmata.sendI2CReadRequest(self.acc_address, 6, self.data); | |
}; | |
LSM303.prototype.read = function() { | |
this.readMagReg(); | |
this.readAccReg(); | |
}; | |
LSM303.prototype.data = function(data) { | |
console.log('DATA: '); | |
console.log(data); | |
}; | |
var mag = new LSM303(); | |
mag.enableDefault(); | |
mag.read(); | |
setInterval(mag.read.bind(mag), 2000); | |
board.repl.inject({ | |
mag: mag | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment