Skip to content

Commit

Permalink
Fixed LTR553 Interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Oct 17, 2023
1 parent d761ad1 commit d0f26c3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
38 changes: 29 additions & 9 deletions examples/LTR553ALS_Sensor/LTR553ALS_Sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void setup()

// Set the high and low thresholds of the proximity sensor.
// If the value exceeds or falls below the set value, an interrupt will be triggered.
// als.setProximityThreshold(10, 30);
als.setProximityThreshold(10, 30);

// Controls the Light Sensor N number of times the measurement data is outside the range
// defined by the upper and lower threshold limits before asserting the interrupt.
Expand Down Expand Up @@ -147,26 +147,46 @@ void setup()
// Number of pulses
als.setPsLedPulses(1);

// Enable proximity sensor saturation indication
als.enablePsIndicator();

// Enable ambient light sensor
als.enableLightSensor();

// Enable proximity sensor
als.enableProximity();

// Enable proximity sensor saturation indication
als.enablePsIndicator();
}

bool canRead()
{
#if SENSOR_IRQ != -1
return digitalRead(SENSOR_IRQ) == LOW;
#else
static uint32_t lastReadMillis;
if (millis() > lastReadMillis) {
lastReadMillis = millis() + 500;
return true;
}
return false;
#endif
}

void loop()
{
/*
* PS Saturation Flag is used for monitoring the internal IC saturation.
* It will be flagged when the IC has reached saturation
* and not able to perform any further PS measurement
* */
bool saturated;
Serial.print(" IRQ:"); Serial.print (digitalRead(SENSOR_IRQ));
Serial.print(" ALS: CH1:"); Serial.print(als.getLightSensor(1));
Serial.print(" - CH0:"); Serial.print(als.getLightSensor(0));
Serial.print(" - PS:"); Serial.print(als.getProximity(&saturated));
Serial.print(" - "); Serial.println(saturated ? "PS saturated" : "PS not saturated");
delay(500);
if (canRead()) {
Serial.print(" ALS: CH1:"); Serial.print(als.getLightSensor(1));
Serial.print(" - CH0:"); Serial.print(als.getLightSensor(0));
Serial.print(" - PS:"); Serial.print(als.getProximity(&saturated));
Serial.print(" - "); Serial.println(saturated ? "PS saturated" : "PS not saturated");
}
delay(5);
}


Expand Down
20 changes: 7 additions & 13 deletions src/SensorLTR553.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SensorLTR553 :
ALS_IRQ_ACTIVE_HIGH // INT pin is considered active when it is a logic 1
};
enum IrqMode {
ALS_IRQ_ONLY_PS, // Only PS measurement can trigger interrupt
ALS_IRQ_ONLY_PS = 1, // Only PS measurement can trigger interrupt
ALS_IRQ_ONLY_ALS, // Only ALS measurement can trigger interrupt
ALS_IRQ_BOTH, // Both ALS and PS measurement can trigger interrupt
};
Expand Down Expand Up @@ -235,21 +235,15 @@ class SensorLTR553 :
// defined by the upper and lower threshold limits before asserting the interrupt.
void setProximityPersists(uint8_t count)
{
writeRegister(LTR553_REG_INTERRUPT_PERSIST, 0x0F, count - 1);
writeRegister(LTR553_REG_INTERRUPT_PERSIST, 0x0F, count == 0 ? 0 : count - 1);
}

void setProximityThreshold(uint16_t low, uint16_t high)
{
uint8_t buffer[4] = {
(uint8_t)(high & 0xFF),
(uint8_t)((high >> 8) & 0xFF),
(uint8_t)(low & 0xFF),
(uint8_t)((low >> 8) & 0xFF),
};
writeRegister(LTR553_REG_PS_THRES_UP_0, (uint8_t)(high & 0xFF));
writeRegister(LTR553_REG_PS_THRES_UP_1, (uint8_t)((high >> 8) & 0xFF) & 0x03);
writeRegister(LTR553_REG_PS_THRES_LOW_0, (uint8_t)(low & 0xFF));
writeRegister(LTR553_REG_PS_THRES_LOW_1, (uint8_t)((low >> 8) & 0xFF) & 0x03);
writeRegister(LTR553_REG_PS_THRES_UP_0, lowByte(high));
writeRegister(LTR553_REG_PS_THRES_UP_1, lowByte(high >> 8) & 0x0F);
writeRegister(LTR553_REG_PS_THRES_LOW_0, lowByte(low));
writeRegister(LTR553_REG_PS_THRES_LOW_1, lowByte(low >> 8) & 0x0F);
}

void setProximityRate(PsRate rate)
Expand Down Expand Up @@ -277,7 +271,7 @@ class SensorLTR553 :
clrRegisterBit(LTR553_REG_PS_CONTR, 5);
}

int getProximity(bool *saturated )
int getProximity(bool *saturated = NULL )
{
uint8_t buffer[2] = {0};
int val = readRegister(LTR553_REG_PS_DATA_0, buffer, 2);
Expand Down

0 comments on commit d0f26c3

Please sign in to comment.