Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dark frame calibration and add calculateLux method as per Si114x AN523 #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions Adafruit_SI1145.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

Adafruit_SI1145::Adafruit_SI1145() {
_addr = SI1145_ADDR;
_vis_dark = 256; // as per Si114x manual - previously ADC_OFFSET but now fixed at 256
_ir_dark = 256;
}


Expand Down Expand Up @@ -108,25 +110,61 @@ void Adafruit_SI1145::reset() {


//////////////////////////////////////////////////////
// returns the visible gain
Adafruit_SI1145::Gain Adafruit_SI1145::readVisibleGain() {
return (Adafruit_SI1145::Gain) readParam(SI1145_PARAM_ALSVISADCGAIN);
}

// adjust the visible gain
void Adafruit_SI1145::setVisibleGain(Adafruit_SI1145::Gain gain) {
uint8_t g = (uint8_t) gain;
writeParam(SI1145_PARAM_ALSVISADCGAIN, g);
}

// returns the IR gain
Adafruit_SI1145::Gain Adafruit_SI1145::readIRGain() {
return (Adafruit_SI1145::Gain) readParam(SI1145_PARAM_ALSIRADCGAIN);
}

// adjust the IR gain
void Adafruit_SI1145::setIRGain(Adafruit_SI1145::Gain gain) {
uint8_t g = (uint8_t) gain;
writeParam(SI1145_PARAM_ALSIRADCGAIN, g);
}

// returns the UV index * 100 (divide by 100 to get the index)
uint16_t Adafruit_SI1145::readUV(void) {
return read16(0x2C);
}

// returns a calculated lux value as per SI144x AN523.6.
float Adafruit_SI1145::calculateLux(uint16_t vis, uint16_t ir) {
uint8_t gain = readVisibleGain();
float lux = ((5.41f * vis) + (-0.08f * ir)) / (1 + 2 * gain);
return lux;
}

// returns visible+IR light levels
uint16_t Adafruit_SI1145::readVisible(void) {
return read16(0x22);
uint16_t vis = read16(SI1145_REG_ALSVISDATA0);
if (vis < _vis_dark)
_vis_dark = vis;
vis -= _vis_dark;
return vis;
}

// returns IR light levels
uint16_t Adafruit_SI1145::readIR(void) {
return read16(0x24);
uint16_t ir = read16(SI1145_REG_ALSIRDATA0);
if (ir < _ir_dark)
_ir_dark = ir;
ir -= _ir_dark;
return ir;
}

// returns "Proximity" - assumes an IR LED is attached to LED
uint16_t Adafruit_SI1145::readProx(void) {
return read16(0x26);
return read16(SI1145_REG_PS1DATA0);
}

/*********************************************************************/
Expand All @@ -149,12 +187,12 @@ uint8_t Adafruit_SI1145::readParam(uint8_t p) {

uint8_t Adafruit_SI1145::read8(uint8_t reg) {
uint16_t val;
Wire.beginTransmission(_addr);
Wire.write((uint8_t)reg);
Wire.endTransmission();
Wire.beginTransmission(_addr);
Wire.write((uint8_t)reg);
Wire.endTransmission();

Wire.requestFrom((uint8_t)_addr, (uint8_t)1);
return Wire.read();
Wire.requestFrom((uint8_t)_addr, (uint8_t)1);
return Wire.read();
}

uint16_t Adafruit_SI1145::read16(uint8_t a) {
Expand Down
20 changes: 20 additions & 0 deletions Adafruit_SI1145.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,28 @@

class Adafruit_SI1145 {
public:
enum Gain {
ADC_0 = 0,
ADC_1,
ADC_2,
ADC_3,
ADC_4,
ADC_5,
ADC_6,
ADC_7
};
Adafruit_SI1145(void);
boolean begin();
void reset();

Gain readVisibleGain();
void setVisibleGain(Gain gain);

Gain readIRGain();
void setIRGain(Gain gain);

float calculateLux(uint16_t vis, uint16_t ir);

uint16_t readUV();
uint16_t readIR();
uint16_t readVisible();
Expand All @@ -167,5 +185,7 @@ class Adafruit_SI1145 {
uint8_t writeParam(uint8_t p, uint8_t v);

uint8_t _addr;
uint16_t _vis_dark;
uint16_t _ir_dark;
};
#endif