diff --git a/library.properties b/library.properties index 6c1e6e8ac..ce970d89d 100644 --- a/library.properties +++ b/library.properties @@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper category=Communication url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino architectures=* -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 +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 diff --git a/platformio.ini b/platformio.ini index 9eb253874..afb209916 100644 --- a/platformio.ini +++ b/platformio.ini @@ -49,6 +49,7 @@ lib_deps = adafruit/Adafruit LTR390 Library adafruit/Adafruit LTR329 and LTR303 adafruit/Adafruit PCT2075 + adafruit/Adafruit QMC5883P Library adafruit/Adafruit SCD30 adafruit/Adafruit SGP30 Sensor adafruit/Adafruit SGP40 Sensor @@ -483,9 +484,10 @@ build_flags = -DNON32XFER_FAST -DDEBUG_DISABLED -DDEBUG_LEVEL_NONE + -DPIO_FRAMEWORK_ARDUINO_MMU_CACHE16_IRAM48 ; avoid section `.text1' will not fit in region `iram1_0_seg' board_build.eesz=4M2M board_build.filesystem = littlefs -upload_port = /dev/cu.SLAB_USBtoUART +upload_port = COM19 ; SAMD51 Boards ; diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 136b529c1..e40638312 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -19,6 +19,8 @@ #define WIPPERSNAPPER_H // Cpp STD +#include + #include // Nanopb dependencies diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index 7e02c9922..d8c95d522 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -787,6 +787,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice( _pm25->configureDriver(msgDeviceInitReq); drivers.push_back(_pm25); WS_DEBUG_PRINTLN("PM2.5 AQI Sensor Initialized Successfully!"); + } else if (strcmp("qmc5883p", msgDeviceInitReq->i2c_device_name) == 0) { + _qmc5883p = new WipperSnapper_I2C_Driver_QMC5883P(this->_i2c, i2cAddress); + if (!_qmc5883p->begin()) { + WS_DEBUG_PRINTLN("ERROR: Failed to initialize QMC5883P Sensor!"); + _busStatusResponse = + wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL; + return false; + } + _qmc5883p->configureDriver(msgDeviceInitReq); + drivers.push_back(_qmc5883p); + WS_DEBUG_PRINTLN("QMC5883P Sensor Initialized Successfully!"); } else if (strcmp("lc709203f", msgDeviceInitReq->i2c_device_name) == 0) { _lc = new WipperSnapper_I2C_Driver_LC709203F(this->_i2c, i2cAddress); if (!_lc->begin()) { diff --git a/src/components/i2c/WipperSnapper_I2C.h b/src/components/i2c/WipperSnapper_I2C.h index a83765370..8d1912316 100644 --- a/src/components/i2c/WipperSnapper_I2C.h +++ b/src/components/i2c/WipperSnapper_I2C.h @@ -65,6 +65,7 @@ #include "drivers/WipperSnapper_I2C_Driver_Out_Ssd1306.h" #include "drivers/WipperSnapper_I2C_Driver_PCT2075.h" #include "drivers/WipperSnapper_I2C_Driver_PM25.h" +#include "drivers/WipperSnapper_I2C_Driver_QMC5883P.h" #include "drivers/WipperSnapper_I2C_Driver_SCD30.h" #include "drivers/WipperSnapper_I2C_Driver_SCD4X.h" #include "drivers/WipperSnapper_I2C_Driver_SEN5X.h" @@ -96,6 +97,7 @@ class WipperSnapper_I2C_Driver_INA260; class WipperSnapper_I2C_Driver_INA237; class WipperSnapper_I2C_Driver_INA238; class WipperSnapper_I2C_Driver_INA228; +class WipperSnapper_I2C_Driver_QMC5883P; /**************************************************************************/ /*! @@ -204,6 +206,7 @@ class WipperSnapper_Component_I2C { WipperSnapper_I2C_Driver_SGP40 *_sgp40 = nullptr; WipperSnapper_I2C_Driver_PCT2075 *_pct2075 = nullptr; WipperSnapper_I2C_Driver_PM25 *_pm25 = nullptr; + WipperSnapper_I2C_Driver_QMC5883P *_qmc5883p = nullptr; WipperSnapper_I2C_Driver_SI7021 *_si7021 = nullptr; WipperSnapper_I2C_Driver_SHT4X *_sht4x = nullptr; WipperSnapper_I2C_Driver_SHT3X *_sht3x = nullptr; diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_QMC5883P.cpp b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_QMC5883P.cpp new file mode 100644 index 000000000..8cae1bc2e --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_QMC5883P.cpp @@ -0,0 +1,106 @@ +/*! + * @file WipperSnapper_I2C_Driver_QMC5883P.cpp + * + * Implementation for the Adafruit QMC5883P magnetometer wrapper. + */ + +#include "WipperSnapper_I2C_Driver_QMC5883P.h" + +#include + +/*! + * @brief Constructor for a QMC5883P sensor. + * @param i2c + * The I2C interface. + * @param sensorAddress + * The 7-bit I2C address of the sensor. + */ +WipperSnapper_I2C_Driver_QMC5883P::WipperSnapper_I2C_Driver_QMC5883P( + TwoWire *i2c, uint16_t sensorAddress) + : WipperSnapper_I2C_Driver(i2c, sensorAddress) {} + +/*! + * @brief Destructor for a QMC5883P sensor. + */ +WipperSnapper_I2C_Driver_QMC5883P::~WipperSnapper_I2C_Driver_QMC5883P() { + if (_qmc) { + delete _qmc; + } +} + +/*! + * @brief Initializes the QMC5883P sensor and begins I2C. + * @return True if initialized successfully, False otherwise. + */ +bool WipperSnapper_I2C_Driver_QMC5883P::begin() { + _qmc = new Adafruit_QMC5883P(); + if (!_qmc->begin(_sensorAddress, _i2c)) { + return false; + } + + // Set to continuous mode + _qmc->setMode(QMC5883P_MODE_CONTINUOUS); + // Set ODR (Output Data Rate) to 50Hz + _qmc->setODR(QMC5883P_ODR_50HZ); + // Set OSR (Over Sample Ratio) to 4 + _qmc->setOSR(QMC5883P_OSR_4); + // Set DSR (Downsample Ratio) to 2 + _qmc->setDSR(QMC5883P_DSR_2); + // Set Range to 30G + _qmc->setRange(QMC5883P_RANGE_30G); + // Set SetReset mode to On + _qmc->setSetResetMode(QMC5883P_SETRESET_ON); + + return true; +} + +/*! + * @brief Get the magnetometer's sensor event. + * @param magEvent + * Pointer to the magnetometer sensor event. + * @return True if the event was obtained successfully, False otherwise. + */ +bool WipperSnapper_I2C_Driver_QMC5883P::getEventRaw(sensors_event_t *magEvent) { + if (!_qmc->isDataReady()) { + return false; + } + + int16_t x, y, z; + float gx, gy, gz; + + // Get raw magnetic data + if (!_qmc->getRawMagnetic(&x, &y, &z)) { + WS_DEBUG_PRINTLN("Failed to read raw magnetic data"); + return false; + } + + // Get Gauss field data + if (!_qmc->getGaussField(&gx, &gy, &gz)) { + WS_DEBUG_PRINTLN("Failed to read Gauss field data"); + WS_DEBUG_PRINT("Raw X: "); + WS_DEBUG_PRINTLN(x); + WS_DEBUG_PRINT("Raw Y: "); + WS_DEBUG_PRINTLN(y); + WS_DEBUG_PRINT("Raw Z: "); + WS_DEBUG_PRINTLN(z); + return false; + } else { + WS_DEBUG_PRINT("Gauss X: "); + WS_DEBUG_PRINTLN(gx); + WS_DEBUG_PRINT("Gauss Y: "); + WS_DEBUG_PRINTLN(gy); + WS_DEBUG_PRINT("Gauss Z: "); + WS_DEBUG_PRINTLN(gz); + } + + // Check for overflow + if (_qmc->isOverflow()) { + WS_DEBUG_PRINTLN("QMC5883P data overflow - skipping reading"); + return false; + } + + // Calculate magnitude in Gauss + float magnitude_G = sqrtf(gx * gx + gy * gy + gz * gz); + magEvent->data[0] = magnitude_G; + return true; +} diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_QMC5883P.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_QMC5883P.h new file mode 100644 index 000000000..7d706fa69 --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_QMC5883P.h @@ -0,0 +1,33 @@ +/*! + * @file WipperSnapper_I2C_Driver_QMC5883P.h + * + * Driver wrapper for the Adafruit QMC5883P 3-axis magnetometer. + * + * Publishes magnetic field magnitude in Gauss as SENSOR_TYPE_MAGNETIC_FIELD. + */ +#ifndef WipperSnapper_I2C_Driver_QMC5883P_H +#define WipperSnapper_I2C_Driver_QMC5883P_H + +#include "WipperSnapper_I2C_Driver.h" +#include "Wippersnapper.h" + +class Adafruit_QMC5883P; // forward + +/**************************************************************************/ +/*! +@brief Class that provides a driver interface for a QMC5883P sensor. +*/ +/**************************************************************************/ +class WipperSnapper_I2C_Driver_QMC5883P : public WipperSnapper_I2C_Driver { +public: + WipperSnapper_I2C_Driver_QMC5883P(TwoWire *i2c, uint16_t sensorAddress); + ~WipperSnapper_I2C_Driver_QMC5883P(); + + bool begin(); + bool getEventRaw(sensors_event_t *magEvent); + +private: + Adafruit_QMC5883P *_qmc = nullptr; ///< Pointer to the QMC5883P sensor object +}; + +#endif // WipperSnapper_I2C_Driver_QMC5883P_H