GY-63_MS5611/libraries/BH1750FVI_RT/BH1750FVI.h

147 lines
3.7 KiB
C
Raw Normal View History

2021-01-29 06:31:58 -05:00
#pragma once
//
// FILE: BH1750FVI.h
2022-10-29 08:25:01 -04:00
// AUTHOR: Rob Tillaart
2023-10-18 10:41:42 -04:00
// VERSION: 0.3.0
2021-01-29 06:31:58 -05:00
// PURPOSE: Arduino library for BH1750FVI (GY-30) lux sensor
2022-10-29 08:25:01 -04:00
// HISTORY: see changelog.md
2021-01-29 06:31:58 -05:00
//
// breakout BH1750FVI / GY-30
//
// +-----------------------+
// GND |o |
// ADD |o |
// SDA |o + | + = sensor
// SCL |o |
// VCC |o |
// +-----------------------+
//
// ADD = ADDRESS:
// 0 = 0x23
// 1 = 0x5C
//
#include "Wire.h"
#include "Arduino.h"
2023-10-18 10:41:42 -04:00
#define BH1750FVI_LIB_VERSION (F("0.3.0"))
2021-01-29 06:31:58 -05:00
2021-12-14 04:53:34 -05:00
#define BH1750FVI_DEFAULT_ADDRESS 0x23
#define BH1750FVI_ALT_ADDRESS 0x5C
2021-01-29 06:31:58 -05:00
2022-10-29 08:25:01 -04:00
#define BH1750FVI_REFERENCE_TIME 0x45 // 69 = default
2021-01-29 06:31:58 -05:00
2021-12-14 04:53:34 -05:00
#define BH1750FVI_MODE_LOW 0x00
#define BH1750FVI_MODE_HIGH 0x01
#define BH1750FVI_MODE_HIGH2 0x02
2021-01-29 06:31:58 -05:00
// ERROR CODES
2021-12-14 04:53:34 -05:00
#define BH1750FVI_OK 0
#define BH1750FVI_ERROR_WIRE_REQUEST -10
2021-01-29 06:31:58 -05:00
class BH1750FVI
{
public:
BH1750FVI(const uint8_t address, TwoWire *wire = &Wire);
2022-10-29 08:25:01 -04:00
// returns true if isConnected()
bool begin(); // resets to constructor defaults. (use with care)
bool isConnected(); // returns true if address is on I2C bus
2021-01-29 06:31:58 -05:00
2021-12-14 04:53:34 -05:00
2022-10-29 08:25:01 -04:00
float getRaw(); // no HIGH2 mode + no sensitivity factor.
2021-01-29 06:31:58 -05:00
float getLux();
int getError();
2021-12-14 04:53:34 -05:00
2023-10-18 10:41:42 -04:00
void powerOn();
void powerOff();
void reset();
2021-01-29 06:31:58 -05:00
2021-12-14 04:53:34 -05:00
2021-01-29 06:31:58 -05:00
// MODE TIME RESOLUTION
2022-10-29 08:25:01 -04:00
// 2 HIGH2 120 ms 0.5 lux // recommended max * 1.5 = 180 ms
2021-01-29 06:31:58 -05:00
// 1 HIGH 120 ms 1.0 lux
// 0 LOW 16 ms 4.0 lux
2023-10-18 10:41:42 -04:00
uint8_t getMode() { return _mode; };
2021-01-29 06:31:58 -05:00
2021-12-14 04:53:34 -05:00
2021-01-29 06:31:58 -05:00
void setContHighRes();
void setContHigh2Res();
void setContLowRes();
2021-12-14 04:53:34 -05:00
2021-01-29 06:31:58 -05:00
void setOnceHighRes();
void setOnceHigh2Res();
void setOnceLowRes();
2022-10-29 08:25:01 -04:00
bool isReady(); // only after setOnce...Res();
2021-01-29 06:31:58 -05:00
2021-12-14 04:53:34 -05:00
2022-10-29 08:25:01 -04:00
// read datasheet P11 about details of the correction or sensitivity factor
// to be used for very high and very low brightness
// or to correct for e.g. transparency
2021-12-14 04:53:34 -05:00
void changeTiming(uint8_t time = BH1750FVI_REFERENCE_TIME); // 69 is default
2023-10-18 10:41:42 -04:00
2022-10-29 08:25:01 -04:00
// returns changeTiming() parameter
2021-12-14 04:53:34 -05:00
uint8_t setCorrectionFactor(float factor = 1); // 0.45 .. 3.68
2022-10-29 08:25:01 -04:00
// returns percentage set.
2021-01-29 06:31:58 -05:00
float getCorrectionFactor();
2021-12-14 04:53:34 -05:00
2022-10-29 08:25:01 -04:00
// read datasheet P3 and check figure 4 and 5.
// setAngle is constrained to -89..+89
// returns the angle correction factor
2021-06-08 08:32:40 -04:00
float setAngle(int degrees = 0);
2021-01-29 06:31:58 -05:00
int getAngle() { return _angle; };
2021-12-14 04:53:34 -05:00
2022-10-29 08:25:01 -04:00
// datasheet P3 figure 7
// Effect of temperature is about 3% / 60<36>C ~~ 1% / 20<32>C
// to be used if temp is really hot or cold.
// returns the temperature correction factor
2021-06-08 08:32:40 -04:00
float setTemperature(int temp = 20);
2021-01-29 06:31:58 -05:00
int getTemperature() { return _temp; };
2022-10-29 08:25:01 -04:00
// datasheet Page 3 figure 1 (experimental correction)
2023-10-18 10:41:42 -04:00
// Effect of wavelength can be substantial,
2022-10-29 08:25:01 -04:00
// correction is calculated by multiple linear approximations.
// wavelength of 580 ==> correction == 1
// returns the wavelength correction factor
2021-06-08 08:32:40 -04:00
float setWaveLength(int waveLength = 580);
2021-01-29 06:31:58 -05:00
int getWaveLength() { return _waveLength; };
2021-12-14 04:53:34 -05:00
2021-01-29 06:31:58 -05:00
private:
uint16_t readData();
void command(uint8_t value);
uint8_t _address;
uint16_t _data;
int _error;
uint8_t _sensitivityFactor;
uint8_t _mode;
2021-12-14 04:53:34 -05:00
2021-01-29 06:31:58 -05:00
uint32_t _requestTime = 0;
float _angleFactor = 1;
int _angle = 0;
2021-06-08 08:32:40 -04:00
float _tempFactor = 1;
2021-01-29 06:31:58 -05:00
int _temp = 20;
float _waveLengthFactor = 1;
int _waveLength = 580;
TwoWire* _wire;
};
2021-12-14 04:53:34 -05:00
2023-10-18 10:41:42 -04:00
// -- END OF FILE --
2022-10-29 08:25:01 -04:00