Working sample

This commit is contained in:
Robert Jaakke 2020-06-06 22:35:41 +02:00
parent 344d544e43
commit 6667951505
12 changed files with 2559 additions and 2 deletions

View File

@ -0,0 +1,4 @@
# Arduino library for the HP303B
### Installation
- Clone this repository or download&unzip [zip file](https://github.com/wemos/LOLIN_HP303B_Library/archive/master.zip) into Arduino/libraries

View File

@ -0,0 +1,98 @@
#include <LOLIN_HP303B.h>
// HP303B Opject
LOLIN_HP303B HP303BPressureSensor = LOLIN_HP303B();
void setup()
{
Serial.begin(115200);
while (!Serial);
//Call begin to initialize HP303BPressureSensor
//The parameter 0x76 is the bus address. The default address is 0x77 and does not need to be given.
//HP303BPressureSensor.begin(Wire, 0x76);
//Use the commented line below instead to use the default I2C address.
HP303BPressureSensor.begin();
//temperature measure rate (value from 0 to 7)
//2^temp_mr temperature measurement results per second
int16_t temp_mr = 2;
//temperature oversampling rate (value from 0 to 7)
//2^temp_osr internal temperature measurements per result
//A higher value increases precision
int16_t temp_osr = 2;
//pressure measure rate (value from 0 to 7)
//2^prs_mr pressure measurement results per second
int16_t prs_mr = 2;
//pressure oversampling rate (value from 0 to 7)
//2^prs_osr internal pressure measurements per result
//A higher value increases precision
int16_t prs_osr = 2;
//startMeasureBothCont enables background mode
//temperature and pressure ar measured automatically
//High precision and hgh measure rates at the same time are not available.
//Consult Datasheet (or trial and error) for more information
int16_t ret = HP303BPressureSensor.startMeasureBothCont(temp_mr, temp_osr, prs_mr, prs_osr);
//Use one of the commented lines below instead to measure only temperature or pressure
//int16_t ret = HP303BPressureSensor.startMeasureTempCont(temp_mr, temp_osr);
//int16_t ret = HP303BPressureSensor.startMeasurePressureCont(prs_mr, prs_osr);
if (ret != 0)
{
Serial.print("Init FAILED! ret = ");
Serial.println(ret);
}
else
{
Serial.println("Init complete!");
}
}
void loop()
{
unsigned char pressureCount = 20;
int32_t pressure[pressureCount];
unsigned char temperatureCount = 20;
int32_t temperature[temperatureCount];
//This function writes the results of continuous measurements to the arrays given as parameters
//The parameters temperatureCount and pressureCount should hold the sizes of the arrays temperature and pressure when the function is called
//After the end of the function, temperatureCount and pressureCount hold the numbers of values written to the arrays
//Note: The HP303B cannot save more than 32 results. When its result buffer is full, it won't save any new measurement results
int16_t ret = HP303BPressureSensor.getContResults(temperature, temperatureCount, pressure, pressureCount);
if (ret != 0)
{
Serial.println();
Serial.println();
Serial.print("FAIL! ret = ");
Serial.println(ret);
}
else
{
Serial.println();
Serial.println();
Serial.print(temperatureCount);
Serial.println(" temperature values found: ");
for (int16_t i = 0; i < temperatureCount; i++)
{
Serial.print(temperature[i]);
Serial.println(" degrees of Celsius");
}
Serial.println();
Serial.print(pressureCount);
Serial.println(" pressure values found: ");
for (int16_t i = 0; i < pressureCount; i++)
{
Serial.print(pressure[i]);
Serial.println(" Pascal");
}
}
//Wait some time, so that the HP303B can refill its buffer
delay(10000);
}

View File

@ -0,0 +1,75 @@
#include <LOLIN_HP303B.h>
// HP303B Opject
LOLIN_HP303B HP303BPressureSensor;
void setup()
{
Serial.begin(115200);
while (!Serial);
//Call begin to initialize HP303BPressureSensor
//The parameter 0x76 is the bus address. The default address is 0x77 and does not need to be given.
//HP303BPressureSensor.begin(Wire, 0x76);
//Use the commented line below instead of the one above to use the default I2C address.
//if you are using the Pressure 3 click Board, you need 0x76
HP303BPressureSensor.begin();
Serial.println("Init complete!");
}
void loop()
{
int32_t temperature;
int32_t pressure;
int16_t oversampling = 7;
int16_t ret;
Serial.println();
//lets the HP303B perform a Single temperature measurement with the last (or standard) configuration
//The result will be written to the paramerter temperature
//ret = HP303BPressureSensor.measureTempOnce(temperature);
//the commented line below does exactly the same as the one above, but you can also config the precision
//oversampling can be a value from 0 to 7
//the HP303B will perform 2^oversampling internal temperature measurements and combine them to one result with higher precision
//measurements with higher precision take more time, consult datasheet for more information
ret = HP303BPressureSensor.measureTempOnce(temperature, oversampling);
if (ret != 0)
{
//Something went wrong.
//Look at the library code for more information about return codes
Serial.print("FAIL! ret = ");
Serial.println(ret);
}
else
{
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" degrees of Celsius");
}
//Pressure measurement behaves like temperature measurement
//ret = HP303BPressureSensor.measurePressureOnce(pressure);
ret = HP303BPressureSensor.measurePressureOnce(pressure, oversampling);
if (ret != 0)
{
//Something went wrong.
//Look at the library code for more information about return codes
Serial.print("FAIL! ret = ");
Serial.println(ret);
}
else
{
Serial.print("Pressure: ");
Serial.print(pressure);
Serial.println(" Pascal");
}
//Wait some time
delay(500);
}

