fix #142 constructor, major refactor ==> 0.4.0

+ redo setManualMode()
+ redo setAutomaticMode()
+ redo setContinuousMode()
+ add clrContinuousMode()
+ add getIntegrationTime()
+ add 4 examples

Signed-off-by: RobTillaart <rob.tillaart@gmail.com>
This commit is contained in:
RobTillaart 2020-01-31 13:58:43 +01:00
parent 029aab4219
commit e0e23a36e8
9 changed files with 469 additions and 168 deletions

View File

@ -1,24 +1,39 @@
#ifndef MAX44009_H
#define MAX44009_H
//
//
// FILE: Max44009.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.3.3
// VERSION: 0.4.0
// PURPOSE: library for MAX44009 lux sensor Arduino
// HISTORY: See Max440099.cpp
//
// Released to the public domain
//
// breakout max44009 / GY-49
//
// +--------+
// VCC |o |
// GND |o |
// SCL |o o| ADDRESS
// SDA |o o| -INT
// +--------+
//
// ADDRESS:
// 0 = 0x4A
// 1 = 0x4B
//
// INT:
// Connect the INT pin to an pull up resistor
// 0 = interrupt
// 1 = no interrupt
// connect to an LED or an interrupt pin of an Arduino
//
#include "Wire.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define MAX44009_LIB_VERSION "0.3.3"
#define MAX44009_LIB_VERSION "0.4.0"
#define MAX44009_DEFAULT_ADDRESS 0x4A
#define MAX44009_ALT_ADDRESS 0x4B
@ -49,66 +64,68 @@
class Max44009
{
public:
// enum class to prevent bool to be implicitly casted to int
enum class Boolean { True, False };
// enum class to prevent bool to be implicitly casted to int
enum class Boolean { True, False };
#if defined(ESP8266) || defined(ESP32)
// dataPin and clockPin can be used for ESP8266
Max44009(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin);
// dataPin and clockPin can be used for ESP8266
Max44009(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin);
#endif
// ctor for UNO
Max44009(const uint8_t address, const Boolean begin = Boolean::True);
// default ctor with default address
Max44009(const Boolean begin = Boolean::True);
// Change I2C interface and address
void configure(const uint8_t address, TwoWire *wire, const Boolean begin = Boolean::True);
// ctor for UNO
Max44009(const uint8_t address, const Boolean begin = Boolean::True);
// default ctor with default address
Max44009(const Boolean begin = Boolean::True);
// Change I2C interface and address
void configure(const uint8_t address, TwoWire *wire, const Boolean begin = Boolean::True);
float getLux();
int getError();
float getLux();
int getError();
void setHighThreshold(const float);
float getHighThreshold(void);
void setLowThreshold(const float);
float getLowThreshold(void);
void setThresholdTimer(const uint8_t);
uint8_t getThresholdTimer();
void setHighThreshold(const float);
float getHighThreshold(void);
void setLowThreshold(const float);
float getLowThreshold(void);
void setThresholdTimer(const uint8_t); // 2 seems practical minimum
uint8_t getThresholdTimer();
void enableInterrupt() { write(MAX44009_INTERRUPT_ENABLE, 1); };
void disableInterrupt() { write(MAX44009_INTERRUPT_ENABLE, 0); };
bool interruptEnabled() { return read(MAX44009_INTERRUPT_ENABLE) & 0x01; };
uint8_t getInterruptStatus() { return read(MAX44009_INTERRUPT_STATUS) & 0x01; };
void enableInterrupt() { write(MAX44009_INTERRUPT_ENABLE, 1); };
void disableInterrupt() { write(MAX44009_INTERRUPT_ENABLE, 0); };
bool interruptEnabled() { return read(MAX44009_INTERRUPT_ENABLE) & 0x01; };
uint8_t getInterruptStatus() { return read(MAX44009_INTERRUPT_STATUS) & 0x01; };
// check datasheet for detailed behavior
void setConfiguration(uint8_t);
uint8_t getConfiguration();
void setAutomaticMode();
void setContinuousMode();
// CDR = Current Divisor Ratio
// CDR = 1 ==> only 1/8th is measured
// TIM = Time Integration Measurement (table)
// 000 800ms
// 001 400ms
// 010 200ms
// 011 100ms
// 100 50ms manual only
// 101 25ms manual only
// 110 12.5ms manual only
// 111 6.25ms manual only
void setManualMode(uint8_t CDR, uint8_t TIM);
// check datasheet for detailed behavior
void setConfiguration(uint8_t);
uint8_t getConfiguration();
void setAutomaticMode();
void setContinuousMode(); // uses more power
void clrContinuousMode(); // uses less power
// CDR = Current Divisor Ratio
// CDR = 1 ==> only 1/8th is measured
// TIM = Time Integration Measurement (table)
// 000 800ms
// 001 400ms
// 010 200ms
// 011 100ms
// 100 50ms manual only
// 101 25ms manual only
// 110 12.5ms manual only
// 111 6.25ms manual only
void setManualMode(uint8_t CDR, uint8_t TIM);
int getIntegrationTime() { return 800 >> (getConfiguration() & 0x07); }; // ms
private:
void setThreshold(uint8_t, float);
float getThreshold(uint8_t);
void setThreshold(uint8_t, float);
float getThreshold(uint8_t);
uint8_t read(uint8_t reg);
void write(uint8_t, uint8_t);
uint8_t read(uint8_t reg);
void write(uint8_t, uint8_t);
uint8_t _address;
uint8_t _data;
int _error;
uint8_t _address;
uint8_t _data;
int _error;
TwoWire* _wire;
TwoWire* _wire;
};
#endif

