MCPcopy Create free account
hub / github.com/RobTillaart/Arduino / read

Method read

libraries/GY521/GY521.cpp:85–192  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

83
84
85int16_t GY521::read()
86{
87 uint32_t now = millis();
88 if (_throttle)
89 {
90 if ((now - _lastTime) < _throttleTime)
91 {
92 // not an error.
93 return GY521_THROTTLED;
94 }
95 }
96 _lastTime = now;
97
98 // Connected ?
99 _wire->beginTransmission(_address);
100 _wire->write(GY521_ACCEL_XOUT_H);
101 if (_wire->endTransmission() != 0)
102 {
103 _error = GY521_ERROR_WRITE;
104 return _error;
105 }
106
107 // Get the data
108 int8_t n = _wire->requestFrom(_address, (uint8_t)14);
109 if (n != 14)
110 {
111 _error = GY521_ERROR_READ;
112 return _error;
113 }
114 // ACCELEROMETER
115 _ax = _WireRead2(); // ACCEL_XOUT_H ACCEL_XOUT_L
116 _ay = _WireRead2(); // ACCEL_YOUT_H ACCEL_YOUT_L
117 _az = _WireRead2(); // ACCEL_ZOUT_H ACCEL_ZOUT_L
118 // TEMPERATURE
119 _temperature = _WireRead2(); // TEMP_OUT_H TEMP_OUT_L
120 // GYROSCOPE
121 _gx = _WireRead2(); // GYRO_XOUT_H GYRO_XOUT_L
122 _gy = _WireRead2(); // GYRO_YOUT_H GYRO_YOUT_L
123 _gz = _WireRead2(); // GYRO_ZOUT_H GYRO_ZOUT_L
124
125 // duration interval
126 now = micros();
127 float duration = (now - _lastMicros) * 1e-6; // duration in seconds.
128 _lastMicros = now;
129
130
131 // next lines might be merged per axis.
132
133 // Convert raw acceleration to g's
134 _ax *= _raw2g;
135 _ay *= _raw2g;
136 _az *= _raw2g;
137
138 // Error correct raw acceleration (in g) measurements // #18 kudos to Merkxic
139 _ax += axe;
140 _ay += aye;
141 _az += aze;
142

Callers 2

getRegisterMethod · 0.45
_WireRead2Method · 0.45

Calls 2

sqrtFunction · 0.85
writeMethod · 0.45

Tested by

no test coverage detected