diff --git a/MikuSHT2x.cpp b/MikuSHT2x.cpp new file mode 100755 index 0000000..560b319 --- /dev/null +++ b/MikuSHT2x.cpp @@ -0,0 +1,46 @@ +#include "MikuDuino.h" +#include "Wire.h" + +/* + *MikuSHT2x.cpp: + * + * Welcome to MikuQ.com! MikuDuino for BananaPi + * + * by MikuQ(i@mikuq.com) + * + * https://github.com/bpiq/MikuPi + * + */ + +#include "MikuSHT2x.h" + +float Miku_SHT2x::GetHumidity(void) +{ + return (-6.0 + 125.0 / 65536.0 * (float)(readSensor(eRHumidityHoldCmd))); +} + +float Miku_SHT2x::GetTemperature(void) +{ + return (-46.85 + 175.72 / 65536.0 * (float)(readSensor(eTempHoldCmd))); +} + +uint16 Miku_SHT2x::readSensor(uint8 command) +{ + uint16 result; + + Wire.beginTransmission(eSHT2xAddress); + Wire.write(command); + delay(100); + Wire.endTransmission(); + + Wire.requestFrom(eSHT2xAddress, 3); + while(Wire.available() < 3) { + ; + } + + result = ((Wire.read()) << 8); + result += Wire.read(); + result &= ~0x0003; + return result; +} + diff --git a/MikuSHT2x.h b/MikuSHT2x.h new file mode 100755 index 0000000..d618ae8 --- /dev/null +++ b/MikuSHT2x.h @@ -0,0 +1,27 @@ +#ifndef _MIKU_SHT2X_H_ +#define _MIKU_SHT2X_H_ + +#include "MikuTypes.h" + +typedef enum { + eSHT2xAddress = 0x40, +} HUM_SENSOR_T; + +typedef enum { + eTempHoldCmd = 0xE3, + eRHumidityHoldCmd = 0xE5, + eTempNoHoldCmd = 0xF3, + eRHumidityNoHoldCmd = 0xF5, +} HUM_MEASUREMENT_CMD_T; + +class Miku_SHT2x +{ + private: + uint16 readSensor(uint8 command); + + public: + float GetHumidity(void); + float GetTemperature(void); +}; + +#endif diff --git a/MikuTypes.h b/MikuTypes.h new file mode 100755 index 0000000..f23bad5 --- /dev/null +++ b/MikuTypes.h @@ -0,0 +1,12 @@ +#ifndef _MIKU_TYPES_H_ +#define _MIKU_TYPES_H_ + +typedef unsigned char uint8; +typedef unsigned short uint16; +typedef unsigned long uint32; + +typedef signed char int8; +typedef signed short int16; +typedef signed long int32; + +#endif diff --git a/Wire.cpp b/Wire.cpp new file mode 100755 index 0000000..e729209 --- /dev/null +++ b/Wire.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include + +/* + *Wire.cpp: + * + * Welcome to MikuQ.com! MikuDuino for BananaPi + * + * by MikuQ(i@mikuq.com) + * + * https://github.com/bpiq/MikuPi + * + */ + +#include "Wire.h" + +uint8 TwoWire::rxBuffer[BUFFER_LENGTH]; +uint8 TwoWire::rxBufferIndex = 0; +uint8 TwoWire::rxBufferLength = 0; + +TwoWire::TwoWire() +{ +} + +void TwoWire::requestFrom(int address, int quantity) +{ + I2cError = 0; + if ((I2cDevHandle = open("/dev/i2c-0", O_RDWR)) < 0) I2cError |= ERROR_I2C_OPEN; + else + { + if (ioctl(I2cDevHandle, I2C_SLAVE,address) < 0) I2cError |= ERROR_I2C_SETUP; + } + if(!I2cError) + { + if (::read(I2cDevHandle, rxBuffer,quantity) != quantity) I2cError |= ERROR_I2C_READ; + else + { + rxBufferIndex = 0; + rxBufferLength = quantity; + } + } + close(I2cDevHandle); +} + +void TwoWire::beginTransmission(int address) +{ + I2cError = 0; + if ((I2cDevHandle = open("/dev/i2c-0", O_RDWR)) < 0) I2cError |= ERROR_I2C_OPEN; + else + { + if (ioctl(I2cDevHandle, I2C_SLAVE,address) < 0) I2cError |= ERROR_I2C_SETUP; + } +} + +void TwoWire::endTransmission() +{ + close(I2cDevHandle); +} + +void TwoWire::write(uint8 cmd) +{ + if(!I2cError) + { + if((::write(I2cDevHandle, &cmd, 1)) != 1) I2cError |= ERROR_I2C_WRITE; + } +} + +int TwoWire::available(void) +{ + return rxBufferLength - rxBufferIndex; +} + +int TwoWire::read(void) +{ + int value = -1; + + if(rxBufferIndex < rxBufferLength){ + value = rxBuffer[rxBufferIndex]; + ++rxBufferIndex; + } + + return value; +} + +TwoWire Wire = TwoWire(); + diff --git a/Wire.h b/Wire.h new file mode 100755 index 0000000..b5bfd70 --- /dev/null +++ b/Wire.h @@ -0,0 +1,33 @@ +#ifndef _TWOWIRE_H_ +#define _TWOWIRE_H_ + +#include "MikuTypes.h" + +#define ERROR_I2C_OPEN 1 +#define ERROR_I2C_SETUP 2 +#define ERROR_I2C_READ 4 +#define ERROR_I2C_WRITE 8 + +#define BUFFER_LENGTH 32 + +class TwoWire +{ + private: + static uint8 rxBuffer[]; + static uint8 rxBufferIndex; + static uint8 rxBufferLength; + int I2cDevHandle; + int I2cError; + public: + TwoWire(); + void beginTransmission(int); + void endTransmission(void); + void requestFrom(int, int); + int available(void); + int read(void); + void write(uint8); +}; + +extern TwoWire Wire; + +#endif