Skip to content

Commit 071f22b

Browse files
committed
add _readRaw()
1 parent e30d950 commit 071f22b

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

99
## [0.6.1] - 2024-07-25
10-
- explicit initialization of gax, gay and gaz
10+
- improve initialization of gax, gay and gaz
11+
- add **_readRaw()** to improve calibrate()
1112

1213

1314
## [0.6.0] - 2024-06-22

GY521.cpp

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void GY521::calibrate(uint16_t times)
8888
// summarize (6x) the measurements.
8989
for (uint16_t i = 0; i < times; i++)
9090
{
91-
read();
91+
_readRaw();
9292
_axe -= getAccelX();
9393
_aye -= getAccelY();
9494
_aze -= getAccelZ();
@@ -133,33 +133,12 @@ int16_t GY521::read()
133133
}
134134
_lastTime = now;
135135

136-
// Connected ?
137-
_wire->beginTransmission(_address);
138-
_wire->write(GY521_ACCEL_XOUT_H);
139-
if (_wire->endTransmission() != 0)
136+
int16_t rv = _readRaw();
137+
if (rv != GY521_OK)
140138
{
141-
_error = GY521_ERROR_WRITE;
142-
return _error;
139+
return rv;
143140
}
144141

145-
// Get the data
146-
int8_t n = _wire->requestFrom(_address, (uint8_t)14);
147-
if (n != 14)
148-
{
149-
_error = GY521_ERROR_READ;
150-
return _error;
151-
}
152-
// ACCELEROMETER
153-
_ax = _WireRead2(); // ACCEL_XOUT_H ACCEL_XOUT_L
154-
_ay = _WireRead2(); // ACCEL_YOUT_H ACCEL_YOUT_L
155-
_az = _WireRead2(); // ACCEL_ZOUT_H ACCEL_ZOUT_L
156-
// TEMPERATURE
157-
_temperature = _WireRead2(); // TEMP_OUT_H TEMP_OUT_L
158-
// GYROSCOPE
159-
_gx = _WireRead2(); // GYRO_XOUT_H GYRO_XOUT_L
160-
_gy = _WireRead2(); // GYRO_YOUT_H GYRO_YOUT_L
161-
_gz = _WireRead2(); // GYRO_ZOUT_H GYRO_ZOUT_L
162-
163142
// duration interval
164143
now = micros();
165144
float duration = (now - _lastMicros) * 1e-6; // duration in seconds.
@@ -545,6 +524,43 @@ uint8_t GY521::getRegister(uint8_t reg)
545524
}
546525

547526

527+
///////////////////////////////////////////////////////////////////
528+
//
529+
// PRIVATE
530+
//
531+
int16_t GY521::_readRaw()
532+
{
533+
// Connected ?
534+
_wire->beginTransmission(_address);
535+
_wire->write(GY521_ACCEL_XOUT_H);
536+
if (_wire->endTransmission() != 0)
537+
{
538+
_error = GY521_ERROR_WRITE;
539+
return _error;
540+
}
541+
542+
// Get the data
543+
int8_t n = _wire->requestFrom(_address, (uint8_t)14);
544+
if (n != 14)
545+
{
546+
_error = GY521_ERROR_READ;
547+
return _error;
548+
}
549+
// ACCELEROMETER
550+
_ax = _WireRead2(); // ACCEL_XOUT_H ACCEL_XOUT_L
551+
_ay = _WireRead2(); // ACCEL_YOUT_H ACCEL_YOUT_L
552+
_az = _WireRead2(); // ACCEL_ZOUT_H ACCEL_ZOUT_L
553+
// TEMPERATURE
554+
_temperature = _WireRead2(); // TEMP_OUT_H TEMP_OUT_L
555+
// GYROSCOPE
556+
_gx = _WireRead2(); // GYRO_XOUT_H GYRO_XOUT_L
557+
_gy = _WireRead2(); // GYRO_YOUT_H GYRO_YOUT_L
558+
_gz = _WireRead2(); // GYRO_ZOUT_H GYRO_ZOUT_L
559+
560+
return GY521_OK;
561+
}
562+
563+
548564
// to read register of 2 bytes.
549565
int16_t GY521::_WireRead2()
550566
{

GY521.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ class GY521
151151

152152
bool _normalize = true; // default true.
153153

154+
int16_t _readRaw();
154155
// to read register of 2 bytes.
155156
int16_t _WireRead2();
156157

0 commit comments

Comments
 (0)