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

280 lines
5.7 KiB
C++
Raw Normal View History

2021-01-29 06:31:58 -05:00
//
// FILE: BH1750FVI.cpp
// AUTHOR: Rob Tillaart
2024-04-09 10:41:53 -04:00
// VERSION: 0.3.1
2021-01-29 06:31:58 -05:00
// PURPOSE: library for BH1750FVI lux sensor Arduino
2024-04-09 10:41:53 -04:00
// URL: https://github.com/RobTillaart/BH1750FVI_RT
2021-01-29 06:31:58 -05:00
#include "BH1750FVI.h"
2023-10-18 10:41:42 -04: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
BH1750FVI::BH1750FVI(const uint8_t address, TwoWire *wire)
{
_address = address;
_wire = wire;
2023-10-18 10:41:42 -04:00
_data = 0;
_error = BH1750FVI_OK;
_sensitivityFactor = BH1750FVI_REFERENCE_TIME;
_mode = BH1750FVI_MODE_HIGH;
2021-01-29 06:31:58 -05:00
}
2021-06-08 08:32:40 -04:00
bool BH1750FVI::begin()
2021-01-29 06:31:58 -05:00
{
_data = 0;
_error = BH1750FVI_OK;
_sensitivityFactor = BH1750FVI_REFERENCE_TIME;
_mode = BH1750FVI_MODE_HIGH;
2021-06-08 08:32:40 -04:00
return isConnected();
2021-01-29 06:31:58 -05:00
}
bool BH1750FVI::isConnected()
{
_wire->beginTransmission(_address);
_error = _wire->endTransmission();
return (_error == 0);
}
bool BH1750FVI::isReady()
{
2022-10-29 08:25:01 -04:00
// max times from datasheet P2 + P11;
2021-01-29 06:31:58 -05:00
uint8_t timeout[3] = { 16, 120, 120 };
if (_mode < 3)
{
float f = timeout[_mode] * _sensitivityFactor / BH1750FVI_REFERENCE_TIME;
return (millis() - _requestTime) > f;
}
return false;
}
float BH1750FVI::getRaw(void)
{
2024-04-09 10:41:53 -04:00
return readData() * 0.833333333333f; // == 1 / 1.2;
2021-01-29 06:31:58 -05:00
}
float BH1750FVI::getLux(void)
{
2022-10-29 08:25:01 -04:00
// lux without mode correction
2021-01-29 06:31:58 -05:00
float lux = getRaw();
2022-10-29 08:25:01 -04:00
// sensitivity factor
2021-01-29 06:31:58 -05:00
if (_sensitivityFactor != BH1750FVI_REFERENCE_TIME)
{
lux *= (1.0 * BH1750FVI_REFERENCE_TIME) / _sensitivityFactor;
}
2022-10-29 08:25:01 -04:00
// angle compensation
2021-01-29 06:31:58 -05:00
if (_angle != 0)
{
lux *= _angleFactor;
}
2022-10-29 08:25:01 -04:00
// temperature compensation.
2021-01-29 06:31:58 -05:00
if (_temp != 20)
{
2021-06-08 08:32:40 -04:00
lux *= _tempFactor;
2021-01-29 06:31:58 -05:00
}
2022-10-29 08:25:01 -04:00
// wavelength compensation.
2021-01-29 06:31:58 -05:00
if (_waveLength != 580)
{
lux *= _waveLengthFactor;
}
if (_mode == BH1750FVI_MODE_HIGH2)
{
2024-04-09 10:41:53 -04:00
lux *= 0.5f; // P11
2021-01-29 06:31:58 -05:00
}
return lux;
}
int BH1750FVI::getError()
{
int e = _error;
_error = BH1750FVI_OK;
return e;
}
2023-10-18 10:41:42 -04:00
void BH1750FVI::powerOn()
{
command(BH1750FVI_POWER_ON);
}
void BH1750FVI::powerOff()
{
command(BH1750FVI_POWER_OFF);
}
void BH1750FVI::reset()
{
command(BH1750FVI_RESET);
}
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////
//
2022-10-29 08:25:01 -04:00
// operational mode
2021-01-29 06:31:58 -05:00
//
void BH1750FVI::setContHighRes()
{
_mode = BH1750FVI_MODE_HIGH;
command(BH1750FVI_CONT_HIGH);
_requestTime = millis();
};
2021-06-08 08:32:40 -04:00
2021-01-29 06:31:58 -05:00
void BH1750FVI::setContHigh2Res()
{
_mode = BH1750FVI_MODE_HIGH2;
command(BH1750FVI_CONT_HIGH2);
_requestTime = millis();
};
2021-06-08 08:32:40 -04:00
2021-01-29 06:31:58 -05:00
void BH1750FVI::setContLowRes()
{
_mode = BH1750FVI_MODE_LOW;
command(BH1750FVI_CONT_LOW);
_requestTime = millis();
};
2021-06-08 08:32:40 -04:00
2021-01-29 06:31:58 -05:00
void BH1750FVI::setOnceHighRes()
{
_mode = BH1750FVI_MODE_HIGH;
command(BH1750FVI_ONCE_HIGH);
_requestTime = millis();
};
2021-06-08 08:32:40 -04:00
2021-01-29 06:31:58 -05:00
void BH1750FVI::setOnceHigh2Res()
{
_mode = BH1750FVI_MODE_HIGH2;
command(BH1750FVI_ONCE_HIGH2);
_requestTime = millis();
};
2021-06-08 08:32:40 -04:00
2021-01-29 06:31:58 -05:00
void BH1750FVI::setOnceLowRes()
{
_mode = BH1750FVI_MODE_LOW;
command(BH1750FVI_ONCE_LOW);
_requestTime = millis();
};
////////////////////////////////////////////
//
2022-10-29 08:25:01 -04:00
// measurement timing
2021-01-29 06:31:58 -05:00
//
2022-10-29 08:25:01 -04:00
// P11 datasheet
2021-12-14 04:53:34 -05:00
void BH1750FVI::changeTiming(uint8_t time)
2021-01-29 06:31:58 -05:00
{
2021-12-14 04:53:34 -05:00
time = constrain(time, 31, 254);
_sensitivityFactor = time;
2022-10-29 08:25:01 -04:00
// P5 instruction set table
2021-12-14 04:53:34 -05:00
uint8_t Hbits = 0x40 | (time >> 5);
uint8_t Lbits = 0x60 | (time & 0x1F);
2021-01-29 06:31:58 -05:00
command(Hbits);
command(Lbits);
}
2021-06-08 08:32:40 -04:00
2021-12-14 04:53:34 -05:00
uint8_t BH1750FVI::setCorrectionFactor(float factor)
2021-01-29 06:31:58 -05:00
{
2022-10-29 08:25:01 -04:00
// 31 .. 254 are range P11 - constrained in changeTIming call
2021-12-14 04:53:34 -05:00
uint8_t timingValue = round(BH1750FVI_REFERENCE_TIME * factor);
2021-01-29 06:31:58 -05:00
changeTiming(timingValue);
2021-06-08 08:32:40 -04:00
return _sensitivityFactor;
2021-01-29 06:31:58 -05:00
}
2021-06-08 08:32:40 -04:00
2021-01-29 06:31:58 -05:00
float BH1750FVI::getCorrectionFactor()
{
float f = 1.0f / BH1750FVI_REFERENCE_TIME;
return _sensitivityFactor * f;
}
2021-06-08 08:32:40 -04:00
float BH1750FVI::setTemperature(int temp)
{
_temp = temp;
2022-10-29 08:25:01 -04:00
// _tempFactor = 1.0f - (_temp - 20.0f) / 2000.0f;
2021-06-08 08:32:40 -04:00
_tempFactor = 1.0f - (_temp - 20.0f) * 0.0005f;
return _tempFactor;
}
float BH1750FVI::setAngle(int degrees)
2021-01-29 06:31:58 -05:00
{
_angle = constrain(degrees, -89, 89);
2024-04-09 10:41:53 -04:00
// Lambert's Law.
2021-01-29 06:31:58 -05:00
_angleFactor = 1.0f / cos(_angle * (PI / 180.0f));
2021-06-08 08:32:40 -04:00
return _angleFactor;
2021-01-29 06:31:58 -05:00
}
2021-06-08 08:32:40 -04:00
2024-04-09 10:41:53 -04:00
// interpolation tables uses more RAM (versus PROGMEM)
2021-06-08 08:32:40 -04:00
float BH1750FVI::setWaveLength(int waveLength)
2021-01-29 06:31:58 -05:00
{
_waveLength = constrain(waveLength, 400, 715);
float tmp = 1.0f;
2021-06-08 08:32:40 -04:00
if (_waveLength < 400) tmp = 0.01f;
else if (_waveLength < 440) tmp = 0.01f + (_waveLength - 400) * 0.09f / 40.0f;
2021-01-29 06:31:58 -05:00
else if (_waveLength < 510) tmp = 0.10f + (_waveLength - 440) * 0.80f / 70.0f;
else if (_waveLength < 545) tmp = 0.90f - (_waveLength - 510) * 0.10f / 35.0f;
else if (_waveLength < 580) tmp = 0.80f + (_waveLength - 545) * 0.20f / 35.0f;
else if (_waveLength < 700) tmp = 1.00f - (_waveLength - 580) * 0.93f / 120.0f;
else if (_waveLength < 715) tmp = 0.07f - (_waveLength - 700) * 0.07f / 15.0f;
2021-06-08 08:32:40 -04:00
else if (_waveLength >= 715) tmp = 0.01f;
2021-01-29 06:31:58 -05:00
_waveLengthFactor = 1.0f / tmp;
2021-06-08 08:32:40 -04:00
return _waveLengthFactor;
2021-01-29 06:31:58 -05:00
}
2021-06-08 08:32:40 -04:00
2021-01-29 06:31:58 -05:00
///////////////////////////////////////////////////////////
//
2022-10-29 08:25:01 -04:00
// PRIVATE
2021-01-29 06:31:58 -05:00
//
uint16_t BH1750FVI::readData()
{
if (_wire->requestFrom(_address, (uint8_t) 2) != 2)
{
_error = BH1750FVI_ERROR_WIRE_REQUEST;
2024-04-09 10:41:53 -04:00
return _data; // last value
2021-01-29 06:31:58 -05:00
}
_data = _wire->read();
_data <<= 8;
_data += _wire->read();
return _data;
}
2021-06-08 08:32:40 -04:00
2021-01-29 06:31:58 -05:00
void BH1750FVI::command(uint8_t value)
{
_wire->beginTransmission(_address);
_wire->write(value);
_error = _wire->endTransmission();
}
2021-10-19 10:54:37 -04:00
2024-04-09 10:41:53 -04:00
// --- END OF FILE ---
2021-10-19 10:54:37 -04:00