View File

@ -0,0 +1,112 @@
#include <LOLIN_HP303B.h>
// HP303B Opject
LOLIN_HP303B HP303BPressureSensor = LOLIN_HP303B();
void onFifoFull();
const unsigned char pressureLength = 50;
unsigned char pressureCount = 0;
int32_t pressure[pressureLength];
unsigned char temperatureCount = 0;
const unsigned char temperatureLength = 50;
int32_t temperature[temperatureLength];
void setup()
{
Serial.begin(115200);
while (!Serial);
//Call begin to initialize HP303BPressureSensor
//The parameter 0x76 is the bus address. The default address is 0x77 and does not need to be given.
//HP303BPressureSensor.begin(Wire, 0x76);
//Use the commented line below instead to use the default I2C address.
HP303BPressureSensor.begin();
int16_t ret = HP303BPressureSensor.setInterruptPolarity(1);
ret = HP303BPressureSensor.setInterruptSources(1, 0, 0);
//clear interrupt flag by reading
HP303BPressureSensor.getIntStatusFifoFull();
//initialization of Interrupt for Controller unit
//SDO pin of HP303B has to be connected with interrupt pin
int16_t interruptPin = 3;
pinMode(interruptPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), onFifoFull, RISING);
//start of a continuous measurement just like before
int16_t temp_mr = 3;
int16_t temp_osr = 2;
int16_t prs_mr = 1;
int16_t prs_osr = 3;
ret = HP303BPressureSensor.startMeasureBothCont(temp_mr, temp_osr, prs_mr, prs_osr);
if (ret != 0)
{
Serial.print("Init FAILED! ret = ");
Serial.println(ret);
}
else
{
Serial.println("Init complete!");
}
}
void loop()
{
//do other stuff
Serial.println("loop running");
delay(500);
//if result arrays are full
//This could also be in the interrupt handler, but it would take too much time for a proper ISR
if (pressureCount == pressureLength && temperatureCount == temperatureLength)
{
//print results
Serial.println();
Serial.println();
Serial.print(temperatureCount);
Serial.println(" temperature values found: ");
for (int16_t i = 0; i < temperatureCount; i++)
{
Serial.print(temperature[i]);
Serial.println(" degrees of Celsius");
}
Serial.println();
Serial.print(pressureCount);
Serial.println(" pressure values found: ");
for (int16_t i = 0; i < pressureCount; i++)
{
Serial.print(pressure[i]);
Serial.println(" Pascal");
}
Serial.println();
Serial.println();
//reset result counters
pressureCount = 0;
temperatureCount = 0;
}
}
//interrupt handler
void onFifoFull()
{
//message for debugging
Serial.println("Interrupt handler called");
//clear interrupt flag by reading
HP303BPressureSensor.getIntStatusFifoFull();
//calculate the number of free indexes in the result arrays
unsigned char prs_freespace = pressureLength - pressureCount;
unsigned char temp_freespace = temperatureLength - temperatureCount;
//read the results from HP303B, new results will be added at the end of the arrays
HP303BPressureSensor.getContResults(&temperature[temperatureCount], temp_freespace, &pressure[pressureCount], prs_freespace);
//after reading the result counters are increased by the amount of new results
pressureCount += prs_freespace;
temperatureCount += temp_freespace;
}

