mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.5.0 MAX44009
This commit is contained in:
parent
4ae99be508
commit
ad7a0cac83
@ -2,10 +2,11 @@
|
||||
|
||||
// FILE: Max44009.h
|
||||
// AUTHOR: Rob dot Tillaart at gmail dot com
|
||||
// VERSION: 0.4.4
|
||||
// VERSION: 0.5.0
|
||||
// PURPOSE: library for MAX44009 lux sensor Arduino
|
||||
// HISTORY: See Max440099.cpp
|
||||
|
||||
|
||||
// breakout MAX44009 / GY-49
|
||||
//
|
||||
// +--------+
|
||||
@ -31,7 +32,7 @@
|
||||
#include "Arduino.h"
|
||||
|
||||
|
||||
#define MAX44009_LIB_VERSION (F("0.4.4"))
|
||||
#define MAX44009_LIB_VERSION (F("0.5.0"))
|
||||
|
||||
#define MAX44009_DEFAULT_ADDRESS 0x4A
|
||||
#define MAX44009_ALT_ADDRESS 0x4B
|
||||
@ -85,9 +86,10 @@ public:
|
||||
float getLux();
|
||||
int getError();
|
||||
|
||||
void setHighThreshold(const float);
|
||||
// threshold must be between 0 and 188006
|
||||
bool setHighThreshold(const float); // returns false if value out of range
|
||||
float getHighThreshold(void);
|
||||
void setLowThreshold(const float);
|
||||
bool setLowThreshold(const float); // returns false if value out of range
|
||||
float getLowThreshold(void);
|
||||
void setThresholdTimer(const uint8_t); // 2 seems practical minimum
|
||||
uint8_t getThresholdTimer();
|
||||
@ -117,8 +119,12 @@ public:
|
||||
void setManualMode(uint8_t CDR, uint8_t TIM);
|
||||
int getIntegrationTime() { return 800 >> (getConfiguration() & 0x07); }; // ms
|
||||
|
||||
// TEST the math
|
||||
float convertToLux(uint8_t datahigh, uint8_t datalow);
|
||||
|
||||
|
||||
private:
|
||||
void setThreshold(uint8_t, float);
|
||||
bool setThreshold(uint8_t, float);
|
||||
float getThreshold(uint8_t);
|
||||
|
||||
uint8_t read(uint8_t reg);
|
||||
@ -132,4 +138,4 @@ private:
|
||||
};
|
||||
|
||||
|
||||
// END OF FILE
|
||||
// -- END OF FILE --
|
||||
|
@ -13,9 +13,9 @@
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/Max44009"
|
||||
"url": "https://github.com/RobTillaart/Max44009.git"
|
||||
},
|
||||
"version": "0.4.4",
|
||||
"version": "0.5.0",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=Max44009
|
||||
version=0.4.4
|
||||
version=0.5.0
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Library for MAX44009 I2C LUX sensor Arduino.
|
||||
|
@ -1,12 +1,13 @@
|
||||
//
|
||||
// FILE: Max44009.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.4.4
|
||||
// VERSION: 0.5.0
|
||||
// PURPOSE: library for MAX44009 lux sensor Arduino
|
||||
// URL: https://github.com/RobTillaart/MAX44009
|
||||
//
|
||||
// HISTORY
|
||||
//
|
||||
// 0.5.0 2021-06-04 fix exponent math.
|
||||
// 0.4.4 2021-05-27 arduino-lint
|
||||
// 0.4.3 2020-12-30 arduino-ci, unit test
|
||||
// 0.4.2 2020-06-19 fix library.json
|
||||
@ -93,42 +94,41 @@ bool Max44009::isConnected()
|
||||
|
||||
float Max44009::getLux(void)
|
||||
{
|
||||
uint8_t dhi = read(MAX44009_LUX_READING_HIGH);
|
||||
uint8_t datahigh = read(MAX44009_LUX_READING_HIGH);
|
||||
if (_error != MAX44009_OK)
|
||||
{
|
||||
_error = MAX44009_ERROR_HIGH_BYTE;
|
||||
return _error;
|
||||
}
|
||||
uint8_t dlo = read(MAX44009_LUX_READING_LOW);
|
||||
uint8_t datalow = read(MAX44009_LUX_READING_LOW);
|
||||
if (_error != MAX44009_OK)
|
||||
{
|
||||
_error = MAX44009_ERROR_LOW_BYTE;
|
||||
return _error;
|
||||
}
|
||||
uint8_t e = dhi >> 4;
|
||||
if (e == 0x0F)
|
||||
uint8_t exponent = datahigh >> 4;
|
||||
if (exponent == 0x0F)
|
||||
{
|
||||
_error = MAX44009_ERROR_OVERFLOW;
|
||||
return _error;
|
||||
}
|
||||
uint32_t m = ((dhi & 0x0F) << 4) + (dlo & 0x0F);
|
||||
m <<= e;
|
||||
float val = m * 0.045;
|
||||
return val;
|
||||
|
||||
float lux = convertToLux(datahigh, datalow);
|
||||
return lux;
|
||||
}
|
||||
|
||||
|
||||
int Max44009::getError()
|
||||
{
|
||||
int e = _error;
|
||||
int err = _error;
|
||||
_error = MAX44009_OK;
|
||||
return e;
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
void Max44009::setHighThreshold(const float value)
|
||||
bool Max44009::setHighThreshold(const float value)
|
||||
{
|
||||
setThreshold(MAX44009_THRESHOLD_HIGH, value);
|
||||
return setThreshold(MAX44009_THRESHOLD_HIGH, value);
|
||||
}
|
||||
|
||||
|
||||
@ -138,9 +138,9 @@ float Max44009::getHighThreshold(void)
|
||||
}
|
||||
|
||||
|
||||
void Max44009::setLowThreshold(const float value)
|
||||
bool Max44009::setLowThreshold(const float value)
|
||||
{
|
||||
setThreshold(MAX44009_THRESHOLD_LOW, value);
|
||||
return setThreshold(MAX44009_THRESHOLD_LOW, value);
|
||||
}
|
||||
|
||||
|
||||
@ -211,34 +211,43 @@ void Max44009::setManualMode(uint8_t CDR, uint8_t TIM)
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
//
|
||||
// PRIVATE
|
||||
//
|
||||
void Max44009::setThreshold(const uint8_t reg, const float value)
|
||||
bool Max44009::setThreshold(const uint8_t reg, const float value)
|
||||
{
|
||||
// TODO CHECK RANGE
|
||||
uint32_t m = round(value * 22.2222222); // was round(value / 0.045); multiply is faster.
|
||||
uint8_t e = 0;
|
||||
while (m > 255)
|
||||
// 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)
|
||||
{
|
||||
m >>= 1; // bits get lost
|
||||
e++;
|
||||
mantissa >>= 1; // bits get lost
|
||||
exponent++;
|
||||
};
|
||||
m = (m >> 4) & 0x0F;
|
||||
e <<= 4;
|
||||
write(reg, e | m);
|
||||
mantissa = (mantissa >> 4) & 0x0F;
|
||||
exponent <<= 4;
|
||||
write(reg, exponent | mantissa);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
float Max44009::getThreshold(uint8_t reg)
|
||||
{
|
||||
uint8_t data = read(reg);
|
||||
uint8_t e = (data & 0xF0) >> 4;
|
||||
uint32_t m = ((data & 0x0F) << 4) + 0x08; // 0x08 = correction for lost bits
|
||||
m <<= e;
|
||||
float val = m * 0.045;
|
||||
return val;
|
||||
uint8_t datahigh = read(reg);
|
||||
float lux = convertToLux(datahigh, 0x08); // 0x08 = correction for lost bits
|
||||
return lux;
|
||||
}
|
||||
|
||||
|
||||
@ -270,4 +279,4 @@ void Max44009::write(uint8_t reg, uint8_t value)
|
||||
}
|
||||
|
||||
|
||||
// --- END OF FILE ---
|
||||
// -- END OF FILE --
|
||||
|
@ -74,11 +74,11 @@ dynamic range from 0.045 lux to 188,000 lux.
|
||||
|
||||
check datasheet for details
|
||||
|
||||
- **void setHighThreshold(float)** sets the upper threshold for the interrupt generation (INT pulls LOW). Works only if INTE bit is set by **enableInterrupt()**.
|
||||
- **bool setHighThreshold(float)** sets the upper threshold for the interrupt generation (INT pulls LOW). Works only if INTE bit is set by **enableInterrupt()**. Function returns false if the value is out of range.
|
||||
- **float getHighThreshold(void)** returns the value set.
|
||||
- **void setLowThreshold(float)** sets the lower threshold for the interrupt generation (INT pulls LOW). Works only if INTE bit is set by **enableInterrupt()**.
|
||||
- **bool setLowThreshold(float)** sets the lower threshold for the interrupt generation (INT pulls LOW). Works only if INTE bit is set by **enableInterrupt()**. Function returns false if the value is out of range.
|
||||
- **float getLowThreshold(void)** returns the value set.
|
||||
- **void setThresholdTimer(uint8_t)** Time the threshold needs to be exceeded, defined in steps of 100ms. 2 seems to be a practical minimum.
|
||||
- **void setThresholdTimer(uint8_t)** Time the threshold needs to be exceeded, defined in steps of 100ms. 2 seems to be a practical minimum.
|
||||
- **uint8_t getThresholdTimer()** returns the value set.
|
||||
|
||||
|
||||
@ -130,6 +130,14 @@ TIM = Integration time.
|
||||
```
|
||||
|
||||
|
||||
### Test functions
|
||||
|
||||
Function for the conversion math, not meant to be used directly,
|
||||
but by making them public they become testable.
|
||||
|
||||
- **float convertToLux(uint8_t datahigh, uint8_t datalow)** convert intern register format to a LUX value.
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
**Max44009\examples\max44009_test01**
|
||||
@ -142,7 +150,7 @@ TIM = Integration time.
|
||||
|
||||
## Notes
|
||||
|
||||
Please be aware this is a 3.3 Volt device so it should not be connected
|
||||
Please be aware this is a **3.3 Volt device** so it should not be connected
|
||||
to an Arduino or other 5 Volt device directly. Use a level convertor to
|
||||
solve this.
|
||||
|
||||
|
@ -60,6 +60,24 @@ unittest(test_constructor)
|
||||
}
|
||||
|
||||
|
||||
unittest(test_convertToLux)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", MAX44009_LIB_VERSION);
|
||||
|
||||
Max44009 LuxA(0x4A);
|
||||
|
||||
assertEqualFloat(0.000, LuxA.convertToLux(0x00, 0x00), 0.0001);
|
||||
assertEqualFloat(0.045, LuxA.convertToLux(0x00, 0x01), 0.0001);
|
||||
assertEqualFloat(0.720, LuxA.convertToLux(0x01, 0x00), 0.0001);
|
||||
assertEqualFloat(1.530, LuxA.convertToLux(0x11, 0x01), 0.0001);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
assertEqualFloat(187269, LuxA.convertToLux(0xEF, 0x0E), 1);
|
||||
assertEqualFloat(188006, LuxA.convertToLux(0xEF, 0x0F), 1);
|
||||
fprintf(stderr, "\ndone...\n");
|
||||
}
|
||||
|
||||
|
||||
unittest_main()
|
||||
|
||||
// --------
|
||||
|
Loading…
Reference in New Issue
Block a user