aboutsummaryrefslogtreecommitdiffstats
path: root/src/hih61xx.c
blob: b68b653f9fcbd58537c89b0b3dd4a4f1fb1f0d4d (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>


#include "hih61xx.h"

extern int file;
extern void bus_err(int ern);
extern void print_help(void);
unsigned char buf[5];




void hih61xx_list_all(void)
{
    printf(
        "00: Relative Humidity [%%]     (reg: 0x00)\n"
        "01: Temperature       [°C]     (reg: 0x00)\n"
        "02: Status            [hex]    (reg: 0x00)\n"
        "\n");
}



void hih61xx_get_data(void)
{
    buf[0] = 0x00;
    
    if(write(file, buf, 1) != 1)    //Start measure
    {
        bus_err(errno);
    }

    usleep(60*1000);  // Wait 60ms for measurement
    
    if(read(file, buf, 4) != 4)  //Read the data
    {
        bus_err(errno);
    } 
}



unsigned char hih61xx_read_status(void)
{
    return ((buf[0] & 0xc0) >> 6);
}



float hih61xx_read_humidity(float offset)
{
    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 char *opts)
{
    hih61xx_get_data();
    
    if(opts != NULL)
    {
        char *ptr;
        printf("00:%f\n", hih61xx_read_humidity(strtof(opts, &ptr)));
        
        ptr++;               // One (,) character allowed for delimiter
        
        printf("01:%f\n", hih61xx_read_temp(strtof(ptr, NULL)));
    }     
    else
    {
        printf("00:%f\n", hih61xx_read_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 char *opts)
{
    int id;
    char *ptr;
 
    hih61xx_get_data();
    id = strtol (opts,&ptr,0); //all format allowed
    ptr++;      //one separator allowed
        
    switch (id)
    {
        case 0x00:
            printf("00:%f\n", hih61xx_read_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();
    } 
}