View File

@ -0,0 +1,26 @@
#######################################
# Syntax Coloring Map For DHT12
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
DHT12 KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
get KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
cTemp LITERAL1
fTemp LITERAL1
humidity LITERAL1

View File

@ -0,0 +1,9 @@
name=LOLIN_HP303B
version=1.0.0
author=WEMOS.CC <support@wemos.cc>
maintainer=WEMOS.CC
sentence=Library for the <a href="https://www.wemos.cc">HP303B.</a>.
paragraph=LOLIN HP303B
category=Device Control
url=https://github.com/wemos/LOLIN_HP303B_Library
architectures=*

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,144 @@
#ifndef __LOLIN_HP303B_H
#define __LOLIN_HP303B_H
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include <SPI.h>
#include "util/hp303b_consts.h"
class LOLIN_HP303B
{
public:
//constructor
LOLIN_HP303B(void);
//destructor
~LOLIN_HP303B(void);
//begin
void begin(TwoWire &bus, uint8_t slaveAddress);
void begin(uint8_t slaveAddress=HP303B__STD_SLAVE_ADDRESS);
void begin(SPIClass &bus, int32_t chipSelect);
void begin(SPIClass &bus, int32_t chipSelect, uint8_t threeWire);
//end
void end(void);
//general
uint8_t getProductId(void);
uint8_t getRevisionId(void);
//Idle Mode
int16_t standby(void);
//Command Mode
int16_t measureTempOnce(int32_t &result);
int16_t measureTempOnce(int32_t &result, uint8_t oversamplingRate);
int16_t startMeasureTempOnce(void);
int16_t startMeasureTempOnce(uint8_t oversamplingRate);
int16_t measurePressureOnce(int32_t &result);
int16_t measurePressureOnce(int32_t &result, uint8_t oversamplingRate);
int16_t startMeasurePressureOnce(void);
int16_t startMeasurePressureOnce(uint8_t oversamplingRate);
int16_t getSingleResult(int32_t &result);
//Background Mode
int16_t startMeasureTempCont(uint8_t measureRate, uint8_t oversamplingRate);
int16_t startMeasurePressureCont(uint8_t measureRate, uint8_t oversamplingRate);
int16_t startMeasureBothCont(uint8_t tempMr, uint8_t tempOsr, uint8_t prsMr, uint8_t prsOsr);
int16_t getContResults(int32_t *tempBuffer, uint8_t &tempCount, int32_t *prsBuffer, uint8_t &prsCount);
//Interrupt Control
int16_t setInterruptPolarity(uint8_t polarity);
int16_t setInterruptSources(uint8_t fifoFull, uint8_t tempReady, uint8_t prsReady);
int16_t getIntStatusFifoFull(void);
int16_t getIntStatusTempReady(void);
int16_t getIntStatusPrsReady(void);
//function to fix a hardware problem on some devices
int16_t correctTemp(void);
private:
//scaling factor table
static const int32_t scaling_facts[HP303B__NUM_OF_SCAL_FACTS];
//enum for operating mode
enum Mode
{
IDLE = 0x00,
CMD_PRS = 0x01,
CMD_TEMP = 0x02,
INVAL_OP_CMD_BOTH = 0x03, //invalid
INVAL_OP_CONT_NONE = 0x04, //invalid
CONT_PRS = 0x05,
CONT_TMP = 0x06,
CONT_BOTH = 0x07
};
Mode m_opMode;
//flags
uint8_t m_initFail;
uint8_t m_productID;
uint8_t m_revisionID;
//settings
uint8_t m_tempMr;
uint8_t m_tempOsr;
uint8_t m_prsMr;
uint8_t m_prsOsr;
uint8_t m_tempSensor;
//compensation coefficients
int32_t m_c0Half;
int32_t m_c1;
int32_t m_c00;
int32_t m_c10;
int32_t m_c01;
int32_t m_c11;
int32_t m_c20;
int32_t m_c21;
int32_t m_c30;
//last measured scaled temperature
//(necessary for pressure compensation)
double m_lastTempScal;
//bus specific
uint8_t m_SpiI2c; //0=SPI, 1=I2C
//used for I2C
TwoWire *m_i2cbus;
uint8_t m_slaveAddress;
//used for SPI
SPIClass *m_spibus;
int32_t m_chipSelect;
uint8_t m_threeWire;
//measurement
void init(void);
int16_t readcoeffs(void);
int16_t setOpMode(uint8_t background, uint8_t temperature, uint8_t pressure);
int16_t setOpMode(uint8_t opMode);
int16_t configTemp(uint8_t temp_mr, uint8_t temp_osr);
int16_t configPressure(uint8_t prs_mr, uint8_t prs_osr);
uint16_t calcBusyTime(uint16_t temp_rate, uint16_t temp_osr);
int16_t getTemp(int32_t *result);
int16_t getPressure(int32_t *result);
int16_t getFIFOvalue(int32_t *value);
int32_t calcTemp(int32_t raw);
int32_t calcPressure(int32_t raw);
//bus specific
int16_t readByte(uint8_t regAddress);
int16_t readByteSPI(uint8_t regAddress);
int16_t readBlock(uint8_t regAddress, uint8_t length, uint8_t *buffer);
int16_t readBlockSPI(uint8_t regAddress, uint8_t length, uint8_t *readbuffer);
int16_t writeByte(uint8_t regAddress, uint8_t data);
int16_t writeByte(uint8_t regAddress, uint8_t data, uint8_t check);
int16_t writeByteSpi(uint8_t regAddress, uint8_t data, uint8_t check);
int16_t writeByteBitfield(uint8_t data, uint8_t regAddress, uint8_t mask, uint8_t shift);
int16_t writeByteBitfield(uint8_t data, uint8_t regAddress, uint8_t mask, uint8_t shift, uint8_t check);
int16_t readByteBitfield(uint8_t regAddress, uint8_t mask, uint8_t shift);
};
#endif

