Skip to content

Commit

Permalink
refactor Constructor (#14)
Browse files Browse the repository at this point in the history
- refactor constructor around I2C interface (simpler)
- remove **configure()**
- update readme.md
- update examples
  • Loading branch information
RobTillaart authored Oct 4, 2023
1 parent d230eb0 commit 5ac8684
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 113 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.6.0] - 2023-10-02
- refactor constructor around I2C interface (simpler)
- remove **configure()**
- update readme.md
- update examples

----

## [0.5.3] - 2022-11-16
- add RP2040 in build-CI
- add changelog.md
- minor edit readme.md


## [0.5.2] - 2022-01-05
- minor edits after creating Max44007 library

Expand Down
43 changes: 3 additions & 40 deletions Max44009.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//
// FILE: Max44009.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.5.3
// VERSION: 0.6.0
// DATE: 2010-??-??
// PURPOSE: library for MAX44009 lux sensor Arduino
// URL: https://github.com/RobTillaart/MAX44009
//
// HISTORY: see changelog.md



#include "Max44009.h"
Expand All @@ -16,47 +14,12 @@
#define MAX44009_MAX_LUX (188006.0)


#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;

if ((dataPin < 255) && (clockPin < 255))
{
_wire->begin(dataPin, clockPin);
} else {
_wire->begin();
}
}
#endif


Max44009::Max44009(const uint8_t address, const Boolean begin)
{
Max44009::configure(address, &Wire, begin);
}


Max44009::Max44009(const Boolean begin)
{
Max44009::configure(MAX44009_DEFAULT_ADDRESS, &Wire, begin);
}


void Max44009::configure(const uint8_t address, TwoWire *wire, const Boolean begin)
Max44009::Max44009(const uint8_t address, TwoWire *wire)
{
_address = address;
_data = 0;
_error = MAX44009_OK;
_wire = wire;

if (begin == Boolean::True)
{
_wire->begin();
}
}


Expand Down
25 changes: 6 additions & 19 deletions Max44009.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

// FILE: Max44009.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.5.3
// AUTHOR: Rob Tillaart
// VERSION: 0.6.0
// DATE: 2010-??-??
// PURPOSE: library for MAX44009 lux sensor Arduino
// HISTORY: See changelog.md



// breakout MAX44009 / GY-49
Expand Down Expand Up @@ -32,7 +33,7 @@
#include "Arduino.h"


#define MAX44009_LIB_VERSION (F("0.5.3"))
#define MAX44009_LIB_VERSION (F("0.6.0"))

#define MAX44009_DEFAULT_ADDRESS 0x4A
#define MAX44009_ALT_ADDRESS 0x4B
Expand Down Expand Up @@ -67,20 +68,7 @@
class Max44009
{
public:
// 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);
#endif

// constructor for UNO
Max44009(const uint8_t address, const Boolean begin = Boolean::True);
// default constructor 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);
Max44009(const uint8_t address = MAX44009_DEFAULT_ADDRESS, TwoWire *wire = &Wire);

bool isConnected();
float getLux();
Expand All @@ -105,7 +93,6 @@ class Max44009
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)
Expand Down
90 changes: 59 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

