From b713ba5290545baf6ca448ed0db398deb573c919 Mon Sep 17 00:00:00 2001 From: marqdevx Date: Fri, 19 Mar 2021 11:46:03 +0100 Subject: [PATCH 1/3] Add files via upload --- README.adoc | 2 - keywords.txt | 18 +++++++ library.properties | 2 +- src/BARO.cpp | 121 +++++++++++++++++++++++++++++++++++++++++++-- src/BARO.h | 22 ++++++++- 5 files changed, 157 insertions(+), 8 deletions(-) diff --git a/README.adoc b/README.adoc index cb7cbfe..345d071 100644 --- a/README.adoc +++ b/README.adoc @@ -1,7 +1,5 @@ = LPS22HB Library for Arduino = -image:https://github.com/arduino-libraries/Arduino_LPS22HB/workflows/Compile%20Examples/badge.svg["Compile Examples Status", link="https://github.com/arduino-libraries/Arduino_LPS22HB/actions?workflow=Compile+Examples"] image:https://github.com/arduino-libraries/Arduino_LPS22HB/workflows/Spell%20Check/badge.svg["Spell Check Status", link="https://github.com/arduino-libraries/Arduino_LPS22HB/actions?workflow=Spell+Check"] - Allows you to read the pressure sensor of your Nano 33 BLE Sense. diff --git a/keywords.txt b/keywords.txt index c10bd30..41f3e9d 100644 --- a/keywords.txt +++ b/keywords.txt @@ -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 ####################################### diff --git a/library.properties b/library.properties index e916b42..9fc82da 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Arduino_LPS22HB -version=1.0.0 +version=1.1.0 author=Arduino maintainer=Arduino sentence=Allows you to read the pressure sensor of your Nano 33 BLE Sense. diff --git a/src/BARO.cpp b/src/BARO.cpp index f344cc3..0c739ab 100644 --- a/src/BARO.cpp +++ b/src/BARO.cpp @@ -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) { @@ -43,7 +51,7 @@ int LPS22HBClass::begin() end(); return 0; } - + softwareReset(); return 1; } @@ -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(); } @@ -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); @@ -106,4 +219,4 @@ int LPS22HBClass::i2cWrite(uint8_t reg, uint8_t val) LPS22HBClass BARO(Wire1); #else LPS22HBClass BARO(Wire); -#endif +#endif \ No newline at end of file diff --git a/src/BARO.h b/src/BARO.h index 0744073..1a40cb4 100644 --- a/src/BARO.h +++ b/src/BARO.h @@ -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); @@ -48,4 +68,4 @@ class LPS22HBClass { extern LPS22HBClass BARO; -#endif +#endif \ No newline at end of file From 9d44b3ec48cfd05a05fc448603c8ed7b221e02d6 Mon Sep 17 00:00:00 2001 From: marqdevx Date: Fri, 19 Mar 2021 11:51:53 +0100 Subject: [PATCH 2/3] Add files via upload --- examples/ConfigInterrupt/ConfigInterrupt.ino | 38 ++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 examples/ConfigInterrupt/ConfigInterrupt.ino diff --git a/examples/ConfigInterrupt/ConfigInterrupt.ino b/examples/ConfigInterrupt/ConfigInterrupt.ino new file mode 100644 index 0000000..62644bc --- /dev/null +++ b/examples/ConfigInterrupt/ConfigInterrupt.ino @@ -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 + +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); +} From 9ac81f25fa413e35d9fcb7b3d14b24fc7c7cf88e Mon Sep 17 00:00:00 2001 From: marqdevx Date: Mon, 7 Jun 2021 11:47:38 +0200 Subject: [PATCH 3/3] Beautified code --- src/BARO.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BARO.cpp b/src/BARO.cpp index 0c739ab..b576eb7 100644 --- a/src/BARO.cpp +++ b/src/BARO.cpp @@ -121,7 +121,7 @@ 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); + i2cWrite(0x12, INT_DRDY_reg); } void LPS22HBClass::setThreshold(uint16_t newThold)