fix #133 overflow of exponent

+ added readme.md
+ some cleanup
This commit is contained in:
RobTillaart 2020-01-21 15:26:58 +01:00
parent 55fa3acd6a
commit 672af9f698
7 changed files with 50 additions and 24 deletions

View File

@ -3,7 +3,7 @@
// //
// FILE: Max44009.h // FILE: Max44009.h
// AUTHOR: Rob dot Tillaart at gmail dot com // AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.2.0 // VERSION: 0.3.1
// PURPOSE: library for MAX44009 lux sensor Arduino // PURPOSE: library for MAX44009 lux sensor Arduino
// HISTORY: See Max440099.cpp // HISTORY: See Max440099.cpp
// //
@ -18,9 +18,9 @@
#include "WProgram.h" #include "WProgram.h"
#endif #endif
#define MAX44009_LIB_VERSION "0.3.0" #define MAX44009_LIB_VERSION "0.3.1"
#define MAX44009_DEFAULT_ADDRESS 0x4A #define MAX44009_DEFAULT_ADDRESS 0x4A
#define MAX44009_ALT_ADDRESS 0x4B #define MAX44009_ALT_ADDRESS 0x4B
// REGISTERS // REGISTERS
#define MAX44009_INTERRUPT_STATUS 0x00 #define MAX44009_INTERRUPT_STATUS 0x00
@ -38,6 +38,11 @@
#define MAX44009_CFG_CDR 0x08 #define MAX44009_CFG_CDR 0x08
#define MAX44009_CFG_TIMER 0x07 #define MAX44009_CFG_TIMER 0x07
// ERROR CODES
#define MAX44009_OK 0
#define MAX44009_ERROR_WIRE_REQUEST -10
#define MAX44009_ERROR_OVERFLOW -20
class Max44009 class Max44009
{ {
@ -84,10 +89,10 @@ public:
// 001 400ms // 001 400ms
// 010 200ms // 010 200ms
// 011 100ms // 011 100ms
// 100 50ms // 100 50ms manual only
// 101 25ms // 101 25ms manual only
// 110 12.5ms // 110 12.5ms manual only
// 111 6.25ms // 111 6.25ms manual only
void setManualMode(uint8_t CDR, uint8_t TIM); void setManualMode(uint8_t CDR, uint8_t TIM);
private: private:

View File

@ -11,7 +11,7 @@
#include "Wire.h" #include "Wire.h"
#include "Max44009.h" #include "Max44009.h"
Max44009 myLux(MAX44009_DEFAULT_ADDRESS); Max44009 myLux(0xCB); // default addr
uint32_t lastDisplay = 0; uint32_t lastDisplay = 0;

View File

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

View File

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

View File

@ -1,12 +1,14 @@
// //
// FILE: Max44009.cpp // FILE: Max44009.cpp
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.2.0 // VERSION: 0.3.1
// PURPOSE: library for MAX44009 lux sensor Arduino // PURPOSE: library for MAX44009 lux sensor Arduino
// URL: https://github.com/RobTillaart/Arduino/tree/master/libraries // URL: https://github.com/RobTillaart/Arduino/tree/master/libraries
// //
// Released to the public domain // Released to the public domain
// //
// 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.2.0 - 2019-08-23 solve #127 == redo #118
// 0.1.10 - 2018-12-08 issue #118 Fix constructor esp8266 // 0.1.10 - 2018-12-08 issue #118 Fix constructor esp8266
// (thanks to Bolukan) // (thanks to Bolukan)
@ -30,7 +32,7 @@ Max44009::Max44009(const uint8_t address, const uint8_t dataPin, const uint8_t c
{ {
_address = address; _address = address;
_data = 0; _data = 0;
_error = 0; _error = MAX44009_OK;
_wire = &Wire; _wire = &Wire;
if ((dataPin < 255) && (clockPin < 255)) if ((dataPin < 255) && (clockPin < 255))
@ -39,7 +41,6 @@ Max44009::Max44009(const uint8_t address, const uint8_t dataPin, const uint8_t c
} else { } else {
_wire->begin(); _wire->begin();
} }
// TWBR = 12; // _wire->setClock(400000);
} }
#endif #endif
@ -47,28 +48,30 @@ Max44009::Max44009(const uint8_t address, Boolean begin)
{ {
_address = address; _address = address;
_data = 0; _data = 0;
_error = 0; _error = MAX44009_OK;
_wire = &Wire; _wire = &Wire;
if(begin == Boolean::True) { if (begin == Boolean::True)
{
_wire->begin(); _wire->begin();
} }
// TWBR = 12; // _wire->setClock(400000);
} }
Max44009::Max44009(const Boolean begin) Max44009::Max44009(const Boolean begin)
{ {
_address = MAX44009_DEFAULT_ADDRESS; _address = MAX44009_DEFAULT_ADDRESS;
_data = 0; _data = 0;
_error = 0; _error = MAX44009_OK;
_wire = &Wire; _wire = &Wire;
if(begin == Boolean::True) { if (begin == Boolean::True)
{
_wire->begin(); _wire->begin();
} }
} }
void Max44009::configure(const uint8_t address, TwoWire *wire) { void Max44009::configure(const uint8_t address, TwoWire *wire)
{
_address = address; _address = address;
_wire = wire; _wire = wire;
} }
@ -78,6 +81,11 @@ float Max44009::getLux(void)
uint8_t dhi = read(MAX44009_LUX_READING_HIGH); uint8_t dhi = read(MAX44009_LUX_READING_HIGH);
uint8_t dlo = read(MAX44009_LUX_READING_LOW); uint8_t dlo = read(MAX44009_LUX_READING_LOW);
uint8_t e = dhi >> 4; uint8_t e = dhi >> 4;
if (e == 0x0F)
{
_error = MAX44009_ERROR_OVERFLOW;
return -1;
}
uint32_t m = ((dhi & 0x0F) << 4) + (dlo & 0x0F); uint32_t m = ((dhi & 0x0F) << 4) + (dlo & 0x0F);
m <<= e; m <<= e;
float val = m * 0.045; float val = m * 0.045;
@ -87,7 +95,7 @@ float Max44009::getLux(void)
int Max44009::getError() int Max44009::getError()
{ {
int e = _error; int e = _error;
_error = 0; _error = MAX44009_OK;
return e; return e;
} }
@ -154,7 +162,7 @@ void Max44009::setManualMode(uint8_t CDR, uint8_t TIM)
uint8_t config = read(MAX44009_CONFIGURATION); uint8_t config = read(MAX44009_CONFIGURATION);
config &= ~MAX44009_CFG_CONTINUOUS; // off config &= ~MAX44009_CFG_CONTINUOUS; // off
config |= MAX44009_CFG_MANUAL; // on config |= MAX44009_CFG_MANUAL; // on
config &= 0xF0; // clear CDR & TIM bits config &= 0xF0; // clear CDR & TIM bits
config |= CDR << 3 | TIM; config |= CDR << 3 | TIM;
write(MAX44009_CONFIGURATION, config); write(MAX44009_CONFIGURATION, config);
} }
@ -193,13 +201,13 @@ uint8_t Max44009::read(uint8_t reg)
_wire->beginTransmission(_address); _wire->beginTransmission(_address);
_wire->write(reg); _wire->write(reg);
_error = _wire->endTransmission(); _error = _wire->endTransmission();
if (_error != 0) if (_error != MAX44009_OK)
{ {
return _data; // last value return _data; // last value
} }
if (_wire->requestFrom(_address, (uint8_t) 1) != 1) if (_wire->requestFrom(_address, (uint8_t) 1) != 1)
{ {
_error = 10; _error = MAX44009_ERROR_WIRE_REQUEST;
return _data; // last value return _data; // last value
} }
#if (ARDUINO < 100) #if (ARDUINO < 100)

View File

@ -0,0 +1,13 @@
MAX44009 I2C LUX sensor
====
About examples
----
__Max44009\examples\max44009_test01__
- use for e.g. UNO
__Max44009\examples\max44009_test02__
- will not compile for UNO
- use for e.g. ESP32