Skip to content

Commit 865ac5c

Browse files
committed
add(QMC5883P): initial work (raw combined magnitude)
1 parent 7a8f167 commit 865ac5c

File tree

6 files changed

+220
-10
lines changed

6 files changed

+220
-10
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
99
architectures=*
10-
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA237 and INA238 Library, Adafruit INA260 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit MLX90632 Library, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, Adafruit LED Backpack Library, Adafruit LiquidCrystal, Adafruit SH110X, Adafruit SSD1306, RTClib, StreamUtils, Adafruit SHT4x Library, Adafruit GPS Library, Adafruit uBlox
10+
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA237 and INA238 Library, Adafruit INA260 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit MLX90632 Library, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit QMC5883P Library, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, Adafruit LED Backpack Library, Adafruit LiquidCrystal, Adafruit SH110X, Adafruit SSD1306, RTClib, StreamUtils, Adafruit SHT4x Library, Adafruit GPS Library, Adafruit uBlox

platformio.ini

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ lib_deps =
4848
adafruit/Adafruit LTR390 Library
4949
adafruit/Adafruit LTR329 and LTR303
5050
adafruit/Adafruit PCT2075
51+
adafruit/Adafruit QMC5883P Library
5152
adafruit/Adafruit SCD30
5253
adafruit/Adafruit SGP30 Sensor
5354
adafruit/Adafruit SGP40 Sensor
@@ -288,7 +289,7 @@ extra_scripts = pre:rename_usb_config.py
288289
[env:adafruit_funhouse_esp32s2_debug]
289290
extends = common:esp32
290291
board = adafruit_funhouse_esp32s2
291-
;lib_extra_dirs =
292+
;lib_extra_dirs =
292293
build_type = debug
293294
build_flags =
294295
-DARDUINO_FUNHOUSE
@@ -421,13 +422,13 @@ monitor_port = auto
421422
; debug_init_break = tbreak clearConfiguration
422423
lib_ignore = USBHost
423424
build_flags = -DUSE_TINYUSB
424-
-D__SAMD51J20A__
425-
-DCRYSTALLESS
426-
-DADAFRUIT_PYPORTAL_M4_TITANO
427-
-D__SAMD51__
428-
-D__FPU_PRESENT
429-
-DARM_MATH_CM4
430-
-mfloat-abi=hard
425+
-D__SAMD51J20A__
426+
-DCRYSTALLESS
427+
-DADAFRUIT_PYPORTAL_M4_TITANO
428+
-D__SAMD51__
429+
-D__FPU_PRESENT
430+
-DARM_MATH_CM4
431+
-mfloat-abi=hard
431432
-mfpu=fpv4-sp-d16
432433
-DCORE_DEBUG_LEVEL=5
433434
-DARDUINO_USB_CDC_ON_BOOT=1
@@ -573,4 +574,3 @@ build_flags =
573574
; ; No USB stack
574575
; build_flags = -DPIO_FRAMEWORK_ARDUINO_NO_USB
575576
; -DPIO_FRAMEWORK_ARDUINO_ENABLE_IPV6
576-

src/components/i2c/controller.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ static const std::map<std::string, FnCreateI2CSensorDriver> I2cFactorySensor = {
255255
const char *driver_name) -> drvBase * {
256256
return new drvPm25(i2c, addr, mux_channel, driver_name);
257257
}},
258+
{"qmc5883p",
259+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
260+
const char *driver_name) -> drvBase * {
261+
return new drvQmc5883p(i2c, addr, mux_channel, driver_name);
262+
}},
258263
{"scd40",
259264
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
260265
const char *driver_name) -> drvBase * {
@@ -411,6 +416,7 @@ static const std::unordered_map<uint16_t, std::vector<const char *>>
411416
{"ltr303", "pct2075", "tsl2591", "veml7700", "vl53l1x", "vl53l4cd",
412417
"vl53l4cx", "vl6180x"}},
413418
{0x2A, {"nau7802"}},
419+
{0x2C, {"qmc5883p"}},
414420
{0x38, {"aht20", "max17048"}},
415421
{0x39, {"tsl2591"}},
416422
{0x3A, {"mlx90632"}},