View File

@ -0,0 +1,258 @@
/**
*
*
*/
#ifndef __HP303B_CONSTS_H_
#define __HP303B_CONSTS_H_
//general Constants
#define HP303B__PROD_ID 0U
#define HP303B__STD_SLAVE_ADDRESS 0x77U
#define HP303B__SPI_WRITE_CMD 0x00U
#define HP303B__SPI_READ_CMD 0x80U
#define HP303B__SPI_RW_MASK 0x80U
#define HP303B__SPI_MAX_FREQ 100000U
#define HP303B__LSB 0x01U
#define HP303B__TEMP_STD_MR 2U
#define HP303B__TEMP_STD_OSR 3U
#define HP303B__PRS_STD_MR 2U
#define HP303B__PRS_STD_OSR 3U
#define HP303B__OSR_SE 3U
//we use 0.1 mS units for time calculations, so 10 units are one millisecond
#define HP303B__BUSYTIME_SCALING 10U
// DPS310 has 10 milliseconds of spare time for each synchronous measurement / per second for asynchronous measurements
// this is for error prevention on friday-afternoon-products :D
// you can set it to 0 if you dare, but there is no warranty that it will still work
#define HP303B__BUSYTIME_FAILSAFE 10U
#define HP303B__MAX_BUSYTIME ((1000U-HP303B__BUSYTIME_FAILSAFE)*HP303B__BUSYTIME_SCALING)
#define HP303B__NUM_OF_SCAL_FACTS 8
#define HP303B__SUCCEEDED 0
#define HP303B__FAIL_UNKNOWN -1
#define HP303B__FAIL_INIT_FAILED -2
#define HP303B__FAIL_TOOBUSY -3
#define HP303B__FAIL_UNFINISHED -4
//Constants for register manipulation
//SPI mode (3 or 4 wire)
#define HP303B__REG_ADR_SPI3W 0x09U
#define HP303B__REG_CONTENT_SPI3W 0x01U
//product id
#define HP303B__REG_INFO_PROD_ID HP303B__REG_ADR_PROD_ID, \
HP303B__REG_MASK_PROD_ID, \
HP303B__REG_SHIFT_PROD_ID
#define HP303B__REG_ADR_PROD_ID 0x0DU
#define HP303B__REG_MASK_PROD_ID 0x0FU
#define HP303B__REG_SHIFT_PROD_ID 0U
//revision id
#define HP303B__REG_INFO_REV_ID HP303B__REG_ADR_REV_ID, \
HP303B__REG_MASK_REV_ID, \
HP303B__REG_SHIFT_REV_ID
#define HP303B__REG_ADR_REV_ID 0x0DU
#define HP303B__REG_MASK_REV_ID 0xF0U
#define HP303B__REG_SHIFT_REV_ID 4U
//operating mode
#define HP303B__REG_INFO_OPMODE HP303B__REG_ADR_OPMODE, \
HP303B__REG_MASK_OPMODE, \
HP303B__REG_SHIFT_OPMODE
#define HP303B__REG_ADR_OPMODE 0x08U
#define HP303B__REG_MASK_OPMODE 0x07U
#define HP303B__REG_SHIFT_OPMODE 0U
//temperature measure rate
#define HP303B__REG_INFO_TEMP_MR HP303B__REG_ADR_TEMP_MR, \
HP303B__REG_MASK_TEMP_MR, \
HP303B__REG_SHIFT_TEMP_MR
#define HP303B__REG_ADR_TEMP_MR 0x07U
#define HP303B__REG_MASK_TEMP_MR 0x70U
#define HP303B__REG_SHIFT_TEMP_MR 4U
//temperature oversampling rate
#define HP303B__REG_INFO_TEMP_OSR HP303B__REG_ADR_TEMP_OSR, \
HP303B__REG_MASK_TEMP_OSR, \
HP303B__REG_SHIFT_TEMP_OSR
#define HP303B__REG_ADR_TEMP_OSR 0x07U
#define HP303B__REG_MASK_TEMP_OSR 0x07U
#define HP303B__REG_SHIFT_TEMP_OSR 0U
//temperature sensor
#define HP303B__REG_INFO_TEMP_SENSOR HP303B__REG_ADR_TEMP_SENSOR, \
HP303B__REG_MASK_TEMP_SENSOR, \
HP303B__REG_SHIFT_TEMP_SENSOR
#define HP303B__REG_ADR_TEMP_SENSOR 0x07U
#define HP303B__REG_MASK_TEMP_SENSOR 0x80U
#define HP303B__REG_SHIFT_TEMP_SENSOR 7U
//temperature sensor recommendation
#define HP303B__REG_INFO_TEMP_SENSORREC HP303B__REG_ADR_TEMP_SENSORREC, \
HP303B__REG_MASK_TEMP_SENSORREC, \
HP303B__REG_SHIFT_TEMP_SENSORREC
#define HP303B__REG_ADR_TEMP_SENSORREC 0x28U
#define HP303B__REG_MASK_TEMP_SENSORREC 0x80U
#define HP303B__REG_SHIFT_TEMP_SENSORREC 7U
//temperature shift enable (if temp_osr>3)
#define HP303B__REG_INFO_TEMP_SE HP303B__REG_ADR_TEMP_SE, \
HP303B__REG_MASK_TEMP_SE, \
HP303B__REG_SHIFT_TEMP_SE
#define HP303B__REG_ADR_TEMP_SE 0x09U
#define HP303B__REG_MASK_TEMP_SE 0x08U
#define HP303B__REG_SHIFT_TEMP_SE 3U
//pressure measure rate
#define HP303B__REG_INFO_PRS_MR HP303B__REG_ADR_PRS_MR, \
HP303B__REG_MASK_PRS_MR, \
HP303B__REG_SHIFT_PRS_MR
#define HP303B__REG_ADR_PRS_MR 0x06U
#define HP303B__REG_MASK_PRS_MR 0x70U
#define HP303B__REG_SHIFT_PRS_MR 4U
//pressure oversampling rate
#define HP303B__REG_INFO_PRS_OSR HP303B__REG_ADR_PRS_OSR, \
HP303B__REG_MASK_PRS_OSR, \
HP303B__REG_SHIFT_PRS_OSR
#define HP303B__REG_ADR_PRS_OSR 0x06U
#define HP303B__REG_MASK_PRS_OSR 0x07U
#define HP303B__REG_SHIFT_PRS_OSR 0U
//pressure shift enable (if prs_osr>3)
#define HP303B__REG_INFO_PRS_SE HP303B__REG_ADR_PRS_SE, \
HP303B__REG_MASK_PRS_SE, \
HP303B__REG_SHIFT_PRS_SE
#define HP303B__REG_ADR_PRS_SE 0x09U
#define HP303B__REG_MASK_PRS_SE 0x04U
#define HP303B__REG_SHIFT_PRS_SE 2U
//temperature ready flag
#define HP303B__REG_INFO_TEMP_RDY HP303B__REG_ADR_TEMP_RDY, \
HP303B__REG_MASK_TEMP_RDY, \
HP303B__REG_SHIFT_TEMP_RDY
#define HP303B__REG_ADR_TEMP_RDY 0x08U
#define HP303B__REG_MASK_TEMP_RDY 0x20U
#define HP303B__REG_SHIFT_TEMP_RDY 5U
//pressure ready flag
#define HP303B__REG_INFO_PRS_RDY HP303B__REG_ADR_PRS_RDY, \
HP303B__REG_MASK_PRS_RDY, \
HP303B__REG_SHIFT_PRS_RDY
#define HP303B__REG_ADR_PRS_RDY 0x08U
#define HP303B__REG_MASK_PRS_RDY 0x10U
#define HP303B__REG_SHIFT_PRS_RDY 4U
//pressure value
#define HP303B__REG_ADR_PRS 0x00U
#define HP303B__REG_LEN_PRS 3U
//temperature value
#define HP303B__REG_ADR_TEMP 0x03U
#define HP303B__REG_LEN_TEMP 3U
//compensation coefficients
#define HP303B__REG_ADR_COEF 0x10U
#define HP303B__REG_LEN_COEF 18
//FIFO enable
#define HP303B__REG_INFO_FIFO_EN HP303B__REG_ADR_FIFO_EN, \
HP303B__REG_MASK_FIFO_EN, \
HP303B__REG_SHIFT_FIFO_EN
#define HP303B__REG_ADR_FIFO_EN 0x09U
#define HP303B__REG_MASK_FIFO_EN 0x02U
#define HP303B__REG_SHIFT_FIFO_EN 1U
//FIFO flush
#define HP303B__REG_INFO_FIFO_FL HP303B__REG_ADR_FIFO_EN, \
HP303B__REG_MASK_FIFO_EN, \
HP303B__REG_SHIFT_FIFO_EN
#define HP303B__REG_ADR_FIFO_FL 0x0CU
#define HP303B__REG_MASK_FIFO_FL 0x80U
#define HP303B__REG_SHIFT_FIFO_FL 7U
//FIFO empty
#define HP303B__REG_INFO_FIFO_EMPTY HP303B__REG_ADR_FIFO_EMPTY, \
HP303B__REG_MASK_FIFO_EMPTY, \
HP303B__REG_SHIFT_FIFO_EMPTY
#define HP303B__REG_ADR_FIFO_EMPTY 0x0BU
#define HP303B__REG_MASK_FIFO_EMPTY 0x01U
#define HP303B__REG_SHIFT_FIFO_EMPTY 0U
//FIFO full
#define HP303B__REG_INFO_FIFO_FULL HP303B__REG_ADR_FIFO_FULL, \
HP303B__REG_MASK_FIFO_FULL, \
HP303B__REG_SHIFT_FIFO_FULL
#define HP303B__REG_ADR_FIFO_FULL 0x0BU
#define HP303B__REG_MASK_FIFO_FULL 0x02U
#define HP303B__REG_SHIFT_FIFO_FULL 1U
//INT HL
#define HP303B__REG_INFO_INT_HL HP303B__REG_ADR_INT_HL, \
HP303B__REG_MASK_INT_HL, \
HP303B__REG_SHIFT_INT_HL
#define HP303B__REG_ADR_INT_HL 0x09U
#define HP303B__REG_MASK_INT_HL 0x80U
#define HP303B__REG_SHIFT_INT_HL 7U
//INT FIFO enable
#define HP303B__REG_INFO_INT_EN_FIFO HP303B__REG_ADR_INT_EN_FIFO, \
HP303B__REG_MASK_INT_EN_FIFO, \
HP303B__REG_SHIFT_INT_EN_FIFO
#define HP303B__REG_ADR_INT_EN_FIFO 0x09U
#define HP303B__REG_MASK_INT_EN_FIFO 0x40U
#define HP303B__REG_SHIFT_INT_EN_FIFO 6U
//INT TEMP enable
#define HP303B__REG_INFO_INT_EN_TEMP HP303B__REG_ADR_INT_EN_TEMP, \
HP303B__REG_MASK_INT_EN_TEMP, \
HP303B__REG_SHIFT_INT_EN_TEMP
#define HP303B__REG_ADR_INT_EN_TEMP 0x09U
#define HP303B__REG_MASK_INT_EN_TEMP 0x20U
#define HP303B__REG_SHIFT_INT_EN_TEMP 5U
//INT PRS enable
#define HP303B__REG_INFO_INT_EN_PRS HP303B__REG_ADR_INT_EN_PRS, \
HP303B__REG_MASK_INT_EN_PRS, \
HP303B__REG_SHIFT_INT_EN_PRS
#define HP303B__REG_ADR_INT_EN_PRS 0x09U
#define HP303B__REG_MASK_INT_EN_PRS 0x10U
#define HP303B__REG_SHIFT_INT_EN_PRS 4U
//INT FIFO flag
#define HP303B__REG_INFO_INT_FLAG_FIFO HP303B__REG_ADR_INT_FLAG_FIFO, \
HP303B__REG_MASK_INT_FLAG_FIFO, \
HP303B__REG_SHIFT_INT_FLAG_FIFO
#define HP303B__REG_ADR_INT_FLAG_FIFO 0x0AU
#define HP303B__REG_MASK_INT_FLAG_FIFO 0x04U
#define HP303B__REG_SHIFT_INT_FLAG_FIFO 2U
//INT TMP flag
#define HP303B__REG_INFO_INT_FLAG_TEMP HP303B__REG_ADR_INT_FLAG_TEMP, \
HP303B__REG_MASK_INT_FLAG_TEMP, \
HP303B__REG_SHIFT_INT_FLAG_TEMP
#define HP303B__REG_ADR_INT_FLAG_TEMP 0x0AU
#define HP303B__REG_MASK_INT_FLAG_TEMP 0x02U
#define HP303B__REG_SHIFT_INT_FLAG_TEMP 1U
//INT PRS flag
#define HP303B__REG_INFO_INT_FLAG_PRS HP303B__REG_ADR_INT_FLAG_PRS, \
HP303B__REG_MASK_INT_FLAG_PRS, \
HP303B__REG_SHIFT_INT_FLAG_PRS
#define HP303B__REG_ADR_INT_FLAG_PRS 0x0AU
#define HP303B__REG_MASK_INT_FLAG_PRS 0x01U
#define HP303B__REG_SHIFT_INT_FLAG_PRS 0U
#endif /* DPS310_CONSTS_H_ */