View File

@ -0,0 +1,66 @@
//
// FILE: max44009_interrupt.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo of max44009 library
// DATE: 2020-01-30
//
// Released to the public domain
//
#include "Wire.h"
#include "Max44009.h"
Max44009 myLux(0x4A);
uint32_t lastDisplay = 0;
void setup()
{
Serial.begin(115200);
Serial.print("\nStart max44009_interrupt : ");
Serial.println(MAX44009_LIB_VERSION);
Wire.begin();
myLux.setContinuousMode();
myLux.setHighThreshold(30);
Serial.print("HighThreshold:\t");
Serial.println(myLux.getHighThreshold());
myLux.setLowThreshold(10);
Serial.print("LowThreshold:\t");
Serial.println(myLux.getLowThreshold());
myLux.setThresholdTimer(2);
myLux.enableInterrupt();
}
void loop()
{
uint32_t interval = 1000;
if (millis() - lastDisplay >= interval)
{
lastDisplay += interval;
float lux = myLux.getLux();
int err = myLux.getError();
int st = myLux.getInterruptStatus();
if (err != 0)
{
Serial.print("Error:\t");
Serial.println(err);
}
else
{
Serial.print("lux:\t");
Serial.print(lux);
if (st == 1) Serial.println("\tIRQ occurred");
else Serial.println();
}
}
}
// END OF FILE

View File

@ -0,0 +1,68 @@
//
// FILE: max44009_setAutomaticMode.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo of max44009 library
// DATE: 2020-01-30
//
// Released to the public domain
//
#include "Wire.h"
#include "Max44009.h"
Max44009 myLux(0x4A);
uint32_t lastDisplay = 0;
void setup()
{
Serial.begin(115200);
Serial.print("\nStart max44009_setAutomaticMode : ");
Serial.println(MAX44009_LIB_VERSION);
Wire.begin();
myLux.setAutomaticMode();
}
void loop()
{
uint32_t interval = 100;
if (millis() - lastDisplay >= interval)
{
lastDisplay += interval;
float lux = myLux.getLux();
int err = myLux.getError();
// in automatic mode TIM & CDR are automatic generated
// and read only (in manual mode they are set by the user
int conf = myLux.getConfiguration();
int CDR = (conf & 0x80) >> 3;
int TIM = (conf & 0x07);
int integrationTime = myLux.getIntegrationTime();
if (err != 0)
{
Serial.print("Error:\t");
Serial.println(err);
}
else
{
Serial.print("lux:\t");
Serial.print(lux);
Serial.print("\tCDR:\t");
Serial.print(CDR);
Serial.print("\tTIM:\t");
Serial.print(TIM);
Serial.print("\t");
Serial.print(integrationTime);
Serial.print(" ms");
Serial.println();
}
}
}
// END OF FILE

View File

