PressureWrapper: add getPressure()

This commit is contained in:
Evgenii Meshcheriakov 2022-10-17 17:24:42 +03:00
parent db4c4efb1a
commit 3bbbf2ae4a
4 changed files with 31 additions and 13 deletions

View File

@ -37,7 +37,7 @@
* this code. * this code.
*/ */
#include "../inc/I2C.h" #include "I2C.h"
I2C::I2C(const I2C_config &cfg): device(nullptr) { I2C::I2C(const I2C_config &cfg): device(nullptr) {

View File

@ -35,13 +35,26 @@ PressureWrapper::~PressureWrapper ()
} }
int PressureWrapper::getPressure() { int PressureWrapper::getPressure() {
int16_t pressure = 0;
if(!getRawPressure ()) {
unsigned int i = 0;
while(i<7200000) i++;
getRawPressure ();
i = 0;
}
// do {
// getRawPressure ();
// } while (crc8(data.rBuffer, 2) != data.crc);
pressure = data.rBuffer[0];
pressure = pressure << 8;
pressure |= data.rBuffer[1];
float result = (float) pressure * 0.95 / 240;
return (int) result;
} }
PRESSURE_DATA* PressureWrapper::getRawPressure () { bool PressureWrapper::getRawPressure () {
uint8_t getMeasurementComm = 0xF1; uint8_t getMeasurementComm = 0xF1;
i2c->transaction(0x40, &getMeasurementComm, 1, data.rBuffer, 3); return (i2c->transaction(ADDRESS, &getMeasurementComm, 1, data.rBuffer, 3));
return &data;
} }

View File

@ -12,8 +12,12 @@
#include <cstdio> #include <cstdio>
#define ADDRESS 0x40 #define ADDRESS 0x40
#define READADD 0x81
#define WRITEADD 0x80
/**
* @brief structure to hold a raw data from
* the pressure sensor
*/
typedef struct _PRESSURE{ typedef struct _PRESSURE{
uint8_t rBuffer[2]; uint8_t rBuffer[2];
@ -24,11 +28,8 @@ class PressureWrapper
{ {
public: public:
PressureWrapper (); PressureWrapper ();
/** /*
* @brief Get the Status object * @return pressure in Pascal
*
* @return true
* @return false
*/ */
int getPressure (); int getPressure ();
@ -37,7 +38,11 @@ public:
private: private:
I2C *i2c; I2C *i2c;
PRESSURE_DATA data = {{0, 0}, 0}; PRESSURE_DATA data = {{0, 0}, 0};
PRESSURE_DATA* getRawPressure (); /*
* @return struct with pressure data in
* rBuffer and CRC check in crc
*/
bool getRawPressure ();
}; };
#endif /* PRESSUREWRAPPER_H_ */ #endif /* PRESSUREWRAPPER_H_ */