diff options
author | Wolfgang (Wolle) Ewald <wolfgang.ewald@wolles-elektronikkiste.de> | 2022-05-01 12:20:48 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-01 12:20:48 +0200 |
commit | 3423c96f6bfbd8ffa5691cb85ab4adfcdf394d82 (patch) | |
tree | 1cc4a831df0fe99662c3a3ecdc249d7937367ef9 | |
parent | Add files via upload (diff) | |
download | ADS1115_WE-3423c96f6bfbd8ffa5691cb85ab4adfcdf394d82.tar ADS1115_WE-3423c96f6bfbd8ffa5691cb85ab4adfcdf394d82.tar.gz ADS1115_WE-3423c96f6bfbd8ffa5691cb85ab4adfcdf394d82.tar.bz2 ADS1115_WE-3423c96f6bfbd8ffa5691cb85ab4adfcdf394d82.tar.lz ADS1115_WE-3423c96f6bfbd8ffa5691cb85ab4adfcdf394d82.tar.xz ADS1115_WE-3423c96f6bfbd8ffa5691cb85ab4adfcdf394d82.tar.zst ADS1115_WE-3423c96f6bfbd8ffa5691cb85ab4adfcdf394d82.zip |
-rw-r--r-- | src/ADS1115_WE.cpp | 73 | ||||
-rw-r--r-- | src/ADS1115_WE.h | 12 | ||||
-rw-r--r-- | src/ADS1115_config.h | 5 |
3 files changed, 70 insertions, 20 deletions
diff --git a/src/ADS1115_WE.cpp b/src/ADS1115_WE.cpp index 5f1174b..a3e3214 100644 --- a/src/ADS1115_WE.cpp +++ b/src/ADS1115_WE.cpp @@ -17,15 +17,20 @@ #include "ADS1115_WE.h"
ADS1115_WE::ADS1115_WE(int addr){
+#ifndef USE_TINY_WIRE_M_
_wire = &Wire;
+#endif
i2cAddress = addr;
}
ADS1115_WE::ADS1115_WE(){
+#ifndef USE_TINY_WIRE_M_
_wire = &Wire;
+#endif
i2cAddress = 0x48;
}
+#ifndef USE_TINY_WIRE_M_
ADS1115_WE::ADS1115_WE(TwoWire *w, int addr){
_wire = w;
i2cAddress = addr;
@@ -35,17 +40,29 @@ ADS1115_WE::ADS1115_WE(TwoWire *w){ _wire = w;
i2cAddress = 0x48;
}
+#endif
void ADS1115_WE::reset(){
+#ifndef USE_TINY_WIRE_M_
_wire->beginTransmission(0);
_wire->write(0x06);
_wire->endTransmission();
+#else
+ TinyWireM.beginTransmission(0);
+ TinyWireM.send(0x06);
+ TinyWireM.endTransmission();
+#endif
}
bool ADS1115_WE::init(){
+#ifndef USE_TINY_WIRE_M_
_wire->beginTransmission(i2cAddress);
uint8_t success = _wire->endTransmission();
+#else
+ TinyWireM.beginTransmission(i2cAddress);
+ uint8_t success = TinyWireM.endTransmission();
+#endif
if(success){
return 0;
}
@@ -322,28 +339,46 @@ int16_t ADS1115_WE::calcLimit(float rawLimit){ }
uint8_t ADS1115_WE::writeRegister(uint8_t reg, uint16_t val){
- _wire->beginTransmission(i2cAddress);
- uint8_t lVal = val & 255;
- uint8_t hVal = val >> 8;
- _wire->write(reg);
- _wire->write(hVal);
- _wire->write(lVal);
- return _wire->endTransmission();
+ uint8_t lVal = val & 255;
+ uint8_t hVal = val >> 8;
+#ifndef USE_TINY_WIRE_M_
+ _wire->beginTransmission(i2cAddress);
+ _wire->write(reg);
+ _wire->write(hVal);
+ _wire->write(lVal);
+ return _wire->endTransmission();
+#else
+ TinyWireM.beginTransmission(i2cAddress);
+ TinyWireM.send(reg);
+ TinyWireM.send(hVal);
+ TinyWireM.send(lVal);
+ return TinyWireM.endTransmission();
+#endif
+
}
uint16_t ADS1115_WE::readRegister(uint8_t reg){
- uint8_t MSByte = 0, LSByte = 0;
- uint16_t regValue = 0;
- _wire->beginTransmission(i2cAddress);
- _wire->write(reg);
- _wire->endTransmission(false);
- _wire->requestFrom(i2cAddress,2);
- if(_wire->available()){
- MSByte = _wire->read();
- LSByte = _wire->read();
- }
- regValue = (MSByte<<8) + LSByte;
- return regValue;
+ uint8_t MSByte = 0, LSByte = 0;
+ uint16_t regValue = 0;
+#ifndef USE_TINY_WIRE_M_
+ _wire->beginTransmission(i2cAddress);
+ _wire->write(reg);
+ _wire->endTransmission(false);
+ _wire->requestFrom(i2cAddress,2);
+ if(_wire->available()){
+ MSByte = _wire->read();
+ LSByte = _wire->read();
+ }
+#else
+ TinyWireM.beginTransmission(i2cAddress);
+ TinyWireM.send(reg);
+ TinyWireM.endTransmission();
+ TinyWireM.requestFrom(i2cAddress,2);
+ MSByte = TinyWireM.receive();
+ LSByte = TinyWireM.receive();
+#endif
+ regValue = (MSByte<<8) + LSByte;
+ return regValue;
}
diff --git a/src/ADS1115_WE.h b/src/ADS1115_WE.h index 9443bd8..19f7aff 100644 --- a/src/ADS1115_WE.h +++ b/src/ADS1115_WE.h @@ -24,8 +24,14 @@ #else #include "WProgram.h" #endif +#include "ADS1115_config.h" -#include <Wire.h> +#ifdef USE_TINY_WIRE_M_ + #include <TinyWireM.h> +#endif +#ifndef USE_TINY_WIRE_M_ + #include <Wire.h> +#endif /* registers */ #define ADS1115_CONV_REG 0x00 // Conversion Register @@ -108,8 +114,10 @@ class ADS1115_WE public: ADS1115_WE(int addr); ADS1115_WE(); // uses default I2C Address 0x48 +#ifndef USE_TINY_WIRE_M_ ADS1115_WE(TwoWire *w, int addr); ADS1115_WE(TwoWire *w); +#endif void reset(); bool init(); @@ -268,7 +276,9 @@ public: private: +#ifndef USE_TINY_WIRE_M_ TwoWire *_wire; +#endif uint16_t voltageRange; ADS1115_MEASURE_MODE deviceMeasureMode; int i2cAddress; diff --git a/src/ADS1115_config.h b/src/ADS1115_config.h new file mode 100644 index 0000000..dabd14b --- /dev/null +++ b/src/ADS1115_config.h @@ -0,0 +1,5 @@ +#ifndef ADS1115_CONFIG_H_
+#define ADS1115_CONFIG_H_
+/* Uncomment the following line to use TinyWireM instead of Wire */
+//#define USE_TINY_WIRE_M_
+#endif
\ No newline at end of file |