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

@ -3,22 +3,37 @@
//
// 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
@ -71,7 +86,7 @@ public:
float getHighThreshold(void);
void setLowThreshold(const float);
float getLowThreshold(void);
void setThresholdTimer(const uint8_t);
void setThresholdTimer(const uint8_t); // 2 seems practical minimum
uint8_t getThresholdTimer();
void enableInterrupt() { write(MAX44009_INTERRUPT_ENABLE, 1); };
@ -83,7 +98,8 @@ public:
void setConfiguration(uint8_t);
uint8_t getConfiguration();
void setAutomaticMode();
void setContinuousMode();
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)
@ -96,6 +112,7 @@ public:
// 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);

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;
@ -45,7 +47,6 @@ Max44009::Max44009(const uint8_t address, const uint8_t dataPin, const uint8_t c
} else {
_wire->begin();
}
setAutomaticMode();
}
#endif
@ -59,7 +60,6 @@ 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;
@ -70,7 +70,6 @@ void Max44009::configure(const uint8_t address, TwoWire *wire, const Boolean beg
if (begin == Boolean::True)
{
_wire->begin();
setAutomaticMode();
}
}
@ -151,16 +150,21 @@ 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
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
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);
}
@ -169,8 +173,7 @@ 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 |= MAX44009_CFG_MANUAL;
config &= 0xF0; // clear old CDR & TIM bits
config |= CDR << 3 | TIM; // set new CDR & TIM bits
write(MAX44009_CONFIGURATION, config);
@ -187,7 +190,7 @@ void Max44009::setThreshold(const uint8_t reg, const float value)
uint8_t e = 0;
while (m > 255)
{
m >>= 1;
m >>= 1; // bits get lost
e++;
};
m = (m >> 4) & 0x0F;
@ -199,7 +202,7 @@ float Max44009::getThreshold(uint8_t reg)
{
uint8_t data = read(reg);
uint8_t e = (data & 0xF0) >> 4;
uint32_t m = ((data & 0x0F) << 4) + 0x0F;
uint32_t m = ((data & 0x0F) << 4) + 0x08; // 0x08 = correction for lost bits
m <<= e;
float val = m * 0.045;
return val;
@ -219,11 +222,7 @@ uint8_t Max44009::read(uint8_t reg)
_error = MAX44009_ERROR_WIRE_REQUEST;
return _data; // last value
}
#if (ARDUINO < 100)
_data = _wire->receive();
#else
_data = _wire->read();
#endif
return _data;
}