@ -0,0 +1,71 @@
//
// FILE: max44009_setContinuousMode.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo of max44009 library
// DATE: 2020-01-30
//
// Released to the public domain
//
// note that lux values are repeated a number of times
// if read faster than the integration time.
// So it makes no sense to call getLux() more than once
// per integration time.
// NB the getLux() call takes a bit more than 1 millisecond
#include "Wire.h"
#include "Max44009.h"
Max44009 myLux(0x4A);
uint32_t lastDisplay = 0;
uint32_t start = 0;
uint32_t stop = 0;
int count = 0;
void setup()
{
Serial.begin(115200);
Serial.print("\nStart max44009_setContinuousMode : ");
Serial.println(MAX44009_LIB_VERSION);
Wire.begin();
myLux.setContinuousMode();
}
void loop()
{
uint32_t interval = 100;
if (millis() - lastDisplay >= interval)
{
lastDisplay += interval;
count++;
start = micros();
float lux = myLux.getLux();
stop = micros();
int err = myLux.getError();
if (err != 0)
{
Serial.print("Error:\t");
Serial.println(err);
}
else
{
Serial.print("lux:\t");
Serial.print(lux);
Serial.print("\ttime:\t");
Serial.print(stop - start);
Serial.println();
}
}
if (count == 5) myLux.clrContinuousMode();
if (count == 10)
{
count = 0;
myLux.setContinuousMode();
}
}
// END OF FILE

View File

@ -0,0 +1,73 @@
//
// FILE: max44009_setManualMode.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo of max44009 library
// DATE: 2020-01-30
//
// Released to the public domain
//
#include "Wire.h"
#include "Max44009.h"
Max44009 myLux(0x4A);
uint32_t lastDisplay = 0;
uint32_t lastChangeCDRTIM = 0;
uint8_t CDR = 0;
uint8_t TIM = 0;
void setup()
{
Serial.begin(115200);
Serial.print("\nStart max44009_setManualMode : ");
Serial.println(MAX44009_LIB_VERSION);
Wire.begin();
myLux.setManualMode(CDR, TIM);
}
void loop()
{
uint32_t interval = 100;
if (millis() - lastDisplay >= interval)
{
lastDisplay += interval;
float lux = myLux.getLux();
int err = myLux.getError();
if (err != 0)
{
Serial.print("Error:\t");
Serial.println(err);
}
else
{
Serial.print("lux:\t");
Serial.println(lux);
}
}
// Change CDR and TIM every 5 seconds
// Note that the value of getLux() is affected
// for up to about a second.
if (millis() - lastChangeCDRTIM >= 5000)
{
lastChangeCDRTIM += 5000;
TIM++;
if (TIM == 4)
{
TIM = 0;
CDR = (CDR + 1) & 1;
}
myLux.setManualMode(CDR, TIM);
Serial.print("CDR:\t");
Serial.print((int)CDR);
Serial.print("\tTIM:\t");
Serial.println((int)TIM);
}
}
// END OF FILE

View File

@ -26,6 +26,13 @@ setAutomaticMode KEYWORD2
setContinuousMode KEYWORD2
setManualMode KEYWORD2
enableInterrupt KEYWORD2
disableInterrupt KEYWORD2
interruptEnabled KEYWORD2
getInterruptStatus KEYWORD2
clrContinuousMode KEYWORD2
getIntegrationTime KEYWORD2
#######################################
# Instances (KEYWORD2)
#######################################

View File

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

View File

