2020-11-27 05:20:37 -05:00
|
|
|
#pragma once
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2016-12-17 14:58:29 -05:00
|
|
|
// FILE: Max44009.h
|
|
|
|
// AUTHOR: Rob dot Tillaart at gmail dot com
|
2022-11-16 10:00:38 -05:00
|
|
|
// VERSION: 0.5.3
|
2016-12-17 14:58:29 -05:00
|
|
|
// PURPOSE: library for MAX44009 lux sensor Arduino
|
2022-11-16 10:00:38 -05:00
|
|
|
// HISTORY: See changelog.md
|
2016-12-17 14:58:29 -05:00
|
|
|
|
2021-06-04 14:31:56 -04:00
|
|
|
|
2022-11-16 10:00:38 -05:00
|
|
|
// breakout MAX44009 / GY-49
|
2020-01-31 07:58:43 -05:00
|
|
|
//
|
2022-11-16 10:00:38 -05:00
|
|
|
// +--------+
|
|
|
|
// VCC |o |
|
|
|
|
// GND |o |
|
|
|
|
// SCL |o o| ADDRESS
|
|
|
|
// SDA |o o| -INT
|
|
|
|
// +--------+
|
2020-01-31 07:58:43 -05:00
|
|
|
//
|
2022-11-16 10:00:38 -05:00
|
|
|
// ADDRESS:
|
|
|
|
// 0 = 0x4A
|
|
|
|
// 1 = 0x4B
|
2020-01-31 07:58:43 -05:00
|
|
|
//
|
2022-11-16 10:00:38 -05:00
|
|
|
// 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
|
2020-01-31 07:58:43 -05:00
|
|
|
//
|
2016-12-17 14:58:29 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-01-31 07:58:43 -05:00
|
|
|
#include "Wire.h"
|
2016-12-17 14:58:29 -05:00
|
|
|
#include "Arduino.h"
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-11-16 10:00:38 -05:00
|
|
|
#define MAX44009_LIB_VERSION (F("0.5.3"))
|
2021-05-28 07:37:15 -04:00
|
|
|
|
2021-12-21 07:31:20 -05:00
|
|
|
#define MAX44009_DEFAULT_ADDRESS 0x4A
|
|
|
|
#define MAX44009_ALT_ADDRESS 0x4B
|
2016-12-17 14:58:29 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2016-12-17 14:58:29 -05:00
|
|
|
// REGISTERS
|
2021-12-21 07:31:20 -05:00
|
|
|
#define MAX44009_INTERRUPT_STATUS 0x00
|
|
|
|
#define MAX44009_INTERRUPT_ENABLE 0x01
|
|
|
|
#define MAX44009_CONFIGURATION 0x02
|
|
|
|
#define MAX44009_LUX_READING_HIGH 0x03
|
|
|
|
#define MAX44009_LUX_READING_LOW 0x04
|
|
|
|
#define MAX44009_THRESHOLD_HIGH 0x05
|
|
|
|
#define MAX44009_THRESHOLD_LOW 0x06
|
|
|
|
#define MAX44009_THRESHOLD_TIMER 0x07
|
2016-12-17 14:58:29 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2016-12-17 14:58:29 -05:00
|
|
|
// CONFIGURATION MASKS
|
2021-12-21 07:31:20 -05:00
|
|
|
#define MAX44009_CFG_CONTINUOUS 0x80
|
|
|
|
#define MAX44009_CFG_MANUAL 0x40
|
|
|
|
#define MAX44009_CFG_CDR 0x08
|
|
|
|
#define MAX44009_CFG_TIMER 0x07
|
2016-12-17 14:58:29 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-01-21 09:26:58 -05:00
|
|
|
// ERROR CODES
|
2021-12-21 07:31:20 -05:00
|
|
|
#define MAX44009_OK 0
|
|
|
|
#define MAX44009_ERROR_WIRE_REQUEST -10
|
|
|
|
#define MAX44009_ERROR_OVERFLOW -20
|
|
|
|
#define MAX44009_ERROR_HIGH_BYTE -30
|
|
|
|
#define MAX44009_ERROR_LOW_BYTE -31
|
2020-01-21 09:26:58 -05:00
|
|
|
|
2016-12-17 14:58:29 -05:00
|
|
|
|
|
|
|
class Max44009
|
|
|
|
{
|
|
|
|
public:
|
2022-11-16 10:00:38 -05:00
|
|
|
// enum class to prevent bool to be implicitly casted to int
|
2021-01-29 06:31:58 -05:00
|
|
|
enum class Boolean { True, False };
|
2019-09-03 06:22:43 -04:00
|
|
|
|
|
|
|
#if defined(ESP8266) || defined(ESP32)
|
2022-11-16 10:00:38 -05:00
|
|
|
// dataPin and clockPin can be used for ESP8266
|
2021-01-29 06:31:58 -05:00
|
|
|
Max44009(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin);
|
2019-09-03 06:22:43 -04:00
|
|
|
#endif
|
2020-01-19 16:53:40 -05:00
|
|
|
|
2022-11-16 10:00:38 -05:00
|
|
|
// constructor for UNO
|
2021-01-29 06:31:58 -05:00
|
|
|
Max44009(const uint8_t address, const Boolean begin = Boolean::True);
|
2022-11-16 10:00:38 -05:00
|
|
|
// default constructor with default address
|
2021-01-29 06:31:58 -05:00
|
|
|
Max44009(const Boolean begin = Boolean::True);
|
2022-11-16 10:00:38 -05:00
|
|
|
// Change I2C interface and address
|
2021-01-29 06:31:58 -05:00
|
|
|
void configure(const uint8_t address, TwoWire *wire, const Boolean begin = Boolean::True);
|
|
|
|
|
|
|
|
bool isConnected();
|
|
|
|
float getLux();
|
|
|
|
int getError();
|
|
|
|
|
2022-11-16 10:00:38 -05:00
|
|
|
// threshold must be between 0 and 188006
|
|
|
|
bool setHighThreshold(const float value); // returns false if value out of range
|
2021-01-29 06:31:58 -05:00
|
|
|
float getHighThreshold(void);
|
2022-11-16 10:00:38 -05:00
|
|
|
bool setLowThreshold(const float value); // returns false if value out of range
|
2021-01-29 06:31:58 -05:00
|
|
|
float getLowThreshold(void);
|
2022-11-16 10:00:38 -05:00
|
|
|
void setThresholdTimer(const uint8_t value); // 2 seems practical minimum
|
2021-01-29 06:31:58 -05:00
|
|
|
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; };
|
|
|
|
|
2022-11-16 10:00:38 -05:00
|
|
|
// check datasheet for detailed behaviour
|
2021-01-29 06:31:58 -05:00
|
|
|
void setConfiguration(uint8_t);
|
|
|
|
uint8_t getConfiguration();
|
|
|
|
void setAutomaticMode();
|
2022-11-16 10:00:38 -05:00
|
|
|
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
|
2021-01-29 06:31:58 -05:00
|
|
|
void setManualMode(uint8_t CDR, uint8_t TIM);
|
2022-11-16 10:00:38 -05:00
|
|
|
int getIntegrationTime() { return 800 >> (getConfiguration() & 0x07); }; // ms
|
2016-12-17 14:58:29 -05:00
|
|
|
|
2022-11-16 10:00:38 -05:00
|
|
|
// TEST the math
|
2021-06-04 14:31:56 -04:00
|
|
|
float convertToLux(uint8_t datahigh, uint8_t datalow);
|
|
|
|
|
|
|
|
|
2022-01-05 10:46:56 -05:00
|
|
|
protected:
|
2021-12-21 07:31:20 -05:00
|
|
|
bool setThreshold(uint8_t reg, float value);
|
|
|
|
float getThreshold(uint8_t reg);
|
2016-12-17 14:58:29 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t read(uint8_t reg);
|
2021-12-21 07:31:20 -05:00
|
|
|
void write(uint8_t reg, uint8_t value);
|
2016-12-17 14:58:29 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t _address;
|
|
|
|
uint8_t _data;
|
|
|
|
int _error;
|
2020-01-19 16:53:40 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
TwoWire* _wire;
|
2016-12-17 14:58:29 -05:00
|
|
|
};
|
2017-07-26 18:24:25 -04:00
|
|
|
|
2021-05-28 07:37:15 -04:00
|
|
|
|
2022-11-16 10:00:38 -05:00
|
|
|
// -- END OF FILE --
|
2021-12-21 07:31:20 -05:00
|
|
|
|