Skip to content

Commit

Permalink
mikroSDK library for Air Quality 5 click
Browse files Browse the repository at this point in the history
  • Loading branch information
KacaPerendic committed Dec 21, 2018
1 parent 3eff54b commit 33a7acd
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 242 deletions.
209 changes: 1 addition & 208 deletions library/__airq5_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ const uint16_t _AIRQ5_CONFIG_COMP_QUE_4CONV = 0x0002;
const uint16_t _AIRQ5_CONFIG_COMP_QUE_0CONV = 0x0003;

/* ---------------------------------------------------------------- VARIABLES */
#define _DBL_MAX_ 3.40282347e+38
#define _FRNDINT(x) ((double)(long)(x))

#ifdef __AIRQ5_DRV_I2C__
static uint8_t _slaveAddress;
Expand All @@ -83,214 +81,9 @@ static uint16_t _dataConfig = 0x8583;

/* -------------------------------------------- PRIVATE FUNCTION DECLARATIONS */

static double _floor(double x);
static double _frexp(double value, int * eptr);
static double _ldexp(double value, int newexp);
static double _eval_poly(double x, const double code * d, int n);
static double _exp(double x);
static double _log(double x);
static double _myPow(double x, double y);

/* --------------------------------------------- PRIVATE FUNCTION DEFINITIONS */

#if defined __MIKROC_PRO_FOR_ARM__ || __MIKROC_PRO_FOR_PIC32__ || __MIKROC_PRO_FOR_DSPIC__ || __MIKROC_PRO_FOR_AVR__ || __MIKROC_PRO_FOR_FT90x__
static union both
{
struct flt
{
unsigned char mant[2];
unsigned hmant:7;
unsigned exp:8;
unsigned sign:1;

} flt;
double fl;
};

//--------------
static double _frexp(double value, int * eptr) {
union both uv;
volatile int bb;

uv.fl = value;
bb = uv.flt.exp - 126;
*eptr = bb;
uv.flt.exp = 126;
return uv.fl;

}

//--------------
static double _ldexp(double value, int newexp) {
union both uv;

uv.fl = value;
newexp += uv.flt.exp;
if (newexp < 0)
return 0.0;
else
if (newexp > 255)
if (value < 0.0)
return -_DBL_MAX_;
else
return _DBL_MAX_;
else
uv.flt.exp = newexp;
return uv.fl;

}
#endif

#if defined __MIKROC_PRO_FOR_PIC__
static double _frexp(double value, int * eptr)
{
char * pom;
pom = &value;
*eptr = pom[3] - 126;
pom[3] = 126;
return value;
}

static double _ldexp(double value, int newexp)
{
char * pom;
pom = &value;
newexp += pom[3];
if (newexp < 0)
return 0.0;
else if (newexp > 255)
if (value < 0.0)
return -_DBL_MAX_;
else
return _DBL_MAX_;
else
pom[3] = newexp;
return value;
}
#endif


static double _floor(double x) {
double i;
int expon;

expon = ((*(unsigned long *)&x >> 23) & 255);
expon = expon- 127;
if(expon < 0)
if (x < 0.0)
return -1.0;
else
return 0.0;
if((unsigned)expon > sizeof(double) * 8 - 8)
return x; /* already an integer */
i = _FRNDINT(x);

if(i > x)
return i - 1.0;
return i;
}


static double _eval_poly(double x, const double code * d, int n) {
double res;

res = d[n];
while(n)
res = x * res + d[--n];

return res;
}

static double _exp(double x) {
int exp;
char sign;

const static double coeff[] = {
1.0000000000e+00,
6.9314718056e-01,
2.4022650695e-01,
5.5504108945e-02,
9.6181261779e-03,
1.3333710529e-03,
1.5399104432e-04,
1.5327675257e-05,
1.2485143336e-06,
1.3908092221e-07,
};

if(x == 0.0)
return 1.0;
if (x > 89.416) //too big?
return _DBL_MAX_;
if (x < -87.33655) //too small?
return 0.0;
sign = x < 0.0;
if(sign)
x = -x;
x *= 1.4426950409; // convert to log2 //
exp = (int)_floor(x);
x -= (double)exp;
x = _ldexp(_eval_poly(x, coeff, sizeof coeff/sizeof coeff[0] - 1), exp);
if(sign) {
if (x == _DBL_MAX_)
return 0.0;
return 1.0/x;
}
return x;
}


//-------------- Returns natural logarithm of given argument - ln(x)
static double _log(double x) {
int exp;
static const double coeff[] = {
0.0000000001, // a0 //
0.9999964239, // a1 //
-0.4998741238, // a2 //
0.3317990258, // a3 //
-0.2407338084, // a4 //
0.1676540711, // a5 //
-0.0953293897, // a6 //
0.0360884937, // a7 //
-0.0064535442, // a8 //
};

// zero or -ve arguments are not defined //

if(x <= 0.0)
return 0.0;
x = _frexp(x, &exp) * 2.0 - 1.0;
exp--;
x = _eval_poly(x, coeff, sizeof coeff/sizeof coeff[0] - 1);
return x + 0.69314718055995 * exp;
//return x + 0.68768932874451 * exp;
}

//-------------- Returns argument x raised to power of argument y
static double _myPow(double x, double y) {
unsigned char sign = 0; // Promenjeno unsigned int u
// unsigned char (optimizacija) MZ 11.08.2008.
long yi;

if(y == 0.0)
return 1.0;
if(x == 0.0)
return 0.0;
if(x < 0.0) {
yi = (long)y;
if((double)yi != y)
return 0.0;
sign = yi & 1;
x = -x;
}
x = _log(x);
x = x*y;
x = _exp(x);

if(sign)
return -x;
return x;
}
/* --------------------------------------------- PRIVATE FUNCTION DEFINITIONS */



Expand Down
34 changes: 0 additions & 34 deletions library/__airq5_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,40 +192,6 @@ uint16_t airq5_readSensorData(uint16_t channel_data);
*/
uint8_t airq5_getInterrupt();

/**
* @brief Functions for calculation NO2 sensor data
*
* @param[in] NO2_Data NO2 data which be calculation
* @retval NO2 data in ppm
*/
float airq5_getNO2Data(uint16_t NO2_data);

/**
* @brief Functions for calculation CO sensor data
*
* @param[in] CO_Data CO data which be calculation
* @param[out] dataBuffer Buffer in which data is stored
*
* Data Buffer:
- dataBuffer[0] - CO in ppm
- dataBuffer[1] - CH4 in ppm
- dataBuffer[2] - H2 in ppm
- dataBuffer[3] - C2H5OH in ppm
*/
void airq5_getCOData(uint16_t CO_Data, float *dataBuffer);

/**
* @brief Functions for calculation NH3 sensor data
*
* @param[in] NH3_Data NH3 data which be calculation
* @param[out] dataBuffer Buffer in which data is stored
*
* Data Buffer:
- dataBuffer[0] - NH3 in ppm
- dataBuffer[1] - C3H8 in ppm
- dataBuffer[2] - C4H10 in ppm
*/
void airq5_getNH3Data(uint16_t NH3_Data, float *dataBuffer);



Expand Down

0 comments on commit 33a7acd

Please sign in to comment.