From 7692ae8cb86881f63c64ff13fbac94e330cbd65e Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Tue, 5 Dec 2023 20:31:39 +0100 Subject: [PATCH] 0.4.0 HMC6352 --- libraries/hmc6352/CHANGELOG.md | 7 ++++- libraries/hmc6352/README.md | 26 ++++++++++++++--- .../continuousDemo/continuousDemo.ino | 15 +++++----- .../examples/hmc6352Config/hmc6352Config.ino | 24 ++++++++-------- .../hmc6352/examples/queryDemo/queryDemo.ino | 15 +++++----- .../set2continuous/set2continuous.ino | 26 +++++++++-------- .../hmc6352/examples/set2query/set2query.ino | 28 +++++++++++-------- .../examples/set2standby/set2standby.ino | 25 +++++++++-------- .../examples/standbyDemo/standbyDemo.ino | 25 +++++++++-------- libraries/hmc6352/hmc6352.cpp | 20 ++----------- libraries/hmc6352/hmc6352.h | 10 ++----- libraries/hmc6352/library.json | 2 +- libraries/hmc6352/library.properties | 2 +- libraries/hmc6352/test/unit_test_001.cpp | 14 ++++++++-- 14 files changed, 135 insertions(+), 104 deletions(-) diff --git a/libraries/hmc6352/CHANGELOG.md b/libraries/hmc6352/CHANGELOG.md index 4af659c7..612b11be 100644 --- a/libraries/hmc6352/CHANGELOG.md +++ b/libraries/hmc6352/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.4.0] - 2023-12-05 +- refactor API, begin() +- update readme.md + +---- + ## [0.3.3] - 2023-11-04 - update readme.md - minor edits - ## [0.3.2] - 2022-11-09 - add changelog.md - add rp2040 to build-CI diff --git a/libraries/hmc6352/README.md b/libraries/hmc6352/README.md index a0e2a946..cac0b795 100644 --- a/libraries/hmc6352/README.md +++ b/libraries/hmc6352/README.md @@ -26,6 +26,23 @@ For switching operational mode one must reboot the device. **WARNING: BETA: WARNING: BETA: WARNING: BETA: WARNING:** +#### 0.4.0 Breaking change + +Version 0.4.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()**. + + +#### Related + +- https://github.com/RobTillaart/Angle +- https://github.com/RobTillaart/AngleConvertor +- https://github.com/RobTillaart/AverageAngle +- https://github.com/RobTillaart/AS5600 + + ## Interface ```cpp @@ -35,9 +52,10 @@ For switching operational mode one must reboot the device. ### Constructor - **hmc6352(uint8_t address, TwoWire \*wire = &Wire)** set the I2C address and optional the Wire interface. -- **bool begin(uint8_t sda, uint8_t scl)** for ESP32 ea. Returns true if address can be seen on the I2C bus. -- **bool begin()** for UNO, Returns true if address can be seen on the I2C bus. -- **bool isConnected()** Returns true if address can be seen on the I2C bus. Can be used as 1st order diagnostics. +- **bool begin()** initializes library. +Returns true if address can be seen on the I2C bus. +- **bool isConnected()** Returns true if address can be seen on the I2C bus. +Can be used as first order diagnostics. ### Base calls standby mode @@ -45,7 +63,7 @@ For switching operational mode one must reboot the device. - **int getHeading()** is a combination of **askHeading()** and **readHeading()** - **int askHeading()** requests a new read of the heading (compass direction). - **int readHeading()** reads the new value from the device. -- **int wakeUp()** if the deivice is in sleep mode, wake it up. +- **int wakeUp()** if the device is in sleep mode, wake it up. - **int sleep()** puts the device in sleep mode. diff --git a/libraries/hmc6352/examples/continuousDemo/continuousDemo.ino b/libraries/hmc6352/examples/continuousDemo/continuousDemo.ino index 7d0ac33b..707613bc 100644 --- a/libraries/hmc6352/examples/continuousDemo/continuousDemo.ino +++ b/libraries/hmc6352/examples/continuousDemo/continuousDemo.ino @@ -4,9 +4,9 @@ // PURPOSE: demo app HMC6352 library - continuous mode for Arduino -#include +#include "hmc6352.h" -hmc6352 Compass(0x21); // 0x21 <==> 33 <==> 66 >> 1 +hmc6352 Compass(0x21); // 0x21 <==> 33 <==> 66 >> 1 int heading; @@ -15,9 +15,10 @@ void setup() { Serial.begin(115200); Serial.println(__FILE__); - Serial.println("LIB: "); + Serial.print("HMC6352_LIB_VERSION: "); Serial.println(HMC6352_LIB_VERSION); + Wire.begin(); Compass.begin(); Serial.print("Output modus: "); @@ -31,9 +32,9 @@ void setup() void loop() { - // in continuous mode it is not needed to ask for a new reading every time - // as it will do a new reading continuously even when not asked for - // Try making a fast turn and see the difference with the query mode + // in continuous mode it is not needed to ask for a new reading every time + // as it will do a new reading continuously even when not asked for + // Try making a fast turn and see the difference with the query mode heading = Compass.readHeading(); Serial.print("Degree : "); Serial.println(heading); @@ -41,5 +42,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/libraries/hmc6352/examples/hmc6352Config/hmc6352Config.ino b/libraries/hmc6352/examples/hmc6352Config/hmc6352Config.ino index 7b59af95..035d5c85 100644 --- a/libraries/hmc6352/examples/hmc6352Config/hmc6352Config.ino +++ b/libraries/hmc6352/examples/hmc6352Config/hmc6352Config.ino @@ -4,7 +4,7 @@ // PURPOSE: test HMC6352 library for Arduino -#include +#include "hmc6352.h" hmc6352 Compass(33); @@ -13,9 +13,10 @@ void setup() { Serial.begin(115200); Serial.println(__FILE__); - Serial.println("LIB: "); + Serial.print("HMC6352_LIB_VERSION: "); Serial.println(HMC6352_LIB_VERSION); + Wire.begin(); Compass.begin(); dumpEEPROM(); @@ -24,7 +25,7 @@ void setup() void loop() { - // SHOWMENU + // SHOW MENU Serial.println("\n\n\t\tHMC6352 MENU\n"); Serial.println("F : Factory Reset"); Serial.println("E : Dump EEPROM & 2 RAM addresses"); @@ -51,11 +52,11 @@ void loop() Serial.println("Enter your choice ..."); - // WAIT FOR COMMAND + // WAIT FOR COMMAND while (Serial.available() == 0); char cmd = Serial.read(); - // EXECUTE COMMAND + // EXECUTE COMMAND switch (cmd) { case 'F': @@ -83,16 +84,16 @@ void loop() OutPutModusMenu(); break; case '7': - // mode , freq , reset - Compass.setOperationalModus(STANDBY, 1, true); // 10 default val + // mode , freq , reset + Compass.setOperationalModus(STANDBY, 1, true); // 10 default val Serial.println("Reboot Arduino"); break; case '8': - Compass.setOperationalModus(QUERY, 1, true); // 10 default val + Compass.setOperationalModus(QUERY, 1, true); // 10 default val Serial.println("Reboot Arduino"); break; case '9': - Compass.setOperationalModus(CONT, 20, true); // 10 default val + Compass.setOperationalModus(CONT, 20, true); // 10 default val Serial.println("Reboot Arduino"); break; case 'U': @@ -134,7 +135,8 @@ void OutPutModusMenu() Serial.println("2 Raw Magnetometer Y"); Serial.println("3 Magnetometer X"); Serial.println("4 Magnetometer Y"); - // WAIT FOR PARAM + + // WAIT FOR PARAM while (Serial.available() == 0); char cmd = Serial.read(); cmd -= '0'; // make a digit out of it @@ -245,5 +247,5 @@ void setI2Caddress() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/libraries/hmc6352/examples/queryDemo/queryDemo.ino b/libraries/hmc6352/examples/queryDemo/queryDemo.ino index 0aee9abc..dee6712a 100644 --- a/libraries/hmc6352/examples/queryDemo/queryDemo.ino +++ b/libraries/hmc6352/examples/queryDemo/queryDemo.ino @@ -4,7 +4,7 @@ // PURPOSE: demo HMC6352 library - query mode for Arduino -#include +#include "hmc6352.h" hmc6352 Compass(0x21); @@ -15,9 +15,10 @@ void setup() { Serial.begin(115200); Serial.println(__FILE__); - Serial.println("LIB: "); + Serial.print("HMC6352_LIB_VERSION: "); Serial.println(HMC6352_LIB_VERSION); + Wire.begin(); Compass.begin(); Serial.print("Output modus: "); @@ -31,10 +32,10 @@ void setup() void loop() { - // in query mode it is not needed to ask for a new reading every time - // as it will do a new reading directly after the chip is read - // and waits until it is asked for. - // Try making a fast turn and smile ... + // in query mode it is not needed to ask for a new reading every time + // as it will do a new reading directly after the chip is read + // and waits until it is asked for. + // Try making a fast turn and smile ... heading = Compass.readHeading(); Serial.print("Degree : "); Serial.println(heading); @@ -42,5 +43,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/libraries/hmc6352/examples/set2continuous/set2continuous.ino b/libraries/hmc6352/examples/set2continuous/set2continuous.ino index b39a467e..9e48e4e6 100644 --- a/libraries/hmc6352/examples/set2continuous/set2continuous.ino +++ b/libraries/hmc6352/examples/set2continuous/set2continuous.ino @@ -4,7 +4,7 @@ // PURPOSE: Reset HMC6352 to continuous mode without library -#include +#include "hmc6352.h" int HMC = 0x21; int ledPin = 13; @@ -14,36 +14,40 @@ boolean ledState = LOW; void setup() { Serial.begin(115200); + Serial.println(__FILE__); + Serial.print("HMC6352_LIB_VERSION: "); + Serial.println(HMC6352_LIB_VERSION); + + Wire.begin(); + Serial.print("HMC6352 : start - "); Serial.println(HMC, HEX); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, ledState); - Wire.begin(); - - delay(1000); // give all things time to start + delay(1000); // give all things time to start } void loop() { - // flashing means not blocking + // flashing means not blocking ledState = !ledState; digitalWrite(ledPin, ledState); - // write continuous mode to RAM 0x74 and do an L command immediately after it - // pull out 5 volt if it reads back 0x72 + // write continuous mode to RAM 0x74 and do an L command immediately after it + // pull out 5 volt if it reads back 0x72 Wire.beginTransmission(HMC); Wire.write('G'); Wire.write(0x74); - Wire.write(0x72); // 20 Hz | Reset = True | CONT + Wire.write(0x72); // 20 Hz | Reset = True | CONT Wire.write('L'); - // Wire.write('O'); optional reset + // Wire.write('O'); optional reset Wire.endTransmission(); delay(10); - // Read the EEPROM value for feedback as this is the next startup value + // Read the EEPROM value for feedback as this is the next startup value Wire.beginTransmission(HMC); Wire.write('r'); Wire.write(0x07); @@ -64,5 +68,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/libraries/hmc6352/examples/set2query/set2query.ino b/libraries/hmc6352/examples/set2query/set2query.ino index 1bc8d2af..7531ec75 100644 --- a/libraries/hmc6352/examples/set2query/set2query.ino +++ b/libraries/hmc6352/examples/set2query/set2query.ino @@ -4,7 +4,7 @@ // PURPOSE: Reset HMC6352 to query mode without library -#include +#include "hmc6352.h" int HMC = 0x21; int ledPin = 13; @@ -14,37 +14,41 @@ boolean ledState = LOW; void setup() { Serial.begin(115200); + Serial.println(__FILE__); + Serial.print("HMC6352_LIB_VERSION: "); + Serial.println(HMC6352_LIB_VERSION); + + Wire.begin(); + Serial.print("HMC6352 : start - "); Serial.println(HMC, HEX); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, ledState); - Wire.begin(); - - delay(1000); // give all things time to start + delay(1000); // give all things time to start } void loop() { - // flashing means not blocking + // flashing means not blocking ledState = !ledState; digitalWrite(ledPin, ledState); - // write query mode to RAM 0x74 and do an L command immediately after it - // this is done to "break through" the continuous mode if needed - // pull out 5 volt if it reads back 0x50 + // write query mode to RAM 0x74 and do an L command immediately after it + // this is done to "break through" the continuous mode if needed + // pull out 5 volt if it reads back 0x50 Wire.beginTransmission(HMC); Wire.write('G'); Wire.write(0x74); - Wire.write(0x51); // 10 Hz | Reset = True | QUERY + Wire.write(0x51); // 10 Hz | Reset = True | QUERY Wire.write('L'); - // Wire.write('O'); optional reset + // Wire.write('O'); optional reset Wire.endTransmission(); delay(10); - // Read the EEPROM value for feedback as this is the next startup value + // Read the EEPROM value for feedback as this is the next startup value Wire.beginTransmission(HMC); Wire.write('r'); Wire.write(0x07); @@ -65,5 +69,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/libraries/hmc6352/examples/set2standby/set2standby.ino b/libraries/hmc6352/examples/set2standby/set2standby.ino index 0cfd12f6..a014aaf0 100644 --- a/libraries/hmc6352/examples/set2standby/set2standby.ino +++ b/libraries/hmc6352/examples/set2standby/set2standby.ino @@ -5,7 +5,7 @@ // (esp if it is struck in continuous mode - Arduino) -#include +#include "hmc6352.h" int HMC = 0x21; int ledPin = 13; @@ -15,37 +15,40 @@ boolean ledState = LOW; void setup() { Serial.begin(115200); + Serial.println(__FILE__); + Serial.print("HMC6352_LIB_VERSION: "); + Serial.println(HMC6352_LIB_VERSION); + + Wire.begin(); Serial.print("HMC6352 : start - "); Serial.println(HMC, HEX); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, ledState); - Wire.begin(); - - delay(1000); // give all things time to start + delay(1000); // give all things time to start } void loop() { - // flashing means not blocking + // flashing means not blocking ledState = !ledState; digitalWrite(ledPin, ledState); - // write stdby mode to RAM 0x74 and do an L command immediately after it - // this is done to "break through" the continuous mode - // pull out 5 volt if it reads back 0x50 + // write standby mode to RAM 0x74 and do an L command immediately after it + // this is done to "break through" the continuous mode + // pull out 5 volt if it reads back 0x50 Wire.beginTransmission(HMC); Wire.write('G'); Wire.write(0x74); Wire.write(0x50); Wire.write('L'); - // Wire.write('O'); optional reset + // Wire.write('O'); optional reset Wire.endTransmission(); delay(10); - // Read the EEPROM value for feedback as this is the next startup value + // Read the EEPROM value for feedback as this is the next startup value Wire.beginTransmission(HMC); Wire.write('r'); Wire.write(0x07); @@ -66,5 +69,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/libraries/hmc6352/examples/standbyDemo/standbyDemo.ino b/libraries/hmc6352/examples/standbyDemo/standbyDemo.ino index d184cbf3..0cff6da5 100644 --- a/libraries/hmc6352/examples/standbyDemo/standbyDemo.ino +++ b/libraries/hmc6352/examples/standbyDemo/standbyDemo.ino @@ -4,7 +4,7 @@ // PURPOSE: demo app HMC6352 library - standby mode for Arduino -#include +#include "hmc6352.h" hmc6352 Compass(0x21); @@ -15,7 +15,7 @@ void setup() { Serial.begin(115200); Serial.println(__FILE__); - Serial.println("LIB: "); + Serial.print("HMC6352_LIB_VERSION: "); Serial.println(HMC6352_LIB_VERSION); Wire.begin(); @@ -28,13 +28,13 @@ void setup() void loop() { - Compass.wakeUp(); // decent wake up from sleep mode + Compass.wakeUp(); // decent wake up from sleep mode - // Note that reading a heading is requires two steps, ask() & read() - // this makes the query and continuous mode more efficient - // without impact on the footprint of the lib. - // this way one can ask a make a reading and fetch it a bit later. - // TODO is it fast enough for IRQ ? + // Note that reading a heading is requires two steps, ask() & read() + // this makes the query and continuous mode more efficient + // without impact on the footprint of the lib. + // this way one can ask a make a reading and fetch it a bit later. + // TODO is it fast enough for IRQ ? int x = Compass.askHeading(); Serial.print("Ask returns: "); Serial.print(x); @@ -43,11 +43,12 @@ void loop() Serial.print("\task & read : "); Serial.println(heading); - Compass.sleep(); // low energy mode + // go to low energy mode + Compass.sleep(); delay(500); - // this is the simplest mode to use the library - // suitable for 99.9% of all robots :) + // this is the simplest mode to use the library + // suitable for 99.9% of all robots :) Compass.wakeUp(); Serial.print("getHeading : "); Serial.println(Compass.getHeading()); @@ -57,5 +58,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/libraries/hmc6352/hmc6352.cpp b/libraries/hmc6352/hmc6352.cpp index be281ee1..71df9513 100644 --- a/libraries/hmc6352/hmc6352.cpp +++ b/libraries/hmc6352/hmc6352.cpp @@ -1,7 +1,7 @@ // // FILE: hmc6352.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.3.3 +// VERSION: 0.4.0 // PURPOSE: Arduino library for HMC6352 digital compass sensor @@ -43,27 +43,13 @@ hmc6352::hmc6352(uint8_t address, TwoWire *wire) { - _address = constrain(address, 0x10, 0xF6); + _address = address; _wire = wire; } -#if defined (ESP8266) || defined(ESP32) -bool hmc6352::begin(uint8_t sda, uint8_t scl) -{ - _wire->begin(sda, scl); - if (! isConnected()) - { - return false; - } - return true; -} -#endif - - bool hmc6352::begin() { - _wire->begin(); if (! isConnected()) { return false; @@ -132,7 +118,7 @@ int hmc6352::sleep() // values obtained from dump int hmc6352::factoryReset() { - writeRAM(0x74, 0x50); // is needed !! + writeRAM(0x74, 0x50); // is needed !! writeCmd(HMC_WRITE_EEPROM, 0, 66); writeCmd(HMC_WRITE_EEPROM, 1, 0); writeCmd(HMC_WRITE_EEPROM, 2, 0); diff --git a/libraries/hmc6352/hmc6352.h b/libraries/hmc6352/hmc6352.h index 9f2e959f..d8b115af 100644 --- a/libraries/hmc6352/hmc6352.h +++ b/libraries/hmc6352/hmc6352.h @@ -2,7 +2,7 @@ // // FILE: hmc6352.h // AUTHOR: Rob Tillaart -// VERSION: 0.3.3 +// VERSION: 0.4.0 // PURPOSE: HMC6352 library for Arduino @@ -10,7 +10,7 @@ #include "Arduino.h" -#define HMC6352_LIB_VERSION (F("0.3.3")) +#define HMC6352_LIB_VERSION (F("0.4.0")) // status function calls #define HMC6532_OK 0 @@ -41,12 +41,8 @@ enum hmcOutputMode class hmc6352 { public: - hmc6352(uint8_t device, TwoWire *wire = &Wire); + hmc6352(uint8_t address, TwoWire *wire = &Wire); - -#if defined (ESP8266) || defined(ESP32) - bool begin(uint8_t sda, uint8_t scl); -#endif bool begin(); bool isConnected(); diff --git a/libraries/hmc6352/library.json b/libraries/hmc6352/library.json index 46888088..0c9f8d32 100644 --- a/libraries/hmc6352/library.json +++ b/libraries/hmc6352/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/HMC6352.git" }, - "version": "0.3.3", + "version": "0.4.0", "license": "MIT", "frameworks": "*", "platforms": "*", diff --git a/libraries/hmc6352/library.properties b/libraries/hmc6352/library.properties index c2ef4bce..95ee4df7 100644 --- a/libraries/hmc6352/library.properties +++ b/libraries/hmc6352/library.properties @@ -1,5 +1,5 @@ name=HMC6352 -version=0.3.3 +version=0.4.0 author=Rob Tillaart maintainer=Rob Tillaart sentence=Experimental Arduino library for HMC6352 digital compass sensor diff --git a/libraries/hmc6352/test/unit_test_001.cpp b/libraries/hmc6352/test/unit_test_001.cpp index 94b39f1d..e9353994 100644 --- a/libraries/hmc6352/test/unit_test_001.cpp +++ b/libraries/hmc6352/test/unit_test_001.cpp @@ -44,7 +44,7 @@ unittest(test_new_operator) assertEqualINF(exp(800)); assertEqualINF(0.0/0.0); assertEqualINF(42); - + assertEqualNAN(INFINITY - INFINITY); assertEqualNAN(0.0/0.0); assertEqualNAN(42); @@ -55,6 +55,8 @@ unittest(test_new_operator) unittest(test_constructor) { hmc6352 Compass(0x21); + + Wire.begin(); assertTrue(Compass.begin()); assertTrue(Compass.isConnected()); } @@ -80,6 +82,8 @@ unittest(test_constants) unittest(test_setOperationalModus) { hmc6352 Compass(0x21); + + Wire.begin(); assertTrue(Compass.begin()); assertTrue(Compass.isConnected()); @@ -98,6 +102,8 @@ unittest(test_setOperationalModus) unittest(test_setOutputModus) { hmc6352 Compass(0x21); + + Wire.begin(); assertTrue(Compass.begin()); assertTrue(Compass.isConnected()); @@ -110,6 +116,8 @@ unittest(test_setOutputModus) unittest(test_setI2CAddress) { hmc6352 Compass(0x21); + + Wire.begin(); assertTrue(Compass.begin()); assertTrue(Compass.isConnected()); @@ -122,4 +130,6 @@ unittest(test_setI2CAddress) unittest_main() -// -------- + +// -- END OF FILE -- +