GY-63_MS5611/libraries/I2C_ASDX/I2C_ASDX.h

130 lines
4.0 KiB
C
Raw Normal View History

2021-01-29 06:31:58 -05:00
#pragma once
//
// FILE: I2C_ASDX.h
// AUTHOR: Rob Tillaart
2023-01-12 13:55:08 -05:00
// VERSION: 0.3.3
// PURPOSE: Arduino library for I2C ASDX pressure sensor.
2021-01-29 06:31:58 -05:00
// URL: https://github.com/RobTillaart/I2C_ASDX
2023-01-12 13:55:08 -05:00
2021-01-29 06:31:58 -05:00
// TESTED TYPES - type A 10% - 90% only
//
// ADDRESS PRESSURE RANGE TYPE P A T V
// 0x58 100 psi 0..6895 mBar SSCDANN 100PG 5 A 5
// 0x38 60 psi 0..4137 mbar SSCDANN 060PG 3 A 5
// 0x28 30 psi 0..2068 mbar SSCDANN 030PG 2 A 5
//
// P = pressure range
// A = I2C address indicator
// T = accuracy range
// V = voltage (3 volt also supported, not tested)
//
2023-01-12 13:55:08 -05:00
2021-01-29 06:31:58 -05:00
#include "Wire.h"
#include "Arduino.h"
2023-01-12 13:55:08 -05:00
#define I2C_ASDX_VERSION (F("0.3.3"))
2021-01-29 06:31:58 -05:00
2021-06-06 13:59:53 -04:00
2021-12-19 11:54:58 -05:00
// factors to convert PSI to mBar and back
#define PSI2MILLIBAR 68.9475729
#define MILLIBAR2PSI 0.01450377377
#define PSI2BAR 0.0689475729
#define BAR2PSI 14.503773773
2021-06-06 13:59:53 -04:00
2021-12-19 11:54:58 -05:00
// factors to convert from mBar to different units
// note: different sources give slightly different values
#define MILLIBAR2BAR 0.001
#define MILLIBAR2ATM 9.872e-4
#define MILLIBAR2DYNES 1000
#define MILLIBAR2INHG 2.9539e-2
#define MILLIBAR2INH2O 0.4018
#define MILLIBAR2PASCAL 100
#define MILLIBAR2TORR 0.75028
#define MILLIBAR2CMHG 0.075028
#define MILLIBAR2CMH2O 1.02056
#define MILLIBAR2MSW 100
2021-01-29 06:31:58 -05:00
2021-12-19 11:54:58 -05:00
#define I2C_ASDX_OK 1
#define I2C_ASDX_INIT 0
#define I2C_ASDX_READ_ERROR -1
#define I2C_ASDX_C000_ERROR -2
#define I2C_ASDX_CONNECT_ERROR -3
2021-01-29 06:31:58 -05:00
2021-06-06 13:59:53 -04:00
2021-01-29 06:31:58 -05:00
class I2C_ASDX
{
public:
2021-06-07 05:49:28 -04:00
// psi: 100, 60, 30, 15, 5 or 1
I2C_ASDX(uint8_t address, uint8_t psi, TwoWire *wire = &Wire);
2021-01-29 06:31:58 -05:00
2021-06-06 13:59:53 -04:00
2021-01-29 06:31:58 -05:00
#if defined (ESP8266) || defined(ESP32)
2021-06-07 05:49:28 -04:00
bool begin(uint8_t sda, uint8_t scl);
2021-01-29 06:31:58 -05:00
#endif
2021-06-07 05:49:28 -04:00
bool begin();
2021-01-29 06:31:58 -05:00
void reset();
2021-06-06 13:59:53 -04:00
bool isConnected();
2023-01-12 13:55:08 -05:00
uint8_t getAddress();
// bool available() { return isConnected(); }; // obsolete 0.3.3
2021-06-06 13:59:53 -04:00
2021-01-29 06:31:58 -05:00
2021-12-19 11:54:58 -05:00
// returns status OK (0) or ERROR ( not 0 )
2021-01-29 06:31:58 -05:00
int read();
2021-06-06 13:59:53 -04:00
2021-12-19 11:54:58 -05:00
// returns the pressure of last successful read in mbar
2021-01-29 06:31:58 -05:00
int getPressure() { return round(_pressure); };
float getMilliBar() { return _pressure; };
2021-06-06 13:59:53 -04:00
float getBar() { return _pressure * MILLIBAR2BAR; };
float getPSI() { return _pressure * MILLIBAR2PSI; };
2021-12-19 11:54:58 -05:00
// conversions added 0.2.3
2021-06-06 13:59:53 -04:00
float getATM() { return _pressure * MILLIBAR2ATM; }
float getDynes() { return _pressure * MILLIBAR2DYNES; }
float getInchHg() { return _pressure * MILLIBAR2INHG; }
float getInchH2O() { return _pressure * MILLIBAR2INH2O; }
float getPascal() { return _pressure * MILLIBAR2PASCAL; }
float getTORR() { return _pressure * MILLIBAR2TORR; }
float getCmHg() { return _pressure * MILLIBAR2CMHG; }
float getCmH2O() { return _pressure * MILLIBAR2CMH2O; }
float getMSW() { return _pressure * MILLIBAR2MSW; }
2021-01-29 06:31:58 -05:00
2021-12-19 11:54:58 -05:00
// # errors since last good read
2021-01-29 06:31:58 -05:00
uint16_t errorCount() { return _errorCount; };
2021-12-19 11:54:58 -05:00
// timestamp of last good read
2021-01-29 06:31:58 -05:00
uint32_t lastRead() { return _lastRead; };
2021-12-19 11:54:58 -05:00
// get the last state
2021-01-29 06:31:58 -05:00
int state() { return _state; };
2021-06-06 13:59:53 -04:00
2021-01-29 06:31:58 -05:00
private:
uint8_t _address;
2021-06-07 05:49:28 -04:00
TwoWire* _wire;
2021-06-06 13:59:53 -04:00
2021-01-29 06:31:58 -05:00
float _maxPressure;
2021-06-06 13:59:53 -04:00
float _pressure;
2021-01-29 06:31:58 -05:00
uint8_t _state;
uint32_t _errorCount;
uint32_t _lastRead;
};
2021-06-06 13:59:53 -04:00
2023-01-12 13:55:08 -05:00
// Convertors ==> see https://github.com/RobTillaart/pressure library.
2021-01-29 06:31:58 -05:00
/*
static float MilliBar2PSI( float mbar ) { return mbar * MILLIBAR2PSI; };
static float MilliBar2Bar( float mbar ) { return mbar * 0.001; };
static float Bar2MilliBar( float bar ) { return bar * 1000; };
static float Bar2PSI( float bar ) { return bar * BAR2PSI; };
static float PSI2MilliBar( float PSI ) { return PSI * PSI2MILLIBAR; };
static float PSI2Bar( float PSI ) { return PSI * PSI2BAR; };
*/
// -- END OF FILE --
2021-12-19 11:54:58 -05:00