0.4.0 HMC6352

This commit is contained in:
Rob Tillaart 2023-12-05 20:31:39 +01:00
parent af4a8b4db1
commit 7692ae8cb8
14 changed files with 135 additions and 104 deletions

View File

@ -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

View File

@ -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.

View File

@ -4,9 +4,9 @@
// PURPOSE: demo app HMC6352 library - continuous mode for Arduino
#include <hmc6352.h>
#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 --

View File

@ -4,7 +4,7 @@
// PURPOSE: test HMC6352 library for Arduino
#include <hmc6352.h>
#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 --

View File

@ -4,7 +4,7 @@
// PURPOSE: demo HMC6352 library - query mode for Arduino
#include <hmc6352.h>
#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 --

View File

@ -4,7 +4,7 @@
// PURPOSE: Reset HMC6352 to continuous mode without library
#include <Wire.h>
#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 --

View File

@ -4,7 +4,7 @@
// PURPOSE: Reset HMC6352 to query mode without library
#include <Wire.h>
#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 --

View File

@ -5,7 +5,7 @@
// (esp if it is struck in continuous mode - Arduino)
#include <Wire.h>
#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 --

View File

@ -4,7 +4,7 @@
// PURPOSE: demo app HMC6352 library - standby mode for Arduino
#include <hmc6352.h>
#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 --

View File

@ -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);

View File

@ -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();

View File

@ -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": "*",

View File

@ -1,5 +1,5 @@
name=HMC6352
version=0.3.3
version=0.4.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Experimental Arduino library for HMC6352 digital compass sensor

View File

@ -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 --