Skip to content

Commit

Permalink
[SensorQMC6310] Fix QMC6310 multiple device addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Jan 24, 2025
1 parent e005e43 commit 7bc3c4f
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void setup()

beginPower();

if (!qmc.begin(Wire, SENSOR_SDA, SENSOR_SCL)) {
if (!qmc.begin(Wire, QMC6310U_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMC6310 - check your wiring!");
while (1) {
delay(1000);
Expand Down
2 changes: 1 addition & 1 deletion examples/QMC6310_CompassExample/QMC6310_CompassExample.ino
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void setup()

beginPower();

if (!qmc.begin(Wire, SENSOR_SDA, SENSOR_SCL)) {
if (!qmc.begin(Wire, QMC6310U_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMC6310 - check your wiring!");
while (1) {
delay(1000);
Expand Down
2 changes: 1 addition & 1 deletion examples/QMC6310_GetDataExample/QMC6310_GetDataExample.ino
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void setup()

beginPower();

if (!qmc.begin(Wire, SENSOR_SDA, SENSOR_SCL)) {
if (!qmc.begin(Wire, QMC6310U_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMC6310 - check your wiring!");
while (1) {
delay(1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void setup()

beginPower();

if (!qmc.begin(Wire, SENSOR_SDA, SENSOR_SCL)) {
if (!qmc.begin(Wire, QMC6310U_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMC6310 - check your wiring!");
while (1) {
delay(1000);
Expand Down
3 changes: 1 addition & 2 deletions src/REG/QMC6310Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
class QMC6310Constants
{
protected:
// Unique I2C device address
static constexpr uint8_t QMC6310_SLAVE_ADDRESS = 0x1C;


// Register addresses
static constexpr uint8_t REG_CHIP_ID = 0x00;
Expand Down
20 changes: 12 additions & 8 deletions src/SensorQMC6310.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#include "REG/QMC6310Constants.h"
#include "SensorPlatform.hpp"

static constexpr uint8_t QMC6310U_SLAVE_ADDRESS = 0x1C;
static constexpr uint8_t QMC6310N_SLAVE_ADDRESS = 0x3C;

class Polar
{
public:
Expand Down Expand Up @@ -98,27 +101,27 @@ class SensorQMC6310 : public QMC6310Constants
}

#if defined(ARDUINO)
bool begin(TwoWire &wire, int sda = -1, int scl = -1)
bool begin(TwoWire &wire, uint8_t addr = QMC6310U_SLAVE_ADDRESS, int sda = -1, int scl = -1)
{
if (!beginCommon<SensorCommI2C, HalArduino>(comm, hal, wire, QMC6310_SLAVE_ADDRESS, sda, scl)) {
if (!beginCommon<SensorCommI2C, HalArduino>(comm, hal, wire, addr, sda, scl)) {
return false;
}
return initImpl();
}
#elif defined(ESP_PLATFORM)

#if defined(USEING_I2C_LEGACY)
bool begin(i2c_port_t port_num, int sda = -1, int scl = -1)
bool begin(i2c_port_t port_num, uint8_t addr = QMC6310U_SLAVE_ADDRESS, int sda = -1, int scl = -1)
{
if (!beginCommon<SensorCommI2C, HalEspIDF>(comm, hal, port_num, QMC6310_SLAVE_ADDRESS, sda, scl)) {
if (!beginCommon<SensorCommI2C, HalEspIDF>(comm, hal, port_num, addr, sda, scl)) {
return false;
}
return initImpl();
}
#else
bool begin(i2c_master_bus_handle_t handle)
bool begin(i2c_master_bus_handle_t handle, uint8_t addr = QMC6310U_SLAVE_ADDRESS)
{
if (!beginCommon<SensorCommI2C, HalEspIDF>(comm, hal, handle, QMC6310_SLAVE_ADDRESS, sda, scl)) {
if (!beginCommon<SensorCommI2C, HalEspIDF>(comm, hal, handle, addr, sda, scl)) {
return false;
}
return initImpl();
Expand All @@ -127,10 +130,11 @@ class SensorQMC6310 : public QMC6310Constants
#endif

bool begin(SensorCommCustom::CustomCallback callback,
SensorCommCustomHal::CustomHalCallback hal_callback)
SensorCommCustomHal::CustomHalCallback hal_callback,
uint8_t addr = QMC6310U_SLAVE_ADDRESS)
{
if (!beginCommCustomCallback<SensorCommCustom, SensorCommCustomHal>(COMM_CUSTOM,
callback, hal_callback, QMC6310_SLAVE_ADDRESS, comm, hal)) {
callback, hal_callback, addr, comm, hal)) {
return false;
}
return initImpl();
Expand Down

0 comments on commit 7bc3c4f

Please sign in to comment.