Skip to content

Added interrupt feature #10

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

Open
wants to merge 5 commits 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
38 changes: 38 additions & 0 deletions examples/ConfigInterrupt/ConfigInterrupt.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
LPS22HB - Pressure interrupt
This example config the INTERRUPT pin of the sensor
that allows to use interrupts attached to that pin
and do something, for example light up a LED

This example code is in the public domain.
*/

#include <Arduino_LPS22HB.h>

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial);

if (!BARO.begin()) {
Serial.println("Failed BAro");
while (1);
}

// Config for the Interrupts and the Interrupt pin
BARO.enableInterruptPin(); // Enables ic's pin
BARO.setOpenDrain(); // Config ic's pin to be in a open drain
BARO.setActiveHigh(); // High means interrupt
BARO.enableHighPressureInterrupt(); // We set to interrupt when the pressure is higher than the Threshold setThreshold()
}

void loop() {
// put your main code here, to run repeatedly:
BARO.setThreshold(16); // +-Interrupt threshold = value /16 , in this case 16/16 = 1 hPa
Serial.println(BARO.readPressure()); // The interrupt is updated when it has been read
delay(1500);

BARO.setThreshold(20000); // +-Interrupt threshold = value /16 , in this case 20000/16 = 125 hPa
Serial.println(BARO.readPressure()); // The interrupt is updated when it has been read
delay(1500);
}
18 changes: 18 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ end KEYWORD2

readPressure KEYWORD2

enableInterruptPin KEYWORD2
disableInterruptPin KEYWORD2
setOpenDrain KEYWORD2
setPushPull KEYWORD2
setActiveHigh KEYWORD2
setActiveLow KEYWORD2

setThreshold KEYWORD2
enableHighPressureInterrupt KEYWORD2
disableHighPressureInterrupt KEYWORD2
enableLowPressureInterrupt KEYWORD2
disableLowPressureInterrupt KEYWORD2

interrupt KEYWORD2
pressureInterrupt KEYWORD2
HighPressureInterrupt KEYWORD2
LowPresureInterrupt KEYWORD2

#######################################
# Constants
#######################################
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Arduino_LPS22HB
version=1.0.1
version=1.1.0
author=Arduino
maintainer=Arduino <info@arduino.cc>
sentence=Allows you to read the pressure sensor of your Nano 33 BLE Sense.
Expand Down
121 changes: 117 additions & 4 deletions src/BARO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
#define LPS22HB_PRESS_OUT_L_REG 0x29
#define LPS22HB_PRESS_OUT_H_REG 0x2a

// Interrupts
#define LPS22HN_CTRL3_REG 0x12 // [INT H/L] [Push-pull / open-dra] [F_FSS5] [F_FTH] [F_OVR] [DRDY] [INT_S1] [INT_S0]
#define LPS22HB_INTERRUPT_CFG_REG 0x0b // b3 Int-generation-enable b1 Int-Low-press b0 INT-high-press
#define LPS22HB_INTERRUPT_THOLD_L_REG 0x0C
#define LPS22HB_INTERRUPT_THOLD_H_REG 0x0D
#define LPS22HB_INTERRUPT_SOURCE_REG 0x25 // [Boot_Status] -- [Interrupt active][Int low][Int high]


LPS22HBClass::LPS22HBClass(TwoWire& wire) :
_wire(&wire)
{
Expand All @@ -43,7 +51,7 @@ int LPS22HBClass::begin()
end();
return 0;
}

softwareReset();
return 1;
}

Expand All @@ -57,8 +65,8 @@ float LPS22HBClass::readPressure(int units)
// trigger one shot
i2cWrite(LPS22HB_CTRL2_REG, 0x01);

// wait for ONE_SHOT bit to be cleared by the hardware
while ((i2cRead(LPS22HB_CTRL2_REG) & 0x01) != 0) {
// wait for completion
while ((i2cRead(LPS22HB_STATUS_REG) & 0x02) == 0) {
yield();
}

Expand All @@ -75,6 +83,111 @@ float LPS22HBClass::readPressure(int units)
}
}

void LPS22HBClass::setOpenDrain()
{
uint8_t ctrl3_reg = (i2cRead(LPS22HN_CTRL3_REG) & 0b10111111) | 0b1 << 6;
i2cWrite(LPS22HN_CTRL3_REG, ctrl3_reg);
}

void LPS22HBClass::setPushPull()
{
uint8_t ctrl3_reg = i2cRead(LPS22HN_CTRL3_REG) & 0b10111111;
i2cWrite(LPS22HN_CTRL3_REG, ctrl3_reg);
}

void LPS22HBClass::setActiveHigh()
{
uint8_t ctrl3_reg = i2cRead(LPS22HN_CTRL3_REG) & 0b1111111 ;
i2cWrite(LPS22HN_CTRL3_REG, ctrl3_reg);
}

void LPS22HBClass::setActiveLow()
{
uint8_t ctrl3_reg = (i2cRead(LPS22HN_CTRL3_REG) & 0b1111111) | 0b1 << 7;
i2cWrite(LPS22HN_CTRL3_REG, ctrl3_reg);
}

