Last active
March 6, 2017 03:09
-
-
Save shunkino/176c4abc9f11a546c435 to your computer and use it in GitHub Desktop.
ADXL345 driver for chirimen.
This file contains 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
function changeToSignedInt(number) { | |
if (number >> 15) { | |
ret = 65536 - number; | |
return ret * -1; | |
} else { | |
return number; | |
} | |
} | |
var adxl345 = { | |
init:function initAccelerometer(port, address) { | |
port.setDeviceAddress(address); | |
return new Promise(function(resolve, reject) { | |
// Select range. | |
port.write8(0x31, 0x03); | |
// Set measure mode. | |
port.write8(0x2d, 0x08); | |
}); | |
}, | |
// read acceleration value. | |
read:function readAcceleration(port, address) { | |
port.setDeviceAddress(address); | |
return new Promise(function(resolve, reject) { | |
Promise.all([ | |
port.read8(0x32, true), | |
port.read8(0x33, true), | |
port.read8(0x34, true), | |
port.read8(0x35, true), | |
port.read8(0x36, true), | |
port.read8(0x37, true) | |
]).then(function(v) { | |
console.log(v); | |
var x = changeToSignedInt(((v[1] << 8) | v[0])); | |
var y = changeToSignedInt(((v[3] << 8) | v[2])); | |
var z = changeToSignedInt(((v[5] << 8) | v[4])); | |
console.log("x:" + x + " y:" + y + " z:" + z); | |
var temp = [x, y, z]; | |
resolve(temp); | |
}, function() { | |
console.log("error"); | |
reject(); | |
}); | |
}); | |
}, | |
// Write a value to address | |
write:function writeAcceleration(port, devAddress, regAddress, value) { | |
port.setDeviceAddress(devAddress); | |
return new Promise(function(resolve, reject) { | |
Promise.all([ | |
port.write8(regAddress, value); | |
]).then(function(v) { | |
console.log("writing " + value + " to " + regAddress + " success"); | |
resolve(value); | |
}, function() { | |
console.log("error"); | |
reject(); | |
}); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment