issue #132, fix for reading full range

set automatic mode in constructor
add some error checking
This commit is contained in:
RobTillaart 2020-01-27 16:40:10 +01:00
parent 672af9f698
commit 8ddfb12b44
5 changed files with 32 additions and 14 deletions

View File

@ -42,6 +42,8 @@
#define MAX44009_OK 0
#define MAX44009_ERROR_WIRE_REQUEST -10
#define MAX44009_ERROR_OVERFLOW -20
#define MAX44009_ERROR_HIGH_BYTE -30
#define MAX44009_ERROR_LOW_BYTE -31
class Max44009

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/Arduino.git"
},
"version": "0.3.1",
"version": "0.3.2",
"frameworks": "arduino",
"platforms": "*",
"export": {

View File

@ -1,5 +1,5 @@
name=Max44009
version=0.3.1
version=0.3.2
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for MAX44009 lux sensor Arduino.

View File

@ -1,12 +1,15 @@
//
// FILE: Max44009.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.1
// VERSION: 0.3.2
// PURPOSE: library for MAX44009 lux sensor Arduino
// URL: https://github.com/RobTillaart/Arduino/tree/master/libraries
//
// Released to the public domain
//
// 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
@ -41,6 +44,7 @@ Max44009::Max44009(const uint8_t address, const uint8_t dataPin, const uint8_t c
} else {
_wire->begin();
}
setAutomaticMode();
}
#endif
@ -54,6 +58,7 @@ Max44009::Max44009(const uint8_t address, Boolean begin)
if (begin == Boolean::True)
{
_wire->begin();
setAutomaticMode();
}
}
@ -67,6 +72,7 @@ Max44009::Max44009(const Boolean begin)
if (begin == Boolean::True)
{
_wire->begin();
setAutomaticMode();
}
}
@ -79,12 +85,22 @@ void Max44009::configure(const uint8_t address, TwoWire *wire)
float Max44009::getLux(void)
{
uint8_t dhi = read(MAX44009_LUX_READING_HIGH);
if (_error != MAX44009_OK)
{
_error = MAX44009_ERROR_HIGH_BYTE;
return _error;
}
uint8_t dlo = read(MAX44009_LUX_READING_LOW);
if (_error != MAX44009_OK)
{
_error = MAX44009_ERROR_LOW_BYTE;
return _error;
}
uint8_t e = dhi >> 4;
if (e == 0x0F)
{
_error = MAX44009_ERROR_OVERFLOW;
return -1;
return _error;
}
uint32_t m = ((dhi & 0x0F) << 4) + (dlo & 0x0F);
m <<= e;
@ -141,6 +157,7 @@ uint8_t Max44009::getConfiguration()
void Max44009::setAutomaticMode()
{
// CDR & TIM cannot be written in automatic mode
uint8_t config = read(MAX44009_CONFIGURATION);
config &= ~MAX44009_CFG_CONTINUOUS; // off
config &= ~MAX44009_CFG_MANUAL; // off
@ -162,8 +179,8 @@ void Max44009::setManualMode(uint8_t CDR, uint8_t TIM)
uint8_t config = read(MAX44009_CONFIGURATION);
config &= ~MAX44009_CFG_CONTINUOUS; // off
config |= MAX44009_CFG_MANUAL; // on
config &= 0xF0; // clear CDR & TIM bits
config |= CDR << 3 | TIM;
config &= 0xF0; // clear old CDR & TIM bits
config |= CDR << 3 | TIM; // set new CDR & TIM bits
write(MAX44009_CONFIGURATION, config);
}
@ -174,7 +191,7 @@ void Max44009::setManualMode(uint8_t CDR, uint8_t TIM)
void Max44009::setThreshold(const uint8_t reg, const float value)
{
// TODO CHECK RANGE
uint32_t m = round(value * 22.2222222); // was round(value / 0.045); mulitply is faster.
uint32_t m = round(value * 22.2222222); // was round(value / 0.045); multiply is faster.
uint8_t e = 0;
while (m > 255)
{

View File

@ -1,13 +1,12 @@
MAX44009 I2C LUX sensor
====
# MAX44009 I2C LUX sensor
About examples
----
__Max44009\examples\max44009_test01__
## Examples
**Max44009\examples\max44009_test01**
- use for e.g. UNO
__Max44009\examples\max44009_test02__
**Max44009\examples\max44009_test02**
- will not compile for UNO
- use for e.g. ESP32