@ -1,5 +1,5 @@
name=Max44009
version=0.3.3
version=0.4.0
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,14 @@
//
// FILE: Max44009.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.3
// VERSION: 0.4.0
// PURPOSE: library for MAX44009 lux sensor Arduino
// URL: https://github.com/RobTillaart/Arduino/tree/master/libraries
//
// Released to the public domain
//
// 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;
@ -34,18 +36,17 @@
#if defined(ESP8266) || defined(ESP32)
Max44009::Max44009(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin)
{
_address = address;
_data = 0;
_error = MAX44009_OK;
_wire = &Wire;
_address = address;
_data = 0;
_error = MAX44009_OK;
_wire = &Wire;
if ((dataPin < 255) && (clockPin < 255))
{
_wire->begin(dataPin, clockPin);
} else {
_wire->begin();
}
setAutomaticMode();
if ((dataPin < 255) && (clockPin < 255))
{
_wire->begin(dataPin, clockPin);
} else {
_wire->begin();
}
}
#endif
@ -59,121 +60,123 @@ Max44009::Max44009(const Boolean begin)
Max44009::configure(MAX44009_DEFAULT_ADDRESS, &Wire, begin);
}
void Max44009::configure(const uint8_t address, TwoWire *wire, const Boolean begin)
{
_address = address;
_data = 0;
_error = MAX44009_OK;
_wire = wire;
_address = address;
_data = 0;
_error = MAX44009_OK;
_wire = wire;
if (begin == Boolean::True)
{
_wire->begin();
setAutomaticMode();
}
if (begin == Boolean::True)
{
_wire->begin();
}
}
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 _error;
}
uint32_t m = ((dhi & 0x0F) << 4) + (dlo & 0x0F);
m <<= e;
float val = m * 0.045;
return val;
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 _error;
}
uint32_t m = ((dhi & 0x0F) << 4) + (dlo & 0x0F);
m <<= e;
float val = m * 0.045;
return val;
}
int Max44009::getError()
{
int e = _error;
_error = MAX44009_OK;
return e;
int e = _error;
_error = MAX44009_OK;
return e;
}
void Max44009::setHighThreshold(const float value)
{
setThreshold(MAX44009_THRESHOLD_HIGH, value);
setThreshold(MAX44009_THRESHOLD_HIGH, value);
}
float Max44009::getHighThreshold(void)
{
return getThreshold(MAX44009_THRESHOLD_HIGH);
return getThreshold(MAX44009_THRESHOLD_HIGH);
}
void Max44009::setLowThreshold(const float value)
{
setThreshold(MAX44009_THRESHOLD_LOW, value);
setThreshold(MAX44009_THRESHOLD_LOW, value);
}
float Max44009::getLowThreshold(void)
{
return getThreshold(MAX44009_THRESHOLD_LOW);
return getThreshold(MAX44009_THRESHOLD_LOW);
}
void Max44009::setThresholdTimer(const uint8_t value)
{
write(MAX44009_THRESHOLD_TIMER, value);
write(MAX44009_THRESHOLD_TIMER, value);
}
uint8_t Max44009::getThresholdTimer()
{
return read(MAX44009_THRESHOLD_TIMER);
return read(MAX44009_THRESHOLD_TIMER);
}
void Max44009::setConfiguration(const uint8_t value)
{
write(MAX44009_CONFIGURATION, value);
write(MAX44009_CONFIGURATION, value);
}
uint8_t Max44009::getConfiguration()
{
return read(MAX44009_CONFIGURATION);
return read(MAX44009_CONFIGURATION);
}
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
write(MAX44009_CONFIGURATION, config);
// CDR & TIM cannot be written in automatic mode
uint8_t config = read(MAX44009_CONFIGURATION);
config &= ~MAX44009_CFG_MANUAL;
write(MAX44009_CONFIGURATION, config);
}
void Max44009::setContinuousMode()
{
uint8_t config = read(MAX44009_CONFIGURATION);
config |= MAX44009_CFG_CONTINUOUS; // on
config &= ~MAX44009_CFG_MANUAL; // off
write(MAX44009_CONFIGURATION, config);
uint8_t config = read(MAX44009_CONFIGURATION);
config |= MAX44009_CFG_CONTINUOUS;
write(MAX44009_CONFIGURATION, config);
}
void Max44009::clrContinuousMode()
{
uint8_t config = read(MAX44009_CONFIGURATION);
config &= ~MAX44009_CFG_CONTINUOUS;
write(MAX44009_CONFIGURATION, config);
}
void Max44009::setManualMode(uint8_t CDR, uint8_t TIM)
{
if (CDR !=0) CDR = 1;
if (TIM > 7) TIM = 7;
uint8_t config = read(MAX44009_CONFIGURATION);
config &= ~MAX44009_CFG_CONTINUOUS; // off
config |= MAX44009_CFG_MANUAL; // on
config &= 0xF0; // clear old CDR & TIM bits
config |= CDR << 3 | TIM; // set new CDR & TIM bits
write(MAX44009_CONFIGURATION, config);
if (CDR !=0) CDR = 1;
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);
}
///////////////////////////////////////////////////////////
@ -182,57 +185,53 @@ 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); multiply is faster.
uint8_t e = 0;
while (m > 255)
{
m >>= 1;
e++;
};
m = (m >> 4) & 0x0F;
e <<= 4;
write(reg, e | m);
// 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)
{
m >>= 1; // bits get lost
e++;
};
m = (m >> 4) & 0x0F;
e <<= 4;
write(reg, e | m);
}
float Max44009::getThreshold(uint8_t reg)
{
uint8_t data = read(reg);
uint8_t e = (data & 0xF0) >> 4;
uint32_t m = ((data & 0x0F) << 4) + 0x0F;
m <<= e;
float val = m * 0.045;
return val;
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 Max44009::read(uint8_t reg)
{
_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
}
#if (ARDUINO < 100)
_data = _wire->receive();
#else
_data = _wire->read();
#endif
return _data;
_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;
}
void Max44009::write(uint8_t reg, uint8_t value)
{
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->write(value);
_error = _wire->endTransmission();
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->write(value);
_error = _wire->endTransmission();
}
// --- END OF FILE ---