wire & sht2x
This commit is contained in:
parent
7b7e940654
commit
d9f184c1f1
46
MikuSHT2x.cpp
Executable file
46
MikuSHT2x.cpp
Executable file
@ -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;
|
||||
}
|
||||
|
||||
27
MikuSHT2x.h
Executable file
27
MikuSHT2x.h
Executable file
@ -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
|
||||
12
MikuTypes.h
Executable file
12
MikuTypes.h
Executable file
@ -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
|
||||
88
Wire.cpp
Executable file
88
Wire.cpp
Executable file
@ -0,0 +1,88 @@
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
|
||||
/*
|
||||
*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();
|
||||
|
||||
33
Wire.h
Executable file
33
Wire.h
Executable file
@ -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
|
||||
Loading…
x
Reference in New Issue
Block a user