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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "hih61xx.h"
#include "deftypes.h"
extern uint16 devicef;
extern void bus_err(int ern);
extern void print_help(void);
unsigned char buf[5];
void hih61xx_print_all(void)
{
printf(
"00: Relative Humidity [%%] (reg: 0x00)\n"
"01: Temperature [°C] (reg: 0x00)\n"
"02: Status [hex] (reg: 0x00)\n"
"\n");
}
void hih61xx_conf_set(const uchar *opts) {}
void hih61xx_get_data(void) //# Get measurement data from device
{
buf[0] = 0x00;
if(write(devicef, buf, 1) != 1){ //# Start the measurement
bus_err(errno);
}
usleep(60*1000); //# Wait 60ms for measurement
if(read(devicef, buf, 4) != 4){ //# Read the data
bus_err(errno);
}
}
unsigned char hih61xx_read_status(void){ //# Get device status
return ((buf[0] & 0xc0) >> 6); //# He ???
}
float hih61xx_calc_humidity(float offset){ //# Measure
unsigned int raw = 0;
float humidity;
if(hih61xx_read_status() != 0x01 )
{
raw = ((buf[0] & 0x3f) << 8) + buf[1]; // two msb status, 14bit data
humidity = ((float)raw/(16384 - 2))*100; // humidity = (14bit data / (2^14 -2)) * 100%
}
else
{
humidity = -1.0 ;
}
return (humidity + offset) ;
}
float hih61xx_read_temp(float offset)
{
unsigned int raw = 0;
float temp;
if(hih61xx_read_status() != 0x01 )
{
raw = ((buf[2] << 8) + buf[1]) >> 2; // 14bit temp data
temp = ((float)raw/(16384 - 2))*165 - 40 ; // temp = ((14bit data / (2^14 -2))*165) - 40 [°C]
}
else
{
temp = -40.0 ;
}
return (temp + offset);
}
void hih61xx_read_all(const uchar *opts)
{
hih61xx_get_data();
if(opts != NULL)
{
char *ptr;
printf("00:%f\n", hih61xx_calc_humidity(strtof((char*)opts, &ptr)));
ptr++; // One (,) character allowed for delimiter
printf("01:%f\n", hih61xx_read_temp(strtof(ptr, NULL)));
}
else
{
printf("00:%f\n", hih61xx_calc_humidity(0.0));
printf("01:%f\n", hih61xx_read_temp(0.0));
}
printf("02:0x%x\n", hih61xx_read_status());
}
void hih61xx_read_one(const uchar *opts)
{
int id;
char *ptr;
hih61xx_get_data();
id = strtol ((char*)opts,&ptr,0); //all format allowed
ptr++; //one separator allowed
switch (id)
{
case 0x00:
printf("00:%f\n", hih61xx_calc_humidity(strtof(ptr, NULL)));
break;
case 0x01:
printf("01:%f\n", hih61xx_read_temp(strtof(ptr, NULL)));
break;
case 0x02:
printf("02:0x%x\n", hih61xx_read_status());
break;
default:
print_help();
}
}
|