From f7645353af4503387455e5ba3d8cb085e348cb58 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Tue, 5 Dec 2023 17:57:43 +0100 Subject: [PATCH] refactor API, begin() (#45) - refactor API, begin() - update readme.md --- CHANGELOG.md | 7 ++++++- GY521.cpp | 16 +--------------- GY521.h | 4 ++-- README.md | 10 +++++++++- examples/GY521_angle/GY521_angle.ino | 1 - examples/GY521_performance/GY521_performance.ino | 3 +-- .../GY521_pitch_roll_yaw.ino | 9 ++++----- .../GY521_readCalibration_1.ino | 11 +++++------ .../GY521_readCalibration_2.ino | 1 - examples/GY521_test_1/GY521_test_1.ino | 1 - examples/GY521_test_2/GY521_test_2.ino | 1 - library.json | 2 +- library.properties | 2 +- test/unit_test_001.cpp | 10 ++++++++++ 14 files changed, 40 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b75f8c..94aec13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,11 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.5.0] - 2023-12-05 +- refactor API, begin() +- update readme.md + +---- + ## [0.4.1] - 2023-11-02 - update readme.md - minor edits - ## [0.4.0] - 2023-06-11 - fix #42 roll + pitch "jumps" after full rotation. - fixed normalization code. diff --git a/GY521.cpp b/GY521.cpp index bc361ec..28f6e9d 100644 --- a/GY521.cpp +++ b/GY521.cpp @@ -1,7 +1,7 @@ // // FILE: GY521.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.4.1 +// VERSION: 0.5.0 // PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor // URL: https://github.com/RobTillaart/GY521 @@ -29,22 +29,8 @@ GY521::GY521(uint8_t address, TwoWire *wire) } -#if defined (ESP8266) || defined(ESP32) -bool GY521::begin(uint8_t sda, uint8_t scl) -{ - _wire->begin(sda, scl); - if (isConnected()) - { - return wakeup(); - } - return false; -} -#endif - - bool GY521::begin() { - _wire->begin(); if (isConnected()) { return wakeup(); diff --git a/GY521.h b/GY521.h index 1a442c6..b3a4c15 100644 --- a/GY521.h +++ b/GY521.h @@ -2,7 +2,7 @@ // // FILE: GY521.h // AUTHOR: Rob Tillaart -// VERSION: 0.4.1 +// VERSION: 0.5.0 // PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor // URL: https://github.com/RobTillaart/GY521 @@ -11,7 +11,7 @@ #include "Wire.h" -#define GY521_LIB_VERSION (F("0.4.1")) +#define GY521_LIB_VERSION (F("0.5.0")) // THROTTLE TIMING diff --git a/README.md b/README.md index 2a94725..29de0e6 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,14 @@ It needs to be tested a lot more. See changelog.md for latest updates. +#### 0.5.0 Breaking change + +Version 0.5.0 introduced a breaking change. +You cannot set the pins in **begin()** any more. +This reduces the dependency of processor dependent Wire implementations. +The user has to call **Wire.begin()** and can optionally set the Wire pins +before calling **begin()**. + #### Examples @@ -75,8 +83,8 @@ AD0 connected to VCC => 0x69 - **GY521(uint8_t address = 0x69, , TwoWire \*wire = &Wire)** Constructor with default address. 0x68 is also a valid address. The wire argument is optional to select Wire1 Wire2 etc. on some boards. -- **bool begin(uint8_t sda, uint8_t scl)** begin for ESP32 et al. Returns true if address can be found on I2C bus. - **bool begin()** Returns true if address can be found on I2C bus. +Note call **Wire.begin()** before **begin()**. - **bool isConnected()** returns true if device can be found on I2C bus. - **void reset()** set all internal values to 0 and throttle time to 10 ms. - **bool wakeUp()** idem. diff --git a/examples/GY521_angle/GY521_angle.ino b/examples/GY521_angle/GY521_angle.ino index 34b1daf..e6121dc 100644 --- a/examples/GY521_angle/GY521_angle.ino +++ b/examples/GY521_angle/GY521_angle.ino @@ -2,7 +2,6 @@ // FILE: GY521_angle.ino // AUTHOR: Rob Tillaart // PURPOSE: read angleX, angleY, angleZ -// DATE: 2022-06-06 #include "GY521.h" diff --git a/examples/GY521_performance/GY521_performance.ino b/examples/GY521_performance/GY521_performance.ino index 99665bc..6988652 100644 --- a/examples/GY521_performance/GY521_performance.ino +++ b/examples/GY521_performance/GY521_performance.ino @@ -2,7 +2,6 @@ // FILE: GY521_performance.ino // AUTHOR: Rob Tillaart // PURPOSE: minimal demo -// DATE: 2021-06-13 #include "GY521.h" @@ -47,5 +46,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/GY521_pitch_roll_yaw/GY521_pitch_roll_yaw.ino b/examples/GY521_pitch_roll_yaw/GY521_pitch_roll_yaw.ino index 9dd669b..255593b 100644 --- a/examples/GY521_pitch_roll_yaw/GY521_pitch_roll_yaw.ino +++ b/examples/GY521_pitch_roll_yaw/GY521_pitch_roll_yaw.ino @@ -2,7 +2,6 @@ // FILE: GY521_pitch_roll_yaw.ino // AUTHOR: Rob Tillaart // PURPOSE: demo pitch roll yaw -// DATE: 2020-08-06 #include "GY521.h" @@ -29,13 +28,13 @@ void setup() Serial.println("\tCould not connect to GY521"); delay(1000); } - sensor.setAccelSensitivity(2); // 8g - sensor.setGyroSensitivity(1); // 500 degrees/s + sensor.setAccelSensitivity(2); // 8g + sensor.setGyroSensitivity(1); // 500 degrees/s sensor.setThrottle(); Serial.println("start..."); - // set calibration values from calibration sketch. + // set calibration values from calibration sketch. sensor.axe = 0.574; sensor.aye = -0.002; sensor.aze = -1.043; @@ -70,5 +69,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/GY521_readCalibration_1/GY521_readCalibration_1.ino b/examples/GY521_readCalibration_1/GY521_readCalibration_1.ino index 86d8d3e..e5ab4ec 100644 --- a/examples/GY521_readCalibration_1/GY521_readCalibration_1.ino +++ b/examples/GY521_readCalibration_1/GY521_readCalibration_1.ino @@ -2,7 +2,6 @@ // FILE: GY521_readCalibration_1.ino // AUTHOR: Rob Tillaart // PURPOSE: read the calibration values / errors for a flat sensor. -// DATE: 2020-07-14 #include "GY521.h" @@ -31,11 +30,11 @@ void setup() Serial.println("Could not connect to GY521"); } // adjust when needed. - sensor.setAccelSensitivity(0); // 2g - sensor.setGyroSensitivity(0); // 250 degrees/s + sensor.setAccelSensitivity(0); // 2g + sensor.setGyroSensitivity(0); // 250 degrees/s sensor.setThrottle(false); - // set all calibration errors to zero + // set all calibration errors to zero sensor.axe = 0; sensor.aye = 0; sensor.aze = 0; @@ -100,7 +99,7 @@ void loop() Serial.print(t * 0.05, 2); Serial.println(); - // adjust calibration errors so table should get all zero's. + // adjust calibration errors so table should get all zero's. sensor.axe += ax * 0.05; sensor.aye += ay * 0.05; sensor.aze += az * 0.05; @@ -113,5 +112,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/GY521_readCalibration_2/GY521_readCalibration_2.ino b/examples/GY521_readCalibration_2/GY521_readCalibration_2.ino index 7c44487..a4694de 100644 --- a/examples/GY521_readCalibration_2/GY521_readCalibration_2.ino +++ b/examples/GY521_readCalibration_2/GY521_readCalibration_2.ino @@ -2,7 +2,6 @@ // FILE: readCalibration_2.ino // AUTHOR: Rob Tillaart // PURPOSE: read the calibration values / errors for a flat sensor. -// DATE: 2022-06-07 #include "GY521.h" diff --git a/examples/GY521_test_1/GY521_test_1.ino b/examples/GY521_test_1/GY521_test_1.ino index 7bfd68e..4d44ce7 100644 --- a/examples/GY521_test_1/GY521_test_1.ino +++ b/examples/GY521_test_1/GY521_test_1.ino @@ -2,7 +2,6 @@ // FILE: GY521_test_1.ino // AUTHOR: Rob Tillaart // PURPOSE: minimal demo to test working of the sensor. -// DATE: 2020-07-01 #include "GY521.h" diff --git a/examples/GY521_test_2/GY521_test_2.ino b/examples/GY521_test_2/GY521_test_2.ino index 52ceca9..e575831 100644 --- a/examples/GY521_test_2/GY521_test_2.ino +++ b/examples/GY521_test_2/GY521_test_2.ino @@ -2,7 +2,6 @@ // FILE: GY521_test_2.ino // AUTHOR: Rob Tillaart // PURPOSE: test set/get functions -// DATE: 2020-08-06 #include "GY521.h" diff --git a/library.json b/library.json index 5861414..62811cd 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/GY521.git" }, - "version": "0.4.1", + "version": "0.5.0", "license": "MIT", "frameworks": "*", "platforms": "*", diff --git a/library.properties b/library.properties index a5afeb7..b5801f4 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=GY521 -version=0.4.1 +version=0.5.0 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for GY521 angle measurement diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp index 3a80402..4a51110 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -44,6 +44,7 @@ unittest(test_constructor) { GY521 sensor(0x69); + Wire.begin(); sensor.begin(); assertEqual(GY521_OK, sensor.getError()); @@ -54,6 +55,8 @@ unittest(test_constructor) unittest(test_get_set_throttle) { GY521 sensor(0x69); + + Wire.begin(); sensor.begin(); assertEqual(GY521_OK, sensor.getError()); @@ -76,6 +79,8 @@ unittest(test_get_set_throttle) unittest(test_get_set_sensitivity) { GY521 sensor(0x69); + + Wire.begin(); sensor.begin(); fprintf(stderr, "setAccelSensitivity() - fails \n"); @@ -116,6 +121,8 @@ unittest(test_initial_values) { GY521 sensor(0x69); + Wire.begin(); + assertEqualFloat(0, sensor.getAccelX(), 0.0001); assertEqualFloat(0, sensor.getAccelY(), 0.0001); assertEqualFloat(0, sensor.getAccelZ(), 0.0001); @@ -143,6 +150,8 @@ unittest(test_initial_calibration_errors) { GY521 sensor(0x69); + Wire.begin(); + assertEqualFloat(0, sensor.axe, 0.0001); assertEqualFloat(0, sensor.aye, 0.0001); assertEqualFloat(0, sensor.aze, 0.0001); @@ -155,5 +164,6 @@ unittest(test_initial_calibration_errors) unittest_main() + // -- END OF FILE --