2016-12-17 14:58:29 -05:00
|
|
|
//
|
|
|
|
// FILE: Max44009.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2021-06-04 14:31:56 -04:00
|
|
|
// VERSION: 0.5.0
|
2016-12-17 14:58:29 -05:00
|
|
|
// PURPOSE: library for MAX44009 lux sensor Arduino
|
2020-11-27 05:20:37 -05:00
|
|
|
// URL: https://github.com/RobTillaart/MAX44009
|
2016-12-17 14:58:29 -05:00
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
// HISTORY
|
|
|
|
//
|
2021-06-04 14:31:56 -04:00
|
|
|
// 0.5.0 2021-06-04 fix exponent math.
|
2021-05-28 07:37:15 -04:00
|
|
|
// 0.4.4 2021-05-27 arduino-lint
|
2021-01-29 06:31:58 -05:00
|
|
|
// 0.4.3 2020-12-30 arduino-ci, unit test
|
|
|
|
// 0.4.2 2020-06-19 fix library.json
|
|
|
|
// 0.4.1 2020-03-21 #pragma, readme.nd, license.md
|
|
|
|
// 0.4.0 2020-01-30 remove automatic mode from constructors;
|
|
|
|
// added example code
|
|
|
|
// 0.3.3 2020-01-27 issue #140 refactor constructors / configure
|
|
|
|
// 0.3.2 2020-01-21 solve #132 cannot read full range in manual mode.
|
|
|
|
// set automatic mode explicitly in constructors;
|
|
|
|
// added some error checks
|
|
|
|
// 0.3.1 2020-01-21 issue #133 overflow of the exponent
|
|
|
|
// 0.3.0 2020-01-20 issue #141 Kudos to Moritz89
|
|
|
|
// 0.2.0 2019-08-23 solve #127 == redo #118
|
|
|
|
// 0.1.10 2018-12-08 issue #118 Fix constructor esp8266
|
|
|
|
// (thanks to Bolukan)
|
|
|
|
// 0.1.9 2018-07-01 issue #108 Fix shift math
|
|
|
|
// (thanks Roland vandecook)
|
|
|
|
// 0.1.8 2018-05-13 issue #105 Fix read register
|
|
|
|
// (thanks morxGrillmeister)
|
|
|
|
// 0.1.7 2018-04-02 issue #98 extend constructor for ESP8266
|
|
|
|
// 0.1.6 2017-07-26 revert double to float
|
|
|
|
// 0.1.5 updated history
|
|
|
|
// 0.1.4 added setAutomaticMode() to max44009.h (thanks debsahu)
|
|
|
|
// 0.1.03 added configuration
|
|
|
|
// 0.1.02 added threshold code
|
|
|
|
// 0.1.01 added interrupt code
|
|
|
|
// 0.1.00 initial version
|
2016-12-17 14:58:29 -05:00
|
|
|
|
2021-05-28 07:37:15 -04:00
|
|
|
|
2016-12-17 14:58:29 -05:00
|
|
|
#include "Max44009.h"
|
|
|
|
|
2021-05-28 07:37:15 -04:00
|
|
|
|
2019-09-03 06:22:43 -04:00
|
|
|
#if defined(ESP8266) || defined(ESP32)
|
2018-04-02 07:29:46 -04:00
|
|
|
Max44009::Max44009(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin)
|
2016-12-17 14:58:29 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
_address = address;
|
|
|
|
_data = 0;
|
|
|
|
_error = MAX44009_OK;
|
|
|
|
_wire = &Wire;
|
2019-09-03 06:22:43 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
if ((dataPin < 255) && (clockPin < 255))
|
|
|
|
{
|
|
|
|
_wire->begin(dataPin, clockPin);
|
|
|
|
} else {
|
|
|
|
_wire->begin();
|
|
|
|
}
|
2019-09-03 06:22:43 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-05-28 07:37:15 -04:00
|
|
|
|
2020-01-27 11:06:52 -05:00
|
|
|
Max44009::Max44009(const uint8_t address, const Boolean begin)
|
2019-09-03 06:22:43 -04:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
Max44009::configure(address, &Wire, begin);
|
2020-01-19 16:53:40 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:37:15 -04:00
|
|
|
|
2020-01-19 16:53:40 -05:00
|
|
|
Max44009::Max44009(const Boolean begin)
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
Max44009::configure(MAX44009_DEFAULT_ADDRESS, &Wire, begin);
|
2020-01-27 11:06:52 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:37:15 -04:00
|
|
|
|
2020-01-27 11:06:52 -05:00
|
|
|
void Max44009::configure(const uint8_t address, TwoWire *wire, const Boolean begin)
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
_address = address;
|
|
|
|
_data = 0;
|
|
|
|
_error = MAX44009_OK;
|
|
|
|
_wire = wire;
|
|
|
|
|
|
|
|
if (begin == Boolean::True)
|
|
|
|
{
|
|
|
|
_wire->begin();
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 16:53:40 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
bool Max44009::isConnected()
|
|
|
|
{
|
|
|
|
_wire->beginTransmission(_address);
|
|
|
|
_error = _wire->endTransmission();
|
|
|
|
return (_error == 0);
|
2020-01-19 16:53:40 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2017-07-26 18:24:25 -04:00
|
|
|
float Max44009::getLux(void)
|
2016-12-17 14:58:29 -05:00
|
|
|
{
|
2021-06-04 14:31:56 -04:00
|
|
|
uint8_t datahigh = read(MAX44009_LUX_READING_HIGH);
|
2021-01-29 06:31:58 -05:00
|
|
|
if (_error != MAX44009_OK)
|
|
|
|
{
|
|
|
|
_error = MAX44009_ERROR_HIGH_BYTE;
|
|
|
|
return _error;
|
|
|
|
}
|
2021-06-04 14:31:56 -04:00
|
|
|
uint8_t datalow = read(MAX44009_LUX_READING_LOW);
|
2021-01-29 06:31:58 -05:00
|
|
|
if (_error != MAX44009_OK)
|
|
|
|
{
|
|
|
|
_error = MAX44009_ERROR_LOW_BYTE;
|
|
|
|
return _error;
|
|
|
|
}
|
2021-06-04 14:31:56 -04:00
|
|
|
uint8_t exponent = datahigh >> 4;
|
|
|
|
if (exponent == 0x0F)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
_error = MAX44009_ERROR_OVERFLOW;
|
|
|
|
return _error;
|
|
|
|
}
|
2021-06-04 14:31:56 -04:00
|
|
|
|
|
|
|
float lux = convertToLux(datahigh, datalow);
|
|
|
|
return lux;
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2016-12-17 14:58:29 -05:00
|
|
|
int Max44009::getError()
|
|
|
|
{
|
2021-06-04 14:31:56 -04:00
|
|
|
int err = _error;
|
2021-01-29 06:31:58 -05:00
|
|
|
_error = MAX44009_OK;
|
2021-06-04 14:31:56 -04:00
|
|
|
return err;
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-06-04 14:31:56 -04:00
|
|
|
bool Max44009::setHighThreshold(const float value)
|
2016-12-17 14:58:29 -05:00
|
|
|
{
|
2021-06-04 14:31:56 -04:00
|
|
|
return setThreshold(MAX44009_THRESHOLD_HIGH, value);
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2017-07-26 18:24:25 -04:00
|
|
|
float Max44009::getHighThreshold(void)
|
2016-12-17 14:58:29 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
return getThreshold(MAX44009_THRESHOLD_HIGH);
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-06-04 14:31:56 -04:00
|
|
|
bool Max44009::setLowThreshold(const float value)
|
2016-12-17 14:58:29 -05:00
|
|
|
{
|
2021-06-04 14:31:56 -04:00
|
|
|
return setThreshold(MAX44009_THRESHOLD_LOW, value);
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2017-07-26 18:24:25 -04:00
|
|
|
float Max44009::getLowThreshold(void)
|
2016-12-17 14:58:29 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
return getThreshold(MAX44009_THRESHOLD_LOW);
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2017-07-26 18:24:25 -04:00
|
|
|
void Max44009::setThresholdTimer(const uint8_t value)
|
2016-12-17 14:58:29 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
write(MAX44009_THRESHOLD_TIMER, value);
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2016-12-17 14:58:29 -05:00
|
|
|
uint8_t Max44009::getThresholdTimer()
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
return read(MAX44009_THRESHOLD_TIMER);
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2017-07-26 18:24:25 -04:00
|
|
|
void Max44009::setConfiguration(const uint8_t value)
|
2016-12-17 14:58:29 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
write(MAX44009_CONFIGURATION, value);
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2016-12-17 14:58:29 -05:00
|
|
|
uint8_t Max44009::getConfiguration()
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
return read(MAX44009_CONFIGURATION);
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2016-12-17 14:58:29 -05:00
|
|
|
void Max44009::setAutomaticMode()
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
// CDR & TIM cannot be written in automatic mode
|
|
|
|
uint8_t config = read(MAX44009_CONFIGURATION);
|
|
|
|
config &= ~MAX44009_CFG_MANUAL;
|
|
|
|
write(MAX44009_CONFIGURATION, config);
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2016-12-17 14:58:29 -05:00
|
|
|
void Max44009::setContinuousMode()
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t config = read(MAX44009_CONFIGURATION);
|
|
|
|
config |= MAX44009_CFG_CONTINUOUS;
|
|
|
|
write(MAX44009_CONFIGURATION, config);
|
2020-01-31 07:58:43 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-01-31 07:58:43 -05:00
|
|
|
void Max44009::clrContinuousMode()
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t config = read(MAX44009_CONFIGURATION);
|
|
|
|
config &= ~MAX44009_CFG_CONTINUOUS;
|
|
|
|
write(MAX44009_CONFIGURATION, config);
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2016-12-17 14:58:29 -05:00
|
|
|
void Max44009::setManualMode(uint8_t CDR, uint8_t TIM)
|
|
|
|
{
|
2021-05-28 07:37:15 -04:00
|
|
|
if (CDR !=0) CDR = 1; // only 0 or 1
|
2021-01-29 06:31:58 -05:00
|
|
|
if (TIM > 7) TIM = 7;
|
|
|
|
uint8_t config = read(MAX44009_CONFIGURATION);
|
|
|
|
config |= MAX44009_CFG_MANUAL;
|
|
|
|
config &= 0xF0; // clear old CDR & TIM bits
|
|
|
|
config |= CDR << 3 | TIM; // set new CDR & TIM bits
|
|
|
|
write(MAX44009_CONFIGURATION, config);
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-06-04 14:31:56 -04:00
|
|
|
float Max44009::convertToLux(uint8_t datahigh, uint8_t datalow)
|
|
|
|
{
|
|
|
|
uint8_t exponent = datahigh >> 4;
|
|
|
|
uint32_t mantissa = ((datahigh & 0x0F) << 4) + (datalow & 0x0F);
|
|
|
|
float lux = ((0x0001 << exponent) * 0.045) * mantissa;
|
|
|
|
return lux;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-17 14:58:29 -05:00
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// PRIVATE
|
|
|
|
//
|
2021-06-04 14:31:56 -04:00
|
|
|
bool Max44009::setThreshold(const uint8_t reg, const float value)
|
2016-12-17 14:58:29 -05:00
|
|
|
{
|
2021-06-04 14:31:56 -04:00
|
|
|
// CHECK RANGE OF VALUE
|
|
|
|
if ((value < 0.0) || (value > 188006)) return false;
|
|
|
|
|
|
|
|
uint32_t mantissa = round(value * 22.2222222); // was round(value / 0.045); multiply is faster.
|
|
|
|
uint8_t exponent = 0;
|
|
|
|
while (mantissa > 255)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-06-04 14:31:56 -04:00
|
|
|
mantissa >>= 1; // bits get lost
|
|
|
|
exponent++;
|
2021-01-29 06:31:58 -05:00
|
|
|
};
|
2021-06-04 14:31:56 -04:00
|
|
|
mantissa = (mantissa >> 4) & 0x0F;
|
|
|
|
exponent <<= 4;
|
|
|
|
write(reg, exponent | mantissa);
|
|
|
|
return true;
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2017-07-26 18:24:25 -04:00
|
|
|
float Max44009::getThreshold(uint8_t reg)
|
2016-12-17 14:58:29 -05:00
|
|
|
{
|
2021-06-04 14:31:56 -04:00
|
|
|
uint8_t datahigh = read(reg);
|
|
|
|
float lux = convertToLux(datahigh, 0x08); // 0x08 = correction for lost bits
|
|
|
|
return lux;
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2018-05-13 10:38:11 -04:00
|
|
|
uint8_t Max44009::read(uint8_t reg)
|
2016-12-17 14:58:29 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
_wire->beginTransmission(_address);
|
|
|
|
_wire->write(reg);
|
|
|
|
_error = _wire->endTransmission();
|
|
|
|
if (_error != MAX44009_OK)
|
|
|
|
{
|
|
|
|
return _data; // last value
|
|
|
|
}
|
|
|
|
if (_wire->requestFrom(_address, (uint8_t) 1) != 1)
|
|
|
|
{
|
|
|
|
_error = MAX44009_ERROR_WIRE_REQUEST;
|
|
|
|
return _data; // last value
|
|
|
|
}
|
|
|
|
_data = _wire->read();
|
|
|
|
return _data;
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2016-12-17 14:58:29 -05:00
|
|
|
void Max44009::write(uint8_t reg, uint8_t value)
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
_wire->beginTransmission(_address);
|
|
|
|
_wire->write(reg);
|
|
|
|
_wire->write(value);
|
|
|
|
_error = _wire->endTransmission();
|
2016-12-17 14:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:37:15 -04:00
|
|
|
|
2021-06-04 14:31:56 -04:00
|
|
|
// -- END OF FILE --
|