From 8179e6bb841971508badcdb2dc5ffdc021bdd2e6 Mon Sep 17 00:00:00 2001 From: Vivien Henry Date: Sun, 5 Feb 2023 13:26:38 +0100 Subject: [PATCH] adding MS5837 driver example --- examples/nucleo_f042k6/ms5837/main.cpp | 73 +++++++++++++++++++++++ examples/nucleo_f042k6/ms5837/project.xml | 11 ++++ 2 files changed, 84 insertions(+) create mode 100644 examples/nucleo_f042k6/ms5837/main.cpp create mode 100644 examples/nucleo_f042k6/ms5837/project.xml diff --git a/examples/nucleo_f042k6/ms5837/main.cpp b/examples/nucleo_f042k6/ms5837/main.cpp new file mode 100644 index 0000000000..5befdf22bb --- /dev/null +++ b/examples/nucleo_f042k6/ms5837/main.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2023, Vivien Henry + * + * 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 << "MS5837 demo" << modm::endl; + + I2cMaster1::connect(); + I2cMaster1::initialize(); + + // Create a sensor object + modm::ms5837data::Data sensor_data; + modm::Ms5837 sensor(sensor_data); + + int32_t press, temp; + float press_f, temp_f; + + // Turn on and configure the pressure sensor + bool success = RF_CALL_BLOCKING(sensor.initialize()); + + if(!success) + { + MODM_LOG_DEBUG << "MS5837 Initialition failed" << modm::endl; + } + + while (true) + { + //Read the sensor data and print it out + success = RF_CALL_BLOCKING(sensor.readout()); + + if(success) + { + press = sensor_data.getPressure(); // int32_t + //press_f = sensor_data.getPressure(); // float + sensor_data.getPressure(press_f); // float via reference + + MODM_LOG_INFO << "Pressure: (tenth of mbar): " << press << " and in mbar (float)" << press_f << modm::endl; + + temp = sensor_data.getTemperature(); + //temp_f = sensor_data.getTemperature(); + sensor_data.getTemperature(temp_f); + + MODM_LOG_INFO << "Temp: (0.01°C): " << temp << " and °C" << temp_f << modm::endl; + } + else + { + MODM_LOG_INFO << "Sensor could not be read!" << modm::endl; + } + modm::delay(1s); + } + return 0; +} diff --git a/examples/nucleo_f042k6/ms5837/project.xml b/examples/nucleo_f042k6/ms5837/project.xml new file mode 100644 index 0000000000..68067ab3ab --- /dev/null +++ b/examples/nucleo_f042k6/ms5837/project.xml @@ -0,0 +1,11 @@ + + modm:nucleo-f042k6 + + + + + modm:build:scons + modm:driver:ms5837 + modm:platform:i2c:1 + +