-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
sht2x_driver.go
259 lines (207 loc) · 6.6 KB
/
sht2x_driver.go
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright (c) 2016-2017 Weston Schmidt <weston_schmidt@alumni.purdue.edu>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package i2c
// SHT2xDriver is a driver for the SHT2x based devices.
//
// This module was tested with Sensirion SHT21 Breakout.
import (
"errors"
"time"
"github.com/sigurn/crc8"
"gobot.io/x/gobot"
)
const (
// SHT2xDefaultAddress is the default I2C address for SHT2x
SHT2xDefaultAddress = 0x40
// SHT2xAccuracyLow is the faster, but lower accuracy sample setting
// 0/1 = 8bit RH, 12bit Temp
SHT2xAccuracyLow = byte(0x01)
// SHT2xAccuracyMedium is the medium accuracy and speed sample setting
// 1/0 = 10bit RH, 13bit Temp
SHT2xAccuracyMedium = byte(0x80)
// SHT2xAccuracyHigh is the high accuracy and slowest sample setting
// 0/0 = 12bit RH, 14bit Temp
// Power on default is 0/0
SHT2xAccuracyHigh = byte(0x00)
// SHT2xTriggerTempMeasureHold is the command for measureing temperature in hold master mode
SHT2xTriggerTempMeasureHold = 0xe3
// SHT2xTriggerHumdMeasureHold is the command for measureing humidity in hold master mode
SHT2xTriggerHumdMeasureHold = 0xe5
// SHT2xTriggerTempMeasureNohold is the command for measureing humidity in no hold master mode
SHT2xTriggerTempMeasureNohold = 0xf3
// SHT2xTriggerHumdMeasureNohold is the command for measureing humidity in no hold master mode
SHT2xTriggerHumdMeasureNohold = 0xf5
// SHT2xWriteUserReg is the command for writing user register
SHT2xWriteUserReg = 0xe6
// SHT2xReadUserReg is the command for reading user register
SHT2xReadUserReg = 0xe7
// SHT2xReadUserReg is the command for reading user register
SHT2xSoftReset = 0xfe
)
// SHT2xDriver is a Driver for a SHT2x humidity and temperature sensor
type SHT2xDriver struct {
Units string
name string
connector Connector
connection Connection
Config
sht2xAddress int
accuracy byte
delay time.Duration
crcTable *crc8.Table
}
// NewSHT2xDriver creates a new driver with specified i2c interface
// Params:
// conn Connector - the Adaptor to use with this Driver
//
// Optional params:
// i2c.WithBus(int): bus to use with this driver
// i2c.WithAddress(int): address to use with this driver
//
func NewSHT2xDriver(a Connector, options ...func(Config)) *SHT2xDriver {
// From the document "CRC Checksum Calculation -- For Safe Communication with SHT2x Sensors":
crc8Params := crc8.Params{0x31, 0x00, false, false, 0x00, 0x00, "CRC-8/SENSIRION-SHT2x"}
s := &SHT2xDriver{
Units: "C",
name: gobot.DefaultName("SHT2x"),
connector: a,
Config: NewConfig(),
crcTable: crc8.MakeTable(crc8Params),
}
for _, option := range options {
option(s)
}
return s
}
// Name returns the name for this Driver
func (d *SHT2xDriver) Name() string { return d.name }
// SetName sets the name for this Driver
func (d *SHT2xDriver) SetName(n string) { d.name = n }
// Connection returns the connection for this Driver
func (d *SHT2xDriver) Connection() gobot.Connection { return d.connector.(gobot.Connection) }
// Start initializes the SHT2x
func (d *SHT2xDriver) Start() (err error) {
bus := d.GetBusOrDefault(d.connector.GetDefaultBus())
address := d.GetAddressOrDefault(SHT2xDefaultAddress)
if d.connection, err = d.connector.GetConnection(address, bus); err != nil {
return
}
if err = d.Reset(); err != nil {
return
}
d.sendAccuracy()
return
}
// Halt returns true if devices is halted successfully
func (d *SHT2xDriver) Halt() (err error) { return }
func (d *SHT2xDriver) Accuracy() byte { return d.accuracy }
// SetAccuracy sets the accuracy of the sampling
func (d *SHT2xDriver) SetAccuracy(acc byte) (err error) {
d.accuracy = acc
if d.connection != nil {
err = d.sendAccuracy()
}
return
}
// Reset does a software reset of the device
func (d *SHT2xDriver) Reset() (err error) {
if err = d.connection.WriteByte(SHT2xSoftReset); err != nil {
return
}
time.Sleep(15 * time.Millisecond) // 15ms delay (from the datasheet 5.5)
return
}
// Temperature returns the current temperature, in celsius degrees.
func (d *SHT2xDriver) Temperature() (temp float32, err error) {
var rawT uint16
if rawT, err = d.readSensor(SHT2xTriggerTempMeasureNohold); err != nil {
return
}
// From the datasheet 6.2:
// T[C] = -46.85 + 175.72 * St / 2^16
temp = -46.85 + 175.72/65536.0*float32(rawT)
return
}
// Humidity returns the current humidity in percentage of relative humidity
func (d *SHT2xDriver) Humidity() (humidity float32, err error) {
var rawH uint16
if rawH, err = d.readSensor(SHT2xTriggerHumdMeasureNohold); err != nil {
return
}
// From the datasheet 6.1:
// RH = -6 + 125 * Srh / 2^16
humidity = -6.0 + 125.0/65536.0*float32(rawH)
return
}
// sendCommandDelayGetResponse is a helper function to reduce duplicated code
func (d *SHT2xDriver) readSensor(cmd byte) (read uint16, err error) {
if err = d.connection.WriteByte(cmd); err != nil {
return
}
//Hang out while measurement is taken. 85ms max, page 9 of datasheet.
time.Sleep(85 * time.Millisecond)
//Comes back in three bytes, data(MSB) / data(LSB) / Checksum
buf := make([]byte, 3)
counter := 0
for {
var got int
got, err = d.connection.Read(buf)
counter++
if counter > 50 {
return
}
if err == nil {
if got != 3 {
err = ErrNotEnoughBytes
return
}
break
}
time.Sleep(1 * time.Millisecond)
}
//Store the result
crc := crc8.Checksum(buf[0:2], d.crcTable)
if buf[2] != crc {
err = errors.New("Invalid crc")
return
}
read = uint16(buf[0])<<8 | uint16(buf[1])
read &= 0xfffc // clear two low bits (status bits)
return
}
func (d *SHT2xDriver) sendAccuracy() (err error) {
if err = d.connection.WriteByte(SHT2xReadUserReg); err != nil {
return
}
userRegister, err := d.connection.ReadByte()
if err != nil {
return
}
userRegister &= 0x7e //Turn off the resolution bits
acc := d.accuracy
acc &= 0x81 //Turn off all other bits but resolution bits
userRegister |= acc //Mask in the requested resolution bits
//Request a write to user register
_, err = d.connection.Write([]byte{SHT2xWriteUserReg, userRegister})
if err != nil {
return
}
userRegister, err = d.connection.ReadByte()
if err != nil {
return
}
return
}