void LPS22HBClass::enableInterruptPin()
{
uint8_t interrupt_cfg = (i2cRead(LPS22HB_INTERRUPT_CFG_REG) & 0b11110111) | 0b1 <<3; // Enable DIFF_EN
i2cWrite(LPS22HB_INTERRUPT_CFG_REG, interrupt_cfg);
uint8_t INT_DRDY_reg = (i2cRead(0x12) & 0b1111110) | 0b1; // INT_S
i2cWrite(0x12, INT_DRDY_reg);
Serial.println(interrupt_cfg);
}

void LPS22HBClass::disableInterruptPin()
{
uint8_t interrupt_cfg = (i2cRead(LPS22HB_INTERRUPT_CFG_REG) & 0b11110111); // Enable DIFF_EN
i2cWrite(LPS22HB_INTERRUPT_CFG_REG, interrupt_cfg);
uint8_t INT_DRDY_reg = (i2cRead(0x12) & 0b1111101); // INT_S
i2cWrite(0x12, INT_DRDY_reg);
}

void LPS22HBClass::setThreshold(uint16_t newThold)
{
i2cWrite(LPS22HB_INTERRUPT_THOLD_L_REG , uint8_t(newThold));
i2cWrite(LPS22HB_INTERRUPT_THOLD_H_REG , uint8_t(newThold>>8));
}

void LPS22HBClass::enableHighPressureInterrupt()
{
uint8_t interrupt_cfg = (i2cRead(0x0b) & 0b1111110) | 0b1; // Enable PHE
i2cWrite(0x0b, interrupt_cfg);
}

void LPS22HBClass::disableHighPressureInterrupt()
{
uint8_t interrupt_cfg = (i2cRead(0x0b) & 0b1111110); // Disable PHE
uint8_t INT_DRDY_reg = (i2cRead(0x12) & 0b1111110); // INT_S
i2cWrite(0x0b, interrupt_cfg);
i2cWrite(0x12, INT_DRDY_reg);
}

void LPS22HBClass::enableLowPressureInterrupt()
{
uint8_t interrupt_cfg = (i2cRead(0x0b) & 0b1111101) | 0b1 <1; // Enable PLE
uint8_t INT_DRDY_reg = (i2cRead(0x12) & 0b1111101) | 0b1 <1; // INT_S
i2cWrite(0x0b, interrupt_cfg);
i2cWrite(0x12, INT_DRDY_reg);
}

void LPS22HBClass::disableLowPressureInterrupt()
{
uint8_t interrupt_cfg = (i2cRead(0x0b) & 0b1111101); // Disable PLE
i2cWrite(0x0b, interrupt_cfg);
}

bool LPS22HBClass::interrupt()
{
return i2cRead(LPS22HB_INTERRUPT_SOURCE_REG) & 0b1 << 2;
}

bool LPS22HBClass::pressureInterrupt()
{
uint8_t reg = i2cRead(LPS22HB_INTERRUPT_SOURCE_REG);
return (reg & 0b1<0) | (reg & 0b1<1) ;
}

bool LPS22HBClass::HighPressureInterrupt()
{
return i2cRead(LPS22HB_INTERRUPT_SOURCE_REG) & 0b1 << 0;
}

bool LPS22HBClass::LowPresureInterrupt()
{
return i2cRead(LPS22HB_INTERRUPT_SOURCE_REG) & 0b1 << 1;
}

void LPS22HBClass::softwareReset()
{
i2cWrite(LPS22HB_CTRL2_REG, 0b100);

while(i2cRead(LPS22HB_CTRL2_REG) & 0b1 << 2){
delay(10);
}
}

int LPS22HBClass::i2cRead(uint8_t reg)
{
_wire->beginTransmission(LPS22HB_ADDRESS);
Expand Down Expand Up @@ -106,4 +219,4 @@ int LPS22HBClass::i2cWrite(uint8_t reg, uint8_t val)
LPS22HBClass BARO(Wire1);
#else
LPS22HBClass BARO(Wire);
#endif
#endif
22 changes: 21 additions & 1 deletion src/BARO.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ class LPS22HBClass {

float readPressure(int units = KILOPASCAL);

void softwareReset();

void enableInterruptPin();
void disableInterruptPin();
void setOpenDrain();
void setPushPull();
void setActiveHigh();
void setActiveLow();

void setThreshold(uint16_t newThold);
void enableHighPressureInterrupt();
void disableHighPressureInterrupt();
void enableLowPressureInterrupt();
void disableLowPressureInterrupt();

bool interrupt();
bool pressureInterrupt();
bool HighPressureInterrupt();
bool LowPresureInterrupt();

private:
int i2cRead(uint8_t reg);
int i2cWrite(uint8_t reg, uint8_t val);
Expand All @@ -48,4 +68,4 @@ class LPS22HBClass {

extern LPS22HBClass BARO;

#endif
#endif