From 719a3a7c18789b2b034b994f760250b20b1e7ebd Mon Sep 17 00:00:00 2001 From: robbrad Date: Fri, 2 Nov 2018 00:07:10 +0000 Subject: [PATCH] Adding C Libs --- @eaDir/HP20x_dev.h@SynoEAStream | Bin 0 -> 244 bytes @eaDir/KalmanFilter.h@SynoEAStream | Bin 0 -> 244 bytes DHT.h | 49 ++++++++++ HP20x_dev.h | 101 +++++++++++++++++++ KalmanFilter.h | 37 +++++++ MutichannelGasSensor.h | 151 +++++++++++++++++++++++++++++ 6 files changed, 338 insertions(+) create mode 100644 @eaDir/HP20x_dev.h@SynoEAStream create mode 100644 @eaDir/KalmanFilter.h@SynoEAStream create mode 100644 DHT.h create mode 100644 HP20x_dev.h create mode 100644 KalmanFilter.h create mode 100644 MutichannelGasSensor.h diff --git a/@eaDir/HP20x_dev.h@SynoEAStream b/@eaDir/HP20x_dev.h@SynoEAStream new file mode 100644 index 0000000000000000000000000000000000000000..cbef6a38a96adfbd3c9d1a3a0b0eed5c55b5b5a3 GIT binary patch literal 244 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDI}@gX1v@xK5u5x_AdBnYYuq+<#YCqVfy ziV?_wiVH;L=cVd-rljVTWTs`N7BMhHgG54#67!1FQj21FxjvsFxJacWUG(Q NF_5EIm07^c1pw)CG1~wD literal 0 HcmV?d00001 diff --git a/@eaDir/KalmanFilter.h@SynoEAStream b/@eaDir/KalmanFilter.h@SynoEAStream new file mode 100644 index 0000000000000000000000000000000000000000..cbef6a38a96adfbd3c9d1a3a0b0eed5c55b5b5a3 GIT binary patch literal 244 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDI}@gX1v@xK5u5x_AdBnYYuq+<#YCqVfy ziV?_wiVH;L=cVd-rljVTWTs`N7BMhHgG54#67!1FQj21FxjvsFxJacWUG(Q NF_5EIm07^c1pw)CG1~wD literal 0 HcmV?d00001 diff --git a/DHT.h b/DHT.h new file mode 100644 index 0000000..7acd2ba --- /dev/null +++ b/DHT.h @@ -0,0 +1,49 @@ +#ifndef DHT_H +#define DHT_H +#if ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +// 8 MHz(ish) AVR --------------------------------------------------------- +#if (F_CPU >= 7400000UL) && (F_CPU <= 9500000UL) +#define COUNT 3 +// 16 MHz(ish) AVR -------------------------------------------------------- +#elif (F_CPU >= 15400000UL) && (F_CPU <= 19000000L) +#define COUNT 6 +#else +#error "CPU SPEED NOT SUPPORTED" +#endif + +/* DHT library + +MIT license +written by Adafruit Industries +*/ + +// how many timing transitions we need to keep track of. 2 * number bits + extra +#define MAXTIMINGS 85 + +#define DHT11 11 +#define DHT22 22 +#define DHT21 21 +#define AM2301 21 + +class DHT { + private: + uint8_t data[6]; + uint8_t _pin, _type, _count; + boolean read(void); + unsigned long _lastreadtime; + boolean firstreading; + + public: + DHT(uint8_t pin, uint8_t type, uint8_t count=COUNT); + void begin(void); + float readTemperature(bool S=false); + float convertCtoF(float); + float readHumidity(void); + +}; +#endif \ No newline at end of file diff --git a/HP20x_dev.h b/HP20x_dev.h new file mode 100644 index 0000000..993be47 --- /dev/null +++ b/HP20x_dev.h @@ -0,0 +1,101 @@ +/* + * File name : HP20x_dev.h + * Description: Driver for I2C PRECISION BAROMETER AND ALTIMETER [HP206C] + * Author : Oliver Wang from Seeed studio + * Version : V0.1 + * Create Time: 2014/04 + * Change Log : +*/ +#ifndef _HP20X_DEV_H +#define _HP20X_DEV_H +/****************************************************************************/ +/*** Including Files ***/ +/****************************************************************************/ +#include +#include +/****************************************************************************/ +/*** Macro Definitions ***/ +/****************************************************************************/ +typedef unsigned int uint; +typedef unsigned char uchar; +typedef unsigned long ulong; + +#define HP20X_I2C_DEV_ID (0xEC)>>1 //CSB PIN is VDD level(address is 0x76) +#define HP20X_I2C_DEV_ID2 (0XEE)>>1 //CSB PIN is GND level(address is 0x77) +#define HP20X_SOFT_RST 0x06 +#define HP20X_WR_CONVERT_CMD 0x40 +#define HP20X_CONVERT_OSR4096 0<<2 +#define HP20X_CONVERT_OSR2048 1<<2 +#define HP20X_CONVERT_OSR1024 2<<2 +#define HP20X_CONVERT_OSR512 3<<2 +#define HP20X_CONVERT_OSR256 4<<2 +#define HP20X_CONVERT_OSR128 5<<2 + +#define HP20X_READ_P 0x30 //read_p command +#define HP20X_READ_A 0x31 //read_a command +#define HP20X_READ_T 0x32 //read_t command +#define HP20X_READ_PT 0x10 //read_pt command +#define HP20X_READ_AT 0x11 //read_at command +#define HP20X_READ_CAL 0X28 //RE-CAL ANALOG + +#define HP20X_WR_REG_MODE 0xC0 +#define HP20X_RD_REG_MODE 0x80 + +#define ERR_WR_DEVID_NACK 0x01 +#define ERR_RD_DEVID_NACK 0x02 +#define ERR_WR_REGADD_NACK 0x04 +#define ERR_WR_REGCMD_NACK 0x08 +#define ERR_WR_DATA_NACK 0x10 +#define ERR_RD_DATA_MISMATCH 0x20 + +#define I2C_DID_WR_MASK 0xFE +#define I2C_DID_RD_MASK 0x01 + +#define T_WIN_EN 0X01 +#define PA_WIN_EN 0X02 +#define T_TRAV_EN 0X04 +#define PA_TRAV_EN 0X08 +#define PA_RDY_EN 0X20 +#define T_RDY_EN 0X10 + +#define T_WIN_CFG 0X01 +#define PA_WIN_CFG 0X02 +#define PA_MODE_P 0X00 +#define PA_MODE_A 0X40 + +#define T_TRAV_CFG 0X04 + +#define OK_HP20X_DEV 0X80 //HP20x_dev successfully initialized +#define REG_PARA 0X0F //Status register + +/****************************************************************************/ +/*** Class Definitions ***/ +/****************************************************************************/ +class HP20x_dev : public TwoWire +{ + /* Public variables and functions */ + public: + uchar OSR_CFG; + uint OSR_ConvertTime; + /* Constructor */ + HP20x_dev(); + void begin(); + uchar isAvailable(); + + /* Read sensor data */ + ulong ReadTemperature(void); + ulong ReadPressure(void); + ulong ReadAltitude(void); + + /* Private variables and functions */ + private: + /* Write a command to HP20x */ + void HP20X_IIC_WriteCmd(uchar uCmd); + /* Read register value */ + uchar HP20X_IIC_ReadReg(uchar bReg); + void HP20X_IIC_WriteReg(uchar bReg,uchar bData); + ulong HP20X_IIC_ReadData(void); + ulong HP20X_IIC_ReadData3byte(void); +}; +extern HP20x_dev HP20x; +#endif \ No newline at end of file diff --git a/KalmanFilter.h b/KalmanFilter.h new file mode 100644 index 0000000..ba188df --- /dev/null +++ b/KalmanFilter.h @@ -0,0 +1,37 @@ +/* + * File name : kalmanFilter.h + * Description: + * Author : Oliver Wang from Seeed studio + * Version : V0.1 + * Create Time: 2014/04 + * Change Log : +*/ + +#ifndef _KALMANFILTER_H +#define _KALMANFILTER_H +/****************************************************************************/ +/*** Include files ***/ +/****************************************************************************/ +#include +#include +/****************************************************************************/ +/*** Local variables ***/ +/****************************************************************************/ + + +/****************************************************************************/ +/*** Class Definitions ***/ +/****************************************************************************/ +class KalmanFilter +{ + public: + KalmanFilter(); + float Filter(float); + private: + /* variables */ + float X_pre, X_post, P_pre, P_post, K_cur; + float Gaussian_Noise_Cov(void); + +}; +extern KalmanFilter kalmanFilter; +#endif \ No newline at end of file diff --git a/MutichannelGasSensor.h b/MutichannelGasSensor.h new file mode 100644 index 0000000..e646b5c --- /dev/null +++ b/MutichannelGasSensor.h @@ -0,0 +1,151 @@ +/* + MutichannelGasSensor.h + 2015 Copyright (c) Seeed Technology Inc. All right reserved. + + Author: Jacky Zhang + 2015-3-17 + http://www.seeed.cc/ + modi by Jack, 2015-8 + + V2 by Loovee + 2016-11-11 + + The MIT License (MIT) + + Copyright (c) 2015 Seeed Technology Inc. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef __MUTICHANNELGASSENSOR_H__ +#define __MUTICHANNELGASSENSOR_H__ + +#define DEFAULT_I2C_ADDR 0x04 + +#define ADDR_IS_SET 0 // if this is the first time to run, if 1126, set +#define ADDR_FACTORY_ADC_NH3 2 +#define ADDR_FACTORY_ADC_CO 4 +#define ADDR_FACTORY_ADC_NO2 6 + +#define ADDR_USER_ADC_HN3 8 +#define ADDR_USER_ADC_CO 10 +#define ADDR_USER_ADC_NO2 12 +#define ADDR_IF_CALI 14 // IF USER HAD CALI + +#define ADDR_I2C_ADDRESS 20 + +#define CH_VALUE_NH3 1 +#define CH_VALUE_CO 2 +#define CH_VALUE_NO2 3 + +#define CMD_ADC_RES0 1 // NH3 +#define CMD_ADC_RES1 2 // CO +#define CMD_ADC_RES2 3 // NO2 +#define CMD_ADC_RESALL 4 // ALL CHANNEL +#define CMD_CHANGE_I2C 5 // CHANGE I2C +#define CMD_READ_EEPROM 6 // READ EEPROM VALUE, RETURN UNSIGNED INT +#define CMD_SET_R0_ADC 7 // SET R0 ADC VALUE +#define CMD_GET_R0_ADC 8 // GET R0 ADC VALUE +#define CMD_GET_R0_ADC_FACTORY 9 // GET FACTORY R0 ADC VALUE +#define CMD_CONTROL_LED 10 +#define CMD_CONTROL_PWR 11 + +enum{CO, NO2, NH3, C3H8, C4H10, CH4, H2, C2H5OH}; + +class MutichannelGasSensor{ + +private: + + int __version; + unsigned char dta_test[20]; + + unsigned int readChAdcValue(int ch); + unsigned int adcValueR0_NH3_Buf; + unsigned int adcValueR0_CO_Buf; + unsigned int adcValueR0_NO2_Buf; + +public: + + uint8_t i2cAddress; //I2C address of this MCU + uint16_t res0[3]; //sensors res0 + uint16_t res[3]; //sensors res + bool r0_inited; + + + inline unsigned int get_addr_dta(unsigned char addr_reg); + inline unsigned int get_addr_dta(unsigned char addr_reg, unsigned char __dta); + inline void write_i2c(unsigned char addr, unsigned char *dta, unsigned char dta_len); + + void sendI2C(unsigned char dta); + int16_t readData(uint8_t cmd); + int16_t readR0(void); + int16_t readR(void); + float calcGas(int gas); + +public: + + void begin(int address); + void begin(); + void changeI2cAddr(uint8_t newAddr); + void powerOn(void); + void powerOff(void); + void doCalibrate(void); + + //get gas concentration, unit: ppm + float measure_CO(){return calcGas(CO);} + float measure_NO2(){return calcGas(NO2);} + float measure_NH3(){return calcGas(NH3);} + float measure_C3H8(){return calcGas(C3H8);} + float measure_C4H10(){return calcGas(C4H10);} + float measure_CH4(){return calcGas(CH4);} + float measure_H2(){return calcGas(H2);} + float measure_C2H5OH(){return calcGas(C2H5OH);} + + float getR0(unsigned char ch); // 0:CH3, 1:CO, 2:NO2 + float getRs(unsigned char ch); // 0:CH3, 1:CO, 2:NO2 + +public: + + void ledOn() + { + dta_test[0] = CMD_CONTROL_LED; + dta_test[1] = 1; + write_i2c(i2cAddress, dta_test, 2); + } + + void ledOff() + { + dta_test[0] = CMD_CONTROL_LED; + dta_test[1] = 0; + write_i2c(i2cAddress, dta_test, 2); + } + + void display_eeprom(); + void factory_setting(); + void change_i2c_address(unsigned char addr); + unsigned char getVersion(); +}; + +extern MutichannelGasSensor gas; + +#endif + +/********************************************************************************************************* + END FILE +*********************************************************************************************************/ \ No newline at end of file