View File

@ -572,8 +572,10 @@ void GetFeatures(void)
#ifdef USE_MCP9808
feature6 |= 0x00002000; // xsns_72_mcp9808.ino
#endif
#ifdef USE_HP303B
feature6 |= 0x00004000; // xsns_73_hp303b.ino
#endif
// feature6 |= 0x00004000;
// feature6 |= 0x00008000;
// feature6 |= 0x00010000;

161
tasmota/xsns_73_hp303b.ino Normal file
View File

@ -0,0 +1,161 @@
/*
xsns_72_hp303b.ino - HP303B digital barometric air pressure sensor support for Tasmota
Copyright (C) 2020 Theo Arends
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef USE_I2C
#ifdef USE_HP303B
/*********************************************************************************************\
* HP303B - Gas (TVOC - Total Volatile Organic Compounds) and Air Quality (CO2)
*
* Source: Lolin LOLIN_HP303B_Library
*
* I2C Address: 0x77 or 0x76
\*********************************************************************************************/
#define XSNS_73 73
#define XI2C_52 52 // See I2CDEVICES.md
#define HP303B_ADDR1 0x77
#define HP303B_ADDR2 0x76
#include <LOLIN_HP303B.h>
// HP303B Opject
LOLIN_HP303B HP303BSensor = LOLIN_HP303B();
struct HP303BDATA
{
uint8_t address;
uint8_t addresses[2] = {HP303B_ADDR1, HP303B_ADDR2};
uint8_t type = 0;
uint8_t valid = 0;
int32_t temperature;
int32_t pressure;
int16_t oversampling = 7;
char types[7] = "HP303B";
} HP303B;
/*********************************************************************************************/
bool HP303B_Read(int32_t &t, int32_t &p, uint8_t hp303b_address)
{
HP303BSensor.begin(hp303b_address);
int16_t ret;
ret = HP303BSensor.measureTempOnce(t, HP303B.oversampling);
if (ret != 0)
return false;
ret = HP303BSensor.measurePressureOnce(p, HP303B.oversampling);
if (ret != 0)
return false;
HP303B.temperature = ConvertTemp(t);
HP303B.pressure = ConvertPressure(p);
return true;
}
/********************************************************************************************/
void HP303B_Detect(void)
{
for (uint32_t i = 0; i < sizeof(HP303B.addresses); i++)
{
if (!I2cSetDevice(HP303B.addresses[i]))
{
continue;
}
int32_t t;
int32_t p;
if (HP303B_Read(t, p, HP303B.addresses[i]))
{
I2cSetActiveFound(HP303B.addresses[i], HP303B.types);
HP303B.address = HP303B.addresses[i];
HP303B.type = 1;
}
}
}
void HP303B_Show(bool json)
{
if (HP303B_Read(HP303B.temperature, HP303B.pressure, HP303B.address))
{
if (json)
{
ResponseAppend_P(PSTR(",\"HP303B\":{\"" D_JSON_TEMPERATURE "\":%d,\"" D_JSON_PRESSURE "\":%d"), HP303B.temperature, HP303B.pressure);
#ifdef USE_DOMOTICZ
if (0 == tele_period)
{
DomoticzSensor(DZ_TEMP, HP303B.temperature);
}
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
}
else
{
char str_temperature[12];
char str_pressure[12];
itoa(HP303B.temperature, str_temperature, 10);
itoa(HP303B.pressure, str_pressure, 10);
WSContentSend_PD(HTTP_SNS_TEMP, "HP303B", str_temperature, TempUnit());
WSContentSend_PD(HTTP_SNS_PRESSURE, "HP303B", str_pressure, PressureUnit().c_str());
#endif // USE_WEBSERVER
}
}
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
bool Xsns73(uint8_t function)
{
if (!I2cEnabled(XI2C_52))
{
return false;
}
bool result = false;
if (FUNC_INIT == function)
{
HP303B_Detect();
}
else if (HP303B.type)
{
switch (function)
{
case FUNC_JSON_APPEND:
HP303B_Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
HP303B_Show(0);
break;
#endif // USE_WEBSERVER
}
}
return result;
}
#endif // USE_HP303B
#endif // USE_I2C

View File

@ -204,7 +204,7 @@ a_features = [[
"USE_KEELOQ","USE_HRXL","USE_SONOFF_D1","USE_HDC1080",
"USE_IAQ","USE_DISPLAY_SEVENSEG","USE_AS3935","USE_PING",
"USE_WINDMETER","USE_OPENTHERM","USE_THERMOSTAT","USE_VEML6075",
"USE_VEML7700","USE_MCP9808","","",
"USE_VEML7700","USE_MCP9808","USE_HP303B","",
"","","","",
"","","","",
"","","","",