Skip to content

Commit dff7293

Browse files
Merge pull request TheThingsNetwork#4 from TheThingsNetwork/fix/add-comments-getacceleration
Add comments for getAcceleration()
2 parents d756542 + b217bae commit dff7293

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/TheThingsNode.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,9 +818,17 @@ uint8_t TheThingsNode::readMotion(unsigned char REG_ADDRESS)
818818

819819
void TheThingsNode::getAcceleration(float *x, float *y, float *z)
820820
{
821+
// Resource: https://github.com/sparkfun/MMA8452_Accelerometer/blob/master/Libraries/Arduino/src/SparkFun_MMA8452Q.cpp
822+
// Read the acceleration from registers 1 through 6 of the MMA8452 accelerometer.
823+
// 2 registers per axis, 12 bits per axis.
824+
// Bit-shifting right does sign extension to preserve negative numbers.
821825
*x = ((short)(readMotion(1)<<8 | readMotion(2))) >> 4;
822826
*y = ((short)(readMotion(3)<<8 | readMotion(4))) >> 4;
823827
*z = ((short)(readMotion(5)<<8 | readMotion(6))) >> 4;
828+
829+
// Scale 12 bit signed values to units of g. The default measurement range is ±2g.
830+
// That is 11 bits for positive values and 11 bits for negative values.
831+
// value = (value / (2^11)) * 2
824832
*x = (float)*x / (float)(1<<11) * (float)(2);
825833
*y = (float)*y / (float)(1<<11) * (float)(2);
826834
*z = (float)*z / (float)(1<<11) * (float)(2);

0 commit comments

Comments
 (0)