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

164 lines
4.5 KiB
C
Raw Normal View History

2021-01-29 06:31:58 -05:00
#pragma once
//
// FILE: BH1750FVI.h
// AUTHOR: Rob dot Tillaart at gmail dot com
2021-12-14 04:53:34 -05:00
// VERSION: 0.2.9
2021-01-29 06:31:58 -05:00
// PURPOSE: Arduino library for BH1750FVI (GY-30) lux sensor
// HISTORY: See BH1750FVI.cpp
//
// 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"
2021-12-14 04:53:34 -05:00
#define BH1750FVI_LIB_VERSION (F("0.2.9"))
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
2021-12-14 04:53:34 -05:00
// COMMANDS P5
#define BH1750FVI_POWER_ON 0x00
#define BH1750FVI_POWER_OFF 0x01
#define BH1750FVI_RESET 0x07
#define BH1750FVI_CONT_HIGH 0x10
#define BH1750FVI_CONT_HIGH2 0x11
#define BH1750FVI_CONT_LOW 0x13
#define BH1750FVI_ONCE_HIGH 0x20
#define BH1750FVI_ONCE_HIGH2 0x21
#define BH1750FVI_ONCE_LOW 0x23
2021-01-29 06:31:58 -05:00
2021-12-14 04:53:34 -05:00
#define BH1750FVI_REFERENCE_TIME 0x45 // 69
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:
#if defined(ESP8266) || defined(ESP32)
// dataPin and clockPin can be used for ESP8266
BH1750FVI(const uint8_t address , const uint8_t dataPin, const uint8_t clockPin);
#endif
BH1750FVI(const uint8_t address, TwoWire *wire = &Wire);
2021-12-14 04:53:34 -05:00
// returns true if isConnected()
2021-06-08 08:32:40 -04:00
bool begin(); // resets to constructor defaults. (use with care)
2021-01-29 06:31:58 -05:00
bool isConnected(); // returns true if address is on I2C bus
2021-12-14 04:53:34 -05:00
2021-01-29 06:31:58 -05:00
float getRaw(); // no HIGH2 mode + no sensitivity factor.
float getLux();
int getError();
2021-12-14 04:53:34 -05:00
2021-01-29 06:31:58 -05:00
void powerOn() { command(BH1750FVI_POWER_ON); };
void powerOff() { command(BH1750FVI_POWER_OFF); };
void reset() { command(BH1750FVI_RESET); };
2021-12-14 04:53:34 -05:00
2021-01-29 06:31:58 -05:00
// MODE TIME RESOLUTION
// 2 HIGH2 120 ms 0.5 lux // recommended max * 1.5 = 180 ms
// 1 HIGH 120 ms 1.0 lux
// 0 LOW 16 ms 4.0 lux
uint8_t getMode() { return _mode; };
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();
bool isReady(); // only after setOnce...Res();
2021-12-14 04:53:34 -05:00
2021-01-29 06:31:58 -05:00
// read datasheet P11 about details of the correction or sensitivity factor
// to be used for very high and very low brightness
2021-12-14 04:53:34 -05:00
// or to correct for e.g. transparency
void changeTiming(uint8_t time = BH1750FVI_REFERENCE_TIME); // 69 is default
// returns changeTiming() parameter
uint8_t setCorrectionFactor(float factor = 1); // 0.45 .. 3.68
// returns percentage set.
2021-01-29 06:31:58 -05:00
float getCorrectionFactor();
2021-12-14 04:53:34 -05:00
2021-01-29 06:31:58 -05:00
// read datasheet P3 and check figure 4 and 5.
// setAngle is constrained to -89..+89
2021-06-08 08:32:40 -04:00
// returns the angle correction factor
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
2021-01-29 06:31:58 -05: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.
2021-06-08 08:32:40 -04:00
// returns the temperature correction factor
float setTemperature(int temp = 20);
2021-01-29 06:31:58 -05:00
int getTemperature() { return _temp; };
// datasheet Page 3 figure 1 (experimental correction)
// Effect of wavelength can be substantial,
2021-06-08 08:32:40 -04:00
// correction is calculated by multiple linear approximations.
// wavelength of 580 ==> correction == 1
// returns the wavelength correction factor
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
2021-01-29 06:31:58 -05:00
// -- END OF FILE --