-
Notifications
You must be signed in to change notification settings - Fork 2
/
testHum.c
79 lines (57 loc) · 1.46 KB
/
testHum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
long getinstanttime()
{
struct timeval tv;
gettimeofday(&tv,NULL);
return((tv.tv_sec*1000000) + tv.tv_usec);
}
typedef uint16_t uint16;
typedef int16_t qint16;
typedef uint8_t uint8;
/* Conversion algorithm, temperature */
double calcHumTmp(uint16 rawT)
{
double v;
//-- calculate temperature [deg C] --
v = -46.85 + 175.72/65536 *(double)(qint16)rawT;
return v;
}
/* Conversion algorithm, humidity */
double calcHumRel(uint16 rawH)
{
double v;
rawH &= ~0x0003; // clear bits [1..0] (status bits)
//-- calculate relative humidity [%RH] --
v = -6.0 + 125.0/65536 * (double)rawH; // RH= -6 + 125 * SRH/2^16
return v;
}
#define BUILD_UINT16(loByte, hiByte) ((uint16)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)))
int main(int argc, char **argv) {
uint16 hexTemp;
uint16 hexHum;
uint8 pData[4];
int inp, i;
FILE *fp;
char filename[200];
filename[0] = 0;
strcat(filename, "/home/optimus-prime/DR-SensorTag-v2/hum_");
strcat(filename, argv[argc-1]);
fp = fopen(filename, "a+");
int j=0;
for(i=2;i<6;i++,j++) {
sscanf(argv[i], "%x", &inp);
pData[j] = (uint8)inp;
}
hexTemp = BUILD_UINT16(pData[0], pData[1]);
hexHum = BUILD_UINT16(pData[2], pData[3]);
fprintf(fp, "%s , ", argv[1]);
fprintf(fp, "%3.2f , ", calcHumTmp(hexTemp));
fprintf(fp, "%3.2f \n", calcHumRel(hexHum));
fclose(fp);
}