Skip to content

Commit

Permalink
Adding C Libs
Browse files Browse the repository at this point in the history
  • Loading branch information
robbrad committed Nov 2, 2018
1 parent 525eebb commit 719a3a7
Show file tree
Hide file tree
Showing 6 changed files with 338 additions and 0 deletions.
Binary file added @eaDir/HP20x_dev.h@SynoEAStream
Binary file not shown.
Binary file added @eaDir/KalmanFilter.h@SynoEAStream
Binary file not shown.
49 changes: 49 additions & 0 deletions DHT.h
Original file line number Diff line number Diff line change
@@ -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
101 changes: 101 additions & 0 deletions HP20x_dev.h
Original file line number Diff line number Diff line change
@@ -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 <Wire.h>
#include <Arduino.h>
/****************************************************************************/
/*** 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
37 changes: 37 additions & 0 deletions KalmanFilter.h
Original file line number Diff line number Diff line change
@@ -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 <Arduino.h>
#include <inttypes.h>
/****************************************************************************/
/*** 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
151 changes: 151 additions & 0 deletions MutichannelGasSensor.h
Original file line number Diff line number Diff line change
@@ -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
*********************************************************************************************************/

0 comments on commit 719a3a7

Please sign in to comment.