IP Configuration
This commit is contained in:
98
lib/Max44009/Max44009.h
Normal file
98
lib/Max44009/Max44009.h
Normal file
@@ -0,0 +1,98 @@
|
||||
#ifndef MAX44009_H
|
||||
#define MAX44009_H
|
||||
//
|
||||
// FILE: Max44009.h
|
||||
// AUTHOR: Rob dot Tillaart at gmail dot com
|
||||
// VERSION: 0.1.9
|
||||
// PURPOSE: library for MAX44009 lux sensor Arduino
|
||||
// HISTORY: See Max440099.cpp
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#include "Wire.h"
|
||||
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#define MAX44009_LIB_VERSION "0.1.9"
|
||||
|
||||
// I2C ADDRESSES
|
||||
#define MAX44009_A0_LOW 0x4A
|
||||
#define MAX44009_A0_HIGH 0x4B
|
||||
|
||||
// REGISTERS
|
||||
#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
|
||||
|
||||
// CONFIGURATION MASKS
|
||||
#define MAX44009_CFG_CONTINUOUS 0x80
|
||||
#define MAX44009_CFG_MANUAL 0x40
|
||||
#define MAX44009_CFG_CDR 0x08
|
||||
#define MAX44009_CFG_TIMER 0x07
|
||||
|
||||
|
||||
class Max44009
|
||||
{
|
||||
public:
|
||||
// dataPin and clockPin are only used by ESP8266
|
||||
// for UNO ignore these (and its warning)
|
||||
Max44009(const uint8_t address, const uint8_t dataPin = 5, const uint8_t clockPin = 4);
|
||||
Max44009(const uint8_t address, bool continuousMode);
|
||||
|
||||
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 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
|
||||
// 101 25ms
|
||||
// 110 12.5ms
|
||||
// 111 6.25ms
|
||||
void setManualMode(uint8_t CDR, uint8_t TIM);
|
||||
|
||||
private:
|
||||
void setThreshold(uint8_t, float);
|
||||
float getThreshold(uint8_t);
|
||||
|
||||
uint8_t read(uint8_t reg);
|
||||
void write(uint8_t, uint8_t);
|
||||
|
||||
uint8_t _address;
|
||||
uint8_t _data;
|
||||
int _error;
|
||||
};
|
||||
#endif
|
||||
|
||||
// END OF FILE
|
||||
47
lib/Max44009/examples/max44009_test01/max44009_test01.ino
Normal file
47
lib/Max44009/examples/max44009_test01/max44009_test01.ino
Normal file
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// FILE: max44009_test01.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.1
|
||||
// PURPOSE: demo of max44009 library
|
||||
// DATE: 2015-08-06
|
||||
// URL: ?
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#include "Wire.h"
|
||||
#include "Max44009.h"
|
||||
|
||||
Max44009 myLux(0xCB); // default addr
|
||||
|
||||
uint32_t lastDisplay = 0;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.print("Start max44009_test01 : ");
|
||||
Serial.println(MAX44009_LIB_VERSION);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (millis() - lastDisplay >= 1000)
|
||||
{
|
||||
lastDisplay += 1000;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
36
lib/Max44009/keywords.txt
Normal file
36
lib/Max44009/keywords.txt
Normal file
@@ -0,0 +1,36 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map For Max44009
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
Max44009 KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
getLux KEYWORD2
|
||||
getError KEYWORD2
|
||||
setHighThreshold KEYWORD2
|
||||
getHighThreshold KEYWORD2
|
||||
setLowThreshold KEYWORD2
|
||||
getLowThreshold KEYWORD2
|
||||
setThresholdTimer KEYWORD2
|
||||
getThresholdTimer KEYWORD2
|
||||
setConfiguration KEYWORD2
|
||||
getConfiguration KEYWORD2
|
||||
setAutomaticMode KEYWORD2
|
||||
setContinuousMode KEYWORD2
|
||||
setManualMode KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Instances (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
24
lib/Max44009/library.json
Normal file
24
lib/Max44009/library.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "Max44009",
|
||||
"keywords": "MAX44009,Lux,light",
|
||||
"description": "library for MAX44009 Lux sensor Arduino.",
|
||||
"authors":
|
||||
[
|
||||
{
|
||||
"name": "Rob Tillaart",
|
||||
"email": "Rob.Tillaart@gmail.com",
|
||||
"maintainer": true
|
||||
}
|
||||
],
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/Arduino.git"
|
||||
},
|
||||
"version":"0.1.9",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
"export": {
|
||||
"include": "libraries/Max44009"
|
||||
}
|
||||
}
|
||||
9
lib/Max44009/library.properties
Normal file
9
lib/Max44009/library.properties
Normal file
@@ -0,0 +1,9 @@
|
||||
name=Max44009
|
||||
version=0.1.9
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Library for MAX44009 lux sensor Arduino.
|
||||
paragraph=
|
||||
category=Sensors
|
||||
url=https://github.com/RobTillaart/Arduino/tree/master/libraries/
|
||||
architectures=*
|
||||
201
lib/Max44009/max44009.cpp
Normal file
201
lib/Max44009/max44009.cpp
Normal file
@@ -0,0 +1,201 @@
|
||||
//
|
||||
// FILE: Max44009.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.9
|
||||
// PURPOSE: library for MAX44009 lux sensor Arduino
|
||||
// URL: https://github.com/RobTillaart/Arduino/tree/master/libraries
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
// 0.1.9 - 2018-07-01 issue #108 Fix shift math
|
||||
// (thanks Roland vandecook)
|
||||
// 0.1.8 - 2018-05-13 issue #105 Fix read register
|
||||
// (thanks morxGrillmeister)
|
||||
// 0.1.7 - 2018-04-02 issue #98 extend constructor for ESP8266
|
||||
// 0.1.6 - 2017-07-26 revert double to float
|
||||
// 0.1.5 - updated history
|
||||
// 0.1.4 - added setAutomaticMode() to max44009.h (thanks debsahu)
|
||||
// 0.1.03 - added configuration
|
||||
// 0.1.02 - added threshold code
|
||||
// 0.1.01 - added interrupt code
|
||||
// 0.1.00 - initial version
|
||||
|
||||
#include "Max44009.h"
|
||||
|
||||
|
||||
Max44009::Max44009(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin)
|
||||
{
|
||||
_address = address;
|
||||
#ifdef ESP8266
|
||||
Wire.begin(dataPin, clockPin);
|
||||
#else // other platforms
|
||||
Wire.begin();
|
||||
#endif
|
||||
// TWBR = 12; // Wire.setClock(400000);
|
||||
_data = 0;
|
||||
_error = 0;
|
||||
}
|
||||
|
||||
Max44009::Max44009(const uint8_t address, bool continuousMode)
|
||||
{
|
||||
_address = address;
|
||||
Wire.begin();
|
||||
// TWBR = 12; // Wire.setClock(400000);
|
||||
_data = 0;
|
||||
_error = 0;
|
||||
if (continuousMode)
|
||||
{
|
||||
setContinuousMode();
|
||||
}
|
||||
else
|
||||
{
|
||||
setAutomaticMode();
|
||||
}
|
||||
}
|
||||
|
||||
float Max44009::getLux(void)
|
||||
{
|
||||
uint8_t dhi = read(MAX44009_LUX_READING_HIGH);
|
||||
uint8_t dlo = read(MAX44009_LUX_READING_LOW);
|
||||
uint8_t e = dhi >> 4;
|
||||
uint32_t m = ((dhi & 0x0F) << 4) + (dlo & 0x0F);
|
||||
m <<= e;
|
||||
float val = m * 0.045;
|
||||
return val;
|
||||
}
|
||||
|
||||
int Max44009::getError()
|
||||
{
|
||||
int e = _error;
|
||||
_error = 0;
|
||||
return e;
|
||||
}
|
||||
|
||||
void Max44009::setHighThreshold(const float value)
|
||||
{
|
||||
setThreshold(MAX44009_THRESHOLD_HIGH, value);
|
||||
}
|
||||
|
||||
float Max44009::getHighThreshold(void)
|
||||
{
|
||||
return getThreshold(MAX44009_THRESHOLD_HIGH);
|
||||
}
|
||||
|
||||
void Max44009::setLowThreshold(const float value)
|
||||
{
|
||||
setThreshold(MAX44009_THRESHOLD_LOW, value);
|
||||
}
|
||||
|
||||
float Max44009::getLowThreshold(void)
|
||||
{
|
||||
return getThreshold(MAX44009_THRESHOLD_LOW);
|
||||
}
|
||||
|
||||
void Max44009::setThresholdTimer(const uint8_t value)
|
||||
{
|
||||
write(MAX44009_THRESHOLD_TIMER, value);
|
||||
}
|
||||
|
||||
uint8_t Max44009::getThresholdTimer()
|
||||
{
|
||||
return read(MAX44009_THRESHOLD_TIMER);
|
||||
}
|
||||
|
||||
void Max44009::setConfiguration(const uint8_t value)
|
||||
{
|
||||
write(MAX44009_CONFIGURATION, value);
|
||||
}
|
||||
|
||||
uint8_t Max44009::getConfiguration()
|
||||
{
|
||||
return read(MAX44009_CONFIGURATION);
|
||||
}
|
||||
|
||||
void Max44009::setAutomaticMode()
|
||||
{
|
||||
uint8_t config = read(MAX44009_CONFIGURATION);
|
||||
config &= ~MAX44009_CFG_CONTINUOUS; // off
|
||||
config &= ~MAX44009_CFG_MANUAL; // off
|
||||
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);
|
||||
}
|
||||
|
||||
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 CDR & TIM bits
|
||||
config |= CDR << 3 | TIM;
|
||||
write(MAX44009_CONFIGURATION, config);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
//
|
||||
// PRIVATE
|
||||
//
|
||||
void Max44009::setThreshold(const uint8_t reg, const float value)
|
||||
{
|
||||
// TODO CHECK RANGE
|
||||
uint32_t m = round(value / 0.045); // mulitply * 22.22222222 is faster.
|
||||
uint8_t e = 0;
|
||||
while (m > 255)
|
||||
{
|
||||
m >>= 1;
|
||||
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 Max44009::read(uint8_t reg)
|
||||
{
|
||||
Wire.beginTransmission(_address);
|
||||
Wire.write(reg);
|
||||
_error = Wire.endTransmission();
|
||||
if (_error != 0)
|
||||
{
|
||||
return _data; // last value
|
||||
}
|
||||
if (Wire.requestFrom(_address, (uint8_t) 1) != 1)
|
||||
{
|
||||
_error = 10;
|
||||
return _data; // last value
|
||||
}
|
||||
#if (ARDUINO < 100)
|
||||
_data = Wire.receive();
|
||||
#else
|
||||
_data = Wire.read();
|
||||
#endif
|
||||
return _data;
|
||||
}
|
||||
|
||||
void Max44009::write(uint8_t reg, uint8_t value)
|
||||
{
|
||||
Wire.beginTransmission(_address);
|
||||
Wire.write(reg);
|
||||
Wire.write(value);
|
||||
_error = Wire.endTransmission();
|
||||
}
|
||||
|
||||
// --- END OF FILE ---
|
||||
Reference in New Issue
Block a user