Skip to content

Commit

Permalink
Added BMM150 Example
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Oct 10, 2023
1 parent e8250ce commit bc18f81
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/arduino_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jobs:
examples/BHI260AP_Orientation/BHI260AP_Orientation.ino,
examples/BHI260AP_DebugInfo/BHI260AP_DebugInfo.ino,
examples/BHI260AP_StepCounter/BHI260AP_StepCounter.ino,
examples/BMM150_GetDataExample/BMM150_GetDataExample.ino,
]

env:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pio.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
examples/BHI260AP_Orientation/BHI260AP_Orientation.ino,
examples/BHI260AP_DebugInfo/BHI260AP_DebugInfo.ino,
examples/BHI260AP_StepCounter/BHI260AP_StepCounter.ino,
examples/BMM150_GetDataExample/BMM150_GetDataExample.ino,
]

steps:
Expand Down
102 changes: 102 additions & 0 deletions examples/BMM150_GetDataExample/BMM150_GetDataExample.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
*
* @license MIT License
*
* Copyright (c) 2023 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BMM150_GetDataExample.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-10-10
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorBMM150.hpp"
#include "SensorWireHelper.h"

#ifndef SENSOR_SDA
#define SENSOR_SDA 33
#endif

#ifndef SENSOR_SCL
#define SENSOR_SCL 35
#endif

#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif

#ifndef SENSOR_RST
#define SENSOR_RST -1
#endif


SensorBMM150 bmm;



void setup()
{
Serial.begin(115200);
while (!Serial);

// Using I2C interface
if (!bmm.init(Wire, SENSOR_SDA, SENSOR_SCL, BMM150_I2C_ADDRESS_CSB_HIGH_SDO_LOW)) {
Serial.print("Failed to init BMM150 - check your wiring!");
while (1) {
delay(1000);
}
}

Serial.println("Init BMM150 Sensor success!");

/* Set magnetometer run mode */
/**
* * POWERMODE_NORMAL,
* * POWERMODE_FORCED,
* * POWERMODE_SLEEP,
* * POWERMODE_SUSPEND,
*/
bmm.setMode(SensorBMM150::POWERMODE_NORMAL);

}


void loop()
{
int16_t x, y, z;
/* Read mag data */
/* Unit for magnetometer data is microtesla(uT) */
if (bmm.getMag(x, y, z)) {
Serial.print("X:");
Serial.print(x);
Serial.print("uT,");
Serial.print("Y:");
Serial.print(y);
Serial.print("uT,");
Serial.print("Z:");
Serial.print(z);
Serial.println("uT");
}
delay(50);
}

3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
; src_dir = examples/BHI260AP_6DoF
; src_dir = examples/BHI260AP_Orientation
; src_dir = examples/BHI260AP_StepCounter
src_dir = examples/BHI260AP_DebugInfo
; src_dir = examples/BHI260AP_DebugInfo
src_dir = examples/BMM150_GetDataExample

; Touch devices support list
; src_dir = examples/TouchDrv_FT3267_LilyGo_T_RGB
Expand Down
114 changes: 111 additions & 3 deletions src/SensorBMM150.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
* Copyright (c) 2023 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -46,8 +46,20 @@

class SensorBMM150
{
friend class BoschParse;
public:

enum PowerMode {
POWERMODE_NORMAL,
POWERMODE_FORCED,
POWERMODE_SLEEP,
POWERMODE_SUSPEND,
};

enum InterruptLevel {
INTERRUPT_HIGH_ACTIVE,
INTERRUPT_LOW_ACTIVE,
};

SensorBMM150(PLATFORM_WIRE_TYPE &w, int sda = DEFAULT_SDA, int scl = DEFAULT_SCL, uint8_t addr = BMM150_DEFAULT_I2C_ADDRESS)
{
__handler.u.i2c_dev.scl = scl;
Expand Down Expand Up @@ -133,6 +145,98 @@ class SensorBMM150
}
}

void sleep()
{
setMode(POWERMODE_SLEEP);
}

bool setMode(PowerMode mode)
{
settings.pwr_mode = mode;
return bmm150_set_op_mode(&settings, dev) == BMM150_OK;
}

bool setThreshold(uint8_t high_th, uint8_t low_th)
{
settings.int_settings.high_threshold = high_th;
settings.int_settings.low_threshold = low_th;
return bmm150_set_sensor_settings(BMM150_SEL_HIGH_THRESHOLD_SETTING, &settings, dev) == BMM150_OK;
}

bool setInterruptLevel(InterruptLevel level)
{
settings.int_settings.high_int_en = level;
return bmm150_set_sensor_settings(BMM150_SEL_HIGH_THRESHOLD_INT, &settings, dev) == BMM150_OK;
}

bool enableINT()
{
settings.int_settings.int_pin_en = BMM150_INT_ENABLE;
return bmm150_set_sensor_settings(BMM150_SEL_INT_PIN_EN, &settings, dev) == BMM150_OK;
}

bool disableINT()
{
settings.int_settings.int_pin_en = BMM150_INT_DISABLE;
return bmm150_set_sensor_settings(BMM150_SEL_INT_PIN_EN, &settings, dev) == BMM150_OK;
}

bool enabledDtatReady()
{
settings.int_settings.drdy_pin_en = BMM150_INT_ENABLE;
return bmm150_set_sensor_settings(BMM150_SEL_DRDY_PIN_EN, &settings, dev) == BMM150_OK;
}

bool disabledDtatReady()
{
settings.int_settings.drdy_pin_en = BMM150_INT_DISABLE;
return bmm150_set_sensor_settings(BMM150_SEL_DRDY_PIN_EN, &settings, dev) == BMM150_OK;
}

uint8_t getChipID()
{
return dev->chip_id;
}

uint8_t getInterruptStatus()
{
bmm150_get_interrupt_status(dev);
return dev->int_status;
}

bool isDataReady()
{
return dev->int_status & BMM150_INT_ASSERTED_DRDY;
}

bool isLowThreshold()
{
return dev->int_status & BMM150_INT_ASSERTED_LOW_THRES;
}

bool isHighThreshold()
{
return dev->int_status & BMM150_INT_ASSERTED_HIGH_THRES;
}

struct bmm150_mag_data getMag()
{
struct bmm150_mag_data data = {0};
bmm150_read_mag_data(&data, dev);
return data;
}

bool getMag(int16_t &x, int16_t &y, int16_t &z)
{
struct bmm150_mag_data data;
if (bmm150_read_mag_data(&data, dev) != BMM150_OK) {
return false;
}
x = data.x;
y = data.y;
z = data.z;
return true;
}

private:

Expand All @@ -141,12 +245,13 @@ class SensorBMM150
*(bool *)(available) = true;
}


bool initImpl()
{
int8_t rslt;
uint8_t product_id = 0;

memset(&settings, 0, sizeof(settings));

if (__handler.rst != SENSOR_PIN_NONE) {
pinMode(__handler.rst, OUTPUT);
}
Expand Down Expand Up @@ -202,6 +307,8 @@ class SensorBMM150
return false;
}

bmm150_get_sensor_settings(&settings, dev);

if (__handler.irq != SENSOR_PIN_NONE) {
#if defined(ARDUINO_ARCH_RP2040)
attachInterruptParam((pin_size_t)(__handler.irq), handleISR, (PinStatus )RISING, (void *)&__data_available);
Expand All @@ -214,6 +321,7 @@ class SensorBMM150
}

protected:
struct bmm150_settings settings;
struct bmm150_dev *dev = NULL;
SensorLibConfigure __handler;
int8_t __error_code;
Expand Down

0 comments on commit bc18f81

Please sign in to comment.