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

166 lines
4.0 KiB
C
Raw Normal View History

2022-01-25 03:55:40 -05:00
#pragma once
//
// FILE: MS5611_SPI.h
// AUTHOR: Rob Tillaart
2022-12-18 10:28:36 -05:00
// VERSION: 0.1.2
2022-10-27 10:59:08 -04:00
// PURPOSE: S5611 (SPI) Temperature & Pressure library for Arduino
2022-01-25 03:55:40 -05:00
// URL: https://github.com/RobTillaart/MS5611_SPI
#include "Arduino.h"
#include "SPI.h"
// BREAKOUT MS5611 aka GY63 - see datasheet
//
// SPI I2C
// +--------+
// VCC VCC | o |
// GND GND | o |
// SCL | o |
// SDI SDA | o |
// CSO | o |
// SDO | o L | L = led
// PS | o O | O = opening PS = protocol select
// +--------+
//
// PS to VCC ==> I2C (GY-63 board has internal pull up, so not needed)
// PS to GND ==> SPI
// CS to VCC ==> 0x76
// CS to GND ==> 0x77
2022-12-18 10:28:36 -05:00
#define MS5611_SPI_LIB_VERSION (F("0.1.2 EXPERIMENTAL"))
2022-01-25 03:55:40 -05:00
#define MS5611_READ_OK 0
2022-10-27 10:59:08 -04:00
#define MS5611_ERROR_2 2 // TODO ??
2022-01-25 03:55:40 -05:00
#define MS5611_NOT_READ -999
enum osr_t
{
OSR_ULTRA_HIGH = 12, // 10 millis
OSR_HIGH = 11, // 5 millis
OSR_STANDARD = 10, // 3 millis
OSR_LOW = 9, // 2 millis
OSR_ULTRA_LOW = 8 // 1 millis Default = backwards compatible
};
class MS5611_SPI
{
public:
explicit MS5611_SPI(uint8_t select, uint8_t dataOut = 255, uint8_t dataIn = 255, uint8_t clock = 255);
bool begin();
2022-10-27 10:59:08 -04:00
bool isConnected();
2022-01-25 03:55:40 -05:00
2022-10-27 10:59:08 -04:00
// reset command + get constants
// mathMode = 0 (default), 1 = factor 2 fix.
// returns false if ROM constants == 0;
bool reset(uint8_t mathMode = 0);
2022-01-25 03:55:40 -05:00
2022-10-27 10:59:08 -04:00
// the actual reading of the sensor;
// returns MS5611_READ_OK upon success
2022-01-25 03:55:40 -05:00
int read(uint8_t bits);
2022-10-27 10:59:08 -04:00
// wrapper, uses the preset oversampling rate.
2022-01-25 03:55:40 -05:00
inline int read() { return read( (uint8_t) _samplingRate); };
2022-10-27 10:59:08 -04:00
// sets oversampling to a value between 8 and 12
2022-01-25 03:55:40 -05:00
void setOversampling(osr_t samplingRate);
2022-10-27 10:59:08 -04:00
// oversampling rate is in osr_t
2022-12-18 10:28:36 -05:00
osr_t getOversampling() const;
2022-01-25 03:55:40 -05:00
2022-10-27 10:59:08 -04:00
// temperature is in ²C
2022-01-25 03:55:40 -05:00
float getTemperature() const;
2022-10-27 10:59:08 -04:00
// pressure is in mBar
2022-01-25 03:55:40 -05:00
float getPressure() const;
2022-12-18 10:28:36 -05:00
// OFFSET
void setPressureOffset(float offset = 0);
float getPressureOffset();
void setTemperatureOffset(float offset = 0);
float getTemperatureOffset();
2022-01-25 03:55:40 -05:00
2022-10-27 10:59:08 -04:00
// to check for failure
2022-12-18 10:28:36 -05:00
int getLastResult() const;
2022-01-25 03:55:40 -05:00
2022-10-27 10:59:08 -04:00
// last time in millis() when the sensor has been read.
2022-12-18 10:28:36 -05:00
uint32_t lastRead() const;
2022-01-25 03:55:40 -05:00
2022-12-18 10:28:36 -05:00
uint32_t getDeviceID() const;
2022-01-25 03:55:40 -05:00
2022-12-18 10:28:36 -05:00
void setCompensation(bool flag = true);
bool getCompensation();
2022-01-25 03:55:40 -05:00
2022-10-27 10:59:08 -04:00
// develop functions.
2022-01-25 03:55:40 -05:00
/*
void setAddress(uint8_t address) { _address = address; }; // RANGE CHECK
uint8_t getAddress() const { return _address; };
uint8_t detectAddress() { todo }; // works with only one on the bus?
*/
2022-10-27 10:59:08 -04:00
// EXPERIMENTAL
uint16_t getManufacturer();
uint16_t getSerialCode();
2022-01-25 03:55:40 -05:00
// speed in Hz
void setSPIspeed(uint32_t speed);
2022-12-18 10:28:36 -05:00
uint32_t getSPIspeed();
2022-01-25 03:55:40 -05:00
2022-10-27 10:59:08 -04:00
// debugging
2022-12-18 10:28:36 -05:00
bool usesHWSPI();
2022-01-25 03:55:40 -05:00
2022-10-27 10:59:08 -04:00
// ESP32 specific
2022-01-25 03:55:40 -05:00
#if defined(ESP32)
2022-12-18 10:28:36 -05:00
void selectHSPI();
void selectVSPI();
bool usesHSPI();
bool usesVSPI();
2022-01-25 03:55:40 -05:00
2022-10-27 10:59:08 -04:00
// to overrule ESP32 default hardware pins
2022-01-25 03:55:40 -05:00
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);
#endif
2022-10-27 10:59:08 -04:00
protected:
2022-01-25 03:55:40 -05:00
void convert(const uint8_t addr, uint8_t bits);
uint32_t readADC();
uint16_t readProm(uint8_t reg);
int command(const uint8_t command);
2022-10-27 10:59:08 -04:00
void initConstants(uint8_t mathMode);
2022-01-25 03:55:40 -05:00
uint8_t _address;
uint8_t _samplingRate;
int32_t _temperature;
int32_t _pressure;
float _pressureOffset;
float _temperatureOffset;
int _result;
float C[7];
uint32_t _lastRead;
uint32_t _deviceID;
bool _compensation;
uint8_t _select;
uint8_t _dataIn;
uint8_t _dataOut;
uint8_t _clock;
bool _hwSPI;
uint32_t _SPIspeed = 1000000;
uint8_t swSPI_transfer(uint8_t value);
2022-12-18 10:28:36 -05:00
2022-01-25 03:55:40 -05:00
SPIClass * mySPI;
SPISettings _spi_settings;
#if defined(ESP32)
bool _useHSPI = true;
#endif
};
// -- END OF FILE --