From 0994a557a73b344673a105eaa2eaedb8a370418b Mon Sep 17 00:00:00 2001 From: Benjamin Carrick Date: Mon, 6 Jul 2020 21:33:11 +0200 Subject: [PATCH] Example for the LIS3MDL driver on the Nucleo F042K6 --- examples/nucleo_f042k6/lis3mdl/main.cpp | 73 ++++++++++++++++++++++ examples/nucleo_f042k6/lis3mdl/project.xml | 11 ++++ 2 files changed, 84 insertions(+) create mode 100644 examples/nucleo_f042k6/lis3mdl/main.cpp create mode 100644 examples/nucleo_f042k6/lis3mdl/project.xml diff --git a/examples/nucleo_f042k6/lis3mdl/main.cpp b/examples/nucleo_f042k6/lis3mdl/main.cpp new file mode 100644 index 0000000000..461a11c476 --- /dev/null +++ b/examples/nucleo_f042k6/lis3mdl/main.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2020, Benjamin Carrick + * + * This file is part of the modm project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include +#include + +using namespace Board; +using namespace std::chrono_literals; + +using I2cSda = GpioA10; +using I2cScl = GpioA9; + +int +main() +{ + Board::initialize(); + LedD13::setOutput(); + + MODM_LOG_INFO << "LIS3MDL demo" << modm::endl; + + I2cMaster1::connect(); + I2cMaster1::initialize(); + + // Create a sensor object with the adress of the sensor built onto the Pololu AltIMU-10 v5 + modm::Lis3mdl sensor(0x1E); + + // Turn on and configure the magnetometer + bool success = RF_CALL_BLOCKING(sensor.configure(modm::lis3mdl::DataRate::Rate_5_Hz, + modm::lis3mdl::Scale::Scale_8_gauss)); + + + if(!success) + { + MODM_LOG_INFO << "Sensor could not be configured!" << modm::endl; + } + + // Set the sensor to continous acquistion and turn on the temperature sensing + success = RF_CALL_BLOCKING(sensor.setMode(modm::lis3mdl::OperationMode::Continous)); + if(!success) + { + MODM_LOG_INFO << "Sensor could not be started!" << modm::endl; + } + + modm::Vector3f magVector; + + while (true) + { + //Read the sensor data and print it out + success = RF_CALL_BLOCKING(sensor.readMagnetometer(magVector)); + + if(success) + { + MODM_LOG_INFO << "Magnetic Vector:" << modm::endl; + MODM_LOG_INFO << "X: "<< magVector.x << " gauss" << modm::endl; + MODM_LOG_INFO << "Y: "<< magVector.y << " gauss" << modm::endl; + MODM_LOG_INFO << "Z: "<< magVector.z << " gauss" << modm::endl; + MODM_LOG_INFO << modm::endl; + } + else + { + MODM_LOG_INFO << "Sensor could not be read!" << modm::endl; + } + modm::delay(1s); + } + return 0; +} diff --git a/examples/nucleo_f042k6/lis3mdl/project.xml b/examples/nucleo_f042k6/lis3mdl/project.xml new file mode 100644 index 0000000000..e03311dcad --- /dev/null +++ b/examples/nucleo_f042k6/lis3mdl/project.xml @@ -0,0 +1,11 @@ + + modm:nucleo-f042k6 + + + + + modm:build:scons + modm:driver:lis3mdl + modm:platform:i2c:1 + +