Skip to content

Commit

Permalink
adding MS5837 driver example
Browse files Browse the repository at this point in the history
  • Loading branch information
lukh authored and salkinium committed Feb 5, 2023
1 parent 1957994 commit 8179e6b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
73 changes: 73 additions & 0 deletions 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 <modm/board.hpp>
#include <modm/driver/pressure/ms5837.hpp>


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<I2cSda::Sda, I2cScl::Scl>();
I2cMaster1::initialize<SystemClock, 100_kBd>();

// Create a sensor object
modm::ms5837data::Data sensor_data;
modm::Ms5837<I2cMaster1> 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;
}
11 changes: 11 additions & 0 deletions examples/nucleo_f042k6/ms5837/project.xml
@@ -0,0 +1,11 @@
<library>
<extends>modm:nucleo-f042k6</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_f042k6/ms5837</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:driver:ms5837</module>
<module>modm:platform:i2c:1</module>
</modules>
</library>

0 comments on commit 8179e6b

Please sign in to comment.