diff options
author | Daniel Friesel <daniel.friesel@uos.de> | 2019-10-18 10:38:48 +0200 |
---|---|---|
committer | Daniel Friesel <daniel.friesel@uos.de> | 2019-10-18 10:38:48 +0200 |
commit | e391c1bc244838bb050ade946e709f671cd9cf94 (patch) | |
tree | f3b5e0260f95f886f54be213098f3362b7f8c815 | |
parent | 8e0cde61737f46ba492768d86c54cff998da07a1 (diff) |
Add preliminary version of HDC1080 driver (not working reliably yet)
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | include/driver/hdc1080.h | 22 | ||||
-rw-r--r-- | src/app/i2cdetect/main.cc | 13 | ||||
-rw-r--r-- | src/driver/hdc1080.cc | 51 |
4 files changed, 91 insertions, 0 deletions
@@ -63,6 +63,11 @@ ifneq ($(findstring max44009,${drivers}), ) COMMON_FLAGS += -DDRIVER_MAX44009 endif +ifneq ($(findstring hdc1080,${drivers}), ) + CXX_TARGETS += src/driver/hdc1080.cc + COMMON_FLAGS += -DDRIVER_HDC1080 +endif + ifneq ($(findstring mmsimple,${drivers}), ) CXX_TARGETS += src/driver/mmsimple.cc COMMON_FLAGS += -DDRIVER_MMSIMPLE diff --git a/include/driver/hdc1080.h b/include/driver/hdc1080.h new file mode 100644 index 0000000..f6f278c --- /dev/null +++ b/include/driver/hdc1080.h @@ -0,0 +1,22 @@ +#ifndef HDC1080_H +#define HDC1080_H + +class HDC1080 { + private: + HDC1080(const HDC1080 ©); + unsigned char const address = 0x40; + unsigned char txbuf[3]; + unsigned char rxbuf[2]; + + public: + HDC1080() {} + + float getTemp(); + float getRH(); + unsigned int getManufacturerID(); + void init(); +}; + +extern HDC1080 hdc1080; + +#endif diff --git a/src/app/i2cdetect/main.cc b/src/app/i2cdetect/main.cc index acf5ef1..e74ce5b 100644 --- a/src/app/i2cdetect/main.cc +++ b/src/app/i2cdetect/main.cc @@ -25,6 +25,9 @@ #ifdef DRIVER_MAX44009 #include "driver/max44009.h" #endif +#ifdef DRIVER_HDC1080 +#include "driver/hdc1080.h" +#endif #ifdef DRIVER_MMSIMPLE #include "driver/mmsimple.h" #endif @@ -81,6 +84,10 @@ void loop(void) #ifdef DRIVER_CCS811 kout << "CCS811 status is " << ccs811.check() << endl; #endif +#ifdef DRIVER_HDC1080 + kout << "HDC1080 temperature " << hdc1080.getTemp() << " degC" << endl; + kout << "HDC1080 humidity " << hdc1080.getRH() << " %H" << endl; +#endif #ifdef DRIVER_MAX44009 kout.printf_float(max44009.getLux()); kout << endl; @@ -118,6 +125,12 @@ int main(void) #ifdef DRIVER_CCS811 ccs811.init(); #endif +#ifdef DRIVER_HDC1080 + hdc1080.init(); + if (hdc1080.getManufacturerID() != 0x5449) { + kout << "[!] invalid HDC1080 manufacturer ID: " << hex << hdc1080.getManufacturerID() << endl; + } +#endif for (unsigned char i = 0; i < sizeof(i2c_status)/sizeof(unsigned int); i++) { i2c_status[i] = 0; diff --git a/src/driver/hdc1080.cc b/src/driver/hdc1080.cc new file mode 100644 index 0000000..8da2ba7 --- /dev/null +++ b/src/driver/hdc1080.cc @@ -0,0 +1,51 @@ +#include "driver/hdc1080.h" +#include "arch.h" +#if defined(MULTIPASS_ARCH_HAS_I2C) && !defined(DRIVER_SOFTI2C) +#include "driver/i2c.h" +#else +#include "driver/soft_i2c.h" +#endif + +float HDC1080::getTemp() +{ + txbuf[0] = 0x00; + + i2c.xmit(address, 1, txbuf, 0, rxbuf); + + arch.delay_ms(10); + + i2c.xmit(address, 0, txbuf, 2, rxbuf); + + return (((unsigned int)rxbuf[0] << 8) | rxbuf[1]) * .00251770019531250000 - 40.; +} + +float HDC1080::getRH() +{ + txbuf[0] = 0x01; + + i2c.xmit(address, 1, txbuf, 0, rxbuf); + + arch.delay_ms(10); + + i2c.xmit(address, 0, txbuf, 2, rxbuf); + + return (((unsigned int)rxbuf[0] << 8) | rxbuf[1]) * .00152587890625000000; +} + +unsigned int HDC1080::getManufacturerID() +{ + txbuf[0] = 0xfe; + i2c.xmit(address, 1, txbuf, 2, rxbuf); + return (unsigned int)rxbuf[0] << 8 | rxbuf[1]; +} + +void HDC1080::init() +{ + txbuf[0] = 0x02; + txbuf[1] = 0x08; + txbuf[2] = 0x00; + i2c.xmit(address, 3, txbuf, 0, rxbuf); + arch.delay_ms(15); +} + +HDC1080 hdc1080; |