Skip to content

Commit b1d8d62

Browse files
authored
Merge pull request #802 from adafruit/add-qmc5883p-vectorless
Add qmc5883p
2 parents e960cee + b338a56 commit b1d8d62

File tree

7 files changed

+159
-2
lines changed

7 files changed

+159
-2
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 INA260 Library, Adafruit INA237 and INA238 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 AS5600 Library, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit BMP5xx 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
10+
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA260 Library, Adafruit INA237 and INA238 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 AS5600 Library, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit BMP5xx 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

platformio.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ lib_deps =
4949
adafruit/Adafruit LTR390 Library
5050
adafruit/Adafruit LTR329 and LTR303
5151
adafruit/Adafruit PCT2075
52+
adafruit/Adafruit QMC5883P Library
5253
adafruit/Adafruit SCD30
5354
adafruit/Adafruit SGP30 Sensor
5455
adafruit/Adafruit SGP40 Sensor
@@ -483,9 +484,10 @@ build_flags =
483484
-DNON32XFER_FAST
484485
-DDEBUG_DISABLED
485486
-DDEBUG_LEVEL_NONE
487+
-DPIO_FRAMEWORK_ARDUINO_MMU_CACHE16_IRAM48 ; avoid section `.text1' will not fit in region `iram1_0_seg'
486488
board_build.eesz=4M2M
487489
board_build.filesystem = littlefs
488-
upload_port = /dev/cu.SLAB_USBtoUART
490+
upload_port = COM19
489491

490492
; SAMD51 Boards ;
491493

src/Wippersnapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#define WIPPERSNAPPER_H
2020

2121
// Cpp STD
22+
#include <math.h>
23+
2224
#include <vector>
2325

