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
/**
* @brief structure to hold a raw data from
* the pressure sensor
*/
typedef struct _PRESSURE{
uint8_t rBuffer[2];
uint8_t crc;
}PRESSURE_DATA;
typedef struct _PRESSURE
{
uint8_t rBuffer[2];
uint8_t crc;
} PRESSURE_DATA;
class PressureWrapper
{
@ -33,11 +33,25 @@ public:
*/
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 ();
private:
I2C *i2c;
PRESSURE_DATA data = {{0, 0}, 0};
PRESSURE_DATA data = { { 0, 0 }, 0 };
/*
* @return struct with pressure data in
* rBuffer and CRC check in crc

View File

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

View File

@ -8,26 +8,32 @@
#include "PressureWrapper.h"
#include <cstdio>
static uint8_t crc8(uint8_t *data, size_t size) {
uint8_t crc = 0x00;
uint8_t byteCtr;
//calculates 8-Bit checksum with given polynomial
for (byteCtr = 0; byteCtr < size; ++byteCtr) {
crc ^= (data[byteCtr]);
for (uint8_t bit = 8; bit > 0; --bit) {
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;
static uint8_t
crc8 (uint8_t *data, size_t size)
{
uint8_t crc = 0x00;
uint8_t byteCtr;
// calculates 8-Bit checksum with given polynomial
for (byteCtr = 0; byteCtr < size; ++byteCtr)
{
crc ^= (data[byteCtr]);
for (uint8_t bit = 8; bit > 0; --bit)
{
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 ()
{
NVIC_DisableIRQ(I2C0_IRQn);
I2C_config config(0, 55000, 1309);
I2C i2c_c(config);
i2c = &i2c_c;
NVIC_DisableIRQ (I2C0_IRQn);
I2C_config config (0, 55000, 1309);
I2C i2c_c (config);
i2c = &i2c_c;
}
PressureWrapper::~PressureWrapper ()
@ -35,27 +41,43 @@ PressureWrapper::~PressureWrapper ()
// TODO Auto-generated destructor stub
}
int PressureWrapper::getPressure() {
int16_t pressure = 0;
if(!getRawPressure ()) {
unsigned int i = 0;
while(i<7200) i++;
getRawPressure ();
i = 0;
}
if(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;
}
return -255;
int
PressureWrapper::getPressure ()
{
int16_t pressure = 0;
if (!getRawPressure ())
{
unsigned int i = 0;
while (i < 7200)
i++;
getRawPressure ();
i = 0;
}
if (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;
}
return -255;
}
bool PressureWrapper::getRawPressure () {
uint8_t getMeasurementComm = 0xF1;
return (i2c->transaction(ADDRESS, &getMeasurementComm, 1, data.rBuffer, 3));
bool
PressureWrapper::isAwake ()
{
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)
{
case Event::eEnter:
break;
pressure_status = pressure->isAwake ();
break;
case Event::eExit:
break;
case Event::eKey:
handleControlButtons (event.value);
break;
case Event::eTick:
if (pressure_status)
{
save (pressure->getPressure (), ((current_mode) ? AUTO : MANUAL));
}
else
{
pressure->wakeUp ();
}
break;
}
}