Skip to content

Commit

Permalink
refactor Constructor (#5)
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 3d7900d commit dd68fcf
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 121 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


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

----

## [0.1.2] - 2022-11-16
- add RP2040 in build-CI
- add changelog.md


## [0.1.1] - 2022-01-05
- minor refactor

Expand Down
48 changes: 5 additions & 43 deletions Max44007.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//
// FILE: Max44007.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// VERSION: 0.2.0
// DATE: 2022-01-04
// PURPOSE: library for Max44007 lux sensor Arduino
// URL: https://github.com/RobTillaart/Max44007
//
// HISTORY: see changelog.md

// URL: https://github.com/RobTillaart/MAX44007


#include "Max44007.h"
Expand All @@ -17,48 +15,12 @@
#define MAX44007_MAX_LUX (104448.0)



#if defined(ESP8266) || defined(ESP32)
Max44007::Max44007(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin)
{
_address = address;
_data = 0;
_error = MAX44007_OK;
_wire = &Wire;

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


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


Max44007::Max44007(const Boolean begin)
{
Max44007::configure(MAX44007_DEFAULT_ADDRESS, &Wire, begin);
}


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

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


Expand Down Expand Up @@ -257,4 +219,4 @@ void Max44007::write(uint8_t reg, uint8_t value)
}


// -- END OF FILE --
// -- END OF FILE --
24 changes: 6 additions & 18 deletions Max44007.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

// FILE: Max44007.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// VERSION: 0.2.0
// DATE: 2022-01-04
// PURPOSE: library for Max44007 lux sensor Arduino
// HISTORY: See Max44007.cpp
// URL: https://github.com/RobTillaart/MAX44007


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


#define MAX44007_LIB_VERSION (F("0.1.2"))
#define MAX44007_LIB_VERSION (F("0.2.0"))

#define MAX44007_DEFAULT_ADDRESS 0x5A
#define MAX44007_ALT_ADDRESS 0x5B
Expand Down Expand Up @@ -68,20 +69,7 @@
class Max44007
{
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
Max44007(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin);
#endif

// constructor for UNO
Max44007(const uint8_t address, const Boolean begin = Boolean::True);
// default constructor with default address
Max44007(const Boolean begin = Boolean::True);
// Change I2C interface and address
void configure(const uint8_t address, TwoWire *wire, const Boolean begin = Boolean::True);
Max44007(const uint8_t address = MAX44007_DEFAULT_ADDRESS, TwoWire *wire = &Wire);

bool isConnected();
float getLux();
Expand All @@ -100,7 +88,7 @@ class Max44007
bool interruptEnabled() { return read(MAX44007_INTERRUPT_ENABLE) & 0x01; };
uint8_t getInterruptStatus() { return read(MAX44007_INTERRUPT_STATUS) & 0x01; };

// check datasheet for detailed behavior
// check datasheet for detailed behaviour
void setConfiguration(uint8_t);
uint8_t getConfiguration();
void setAutomaticMode();
Expand Down
90 changes: 61 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
[![Arduino CI](https://github.com/RobTillaart/Max44007/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/Max44007/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/Max44007/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/Max44007/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/Max44007/actions/workflows/jsoncheck.yml)
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/Max44007.svg)](https://github.com/RobTillaart/Max44007/issues)

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


# Max44007 I2C LUX sensor
Expand All @@ -13,12 +16,20 @@ Library for Max44007 I2C lux sensor.

## Description

The Max44007 ambient light sensor is an I2C sensor, that has a 22 bit
The Max44007 ambient light sensor is an I2C sensor, that has a 22 bit
dynamic range from 0.025 lux to 104,448 lux.

This library is experimental and created by changing some key values in the
very similar MAX44009 library. As there was no hardware available the library
is not tested yet. If you do, please share the results (on GitHub)
very similar MAX44009 library. As there was no hardware available the library
is not tested yet. If you do, please share the results (open an issue on GitHub).

Note: version 0.2.0 simplified the constructor interface and removed **configure()**.


#### 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 Max44007
Expand Down Expand Up @@ -49,21 +60,22 @@ is not tested yet. If you do, please share the results (on GitHub)

## Interface

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

### Constructor

### Constructor

- **Max44007(uint8_t address, uint8_t dataPin, uint8_t clockPin)** Constructor with dataPin (sda) and clockPin (scl) for ESP32 and ESP8266.
- **Max44007(uint8_t address, begin = Boolean::True)** Constructor for other boards e.g. UNO.
- **Max44007(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.
- **Max44007(const uint8_t address = MAX44007_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
### Basic

- **float getLux()** read the sensor and return the value in LUX. If the value is negative, an error has occurred.
- **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 +92,15 @@ is not tested yet. If you do, please share the results (on GitHub)

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,25 +119,25 @@ 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.


### Configure sample mode

check datasheet for details
Check datasheet for details

CCR = Current Divisor Ratio.

TIM = Integration time.

- **void setAutomaticMode()** in automatic mode the Max44007 determines the CDR and TIM
- **void setAutomaticMode()** in automatic mode the Max44007 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 +159,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,16 +179,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 or other 5 Volt device directly. Use a level convertor to
solve this.
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

- follow MAX44009
- Merge MAX44007 into MAX44009 class hierarchy.

#### Should

#### Could

- 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,

4 changes: 2 additions & 2 deletions examples/max44007_interrupt/max44007_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 Max44007_interrupt : ");
Serial.println(__FILE__);
Serial.print("MAX44007_LIB_VERSION: ");
Serial.println(MAX44007_LIB_VERSION);

Wire.begin();
Expand Down Expand Up @@ -62,4 +63,3 @@ void loop()


// -- END OF FILE --

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 Max44007_setAutomaticMode : ");
Serial.println(__FILE__);
Serial.print("MAX44007_LIB_VERSION: ");
Serial.println(MAX44007_LIB_VERSION);

Wire.begin();
Expand Down Expand Up @@ -64,4 +65,3 @@ void loop()


// -- END OF FILE --

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 Max44007_setContinuousMode : ");
Serial.println(__FILE__);
Serial.print("MAX44007_LIB_VERSION: ");
Serial.println(MAX44007_LIB_VERSION);

Wire.begin();
Expand Down Expand Up @@ -69,4 +70,3 @@ void loop()


// -- END OF FILE --

6 changes: 3 additions & 3 deletions examples/max44007_setManualMode/max44007_setManualMode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "Wire.h"
#include "Max44007.h"

Max44007 myLux(0x5A);
Max44007 myLux;

uint32_t lastDisplay = 0;
uint32_t lastChangeCDRTIM = 0;
Expand All @@ -19,7 +19,8 @@ uint8_t TIM = 0;
void setup()
{
Serial.begin(115200);
Serial.print("\nStart Max44007_setManualMode : ");
Serial.println(__FILE__);
Serial.print("MAX44007_LIB_VERSION: ");
Serial.println(MAX44007_LIB_VERSION);

Wire.begin();
Expand Down Expand Up @@ -70,4 +71,3 @@ void loop()


// -- END OF FILE --

Loading

0 comments on commit dd68fcf

Please sign in to comment.