Skyward boardcore
Loading...
Searching...
No Matches
LPS331AP.cpp
Go to the documentation of this file.
1
2/* Copyright (c) 2023 Skyward Experimental Rocketry
3 * Author: Alberto Nidasio
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#include "LPS331AP.h"
25
27
28namespace Boardcore
29{
30
31LPS331AP::LPS331AP(I2C& bus, ODR odr) : bus(bus), odr(odr) {}
32
34{
35 if (!checkWhoAmI())
36 return false;
37
38 uint8_t ctrlReg1 = 0;
39 ctrlReg1 |= 0x80; // Active mode
40 ctrlReg1 |= static_cast<uint8_t>(odr) << 4;
41
42 if (!bus.writeRegister(slaveConfig, REG_CTRL_REG1, ctrlReg1))
43 {
45 return false;
46 }
47
48 // Maximum possible oversampling to lower the noise
49 uint8_t ctrlConf = odr != ODR::ODR_25Hz ? 0x69 : 0x7a;
50
51 if (!bus.writeRegister(slaveConfig, REG_RES_CONF, ctrlConf))
52 {
54 return false;
55 }
56
57 return true;
58}
59
60bool LPS331AP::selfTest() { return checkWhoAmI(); }
61
63{
64 uint8_t buffer[5];
65 if (bus.readFromRegister(slaveConfig, REG_PRESS_XLSB, buffer, 5))
66 {
67 LPS331APData data;
68
69 int32_t pressure = 0;
70 pressure |= static_cast<uint32_t>(buffer[0]) << 16;
71 pressure |= static_cast<uint32_t>(buffer[1]) << 8;
72 pressure |= static_cast<uint32_t>(buffer[2]);
73
74 int32_t temperature = 0;
75 temperature |= static_cast<uint32_t>(buffer[3]) << 8;
76 temperature |= static_cast<uint32_t>(buffer[4]);
77
80 data.pressure = pressure / 4096.0f;
81 data.temperature = temperature / 480.0f + 42.5f;
82
83 return data;
84 }
85 else
86 {
88 return lastSample;
89 }
90}
91
92bool LPS331AP::checkWhoAmI()
93{
94 uint8_t whoAmIValue;
95
96 if (bus.readRegister(slaveConfig, REG_WHO_AM_I, whoAmIValue))
97 {
98 if (whoAmIValue == WHO_AM_I_VAL)
99 {
100 return true;
101 }
102 else
103 {
104 LOG_ERR(logger, "Invalid WHO AM I");
106 return false;
107 }
108 }
109 else
110 {
112 return false;
113 }
114}
115
116} // namespace Boardcore
#define LOG_ERR(logger,...)
SensorErrors lastError
Definition Sensor.h:54
High level driver for the I2C peripherals.
Definition I2C.h:40
bool readFromRegister(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, void *buffer, const size_t nBytes)
Non blocking operation to read n-bytes from register from a slave.
Definition I2C.cpp:189
bool writeRegister(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, const uint8_t registerContent)
Non blocking operation to write an 8-bit register from a slave.
Definition I2C.cpp:111
bool readRegister(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, uint8_t &registerContent)
Non blocking operation to read an 8-bit register from a slave.
Definition I2C.cpp:48
LPS331APData sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
Definition LPS331AP.cpp:62
bool init() override
Initialize the sensor.
Definition LPS331AP.cpp:33
LPS331AP(I2C &bus, ODR odr=ODR::ODR_25Hz)
Definition LPS331AP.cpp:31
bool selfTest() override
Check if the sensor is working.
Definition LPS331AP.cpp:60
uint64_t getTimestamp()
Returns the current timer value in microseconds.
This file includes all the types the logdecoder script will decode.
@ INVALID_WHOAMI
Definition SensorData.h:40