Skip to content

Commit af8c742

Browse files
committed
add(qmc5883p): initial vectorless result
1 parent a1b412c commit af8c742

File tree

6 files changed

+117
-1
lines changed

6 files changed

+117
-1
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 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
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 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

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ lib_deps =
4747
adafruit/Adafruit LTR390 Library
4848
adafruit/Adafruit LTR329 and LTR303
4949
adafruit/Adafruit PCT2075
50+
adafruit/Adafruit QMC5883P Library
5051
adafruit/Adafruit SCD30
5152
adafruit/Adafruit SGP30 Sensor
5253
adafruit/Adafruit SGP40 Sensor

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
737737
_pm25->configureDriver(msgDeviceInitReq);
738738
drivers.push_back(_pm25);
739739
WS_DEBUG_PRINTLN("PM2.5 AQI Sensor Initialized Successfully!");
740+
} else if (strcmp("qmc5883p", msgDeviceInitReq->i2c_device_name) == 0) {
741+
_qmc5883p = new WipperSnapper_I2C_Driver_QMC5883P(this->_i2c, i2cAddress);
742+
if (!_qmc5883p->begin()) {
743+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize QMC5883P Sensor!");
744+
_busStatusResponse =
745+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
746+
return false;
747+
}
748+
_qmc5883p->configureDriver(msgDeviceInitReq);
749+
drivers.push_back(_qmc5883p);
750+
WS_DEBUG_PRINTLN("QMC5883P Sensor Initialized Successfully!");
740751
} else if (strcmp("lc709203f", msgDeviceInitReq->i2c_device_name) == 0) {
741752
_lc = new WipperSnapper_I2C_Driver_LC709203F(this->_i2c, i2cAddress);
742753
if (!_lc->begin()) {

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include "drivers/WipperSnapper_I2C_Driver_Out_Ssd1306.h"
6363
#include "drivers/WipperSnapper_I2C_Driver_PCT2075.h"
6464
#include "drivers/WipperSnapper_I2C_Driver_PM25.h"
65+
#include "drivers/WipperSnapper_I2C_Driver_QMC5883P.h"
6566
#include "drivers/WipperSnapper_I2C_Driver_SCD30.h"
6667
#include "drivers/WipperSnapper_I2C_Driver_SCD4X.h"
6768
#include "drivers/WipperSnapper_I2C_Driver_SEN5X.h"
@@ -93,6 +94,7 @@ class WipperSnapper_I2C_Driver_INA260;
9394
class WipperSnapper_I2C_Driver_INA237;
9495
class WipperSnapper_I2C_Driver_INA238;
9596
class WipperSnapper_I2C_Driver_INA228;
97+
class WipperSnapper_I2C_Driver_QMC5883P;
9698

9799
/**************************************************************************/
98100
/*!
@@ -197,6 +199,7 @@ class WipperSnapper_Component_I2C {
197199
WipperSnapper_I2C_Driver_SGP40 *_sgp40 = nullptr;
198200
WipperSnapper_I2C_Driver_PCT2075 *_pct2075 = nullptr;
199201
WipperSnapper_I2C_Driver_PM25 *_pm25 = nullptr;
202+
WipperSnapper_I2C_Driver_QMC5883P *_qmc5883p = nullptr;
200203
WipperSnapper_I2C_Driver_SI7021 *_si7021 = nullptr;
201204
WipperSnapper_I2C_Driver_SHT4X *_sht4x = nullptr;
202205
WipperSnapper_I2C_Driver_SHT3X *_sht3x = nullptr;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
#include <math.h>
11+
12+
WipperSnapper_I2C_Driver_QMC5883P::WipperSnapper_I2C_Driver_QMC5883P(
13+
TwoWire *i2c, uint16_t sensorAddress)
14+
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {}
15+
16+
WipperSnapper_I2C_Driver_QMC5883P::~WipperSnapper_I2C_Driver_QMC5883P() {
17+
if (_qmc) {
18+
delete _qmc;
19+
}
20+
}
21+
22+
bool WipperSnapper_I2C_Driver_QMC5883P::begin() {
23+
_qmc = new Adafruit_QMC5883P();
24+
if (!_qmc->begin(_sensorAddress, _i2c)) {
25+
return false;
26+
}
27+
28+
// Set to normal mode
29+
_qmc->setMode(QMC5883P_MODE_NORMAL);
30+
// Set ODR (Output Data Rate) to 50Hz
31+
_qmc->setODR(QMC5883P_ODR_50HZ);
32+
// Set OSR (Over Sample Ratio) to 4
33+
_qmc->setOSR(QMC5883P_OSR_4);
34+
// Set DSR (Downsample Ratio) to 2
35+
_qmc->setDSR(QMC5883P_DSR_2);
36+
// Set Range to 8G
37+
_qmc->setRange(QMC5883P_RANGE_8G);
38+
// Set SetReset mode to On
39+
_qmc->setSetResetMode(QMC5883P_SETRESET_ON);
40+
41+
return true;
42+
}
43+
44+
bool WipperSnapper_I2C_Driver_QMC5883P::getEventRaw(
45+
sensors_event_t *magEvent) {
46+
if (!_qmc->isDataReady()) {
47+
return false;
48+
}
49+
50+
int16_t x, y, z;
51+
float gx, gy, gz;
52+
53+
// Get raw magnetic data
54+
if (!_qmc->getRawMagnetic(&x, &y, &z)) {
55+
return false;
56+
}
57+
58+
// Get Gauss field data
59+
if (!_qmc->getGaussField(&gx, &gy, &gz)) {
60+
return false;
61+
}
62+
63+
// Check for overflow
64+
if (_qmc->isOverflow()) {
65+
WS_DEBUG_PRINTLN("QMC5883P data overflow - skipping reading");
66+
return false;
67+
}
68+
69+
// Calculate magnitude in Gauss
70+
float magnitude_G = sqrtf(gx * gx + gy * gy + gz * gz);
71+
magEvent->data[0] = magnitude_G;
72+
return true;
73+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
class WipperSnapper_I2C_Driver_QMC5883P : public WipperSnapper_I2C_Driver {
17+
public:
18+
WipperSnapper_I2C_Driver_QMC5883P(TwoWire *i2c, uint16_t sensorAddress);
19+
~WipperSnapper_I2C_Driver_QMC5883P();
20+
21+
bool begin();
22+
bool getEventRaw(sensors_event_t *magEvent);
23+
24+
private:
25+
Adafruit_QMC5883P *_qmc = nullptr;
26+
};
27+
28+
#endif // WipperSnapper_I2C_Driver_QMC5883P_H

0 commit comments

Comments
 (0)