2426
// Nanopb dependencies

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
787787
_pm25->configureDriver(msgDeviceInitReq);
788788
drivers.push_back(_pm25);
789789
WS_DEBUG_PRINTLN("PM2.5 AQI Sensor Initialized Successfully!");
790+
} else if (strcmp("qmc5883p", msgDeviceInitReq->i2c_device_name) == 0) {
791+
_qmc5883p = new WipperSnapper_I2C_Driver_QMC5883P(this->_i2c, i2cAddress);
792+
if (!_qmc5883p->begin()) {
793+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize QMC5883P Sensor!");
794+
_busStatusResponse =
795+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
796+
return false;
797+
}
798+
_qmc5883p->configureDriver(msgDeviceInitReq);
799+
drivers.push_back(_qmc5883p);
800+
WS_DEBUG_PRINTLN("QMC5883P Sensor Initialized Successfully!");
790801
} else if (strcmp("lc709203f", msgDeviceInitReq->i2c_device_name) == 0) {
791802
_lc = new WipperSnapper_I2C_Driver_LC709203F(this->_i2c, i2cAddress);
792803
if (!_lc->begin()) {

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "drivers/WipperSnapper_I2C_Driver_Out_Ssd1306.h"
6666
#include "drivers/WipperSnapper_I2C_Driver_PCT2075.h"
6767
#include "drivers/WipperSnapper_I2C_Driver_PM25.h"
68+
#include "drivers/WipperSnapper_I2C_Driver_QMC5883P.h"
6869
#include "drivers/WipperSnapper_I2C_Driver_SCD30.h"
6970
#include "drivers/WipperSnapper_I2C_Driver_SCD4X.h"
7071
#include "drivers/WipperSnapper_I2C_Driver_SEN5X.h"
@@ -96,6 +97,7 @@ class WipperSnapper_I2C_Driver_INA260;
9697
class WipperSnapper_I2C_Driver_INA237;
9798
class WipperSnapper_I2C_Driver_INA238;
9899
class WipperSnapper_I2C_Driver_INA228;
100+
class WipperSnapper_I2C_Driver_QMC5883P;
99101

100102
/**************************************************************************/
101103
/*!
@@ -204,6 +206,7 @@ class WipperSnapper_Component_I2C {
204206
WipperSnapper_I2C_Driver_SGP40 *_sgp40 = nullptr;
205207
WipperSnapper_I2C_Driver_PCT2075 *_pct2075 = nullptr;
206208
WipperSnapper_I2C_Driver_PM25 *_pm25 = nullptr;
209+
WipperSnapper_I2C_Driver_QMC5883P *_qmc5883p = nullptr;
207210
WipperSnapper_I2C_Driver_SI7021 *_si7021 = nullptr;
208211
WipperSnapper_I2C_Driver_SHT4X *_sht4x = nullptr;
209212
WipperSnapper_I2C_Driver_SHT3X *_sht3x = nullptr;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_QMC5883P.cpp
3+
*
4+
* Implementation for the Adafruit QMC5883P magnetometer wrapper.
5+
*/
6+
7+
#include "WipperSnapper_I2C_Driver_QMC5883P.h"
8+
9+
#include <Adafruit_QMC5883P.h>
10+
11+
/*!
12+
* @brief Constructor for a QMC5883P sensor.
13+
* @param i2c
14+
* The I2C interface.
15+
* @param sensorAddress
16+
* The 7-bit I2C address of the sensor.
17+
*/
18+
WipperSnapper_I2C_Driver_QMC5883P::WipperSnapper_I2C_Driver_QMC5883P(
19+
TwoWire *i2c, uint16_t sensorAddress)
20+
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {}
21+
22+
/*!
23+
* @brief Destructor for a QMC5883P sensor.
24+
*/
25+
WipperSnapper_I2C_Driver_QMC5883P::~WipperSnapper_I2C_Driver_QMC5883P() {
26+
if (_qmc) {
27+
delete _qmc;
28+
}
29+
}
30+
31+
/*!
32+
* @brief Initializes the QMC5883P sensor and begins I2C.
33+
* @return True if initialized successfully, False otherwise.
34+
*/
35+
bool WipperSnapper_I2C_Driver_QMC5883P::begin() {
36+
_qmc = new Adafruit_QMC5883P();
37+
if (!_qmc->begin(_sensorAddress, _i2c)) {
38+
return false;
39+
}
40+
41+
// Set to continuous mode
42+
_qmc->setMode(QMC5883P_MODE_CONTINUOUS);
43+
// Set ODR (Output Data Rate) to 50Hz
44+
_qmc->setODR(QMC5883P_ODR_50HZ);
45+
// Set OSR (Over Sample Ratio) to 4
46+
_qmc->setOSR(QMC5883P_OSR_4);
47+
// Set DSR (Downsample Ratio) to 2
48+
_qmc->setDSR(QMC5883P_DSR_2);
49+
// Set Range to 30G
50+
_qmc->setRange(QMC5883P_RANGE_30G);
51+
// Set SetReset mode to On
52+
_qmc->setSetResetMode(QMC5883P_SETRESET_ON);
53+
54+
return true;
55+
}
56+
57+
/*!
58+
* @brief Get the magnetometer's sensor event.
59+
* @param magEvent
60+
* Pointer to the magnetometer sensor event.
61+
* @return True if the event was obtained successfully, False otherwise.
62+
*/
63+
bool WipperSnapper_I2C_Driver_QMC5883P::getEventRaw(sensors_event_t *magEvent) {
64+
if (!_qmc->isDataReady()) {
65+
return false;
66+
}
67+
68+
int16_t x, y, z;
69+
float gx, gy, gz;
70+
71+
// Get raw magnetic data
72+
if (!_qmc->getRawMagnetic(&x, &y, &z)) {
73+
WS_DEBUG_PRINTLN("Failed to read raw magnetic data");
74+
return false;
75+
}
76+
77+
// Get Gauss field data
78+
if (!_qmc->getGaussField(&gx, &gy, &gz)) {
79+
WS_DEBUG_PRINTLN("Failed to read Gauss field data");
80+
WS_DEBUG_PRINT("Raw X: ");
81+
WS_DEBUG_PRINTLN(x);
82+
WS_DEBUG_PRINT("Raw Y: ");
83+
WS_DEBUG_PRINTLN(y);
84+
WS_DEBUG_PRINT("Raw Z: ");
85+
WS_DEBUG_PRINTLN(z);
86+
return false;
87+
} else {
88+
WS_DEBUG_PRINT("Gauss X: ");
89+
WS_DEBUG_PRINTLN(gx);
90+
WS_DEBUG_PRINT("Gauss Y: ");
91+
WS_DEBUG_PRINTLN(gy);
92+
WS_DEBUG_PRINT("Gauss Z: ");
93+
WS_DEBUG_PRINTLN(gz);
94+
}
95+
96+
// Check for overflow
97+
if (_qmc->isOverflow()) {
98+
WS_DEBUG_PRINTLN("QMC5883P data overflow - skipping reading");
99+
return false;
100+
}
101+
102+
// Calculate magnitude in Gauss
103+
float magnitude_G = sqrtf(gx * gx + gy * gy + gz * gz);
104+
magEvent->data[0] = magnitude_G;
105+
return true;
106+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_QMC5883P.h
3+
*
4+
* Driver wrapper for the Adafruit QMC5883P 3-axis magnetometer.
5+
*
6+
* Publishes magnetic field magnitude in Gauss as SENSOR_TYPE_MAGNETIC_FIELD.
7+
*/
8+
#ifndef WipperSnapper_I2C_Driver_QMC5883P_H
9+
#define WipperSnapper_I2C_Driver_QMC5883P_H
10+
11+
#include "WipperSnapper_I2C_Driver.h"
12+
#include "Wippersnapper.h"
13+
14+
class Adafruit_QMC5883P; // forward
15+
16+
/**************************************************************************/
17+
/*!
18+
@brief Class that provides a driver interface for a QMC5883P sensor.
19+
*/
20+
/**************************************************************************/
21+
class WipperSnapper_I2C_Driver_QMC5883P : public WipperSnapper_I2C_Driver {
22+
public:
23+
WipperSnapper_I2C_Driver_QMC5883P(TwoWire *i2c, uint16_t sensorAddress);
24+
~WipperSnapper_I2C_Driver_QMC5883P();
25+
26+
bool begin();
27+
bool getEventRaw(sensors_event_t *magEvent);
28+
29+
private:
30+
Adafruit_QMC5883P *_qmc = nullptr; ///< Pointer to the QMC5883P sensor object
31+
};
32+
33+
#endif // WipperSnapper_I2C_Driver_QMC5883P_H

0 commit comments

Comments
 (0)