src/components/i2c/controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "drivers/drvOutputBase.h" ///< Base i2c output driver class
6060
#include "drivers/drvPct2075.h"
6161
#include "drivers/drvPm25.h"
62+
#include "drivers/drvQmc5883p.h"
6263
#include "drivers/drvScd30.h"
6364
#include "drivers/drvScd4x.h"
6465
#include "drivers/drvSen5x.h"
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*!
2+
* @file drvQmc5883p.cpp
3+
*
4+
* Driver wrapper for the Adafruit QMC5883P 3-axis magnetometer.
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
16+
#include "drvQmc5883p.h"
17+
#include <Adafruit_QMC5883P.h>
18+
19+
/*******************************************************************************/
20+
/*!
21+
@brief Destructor for a QMC5883P sensor.
22+
*/
23+
/*******************************************************************************/
24+
drvQmc5883p::~drvQmc5883p() {
25+
if (_qmc) {
26+
delete _qmc;
27+
_qmc = nullptr;
28+
}
29+
}
30+
31+
/*******************************************************************************/
32+
/*!
33+
@brief Initializes the QMC5883P sensor and begins I2C.
34+
@returns True if initialized successfully, False otherwise.
35+
*/
36+
/*******************************************************************************/
37+
bool drvQmc5883p::begin() {
38+
_qmc = new Adafruit_QMC5883P();
39+
if (!_qmc->begin(_address, _i2c)) {
40+
WS_DEBUG_PRINTLN("QMC5883P failed to initialise!");
41+
return false;
42+
}
43+
44+
// Configure sensor with same settings as example sketch
45+
// Set to continuous mode for continuous reading
46+
_qmc->setMode(QMC5883P_MODE_CONTINUOUS);
47+
48+
// Set ODR (Output Data Rate) to 50Hz
49+
_qmc->setODR(QMC5883P_ODR_50HZ);
50+
51+
// Set OSR (Over Sample Ratio) to 4
52+
_qmc->setOSR(QMC5883P_OSR_4);
53+
54+
// Set DSR (Downsample Ratio) to 2
55+
_qmc->setDSR(QMC5883P_DSR_2);
56+
57+
// Set Range to 30G for maximum sensitivity range
58+
_qmc->setRange(QMC5883P_RANGE_30G);
59+
60+
// Set SetReset mode to On
61+
_qmc->setSetResetMode(QMC5883P_SETRESET_ON);
62+
63+
return true;
64+
}
65+
66+
/*******************************************************************************/
67+
/*!
68+
@brief Gets the QMC5883P's magnetometer sensor event.
69+
@param magEvent
70+
Pointer to the magnetometer sensor event.
71+
@returns True if the sensor event was obtained successfully, False
72+
otherwise.
73+
*/
74+
/*******************************************************************************/
75+
bool drvQmc5883p::getEventRaw(sensors_event_t *magEvent) {
76+
// Check if data is ready before reading
77+
if (!_qmc->isDataReady()) {
78+
return false;
79+
}
80+
81+
int16_t x, y, z;
82+
float gx, gy, gz;
83+
84+
// Get raw magnetic data
85+
if (!_qmc->getRawMagnetic(&x, &y, &z)) {
86+
WS_DEBUG_PRINTLN("Failed to read raw magnetic data");
87+
return false;
88+
}
89+
90+
// Get Gauss field data
91+
if (!_qmc->getGaussField(&gx, &gy, &gz)) {
92+
WS_DEBUG_PRINTLN("Failed to read Gauss field data");
93+
WS_DEBUG_PRINT("Raw X: ");
94+
WS_DEBUG_PRINTLN(x);
95+
WS_DEBUG_PRINT("Raw Y: ");
96+
WS_DEBUG_PRINTLN(y);
97+
WS_DEBUG_PRINT("Raw Z: ");
98+
WS_DEBUG_PRINTLN(z);
99+
return false;
100+
} else {
101+
WS_DEBUG_PRINT("Gauss X: ");
102+
WS_DEBUG_PRINTLN(gx);
103+
WS_DEBUG_PRINT("Gauss Y: ");
104+
WS_DEBUG_PRINTLN(gy);
105+
WS_DEBUG_PRINT("Gauss Z: ");
106+
WS_DEBUG_PRINTLN(gz);
107+
}
108+
109+
// Check for overflow
110+
if (_qmc->isOverflow()) {
111+
WS_DEBUG_PRINTLN("QMC5883P data overflow - skipping reading");
112+
return false;
113+
}
114+
115+
// Calculate magnitude in Gauss
116+
float magnitude_G = sqrtf(gx * gx + gy * gy + gz * gz);
117+
magEvent->data[0] = magnitude_G;
118+
return true;
119+
}
120+
121+
void drvQmc5883p::ConfigureDefaultSensorTypes() {
122+
_default_sensor_types_count = 1;
123+
_default_sensor_types[0] =
124+
wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW;
125+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*!
2+
* @file drvQmc5883p.h
3+
*
4+
* Driver wrapper for the Adafruit QMC5883P 3-axis magnetometer.
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#ifndef DRV_QMC5883P_H
16+
#define DRV_QMC5883P_H
17+
18+
#include "drvBase.h"
19+
20+
class Adafruit_QMC5883P; // forward
21+
22+
/**************************************************************************/
23+
/*!
24+
@brief Class that provides a driver interface for a QMC5883P sensor.
25+
*/
26+
/**************************************************************************/
27+
class drvQmc5883p : public drvBase {
28+
public:
29+
/*******************************************************************************/
30+
/*!
31+
@brief Constructor for a QMC5883P sensor.
32+
@param i2c
33+
The I2C interface.
34+
@param sensorAddress
35+
7-bit device address.
36+
@param mux_channel
37+
The I2C multiplexer channel.
38+
@param driver_name
39+
The name of the driver.
40+
*/
41+
/*******************************************************************************/
42+
drvQmc5883p(TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel,
43+
const char *driver_name)
44+
: drvBase(i2c, sensorAddress, mux_channel, driver_name) {}
45+
46+
/*******************************************************************************/
47+
/*!
48+
@brief Destructor for a QMC5883P sensor.
49+
*/
50+
/*******************************************************************************/
51+
~drvQmc5883p();
52+
53+
/*******************************************************************************/
54+
/*!
55+
@brief Initializes the QMC5883P sensor and begins I2C.
56+
@returns True if initialized successfully, False otherwise.
57+
*/
58+
/*******************************************************************************/
59+
bool begin();
60+
61+
/*******************************************************************************/
62+
/*!
63+
@brief Gets the QMC5883P's magnetometer sensor event.
64+
@param magEvent
65+
Pointer to the magnetometer sensor event.
66+
@returns True if the sensor event was obtained successfully, False
67+
otherwise.
68+
*/
69+
/*******************************************************************************/
70+
bool getEventRaw(sensors_event_t *magEvent);
71+
72+
void ConfigureDefaultSensorTypes() override;
73+
74+
protected:
75+
Adafruit_QMC5883P *_qmc = nullptr; ///< Pointer to QMC5883P sensor object
76+
};
77+
78+
#endif // DRV_QMC5883P_H

0 commit comments

Comments
 (0)