Skip to content

Commit

Permalink
Merge pull request #7 from Abestanis/feature/read_timeout
Browse files Browse the repository at this point in the history
Add an optional read timout to the TSL2561_CalculateLux class
  • Loading branch information
baorepo authored Oct 15, 2024
2 parents 6f62dff + 7b50b85 commit 6ccbc9f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Digital_Light_TSL2561.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,25 @@
#include <Digital_Light_TSL2561.h>
#include <Arduino.h>
#include <Wire.h>


TSL2561_CalculateLux::TSL2561_CalculateLux(uint32_t read_timeout) : read_timeout(read_timeout) {
}

uint8_t TSL2561_CalculateLux::readRegister(int deviceAddress, int address) {

uint8_t value;
Wire.beginTransmission(deviceAddress);
Wire.write(address); // register to read
Wire.endTransmission();
Wire.requestFrom(deviceAddress, 1); // read a byte
while (!Wire.available());
uint32_t readStart = micros();
while (!Wire.available()) {
if (read_timeout != TSL2561_NO_READ_TIMEOUT && micros() - readStart >= read_timeout) {
hadTimeout = true;
break;
}
}
value = Wire.read();
//delay(100);
return value;
Expand Down Expand Up @@ -200,6 +211,13 @@ unsigned long TSL2561_CalculateLux::calculateLux(unsigned int iGain, unsigned in
lux = temp >> LUX_SCALE;
return (lux);
}

bool TSL2561_CalculateLux::checkHadTimeout(){
bool timeout = hadTimeout;
hadTimeout = false;
return timeout;
}

TSL2561_CalculateLux TSL2561;


29 changes: 29 additions & 0 deletions Digital_Light_TSL2561.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,20 @@
#define K8C 0x029a // 1.3 * 2^RATIO_SCALE
#define B8C 0x0000 // 0.000 * 2^LUX_SCALE
#define M8C 0x0000 // 0.000 * 2^LUX_SCALE

/** No timeout while waiting for a read response from the digital light sensor. */
#define TSL2561_NO_READ_TIMEOUT 0

class TSL2561_CalculateLux {
public:

/**
* Create a new interface to a TSL2561 light sensor.
*
* @param read_timeout The timeout to wait for read responses in microseconds.
*/
explicit TSL2561_CalculateLux(uint32_t read_timeout = TSL2561_NO_READ_TIMEOUT);

signed long readVisibleLux();
uint16_t readIRLuminosity();
uint16_t readFSpecLuminosity();
Expand All @@ -110,6 +122,14 @@ class TSL2561_CalculateLux {
void init(void);
uint8_t readRegister(int deviceAddress, int address);
void writeRegister(int deviceAddress, int address, uint8_t val);

/**
* Check whether there was a read timeout since the last call to this function
* and clear the flag.
*
* @return whether there was a read timeout since the last check.
*/
bool checkHadTimeout();
private:
uint8_t CH0_LOW, CH0_HIGH, CH1_LOW, CH1_HIGH;
uint16_t ch0, ch1;
Expand All @@ -122,6 +142,15 @@ class TSL2561_CalculateLux {
unsigned long temp;
unsigned long lux;

/**
* The configured read timeout in microseconds.
*/
uint32_t read_timeout;

/**
* Whether a read timeout occurred since the last time checkHadTimeout was called.
*/
bool hadTimeout = false;
};
extern TSL2561_CalculateLux TSL2561;
#endif
Expand Down

0 comments on commit 6ccbc9f

Please sign in to comment.