Skip to content

Commit 1fc14c1

Browse files
authored
Merge pull request #9 from RobTillaart/teensy
fix #6 teensy math
2 parents f9459ed + 52ee614 commit 1fc14c1

File tree

5 files changed

+35
-23
lines changed

5 files changed

+35
-23
lines changed

GY521.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: GY521.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.4
4+
// VERSION: 0.1.5
55
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
66
// URL: https://github.com/RobTillaart/GY521
77
//
@@ -10,7 +10,8 @@
1010
// 0.1.1 2020-07-09 refactor + initial release
1111
// 0.1.2 2020-08-06 fix setAccelSensitivity + add getters
1212
// 0.1.3 2020-08-07 fix ESP support + pitch roll yaw demo
13-
// 0.1.4 2020-09-29 fix #5
13+
// 0.1.4 2020-09-29 fix #5 missing ;
14+
// 0.1.5 2020-09-29 fix #6 fix math for Teensy
1415

1516

1617
#include "GY521.h"
@@ -67,7 +68,7 @@ bool GY521::wakeup()
6768
return Wire.endTransmission() == 0;
6869
}
6970

70-
int GY521::read()
71+
int16_t GY521::read()
7172
{
7273
if (_throttle)
7374
{
@@ -80,20 +81,21 @@ int GY521::read()
8081
// Connected ?
8182
Wire.beginTransmission(_address);
8283
Wire.write(GY521_ACCEL_XOUT_H);
83-
if (Wire.endTransmission() != 0) return GY521_ERROR_READ;
84+
if (Wire.endTransmission() != 0) return GY521_ERROR_WRITE;
8485

8586
// Get the data
86-
Wire.requestFrom(_address, (uint8_t)14);
87+
int8_t n = Wire.requestFrom(_address, (uint8_t)14);
88+
if (n != 14) return GY521_ERROR_READ;
8789
// ACCELEROMETER
88-
_ax = ( ( ((int)Wire.read()) << 8) | Wire.read() ); // ACCEL_XOUT_H ACCEL_XOUT_L
89-
_ay = ( ( ((int)Wire.read()) << 8) | Wire.read() ); // ACCEL_YOUT_H ACCEL_YOUT_L
90-
_az = ( ( ((int)Wire.read()) << 8) | Wire.read() ); // ACCEL_ZOUT_H ACCEL_ZOUT_L
90+
_ax = _WireRead2(); // ACCEL_XOUT_H ACCEL_XOUT_L
91+
_ay = _WireRead2(); // ACCEL_YOUT_H ACCEL_YOUT_L
92+
_az = _WireRead2(); // ACCEL_ZOUT_H ACCEL_ZOUT_L
9193
// TEMPERATURE
92-
_temperature = ( ((int)Wire.read()) << 8) | Wire.read(); // TEMP_OUT_H TEMP_OUT_L
94+
_temperature = _WireRead2(); // TEMP_OUT_H TEMP_OUT_L
9395
// GYROSCOPE
94-
_gx = (( ((int)Wire.read()) << 8) | Wire.read()); // GYRO_XOUT_H GYRO_XOUT_L
95-
_gy = (( ((int)Wire.read()) << 8) | Wire.read()); // GYRO_YOUT_H GYRO_YOUT_L
96-
_gz = (( ((int)Wire.read()) << 8) | Wire.read()); // GYRO_ZOUT_H GYRO_ZOUT_L
96+
_gx = _WireRead2(); // GYRO_XOUT_H GYRO_XOUT_L
97+
_gy = _WireRead2(); // GYRO_YOUT_H GYRO_YOUT_L
98+
_gz = _WireRead2(); // GYRO_ZOUT_H GYRO_ZOUT_L
9799

98100
// time interval
99101
uint32_t now = millis();
@@ -200,9 +202,19 @@ uint8_t GY521::getRegister(uint8_t reg)
200202
Wire.beginTransmission(_address);
201203
Wire.write(reg);
202204
if (Wire.endTransmission() != 0) return GY521_ERROR_WRITE;
203-
Wire.requestFrom(_address, (uint8_t) 1);
205+
uint8_t n = Wire.requestFrom(_address, (uint8_t) 1);
206+
if (n != 1) return GY521_ERROR_READ;
204207
uint8_t val = Wire.read();
205208
return val;
206209
}
207210

208-
// END OF FILE
211+
// to read register of 2 bytes.
212+
int16_t GY521::_WireRead2()
213+
{
214+
int16_t tmp = Wire.read();
215+
tmp <<= 8;
216+
tmp |= Wire.read();
217+
return tmp;
218+
}
219+
220+
// -- END OF FILE --

GY521.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: GY521.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.4
5+
// VERSION: 0.1.5
66
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
77
// URL: https://github.com/RobTillaart/GY521
88
//
@@ -13,7 +13,7 @@
1313
#include "Arduino.h"
1414
#include "Wire.h"
1515

16-
#define GY521_LIB_VERSION (F("0.1.4"))
16+
#define GY521_LIB_VERSION (F("0.1.5"))
1717

1818
#ifndef GY521_THROTTLE_TIME
1919
#define GY521_THROTTLE_TIME 10 // milliseconds
@@ -44,7 +44,7 @@ class GY521
4444
// 0..65535 millis == roughly 1 minute.
4545
void setThrottleTime(uint16_t ti ) { _throttleTime = ti; };
4646
uint16_t getThrottleTime() { return _throttleTime; };
47-
int read();
47+
int16_t read();
4848

4949
// as = 0,1,2,3 ==> 2g 4g 8g 16g
5050
void setAccelSensitivity(uint8_t as);
@@ -97,7 +97,9 @@ class GY521
9797
float _pitch, _roll, _yaw; // used by user
9898

9999
float _temperature = 0;
100+
101+
// to read register of 2 bytes.
102+
int16_t _WireRead2();
100103
};
101104

102-
103-
// END OF FILE
105+
// -- END OF FILE --

keywords.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Syntax Coloring Map for GY521
22

3-
43
# Datatypes (KEYWORD1)
54
GY521 KEYWORD1
65

7-
86
# Methods and Functions (KEYWORD2)
97
wakeup KEYWORD2
108
read KEYWORD2

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/GY521.git"
1717
},
18-
"version":"0.1.4",
18+
"version":"0.1.5",
1919
"frameworks": "arduino",
2020
"platforms": "*"
2121
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=GY521
2-
version=0.1.4
2+
version=0.1.5
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library for GY521 angle measurement

0 commit comments

Comments
 (0)