···11+The Accelerometer Module project is copyright 2014
22+Technical Machine, Inc.
33+44+Licensed under the Apache License, Version 2.0
55+<LICENSE-APACHE or
66+http://www.apache.org/licenses/LICENSE-2.0> or the MIT
77+license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
88+at your option. All files in the project carrying such
99+notice may not be copied, modified, or distributed except
1010+according to those terms.
···11+Apache License
22+ Version 2.0, January 2004
33+ http://www.apache.org/licenses/
44+55+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
66+77+1. Definitions.
88+99+ "License" shall mean the terms and conditions for use, reproduction,
1010+ and distribution as defined by Sections 1 through 9 of this document.
1111+1212+ "Licensor" shall mean the copyright owner or entity authorized by
1313+ the copyright owner that is granting the License.
1414+1515+ "Legal Entity" shall mean the union of the acting entity and all
1616+ other entities that control, are controlled by, or are under common
1717+ control with that entity. For the purposes of this definition,
1818+ "control" means (i) the power, direct or indirect, to cause the
1919+ direction or management of such entity, whether by contract or
2020+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
2121+ outstanding shares, or (iii) beneficial ownership of such entity.
2222+2323+ "You" (or "Your") shall mean an individual or Legal Entity
2424+ exercising permissions granted by this License.
2525+2626+ "Source" form shall mean the preferred form for making modifications,
2727+ including but not limited to software source code, documentation
2828+ source, and configuration files.
2929+3030+ "Object" form shall mean any form resulting from mechanical
3131+ transformation or translation of a Source form, including but
3232+ not limited to compiled object code, generated documentation,
3333+ and conversions to other media types.
3434+3535+ "Work" shall mean the work of authorship, whether in Source or
3636+ Object form, made available under the License, as indicated by a
3737+ copyright notice that is included in or attached to the work
3838+ (an example is provided in the Appendix below).
3939+4040+ "Derivative Works" shall mean any work, whether in Source or Object
4141+ form, that is based on (or derived from) the Work and for which the
4242+ editorial revisions, annotations, elaborations, or other modifications
4343+ represent, as a whole, an original work of authorship. For the purposes
4444+ of this License, Derivative Works shall not include works that remain
4545+ separable from, or merely link (or bind by name) to the interfaces of,
4646+ the Work and Derivative Works thereof.
4747+4848+ "Contribution" shall mean any work of authorship, including
4949+ the original version of the Work and any modifications or additions
5050+ to that Work or Derivative Works thereof, that is intentionally
5151+ submitted to Licensor for inclusion in the Work by the copyright owner
5252+ or by an individual or Legal Entity authorized to submit on behalf of
5353+ the copyright owner. For the purposes of this definition, "submitted"
5454+ means any form of electronic, verbal, or written communication sent
5555+ to the Licensor or its representatives, including but not limited to
5656+ communication on electronic mailing lists, source code control systems,
5757+ and issue tracking systems that are managed by, or on behalf of, the
5858+ Licensor for the purpose of discussing and improving the Work, but
5959+ excluding communication that is conspicuously marked or otherwise
6060+ designated in writing by the copyright owner as "Not a Contribution."
6161+6262+ "Contributor" shall mean Licensor and any individual or Legal Entity
6363+ on behalf of whom a Contribution has been received by Licensor and
6464+ subsequently incorporated within the Work.
6565+6666+2. Grant of Copyright License. Subject to the terms and conditions of
6767+ this License, each Contributor hereby grants to You a perpetual,
6868+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
6969+ copyright license to reproduce, prepare Derivative Works of,
7070+ publicly display, publicly perform, sublicense, and distribute the
7171+ Work and such Derivative Works in Source or Object form.
7272+7373+3. Grant of Patent License. Subject to the terms and conditions of
7474+ this License, each Contributor hereby grants to You a perpetual,
7575+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
7676+ (except as stated in this section) patent license to make, have made,
7777+ use, offer to sell, sell, import, and otherwise transfer the Work,
7878+ where such license applies only to those patent claims licensable
7979+ by such Contributor that are necessarily infringed by their
8080+ Contribution(s) alone or by combination of their Contribution(s)
8181+ with the Work to which such Contribution(s) was submitted. If You
8282+ institute patent litigation against any entity (including a
8383+ cross-claim or counterclaim in a lawsuit) alleging that the Work
8484+ or a Contribution incorporated within the Work constitutes direct
8585+ or contributory patent infringement, then any patent licenses
8686+ granted to You under this License for that Work shall terminate
8787+ as of the date such litigation is filed.
8888+8989+4. Redistribution. You may reproduce and distribute copies of the
9090+ Work or Derivative Works thereof in any medium, with or without
9191+ modifications, and in Source or Object form, provided that You
9292+ meet the following conditions:
9393+9494+ (a) You must give any other recipients of the Work or
9595+ Derivative Works a copy of this License; and
9696+9797+ (b) You must cause any modified files to carry prominent notices
9898+ stating that You changed the files; and
9999+100100+ (c) You must retain, in the Source form of any Derivative Works
101101+ that You distribute, all copyright, patent, trademark, and
102102+ attribution notices from the Source form of the Work,
103103+ excluding those notices that do not pertain to any part of
104104+ the Derivative Works; and
105105+106106+ (d) If the Work includes a "NOTICE" text file as part of its
107107+ distribution, then any Derivative Works that You distribute must
108108+ include a readable copy of the attribution notices contained
109109+ within such NOTICE file, excluding those notices that do not
110110+ pertain to any part of the Derivative Works, in at least one
111111+ of the following places: within a NOTICE text file distributed
112112+ as part of the Derivative Works; within the Source form or
113113+ documentation, if provided along with the Derivative Works; or,
114114+ within a display generated by the Derivative Works, if and
115115+ wherever such third-party notices normally appear. The contents
116116+ of the NOTICE file are for informational purposes only and
117117+ do not modify the License. You may add Your own attribution
118118+ notices within Derivative Works that You distribute, alongside
119119+ or as an addendum to the NOTICE text from the Work, provided
120120+ that such additional attribution notices cannot be construed
121121+ as modifying the License.
122122+123123+ You may add Your own copyright statement to Your modifications and
124124+ may provide additional or different license terms and conditions
125125+ for use, reproduction, or distribution of Your modifications, or
126126+ for any such Derivative Works as a whole, provided Your use,
127127+ reproduction, and distribution of the Work otherwise complies with
128128+ the conditions stated in this License.
129129+130130+5. Submission of Contributions. Unless You explicitly state otherwise,
131131+ any Contribution intentionally submitted for inclusion in the Work
132132+ by You to the Licensor shall be under the terms and conditions of
133133+ this License, without any additional terms or conditions.
134134+ Notwithstanding the above, nothing herein shall supersede or modify
135135+ the terms of any separate license agreement you may have executed
136136+ with Licensor regarding such Contributions.
137137+138138+6. Trademarks. This License does not grant permission to use the trade
139139+ names, trademarks, service marks, or product names of the Licensor,
140140+ except as required for reasonable and customary use in describing the
141141+ origin of the Work and reproducing the content of the NOTICE file.
142142+143143+7. Disclaimer of Warranty. Unless required by applicable law or
144144+ agreed to in writing, Licensor provides the Work (and each
145145+ Contributor provides its Contributions) on an "AS IS" BASIS,
146146+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147147+ implied, including, without limitation, any warranties or conditions
148148+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149149+ PARTICULAR PURPOSE. You are solely responsible for determining the
150150+ appropriateness of using or redistributing the Work and assume any
151151+ risks associated with Your exercise of permissions under this License.
152152+153153+8. Limitation of Liability. In no event and under no legal theory,
154154+ whether in tort (including negligence), contract, or otherwise,
155155+ unless required by applicable law (such as deliberate and grossly
156156+ negligent acts) or agreed to in writing, shall any Contributor be
157157+ liable to You for damages, including any direct, indirect, special,
158158+ incidental, or consequential damages of any character arising as a
159159+ result of this License or out of the use or inability to use the
160160+ Work (including but not limited to damages for loss of goodwill,
161161+ work stoppage, computer failure or malfunction, or any and all
162162+ other commercial damages or losses), even if such Contributor
163163+ has been advised of the possibility of such damages.
164164+165165+9. Accepting Warranty or Additional Liability. While redistributing
166166+ the Work or Derivative Works thereof, You may choose to offer,
167167+ and charge a fee for, acceptance of support, warranty, indemnity,
168168+ or other liability obligations and/or rights consistent with this
169169+ License. However, in accepting such obligations, You may act only
170170+ on Your own behalf and on Your sole responsibility, not on behalf
171171+ of any other Contributor, and only if You agree to indemnify,
172172+ defend, and hold each Contributor harmless for any liability
173173+ incurred by, or claims asserted against, such Contributor by reason
174174+ of your accepting any such warranty or additional liability.
175175+176176+END OF TERMS AND CONDITIONS
177177+178178+APPENDIX: How to apply the Apache License to your work.
179179+180180+ To apply the Apache License to your work, attach the following
181181+ boilerplate notice, with the fields enclosed by brackets "[]"
182182+ replaced with your own identifying information. (Don't include
183183+ the brackets!) The text should be enclosed in the appropriate
184184+ comment syntax for the file format. We also recommend that a
185185+ file or class name and description of purpose be included on the
186186+ same "printed page" as the copyright notice for easier
187187+ identification within third-party archives.
188188+189189+Copyright [yyyy] [name of copyright owner]
190190+191191+Licensed under the Apache License, Version 2.0 (the "License");
192192+you may not use this file except in compliance with the License.
193193+You may obtain a copy of the License at
194194+195195+ http://www.apache.org/licenses/LICENSE-2.0
196196+197197+Unless required by applicable law or agreed to in writing, software
198198+distributed under the License is distributed on an "AS IS" BASIS,
199199+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200200+See the License for the specific language governing permissions and
201201+limitations under the License.
···11+Copyright (c) 2013-2014 Technical Machine, Inc
22+33+Permission is hereby granted, free of charge, to any
44+person obtaining a copy of this software and associated
55+documentation files (the "Software"), to deal in the
66+Software without restriction, including without
77+limitation the rights to use, copy, modify, merge,
88+publish, distribute, sublicense, and/or sell copies of
99+the Software, and to permit persons to whom the Software
1010+is furnished to do so, subject to the following
1111+conditions:
1212+1313+The above copyright notice and this permission notice
1414+shall be included in all copies or substantial portions
1515+of the Software.
1616+1717+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
1818+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
1919+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
2020+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
2121+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
2222+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
2323+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
2424+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2525+DEALINGS IN THE SOFTWARE.
···11+#Accelerometer
22+Driver for the accel-mma84 Tessel accelerometer module ([MMA8452Q](http://www.freescale.com/files/sensors/doc/data_sheet/MMA8452Q.pdf)).
33+44+##Installation
55+```sh
66+npm install accel-mma84
77+```
88+99+##Example
1010+```js
1111+/*********************************************
1212+This basic accelerometer example logs a stream
1313+of x, y, and z data from the accelerometer
1414+*********************************************/
1515+1616+var tessel = require('tessel');
1717+var accel = require('accel-mma84').use(tessel.port['A']);
1818+1919+// Initialize the accelerometer.
2020+accel.on('ready', function () {
2121+ // Stream accelerometer data
2222+ accel.on('data', function (xyz) {
2323+ console.log("x:", xyz[0].toFixed(2),
2424+ "y:", xyz[1].toFixed(2),
2525+ "z:", xyz[2].toFixed(2));
2626+ });
2727+});
2828+2929+accel.on('error', function(err) {
3030+ console.log('error connecting', err);
3131+});
3232+3333+setInterval(function(){}, 20000);
3434+```
3535+3636+##Methods
3737+3838+##### * `accel.availableOutputRates()` Logs the available interrupt rates in Hz.
3939+4040+##### * `accel.availableScaleRanges()` Logs the available accelerometer ranges (in units of Gs).
4141+4242+##### * `accel.enableDataInterrupts(trueOrFalse, callback(err))` Enables or disables data interrupts. Set the first param truthy to enable, falsy to disable.
4343+4444+##### * `accel.getAcceleration(callback(err, xyz))` Gets the acceleration from the device, outputs as array [x, y, z].
4545+4646+##### * `accel.setOutputRate(rateInHz, callback(err))` Sets the output rate of the data (1.56-800 Hz).
4747+4848+##### * `accel.setScaleRange(scaleRange, callback(err))` Sets the accelerometer to read up to 2, 4, or 8 Gs of acceleration (smaller range = better precision).
4949+5050+##Events
5151+5252+##### * `accel.on('data', callback(xyz))` Emitted when data is available. `xyz` is an array in the form of [x, y, z].
5353+5454+##### * `accel.on('error', callback(err))` Emitted upon error.
5555+5656+##### * `accel.on('ready', callback())` Emitted upon first successful communication between the Tessel and the module.
5757+5858+##Further Examples
5959+See the examples folder for code.
6060+6161+* show-axes: Manipulate LEDs based on acceleration in the three axes.
6262+6363+* change-rates: Change the polling rate.
6464+6565+## License
6666+6767+MIT
6868+APACHE
···11+// Any copyright is dedicated to the Public Domain.
22+// http://creativecommons.org/publicdomain/zero/1.0/
33+44+/*********************************************
55+This basic accelerometer example logs a stream
66+of x, y, and z data from the accelerometer
77+*********************************************/
88+99+var tessel = require('tessel');
1010+var accel = require('../').use(tessel.port['A']); // Replace '../' with 'accel-mma84' in your own code
1111+1212+// Initialize the accelerometer.
1313+accel.on('ready', function () {
1414+ // Stream accelerometer data
1515+ accel.on('data', function (xyz) {
1616+ console.log('x:', xyz[0].toFixed(2),
1717+ 'y:', xyz[1].toFixed(2),
1818+ 'z:', xyz[2].toFixed(2));
1919+ });
2020+2121+});
2222+2323+accel.on('error', function(err){
2424+ console.log('Error:', err);
2525+});
···11+// Any copyright is dedicated to the Public Domain.
22+// http://creativecommons.org/publicdomain/zero/1.0/
33+44+/*********************************************
55+This more advanced accelerometer example logs
66+a stream of x, y, and z data, then stops the
77+stream, changes the polling rate, and resumes
88+streaming from the accelerometer
99+*********************************************/
1010+1111+var tessel = require('tessel');
1212+var accel = require('../').use(tessel.port['A']); // Replace '../' with 'accel-mma84' in your own code
1313+1414+// Initialize the accelerometer.
1515+accel.on('ready', function () {
1616+ // Stream accelerometer data
1717+ accel.on('data', function(xyz) {
1818+ console.log('x:', xyz[0].toFixed(2),
1919+ 'y:', xyz[1].toFixed(2),
2020+ 'z:', xyz[2].toFixed(2));
2121+ });
2222+ //After two seconds, change stream rate, then stream again
2323+ setTimeout(function () {
2424+ console.log('Changing the output rate...');
2525+ accel.removeAllListeners('data');
2626+ // Setting the output data rate in Hz
2727+ accel.setOutputRate(1.56, function rateSet() {
2828+ accel.on('data', function (xyz) {
2929+ console.log('slower x:', xyz[0].toFixed(2),
3030+ 'slower y:', xyz[1].toFixed(2),
3131+ 'slower z:', xyz[2].toFixed(2));
3232+ });
3333+ });
3434+ }, 2000);
3535+});
3636+3737+accel.on('error', function(err){
3838+ console.log('Error:', err);
3939+});
···11+// Copyright 2014 Technical Machine, Inc. See the COPYRIGHT
22+// file at the top-level directory of this distribution.
33+//
44+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
55+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
66+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
77+// option. This file may not be copied, modified, or distributed
88+// except according to those terms.
99+1010+var util = require('util');
1111+var EventEmitter = require('events').EventEmitter;
1212+var queue = require('sync-queue');
1313+1414+// The SparkFun breakout board defaults to 1, set to 0 if SA0 jumper on the bottom of the board is set
1515+var I2C_ADDRESS = 0x1D; // 0x1D if SA0 is high, 0x1C if low
1616+1717+// See the many application notes for more info on setting all of these registers:
1818+// http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MMA8452Q
1919+// MMA8452 registers
2020+var OUT_X_MSB = 0x01;
2121+var XYZ_DATA_CFG = 0x0E;
2222+var WHO_AM_I = 0x0D;
2323+var CTRL_REG1 = 0x2A;
2424+var CTRL_REG4 = 0x2D;
2525+2626+function Accelerometer (hardware, callback) {
2727+ var self = this;
2828+ // Command Queue
2929+ self.queue = new queue();
3030+ // Port assignment
3131+ self.hardware = hardware;
3232+ // Rate at which data is collected and is ready to be read
3333+ self.outputRate = 12.5;
3434+ // Sets full-scale range to +/-2, 4, or 8g. Used to calc real g values.
3535+ self.scaleRange = 2;
3636+ // Interrupt pin for the data ready event
3737+ self.dataInterrupt = self.hardware.digital[1];
3838+ // Address for i2C
3939+ // TODO: Account for manual address changes?
4040+ self.i2c = hardware.I2C(I2C_ADDRESS);
4141+4242+ // Check that we can read the correct chip id
4343+ self.queue.place(function one() {
4444+ self._getChipID(function IDRead(err, c) {
4545+ if (err) {
4646+ err = new Error("Could not connect to MMA8452Q. No reponse on I2C lines. Error: "+err);
4747+ return self._failProcedure(err);
4848+ }
4949+ // should always return 0x2A
5050+ if (c !== 0x2A) {
5151+ // This is the wrong chip
5252+ err = new Error("Could not connect to MMA8452Q, received " + c.toString() + ". Expected 0x2A.");
5353+ // Fail the init
5454+ return self._failProcedure(err);
5555+ }
5656+5757+ // Set the scale range to standard
5858+ self.setScaleRange(self.scaleRange, function(err) {
5959+ if (err) {
6060+ return self._failProcedure(err, callback);
6161+ }
6262+ else {
6363+ // Set the output rate to standard
6464+ self.setOutputRate(self.outputRate, function(err) {
6565+ if (err) {
6666+ return self._failProcedure(err, callback);
6767+ }
6868+ else {
6969+ // Emit the ready event
7070+ setImmediate(function emitReady() {
7171+ self.emit('ready');
7272+ self.queue.next();
7373+ });
7474+ // Call the callback with object
7575+ if (callback) callback(null, self);
7676+7777+ return;
7878+ }
7979+ });
8080+ }
8181+ });
8282+8383+ // Set up an interrupt handler for data ready
8484+ self.dataInterrupt.once('low', self._dataReady.bind(self));
8585+ });
8686+ });
8787+8888+ self.on('newListener', function(event) {
8989+ // If we have a new sample listener
9090+ if (event == 'data' || event == 'sample') {
9191+ // Enable interrupts at whatever rate was previously set.
9292+ self.enableDataInterrupts(true, queueNext);
9393+ }
9494+ });
9595+9696+ self.on('removeListener', function(event) {
9797+ // If we have a new || event == 'sample' listener
9898+ if (event == 'data' || event == 'sample') {
9999+ // Disable interrupt.
100100+ self.enableDataInterrupts(false, queueNext);
101101+ }
102102+ });
103103+104104+ self.queue.next();
105105+}
106106+107107+util.inherits(Accelerometer, EventEmitter);
108108+109109+Accelerometer.prototype._changeRegister = function(change, callback) {
110110+ var self = this;
111111+112112+ // Put the accelerometer into standby
113113+ self._modeStandby(function inStandby(err) {
114114+ if (err) {
115115+ return self._failProcedure(err, callback);
116116+ }
117117+ else {
118118+ // Make whatever change was requested
119119+ change( function setActive(err) {
120120+ if (err) {
121121+ return self._failProcedure(err, callback);
122122+ }
123123+ else {
124124+ // Put the accelerometer back into active mode
125125+ self._modeActive(callback);
126126+ }
127127+ });
128128+ }
129129+ });
130130+};
131131+132132+Accelerometer.prototype._dataReady = function() {
133133+ var self = this;
134134+ // Data is ready so grab the data
135135+ self.getAcceleration(function(err, xyz) {
136136+ // If we had an error, emit it
137137+ if (err) {
138138+ // Emitting error
139139+ self.emit('error', err);
140140+ }
141141+ // If there was no error
142142+ else {
143143+ // Emit the data
144144+ self.emit('data', xyz); // old-style, deprecated
145145+ self.emit('sample', xyz);
146146+ }
147147+148148+ self.dataInterrupt.once('low', self._dataReady.bind(self));
149149+ });
150150+};
151151+152152+Accelerometer.prototype._failProcedure = function(err, callback) {
153153+ var self = this;
154154+155155+ // Emit the error
156156+ setImmediate(function emitErr() {
157157+ self.emit('error', err);
158158+ });
159159+ // Call the callback
160160+ if (callback) callback(err);
161161+162162+ return;
163163+};
164164+165165+// Get the id of the chip
166166+Accelerometer.prototype._getChipID = function(callback) {
167167+ this._readRegister(WHO_AM_I, function (err, c) {
168168+ if (callback) callback(err, c);
169169+ });
170170+};
171171+172172+Accelerometer.prototype._getClosestOutputRate = function(requestedRate, callback) {
173173+174174+ // If a negative number is requested, stop output (0 hz)
175175+ if (requestedRate < 0) requestedRate = 0;
176176+177177+ // If 0 hz is requested, return just that so that output will be stopped
178178+ if (requestedRate === 0) {
179179+180180+ if (callback) callback(null, 0);
181181+182182+ return;
183183+ }
184184+185185+ // Get the available rates
186186+ var available = this.availableOutputRates();
187187+ // Iterate through each
188188+ for (var i = 0; i < available.length; i++) {
189189+ // The first available rate less than or equal to requested is a match
190190+ if (available[i] <= requestedRate) {
191191+ // Send the match back
192192+ if (callback) callback(null, available[i]);
193193+ return;
194194+ }
195195+ }
196196+197197+ // If there were no match, this number must be between 0 and 1.56. Use 1.56
198198+ if (callback) callback(null, available[available.length-1]);
199199+};
200200+201201+// Sets the MMA8452 to active mode. Needs to be in this mode to output data
202202+Accelerometer.prototype._modeActive = function (callback) {
203203+ var self = this;
204204+ // Set the active bit to begin detection
205205+ self._readRegister(CTRL_REG1, function (err, c) {
206206+ if (err) {
207207+ return _failProcedure(err);
208208+ }
209209+ else {
210210+ return self._writeRegister(CTRL_REG1, c | (0x01), callback);
211211+ }
212212+ });
213213+};
214214+215215+// Sets the MMA8452 to standby mode. It must be in standby to change most register settings
216216+Accelerometer.prototype._modeStandby = function (callback) {
217217+ var self = this;
218218+ // Clear the active bit to go into standby
219219+ self._readRegister(CTRL_REG1, function (err, c) {
220220+ if (err) {
221221+ return self._failProcedure(err, callback);
222222+ }
223223+ else {
224224+ return self._writeRegister(CTRL_REG1, c & ~(0x01), callback);
225225+ }
226226+ });
227227+};
228228+229229+Accelerometer.prototype._readRegister = function (addressToRead, callback) {
230230+ this._readRegisters(addressToRead, 1, function (err, regs) {
231231+ callback(err, regs && regs[0]);
232232+ });
233233+};
234234+235235+Accelerometer.prototype._readRegisters = function (addressToRead, bytesToRead, callback) {
236236+ this.i2c.transfer(new Buffer([addressToRead]), bytesToRead, callback);
237237+};
238238+239239+// Write a single byte to the register.
240240+Accelerometer.prototype._writeRegister = function (addressToWrite, dataToWrite, callback) {
241241+ this.i2c.send(new Buffer([addressToWrite, dataToWrite]), callback);
242242+};
243243+244244+// Sets the accelerometer to read up to 2, 4, or 8 Gs of acceleration (smaller range = better precision)
245245+Accelerometer.prototype._unsafeSetScaleRange = function(scaleRange, callback) {
246246+ var self = this;
247247+248248+ var fsr = scaleRange;
249249+ if (fsr > 8) fsr = 8; //Easy error check
250250+ fsr >>= 2; // Neat trick, see page 22. 00 = 2G, 01 = 4G, 10 = 8G
251251+252252+ // Go into standby to edit registers
253253+ self._changeRegister(function change(complete) {
254254+ if (err) {
255255+ return complete(err);
256256+ }
257257+ else {
258258+ // Write the new scale into the register
259259+ self._writeRegister(XYZ_DATA_CFG, fsr, function wroteReg(err) {
260260+ self.scaleRange = scaleRange;
261261+ return complete(err);
262262+ });
263263+ }
264264+ }, function scaleSet(err) {
265265+ if (callback) {
266266+ callback(err);
267267+ }
268268+ setImmediate(self.queue.next);
269269+ });
270270+}
271271+272272+// Sets the output rate of the data (1.56-800 Hz)
273273+Accelerometer.prototype._unsafeSetOutputRate = function (hz, callback) {
274274+ var self = this;
275275+276276+ // Put accel into standby
277277+ self._changeRegister( function setRegisters(finishChange) {
278278+ // Find the closest available rate (rounded down)
279279+ self._getClosestOutputRate(hz, function gotRequested(err, closest) {
280280+ if (err) {
281281+ return finishChange(new Error("Rate must be >= 1.56Hz"));
282282+ }
283283+ else {
284284+ // Set our property
285285+ self.outputRate = closest;
286286+287287+ // Get the binary representation of the rate (for the register)
288288+ var bin = self.availableOutputRates().indexOf(closest);
289289+ // If the binary rep could be found
290290+ if (bin !== -1) {
291291+ // Read the current register value
292292+ self._readRegister(CTRL_REG1, function readComplete(err, regVal) {
293293+ if (err) {
294294+ return finishChange(err);
295295+ }
296296+ else {
297297+ // Clear the three bits of output rate control (0b11000111 = 199)
298298+ regVal &= 199;
299299+ // Move the binary rep into place (bits 3:5)
300300+ if (bin !== 0) regVal |= (bin << 3);
301301+ // Write that value into the control register
302302+ self._writeRegister(CTRL_REG1, regVal, finishChange);
303303+ }
304304+ });
305305+ }
306306+ else {
307307+ return finishChange(new Error("Invalid output rate."));
308308+ }
309309+ }
310310+ })
311311+ },
312312+ function rateSet(err) {
313313+ if (callback) {
314314+ callback(err);
315315+ }
316316+ setImmediate(self.queue.next);
317317+ });
318318+};
319319+320320+321321+// Logs the available interrupt rates in Hz
322322+Accelerometer.prototype.availableOutputRates = function() {
323323+ return [800, 400, 200, 100, 50, 12.5, 6.25, 1.56];
324324+};
325325+326326+// Logs the available accelerometer ranges (in units of Gs)
327327+Accelerometer.prototype.availableScaleRanges = function() {
328328+ // The higher the range, the less accurate the readings are
329329+ return [2, 4, 8];
330330+};
331331+332332+// Enables or disables data interrupts. Set the first param truthy to enable, false to disable.
333333+Accelerometer.prototype.enableDataInterrupts = function(enable, callback) {
334334+ var self = this;
335335+336336+ // Don't call unnecessarily.
337337+ if (this._dataInterrupts == !!enable) {
338338+ return callback && callback();
339339+ }
340340+ this._dataInterrupts = !!enable;
341341+342342+ self.queue.place(function queueEnable() {
343343+ // We're going to change register 4
344344+ self._changeRegister(function change(complete) {
345345+ // Read the register first
346346+ self._readRegister(CTRL_REG4, function(err, reg4) {
347347+ if (err) {
348348+ return complete(err);
349349+ }
350350+ else {
351351+ // If we are enabling, set first bit to 1, else 0
352352+ var regVal = (enable ? (reg4 |= 1) : (reg4 &= ~1));
353353+ // Write to the register
354354+ self._writeRegister(CTRL_REG4, regVal, function(err) {
355355+ return complete(err);
356356+ });
357357+ }
358358+ });
359359+ }, function intSet(err) {
360360+ if (callback) {
361361+ callback(err);
362362+ }
363363+ setImmediate(self.queue.next);
364364+ });
365365+ });
366366+};
367367+368368+// Gets the acceleration from the device, outputs as array [x, y, z]
369369+Accelerometer.prototype.getAcceleration = function (callback) {
370370+ var self = this;
371371+372372+ self.queue.place( function readAccel() {
373373+ self._readRegisters(OUT_X_MSB, 6, function (err, rawData) {
374374+ if (err) throw err;
375375+ // Loop to calculate 12-bit ADC and g value for each axis
376376+ var out = [];
377377+ for (var i = 0; i < 3 ; i++) {
378378+ var gCount = (rawData[i*2] << 8) | rawData[(i*2)+1]; // Combine the two 8 bit registers into one 12-bit number
379379+380380+ gCount = (gCount >> 4); // The registers are left align, here we right align the 12-bit integer
381381+382382+ // If the number is negative, we have to make it so manually (no 12-bit data type)
383383+ if (rawData[i*2] > 0x7F) {
384384+ gCount = -(1 + 0xFFF - gCount); // Transform into negative 2's complement
385385+ }
386386+387387+ out[i] = gCount / ((1<<12)/(2*self.scaleRange));
388388+ }
389389+390390+ callback(null, out);
391391+392392+ setImmediate(self.queue.next);
393393+ });
394394+ });
395395+};
396396+397397+// Queueing version of Accelerometer#_unsafeSetOutputRate
398398+Accelerometer.prototype.setOutputRate = function (hz, callback) {
399399+ this.queue.place(this._unsafeSetOutputRate.bind(this, hz, callback));
400400+};
401401+402402+// Queueing version of Accelerometer#_unsafeSetScaleRange
403403+Accelerometer.prototype.setScaleRange = function(scaleRange, callback) {
404404+ this.queue.place(this._unsafeSetScaleRange.bind(this, scaleRange, callback));
405405+};
406406+407407+function use (hardware, callback) {
408408+ return new Accelerometer(hardware, callback);
409409+}
410410+411411+exports.Accelerometer = Accelerometer;
412412+exports.use = use;
···11+Copyright (c) 2014, Technical Machine, Inc.
22+All rights reserved.
33+44+Redistribution and use in source and binary forms, with or without
55+modification, are permitted provided that the following conditions are met:
66+77+* Redistributions of source code must retain the above copyright notice, this
88+ list of conditions and the following disclaimer.
99+1010+* Redistributions in binary form must reproduce the above copyright notice,
1111+ this list of conditions and the following disclaimer in the documentation
1212+ and/or other materials provided with the distribution.
1313+1414+* The name Michael Bostock may not be used to endorse or promote products
1515+ derived from this software without specific prior written permission.
1616+1717+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1818+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1919+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2020+DISCLAIMED. IN NO EVENT SHALL MICHAEL BOSTOCK BE LIABLE FOR ANY DIRECT,
2121+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2222+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2323+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
2424+OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2525+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
2626+EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
···11+#Sync-Queue
22+33+## Install
44+`npm install sync-queue`
55+66+## Usage
77+88+```.js
99+var Queue = require('sync-queue')
1010+var queue = new Queue();
1111+1212+queue.place(function one() {
1313+ console.log('I'm func one);
1414+1515+ setTimeout(function() {
1616+ console.log("and I'm still finishing up...");
1717+ queue.next();
1818+ }, 1000);
1919+})
2020+2121+queue.place(function two() {
2222+ console.log.bind(this, "I'm the last func.");
2323+ queue.next();
2424+});
2525+```
2626+2727+## Description
2828+Use `place` to put things in the queue. They will start being executed automatically. Call `next` when you want the thing you put in the queue is finished.
···11+function queue() {
22+33+ // Create an empty array of commands
44+ var queue = [];
55+ // We're inactive to begin with
66+ queue.active = false;
77+ // Method for adding command chain to the queue
88+ queue.place = function (command) {
99+ // Push the command onto the command array
1010+ queue.push(command);
1111+ // If we're currently inactive, start processing
1212+ if (!queue.active) queue.next();
1313+ };
1414+ // Method for calling the next command chain in the array
1515+ queue.next = function () {
1616+ // If this is the end of the queue
1717+ if (!queue.length) {
1818+ // We're no longer active
1919+ queue.active = false;
2020+ // Stop execution
2121+ return;
2222+ }
2323+ // Grab the next command
2424+ var command = queue.shift();
2525+ // We're active
2626+ queue.active = true;
2727+ // Call the command
2828+ command();
2929+ };
3030+3131+ return queue;
3232+}
3333+3434+module.exports = queue;