[![Arduino CI](https://github.com/RobTillaart/MAX44009/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/Max44009/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/Max44009/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/Max44009/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/Max44009/actions/workflows/jsoncheck.yml)
[![Arduino-lint](https://github.com/RobTillaart/MAX44009/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/MAX44009/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/MAX44009/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/MAX44009/actions/workflows/jsoncheck.yml)
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/MAX44009.svg)](https://github.com/RobTillaart/MAX44009/issues)

[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/MAX44009/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/MAX44009.svg?maxAge=3600)](https://github.com/RobTillaart/MAX44009/releases)
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/MAX44009.svg)](https://registry.platformio.org/libraries/robtillaart/MAX44009)


# MAX44009 I2C LUX sensor
Expand All @@ -15,10 +18,20 @@ Library for MAX44009 / GY-49 I2C lux sensor.

a.k.a. GY-49

The MAX44009 ambient light sensor is an I2C sensor, that has a 22 bit
The MAX44009 ambient light sensor is an I2C sensor, that has a 22 bit
dynamic range from 0.045 lux to 188,000 lux.

Relates to https://github.com/RobTillaart/Max44007

#### MAX44007

The MAX44007 is an almost identical sensor that uses a step size of 0.025.
This implies that this library is not useable 1 to 1 for the MAX44007, however some parts will work/


#### Related

- https://github.com/RobTillaart/Max44007 (range 0.025 lux to 104,448 lux)
- https://github.com/RobTillaart/Max44009 (range 0.045 lux to 188,000 lux)


## Schema breakout max44009 / GY-49
Expand Down Expand Up @@ -49,21 +62,22 @@ Relates to https://github.com/RobTillaart/Max44007

## Interface

- **enum class Boolean { True, False }** enum class to prevent bool to be implicitly casted to int.
```cpp
#include "Max44009.h"
```

### Constructor

### Constructor

- **Max44009(uint8_t address, uint8_t dataPin, uint8_t clockPin)** Constructor with dataPin (sda) and clockPin (scl) for ESP32 and ESP8266.
- **Max44009(uint8_t address, begin = Boolean::True)** Constructor for other boards e.g. UNO.
- **Max44009(begin = Boolean::True)** Constructor with default I2C address 0x4A == 74.
- **void configure(uint8_t address, TwoWire \*wire, begin = Boolean::True)** Change I2C interface and address.
- **Max44009(const uint8_t address = MAX44009_DEFAULT_ADDRESS, TwoWire \*wire = &Wire)** Constructor.
Optional address and optional I2C interface.\
- **bool isConnected()** returns true if the device address configured is available on I2C bus.

NOTE: The user must call **Wire.begin()** or **Wire.begin(SDA, SCL)** in **setup()**.

### Basic

- **float getLux()** read the sensor and return the value in LUX. If the value is negative, an error has occurred.
### Basic

- **float getLux()** read the sensor and return the value in LUX. If the value is negative, an error has occurred.
- **int getError()** returns last error.

```cpp
Expand All @@ -80,15 +94,15 @@ Relates to https://github.com/RobTillaart/Max44007

check datasheet for details

- **bool setHighThreshold(float value)** sets the upper threshold for the interrupt
generation (INT pulls LOW). Works only if INTE bit is set by **enableInterrupt()**.
- **bool setHighThreshold(float value)** 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()** returns the value set.
- **bool setLowThreshold(float value)** sets the lower threshold for the interrupt
generation (INT pulls LOW). Works only if INTE bit is set by **enableInterrupt()**.
- **bool setLowThreshold(float value)** 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()** returns the value set.
- **void setThresholdTimer(uint8_t value)** Time the threshold needs to be exceeded,
- **void setThresholdTimer(uint8_t value)** 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.

Expand All @@ -107,7 +121,7 @@ check datasheet for details

check datasheet for details

- **void setConfiguration(uint8_t)** writes directly to configuration register.
- **void setConfiguration(uint8_t)** writes directly to configuration register.
**warning** Use with care.
- **uint8_t getConfiguration()** reads the current configuration register.

Expand All @@ -120,12 +134,12 @@ CCR = Current Divisor Ratio.

TIM = Integration time.

- **void setAutomaticMode()** in automatic mode the MAX44009 determines the CDR and TIM
- **void setAutomaticMode()** in automatic mode the MAX44009 determines the CDR and TIM
parameters.
- **void setContinuousMode()** continuous mode uses more power than a "single" conversion.
- **void setContinuousMode()** continuous mode uses more power than a "single" conversion.
Advantage is that the latest data is always available fast.
- **void clrContinuousMode()** uses less power so better for LOW power configurations.
- **void setManualMode(uint8_t CDR, uint8_t TIM)** Set the Current Divisor Ratio and the
- **void clrContinuousMode()** uses less power so better for LOW power configurations.
- **void setManualMode(uint8_t CDR, uint8_t TIM)** Set the Current Divisor Ratio and the
integration time manually. Effectively disable automatic mode.
- **int getIntegrationTime()** returns the set integration time in milliseconds

Expand All @@ -147,11 +161,11 @@ integration time manually. Effectively disable automatic mode.

### Test functions

Function for the conversion math, not meant to be used directly,
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.
- **float convertToLux(uint8_t dataHigh, uint8_t dataLow)** convert intern register
format to a LUX value.


## Examples
Expand All @@ -167,22 +181,36 @@ format to a LUX value.
## Notes

Please be aware this is a **3.3 Volt device** so it should not be connected
to an Arduino UNO or other 5 Volt device directly.
to an Arduino UNO or other 5 Volt device directly.
Use a level convertor to solve this.

Do not forget to connect the address pin as you cannot read the sensor
in a reliable way. As the line will float it will sometimes have the
right address and sometimes not. (been there ;)

Pull ups on I2C bus are recommended.


## Future

#### Must

- improve documentation
- merge MAX44007 library in the future. (shared base class?)

#### Should

#### MAX44007
#### Could

The MAX44007 is an almost identical sensor that uses a step size of 0.025.
This implies that this library is not useable 1 to 1 for the MAX44007, however some parts will work/
- merge MAX44007 / MAX44009 library in the future. (shared base class?)

#### Wont


## Support

If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.

Thank you,

3 changes: 2 additions & 1 deletion examples/max44009_interrupt/max44009_interrupt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ uint32_t lastDisplay = 0;
void setup()
{
Serial.begin(115200);
Serial.print("\nStart max44009_interrupt : ");
Serial.println(__FILE__);
Serial.print("MAX44009_LIB_VERSION: ");
Serial.println(MAX44009_LIB_VERSION);

Wire.begin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ uint32_t lastDisplay = 0;
void setup()
{
Serial.begin(115200);
Serial.print("\nStart max44009_setAutomaticMode : ");
Serial.println(__FILE__);
Serial.print("MAX44009_LIB_VERSION: ");
Serial.println(MAX44009_LIB_VERSION);

Wire.begin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ int count = 0;
void setup()
{
Serial.begin(115200);
Serial.print("\nStart max44009_setContinuousMode : ");
Serial.println(__FILE__);
Serial.print("MAX44009_LIB_VERSION: ");
Serial.println(MAX44009_LIB_VERSION);

Wire.begin();
Expand Down
3 changes: 2 additions & 1 deletion examples/max44009_setManualMode/max44009_setManualMode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ uint8_t TIM = 0;
void setup()
{
Serial.begin(115200);
Serial.print("\nStart max44009_setManualMode : ");
Serial.println(__FILE__);
Serial.print("MAX44009_LIB_VERSION: ");
Serial.println(MAX44009_LIB_VERSION);

Wire.begin();
Expand Down
3 changes: 2 additions & 1 deletion examples/max44009_test01/max44009_test01.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ uint32_t lastDisplay = 0;
void setup()
{
Serial.begin(115200);
Serial.print("Start max44009_test01 : ");
Serial.println(__FILE__);
Serial.print("MAX44009_LIB_VERSION: ");
Serial.println(MAX44009_LIB_VERSION);

Wire.begin();
Expand Down
15 changes: 6 additions & 9 deletions examples/max44009_test02/max44009_test02.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,23 @@
#include "Max44009.h"

// Don't begin I2C interface (Wire). Will be called in setup()
Max44009 myLuxA(Max44009::Boolean::False);
Max44009 myLuxB(Max44009::Boolean::False);
Max44009 myLuxC(Max44009::Boolean::False);
Max44009 myLuxD(Max44009::Boolean::False);
Max44009 myLuxA(MAX44009_DEFAULT_ADDRESS, &Wire);
Max44009 myLuxB(MAX44009_ALT_ADDRESS, &Wire);
Max44009 myLuxC(MAX44009_DEFAULT_ADDRESS, &Wire1);
Max44009 myLuxD(MAX44009_ALT_ADDRESS, &Wire1);

uint32_t lastDisplay = 0;


void setup()
{
Serial.begin(115200);
Serial.print("Start max44009_test02 : ");
Serial.println(__FILE__);
Serial.print("MAX44009_LIB_VERSION: ");
Serial.println(MAX44009_LIB_VERSION);

Wire.begin(19, 18);
Wire1.begin(22, 23);
myLuxA.configure(MAX44009_DEFAULT_ADDRESS, &Wire);
myLuxB.configure(MAX44009_ALT_ADDRESS, &Wire);
myLuxC.configure(MAX44009_DEFAULT_ADDRESS, &Wire1);
myLuxD.configure(MAX44009_ALT_ADDRESS, &Wire1);
}


Expand Down
Loading

0 comments on commit 5ac8684

Please sign in to comment.