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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h> //# isdigit()
#include "hih61xx.h"
#include "deftypes.h"
extern uint16 devicef;
extern void bus_err(int ern);
extern void print_help(void);
extern uchar *xchg_data (uchar *buf, uint8 wrlen, uint8 waitlen, uint8 rdlen);
unsigned char buf[32];
void hih61xx_print_all(void)
{
printf(
"============ HiH61xx interface ===========\n"
"Registers:\n"
" 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) {} //Maybe later
// 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);
// }
// }
// uchar *xchg_data (uchar *buf, uint8 wrlen, uint8 waitlen, uint8 rdlen){ //# [buf] = 32 byte
// if(write(devicef, buf, wrlen) != wrlen){ //# write one byte to device
// bus_err(errno);
// }
// usleep(waitlen*1000); //# Wait 10ms for reading
// if(read(devicef, buf, rdlen) != rdlen) { //# read the result
// bus_err(errno);
// }
// return buf;
// }
// unsigned char hih61xx_read_status(void){ //# Get device status
static uchar calc_status(uchar *buf){
return ((buf[0] & 0xc0) >> 6); //# He ???
}
//float hih61xx_calc_humidity(float offset){ //# Measure
static float calc_humidity(uchar *buf, float offset){ //# Measure
uint16 raw = 0;
float humidity;
if(calc_status(buf) != 0x01 )
{ //# Data format: |s1,s0,h13,h12|h11,h10,h9,h8|_|h7,h6,h5,h4|h3,h2,h1,h0|_|t13,t12,t11,t10|t9,t8,t7,t6|_|t5,t4,t3,t2|t1,t0,x,x|
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)
static float calc_temp(uchar *buf, float offset)
{
uint16 raw = 0;
float temp;
if(calc_status(buf) != 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();
buf[0] = 0x00;
xchg_data(buf,1,60,4);
// if(opts != NULL)
// {
// char *ptr;
// printf("00:%f\n", calc_humidity(buf, 0.0));
// ptr++; // One (,) character allowed for delimiter
// printf("01:%f\n", calc_temp(buf, 0.0));
// } else {
printf("00:%f\n", calc_humidity(buf, 0.0));
printf("01:%f\n", calc_temp(buf, 0.0));
// }
printf("02:0x%x\n", calc_status(buf));
}
void hih61xx_read_one(const uchar *opts)
{
uint16 i,id;
uchar temp[256];
buf[0] = 0x00;
xchg_data(buf,1,60,4);
if(opts != NULL){
for(i = 0; i < strlen((char*)opts); i++ ){
if (*(opts+i) == ','){ //# .... conf_set 0x49 thys,-10.5
break;
}else{
if( !isdigit(*(opts+i)) ){ //# Check the register string
fprintf(stderr, "The Register address must be an integer!\n");
print_help();
hih61xx_print_all();
exit (EXIT_FAILURE);
}
temp[i] = *(opts+i);
temp[i+1] = '\0';
}
}
id = atoi((char*)temp); //# Convert register to number
strncpy((char*)temp, (char*)opts+i+1, 255); //# Copy remain to temp
// id = strtol ((char*)opts,&ptr,0); //all format allowed
// ptr++; //one separator allowed
switch (id)
{
case 0x00:
printf("%f\n", calc_humidity(buf, atof((char*)temp)) );
break;
case 0x01:
printf("%f\n", calc_temp(buf, atof((char*)temp)) );
break;
case 0x02:
printf("0x%x\n", calc_status(buf));
break;
default:
print_help();
hih61xx_print_all();
exit(EXIT_FAILURE);
}
}
}
|