pressure: create state for reading pressure

This commit is contained in:
Vasily Davydov 2022-10-24 16:16:49 +03:00
parent cf8d491516
commit 7871953fe6
4 changed files with 89 additions and 43 deletions

View File

@ -13,16 +13,16 @@
#define ADDRESS 0x40 #define ADDRESS 0x40
/** /**
* @brief structure to hold a raw data from * @brief structure to hold a raw data from
* the pressure sensor * the pressure sensor
*/ */
typedef struct _PRESSURE{ typedef struct _PRESSURE
uint8_t rBuffer[2]; {
uint8_t crc; uint8_t rBuffer[2];
}PRESSURE_DATA; uint8_t crc;
} PRESSURE_DATA;
class PressureWrapper class PressureWrapper
{ {
@ -33,11 +33,25 @@ public:
*/ */
int getPressure (); int getPressure ();
/**
* @brief Check if sensor is ready
*
* @return true if awake
* @return false if asleep
*/
bool isAwake ();
/**
* @brief Wake the sensor up
*
*/
void wakeUp ();
virtual ~PressureWrapper (); virtual ~PressureWrapper ();
private: private:
I2C *i2c; I2C *i2c;
PRESSURE_DATA data = {{0, 0}, 0}; PRESSURE_DATA data = { { 0, 0 }, 0 };
/* /*
* @return struct with pressure data in * @return struct with pressure data in
* rBuffer and CRC check in crc * rBuffer and CRC check in crc

View File

@ -132,6 +132,7 @@ private:
LiquidCrystal *_lcd; LiquidCrystal *_lcd;
ModbusRegister *A01; ModbusRegister *A01;
PressureWrapper *pressure; PressureWrapper *pressure;
bool pressure_status;
Timer *state_timer; Timer *state_timer;
/* CO2 sensor object */ /* CO2 sensor object */
GMP252 co2; GMP252 co2;

View File

@ -8,26 +8,32 @@
#include "PressureWrapper.h" #include "PressureWrapper.h"
#include <cstdio> #include <cstdio>
static uint8_t crc8(uint8_t *data, size_t size) { static uint8_t
uint8_t crc = 0x00; crc8 (uint8_t *data, size_t size)
uint8_t byteCtr; {
//calculates 8-Bit checksum with given polynomial uint8_t crc = 0x00;
for (byteCtr = 0; byteCtr < size; ++byteCtr) { uint8_t byteCtr;
crc ^= (data[byteCtr]); // calculates 8-Bit checksum with given polynomial
for (uint8_t bit = 8; bit > 0; --bit) { for (byteCtr = 0; byteCtr < size; ++byteCtr)
if (crc & 0x80) crc = (crc << 1) ^ 0x131; //P(x)=x^8+x^5+x^4+1 = 0001 0011 0001 {
else crc = (crc << 1); crc ^= (data[byteCtr]);
} for (uint8_t bit = 8; bit > 0; --bit)
} {
return crc; if (crc & 0x80)
crc = (crc << 1) ^ 0x131; // P(x)=x^8+x^5+x^4+1 = 0001 0011 0001
else
crc = (crc << 1);
}
}
return crc;
} }
PressureWrapper::PressureWrapper () PressureWrapper::PressureWrapper ()
{ {
NVIC_DisableIRQ(I2C0_IRQn); NVIC_DisableIRQ (I2C0_IRQn);
I2C_config config(0, 55000, 1309); I2C_config config (0, 55000, 1309);
I2C i2c_c(config); I2C i2c_c (config);
i2c = &i2c_c; i2c = &i2c_c;
} }
PressureWrapper::~PressureWrapper () PressureWrapper::~PressureWrapper ()
@ -35,27 +41,43 @@ PressureWrapper::~PressureWrapper ()
// TODO Auto-generated destructor stub // TODO Auto-generated destructor stub
} }
int PressureWrapper::getPressure() { int
int16_t pressure = 0; PressureWrapper::getPressure ()
if(!getRawPressure ()) { {
unsigned int i = 0; int16_t pressure = 0;
while(i<7200) i++; if (!getRawPressure ())
getRawPressure (); {
i = 0; unsigned int i = 0;
} while (i < 7200)
if(crc8(data.rBuffer, 2) == data.crc){ i++;
pressure = data.rBuffer[0]; getRawPressure ();
pressure = pressure << 8; i = 0;
pressure |= data.rBuffer[1]; }
float result = (float) pressure * 0.95 / 240; if (crc8 (data.rBuffer, 2) == data.crc)
return (int) result; {
} pressure = data.rBuffer[0];
return -255; pressure = pressure << 8;
pressure |= data.rBuffer[1];
float result = (float)pressure * 0.95 / 240;
return (int)result;
}
return -255;
} }
bool PressureWrapper::getRawPressure () { bool
uint8_t getMeasurementComm = 0xF1; PressureWrapper::isAwake ()
return (i2c->transaction(ADDRESS, &getMeasurementComm, 1, data.rBuffer, 3)); {
return true;
} }
void
PressureWrapper::wakeUp ()
{
}
bool
PressureWrapper::getRawPressure ()
{
uint8_t getMeasurementComm = 0xF1;
return (i2c->transaction (ADDRESS, &getMeasurementComm, 1, data.rBuffer, 3));
}

View File

@ -202,13 +202,22 @@ StateHandler::stateGetPressure (const Event &event)
switch (event.type) switch (event.type)
{ {
case Event::eEnter: case Event::eEnter:
break; pressure_status = pressure->isAwake ();
break;
case Event::eExit: case Event::eExit:
break; break;
case Event::eKey: case Event::eKey:
handleControlButtons (event.value);
break; break;
case Event::eTick: case Event::eTick:
if (pressure_status)
{
save (pressure->getPressure (), ((current_mode) ? AUTO : MANUAL));
}
else
{
pressure->wakeUp ();
}
break; break;
} }
} }