| 1 |
//-------------------------------------------------------------------------------------------------- |
| 2 |
// |
| 3 |
// Copyright (c) 2018 Denis Dyakov |
| 4 |
// |
| 5 |
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
| 6 |
// associated documentation files (the "Software"), to deal in the Software without restriction, |
| 7 |
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
| 9 |
// subject to the following conditions: |
| 10 |
// |
| 11 |
// The above copyright notice and this permission notice shall be included in all copies or substantial |
| 12 |
// portions of the Software. |
| 13 |
// |
| 14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| 15 |
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 16 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 17 |
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 |
// |
| 20 |
//-------------------------------------------------------------------------------------------------- |
| 21 |
|
| 22 |
package sht3x |
| 23 |
|
| 24 |
import ( |
| 25 |
"bytes" |
| 26 |
"context" |
| 27 |
"encoding/binary" |
| 28 |
"errors" |
| 29 |
"os" |
| 30 |
"reflect" |
| 31 |
"syscall" |
| 32 |
"time" |
| 33 |
|
| 34 |
i2c "github.com/d2r2/go-i2c" |
| 35 |
shell "github.com/d2r2/go-shell" |
| 36 |
"github.com/davecgh/go-spew/spew" |
| 37 |
) |
| 38 |
|
| 39 |
// Command byte's sequences |
| 40 |
var ( |
| 41 |
// Measure values in "single shot mode". |
| 42 |
CMD_SINGLE_MEASURE_HIGH_CSE = []byte{0x2C, 0x06} // Single Measure of Temp. and Hum.; High precise; Clock stretching enabled |
| 43 |
CMD_SINGLE_MEASURE_MEDIUM_CSE = []byte{0x2C, 0x0D} // Single Measure of Temp. and Hum.; Medium precise; Clocl stretching enabled |
| 44 |
CMD_SINGLE_MEASURE_LOW_CSE = []byte{0x2C, 0x10} // Single Measure of Temp. and Hum.; Low precise; Clock stretching enabled |
| 45 |
CMD_SINGLE_MEASURE_HIGH = []byte{0x24, 0x00} // Single Measure of Temp. and Hum.; High precise |
| 46 |
CMD_SINGLE_MEASURE_MEDIUM = []byte{0x24, 0x0B} // Single Measure of Temp. and Hum.; Medium precise |
| 47 |
CMD_SINGLE_MEASURE_LOW = []byte{0x24, 0x16} // Single Measure of Temp. and Hum.; Low precise |
| 48 |
|
| 49 |
// Measure values in "periodic acquisition mode". |
| 50 |
CMD_PERIOD_MEASURE_05MPS_HIGH = []byte{0x20, 0x32} // Periodic Measure of Temp. and Hum.; 0.5 Measurements Per Second; High precise |
| 51 |
CMD_PERIOD_MEASURE_05MPS_MEDIUM = []byte{0x20, 0x24} // Periodic Measure of Temp. and Hum.; 0.5 Measurements Per Second; Medium precise |
| 52 |
CMD_PERIOD_MEASURE_05MPS_LOW = []byte{0x20, 0x2F} // Periodic Measure of Temp. and Hum.; 0.5 Measurements Per Second; Low precise |
| 53 |
CMD_PERIOD_MEASURE_1MPS_HIGH = []byte{0x21, 0x30} // Periodic Measure of Temp. and Hum.; 1 Measurements Per Second; High precise |
| 54 |
CMD_PERIOD_MEASURE_1MPS_MEDIUM = []byte{0x21, 0x26} // Periodic Measure of Temp. and Hum.; 1 Measurements Per Second; Medium precise |
| 55 |
CMD_PERIOD_MEASURE_1MPS_LOW = []byte{0x21, 0x2D} // Periodic Measure of Temp. and Hum.; 1 Measurements Per Second; Low precise |
| 56 |
CMD_PERIOD_MEASURE_2MPS_HIGH = []byte{0x22, 0x36} // Periodic Measure of Temp. and Hum.; 2 Measurements Per Second; High precise |
| 57 |
CMD_PERIOD_MEASURE_2MPS_MEDIUM = []byte{0x22, 0x20} // Periodic Measure of Temp. and Hum.; 2 Measurements Per Second; Medium precise |
| 58 |
CMD_PERIOD_MEASURE_2MPS_LOW = []byte{0x22, 0x2B} // Periodic Measure of Temp. and Hum.; 2 Measurements Per Second; Low precise |
| 59 |
CMD_PERIOD_MEASURE_4MPS_HIGH = []byte{0x23, 0x34} // Periodic Measure of Temp. and Hum.; 4 Measurements Per Second; High precise |
| 60 |
CMD_PERIOD_MEASURE_4MPS_MEDIUM = []byte{0x23, 0x22} // Periodic Measure of Temp. and Hum.; 4 Measurements Per Second; Medium precise |
| 61 |
CMD_PERIOD_MEASURE_4MPS_LOW = []byte{0x23, 0x29} // Periodic Measure of Temp. and Hum.; 4 Measurements Per Second; Low precise |
| 62 |
CMD_PERIOD_MEASURE_10MPS_HIGH = []byte{0x27, 0x37} // Periodic Measure of Temp. and Hum.; 10 Measurements Per Second; High precise |
| 63 |
CMD_PERIOD_MEASURE_10MPS_MEDIUM = []byte{0x27, 0x21} // Periodic Measure of Temp. and Hum.; 10 Measurements Per Second; Medium precise |
| 64 |
CMD_PERIOD_MEASURE_10MPS_LOW = []byte{0x27, 0x2A} // Periodic Measure of Temp. and Hum.; 10 Measurements Per Second; Low precise |
| 65 |
|
| 66 |
// Alert management commands. |
| 67 |
// Works in conjunction with "periodic acquisition mode". |
| 68 |
// Equation must be respected: HIGH SET > HIGH CLEAR > LOW CLEAR > LOW SET. |
| 69 |
CMD_ALERT_READ_HIGH_SET = []byte{0xE1, 0x1F} // Read high defined level, exceeding which alert is triggered ON |
| 70 |
CMD_ALERT_READ_HIGH_CLEAR = []byte{0xE1, 0x14} // Read high defined level, falling behind which alert is triggering OFF |
| 71 |
CMD_ALERT_READ_LOW_CLEAR = []byte{0xE1, 0x09} // Read low defined level, exceeding which alert is triggered OFF |
| 72 |
CMD_ALERT_READ_LOW_SET = []byte{0xE1, 0x02} // Read low defined level, falling behind which alert is triggering ON |
| 73 |
CMD_ALERT_WRITE_HIGH_SET = []byte{0x61, 0x1D} // Write high level, exceeding which alert is triggered ON |
| 74 |
CMD_ALERT_WRITE_HIGH_CLEAR = []byte{0x61, 0x16} // Write high level, falling behind which alert is triggering OFF |
| 75 |
CMD_ALERT_WRITE_LOW_CLEAR = []byte{0x61, 0x0B} // Write low level, exceeding which alert is triggered OFF |
| 76 |
CMD_ALERT_WRITE_LOW_SET = []byte{0x61, 0x00} // Write low level, falling behind which alert is triggering ON |
| 77 |
|
| 78 |
// Heater management commands. |
| 79 |
CMD_ENABLE_HEATER = []byte{0x30, 0x6D} // Switch heater on |
| 80 |
CMD_DISABLE_HEATER = []byte{0x30, 0x66} // Switch heater off |
| 81 |
|
| 82 |
// Status register commands. |
| 83 |
CMD_READ_STATUS_REG = []byte{0xF3, 0x2D} // Read status register |
| 84 |
CMD_CLEAR_STATUS_REG = []byte{0x30, 0x41} // Clear status register |
| 85 |
|
| 86 |
// Other commands. |
| 87 |
CMD_PERIOD_FETCH = []byte{0xE0, 0x00} // Read data after being measured by periodic acquisition mode command |
| 88 |
CMD_ART = []byte{0x2B, 0x32} // Activate "accelerated response time" |
| 89 |
CMD_BREAK = []byte{0x30, 0x93} // Interrupt "periodic acqusition mode" and return to "single shot mode" |
| 90 |
CMD_RESET = []byte{0x30, 0xA2} // Soft reset command |
| 91 |
) |
| 92 |
|
| 93 |
// MeasureRepeatability used to define measure precision. |
| 94 |
type MeasureRepeatability int |
| 95 |
|
| 96 |
const ( |
| 97 |
RepeatabilityLow MeasureRepeatability = iota + 1 // Low precision |
| 98 |
RepeatabilityMedium // Medium precision |
| 99 |
RepeatabilityHigh // High precision |
| 100 |
) |
| 101 |
|
| 102 |
// String define stringer interface. |
| 103 |
func (v MeasureRepeatability) String() string { |
| 104 |
switch v { |
| 105 |
case RepeatabilityLow: |
| 106 |
return "Measure Repeatability Low" |
| 107 |
case RepeatabilityMedium: |
| 108 |
return "Measure Repeatability Medium" |
| 109 |
case RepeatabilityHigh: |
| 110 |
return "Measure Repeatability High" |
| 111 |
default: |
| 112 |
return "<unknown>" |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
// GetMeasureTime define how long to wait for the measure process |
| 117 |
// to complete according to specification. |
| 118 |
func (v MeasureRepeatability) GetMeasureTime() time.Duration { |
| 119 |
switch v { |
| 120 |
case RepeatabilityLow: |
| 121 |
return 4500 * time.Microsecond |
| 122 |
case RepeatabilityMedium: |
| 123 |
return 6500 * time.Microsecond |
| 124 |
case RepeatabilityHigh: |
| 125 |
return 15500 * time.Microsecond |
| 126 |
default: |
| 127 |
return 0 |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
// StatusRegFlag determine sensor states. |
| 132 |
// It shows various sensor pending events and returns heater status. |
| 133 |
type StatusRegFlag uint16 |
| 134 |
|
| 135 |
const ( |
| 136 |
ALERT_PENDING StatusRegFlag = 0x8000 |
| 137 |
HEATER_ENABLED StatusRegFlag = 0x2000 |
| 138 |
HUMIDITY_ALERT StatusRegFlag = 0x0800 |
| 139 |
TEMPERATURE_ALERT StatusRegFlag = 0x0400 |
| 140 |
RESET_DETECTED StatusRegFlag = 0x0010 |
| 141 |
COMMAND_FAILED StatusRegFlag = 0x0002 |
| 142 |
WRITE_DATA_CRC_FAILED StatusRegFlag = 0x0001 |
| 143 |
) |
| 144 |
|
| 145 |
// String define stringer interface. |
| 146 |
func (v StatusRegFlag) String() string { |
| 147 |
const divider = " | " |
| 148 |
var buf bytes.Buffer |
| 149 |
if v&ALERT_PENDING != 0 { |
| 150 |
buf.WriteString("ALERT_PENDING" + divider) |
| 151 |
} |
| 152 |
if v&HEATER_ENABLED != 0 { |
| 153 |
buf.WriteString("HEATER_ENABLED" + divider) |
| 154 |
} |
| 155 |
if v&HUMIDITY_ALERT != 0 { |
| 156 |
buf.WriteString("HUMIDITY_ALERT" + divider) |
| 157 |
} |
| 158 |
if v&TEMPERATURE_ALERT != 0 { |
| 159 |
buf.WriteString("TEMPERATURE_ALERT" + divider) |
| 160 |
} |
| 161 |
if v&RESET_DETECTED != 0 { |
| 162 |
buf.WriteString("RESET_DETECTED" + divider) |
| 163 |
} |
| 164 |
if v&COMMAND_FAILED != 0 { |
| 165 |
buf.WriteString("COMMAND_FAILED" + divider) |
| 166 |
} |
| 167 |
if v&WRITE_DATA_CRC_FAILED != 0 { |
| 168 |
buf.WriteString("WRITE_DATA_CRC_FAILED" + divider) |
| 169 |
} |
| 170 |
if buf.Len() > 0 { |
| 171 |
buf.Truncate(buf.Len() - len(divider)) |
| 172 |
} |
| 173 |
return buf.String() |
| 174 |
} |
| 175 |
|
| 176 |
// PeriodicMeasure identify pause between subsequent measures |
| 177 |
// in "periodic data acquisition" mode. |
| 178 |
type PeriodicMeasure int |
| 179 |
|
| 180 |
const ( |
| 181 |
PeriodicHalfMPS PeriodicMeasure = iota + 1 // 1 measurement per each 2 seconds |
| 182 |
Periodic1MPS // 1 measurement per second |
| 183 |
Periodic2MPS // 2 measurements per second |
| 184 |
Periodic4MPS // 4 measurements per second |
| 185 |
Periodic10MPS // 10 measurements per second |
| 186 |
) |
| 187 |
|
| 188 |
// String define stringer interface. |
| 189 |
func (v PeriodicMeasure) String() string { |
| 190 |
switch v { |
| 191 |
case PeriodicHalfMPS: |
| 192 |
return "Periodic Measurement 0.5 MPS" |
| 193 |
case Periodic1MPS: |
| 194 |
return "Periodic Measurement 1 MPS" |
| 195 |
case Periodic2MPS: |
| 196 |
return "Periodic Measurement 2 MPS" |
| 197 |
case Periodic4MPS: |
| 198 |
return "Periodic Measurement 4 MPS" |
| 199 |
case Periodic10MPS: |
| 200 |
return "Periodic Measurement 10 MPS" |
| 201 |
default: |
| 202 |
return "<unknown>" |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// GetWaitDuration identify pause between measures depending on PeriodicMeasure value. |
| 207 |
func (v PeriodicMeasure) GetWaitDuration() time.Duration { |
| 208 |
var timeDur time.Duration |
| 209 |
switch v { |
| 210 |
case PeriodicHalfMPS: |
| 211 |
timeDur = time.Millisecond * 2000 |
| 212 |
case Periodic1MPS: |
| 213 |
timeDur = time.Millisecond * 1000 |
| 214 |
case Periodic2MPS: |
| 215 |
timeDur = time.Millisecond * 500 |
| 216 |
case Periodic4MPS: |
| 217 |
timeDur = time.Millisecond * 250 |
| 218 |
case Periodic10MPS: |
| 219 |
timeDur = time.Millisecond * 100 |
| 220 |
} |
| 221 |
return timeDur |
| 222 |
} |
| 223 |
|
| 224 |
// SHT3X is a sensor itself. |
| 225 |
type SHT3X struct { |
| 226 |
lastStatusReg *uint16 |
| 227 |
lastCmd []byte |
| 228 |
lastPeriodic PeriodicMeasure |
| 229 |
lastPrecision MeasureRepeatability |
| 230 |
} |
| 231 |
|
| 232 |
// NewSHT3X return new sensor instance. |
| 233 |
func NewSHT3X() *SHT3X { |
| 234 |
v := &SHT3X{} |
| 235 |
return v |
| 236 |
} |
| 237 |
|
| 238 |
// ReadStatusReg return status register flags. |
| 239 |
// You should use constants of type StatusRegFlag to distinguish |
| 240 |
// individual states received from sensor. |
| 241 |
func (v *SHT3X) ReadStatusReg(i2c *i2c.I2C) (uint16, error) { |
| 242 |
if v.lastStatusReg == nil { |
| 243 |
_, err := i2c.WriteBytes(CMD_READ_STATUS_REG) |
| 244 |
if err != nil { |
| 245 |
return 0, err |
| 246 |
} |
| 247 |
reg, err := v.readDataWithCRCCheck(i2c, 1) |
| 248 |
if err != nil { |
| 249 |
return 0, err |
| 250 |
} |
| 251 |
v.lastStatusReg = ®[0] |
| 252 |
} |
| 253 |
return *v.lastStatusReg, nil |
| 254 |
} |
| 255 |
|
| 256 |
// readDataWithCRCCheck read block of data which ordinary contain |
| 257 |
// uncompensated temperature and humidity values. |
| 258 |
func (v *SHT3X) readDataWithCRCCheck(i2c *i2c.I2C, blockCount int) ([]uint16, error) { |
| 259 |
const blockSize = 2 + 1 |
| 260 |
data := make([]struct { |
| 261 |
Data [2]byte |
| 262 |
CRC byte |
| 263 |
}, blockCount) |
| 264 |
|
| 265 |
err := readDataToStruct(i2c, blockSize*blockCount, binary.BigEndian, data) |
| 266 |
if err != nil { |
| 267 |
return nil, err |
| 268 |
} |
| 269 |
var results []uint16 |
| 270 |
for i := 0; i < blockCount; i++ { |
| 271 |
calcCRC := calcCRC_SHT3X(0xFF, data[i].Data[:2]) |
| 272 |
crc := data[i].CRC |
| 273 |
if calcCRC != crc { |
| 274 |
err := errors.New(spew.Sprintf( |
| 275 |
"CRCs doesn't match: CRC from sensor (0x%0X) != calculated CRC (0x%0X)", |
| 276 |
crc, calcCRC)) |
| 277 |
return nil, err |
| 278 |
} else { |
| 279 |
lg.Debugf("CRCs verified: CRC from sensor (0x%0X) = calculated CRC (0x%0X)", |
| 280 |
crc, calcCRC) |
| 281 |
} |
| 282 |
results = append(results, getU16BE(data[i].Data[:2])) |
| 283 |
|
| 284 |
} |
| 285 |
return results, nil |
| 286 |
} |
| 287 |
|
| 288 |
// Reset reboot a sensor. |
| 289 |
func (v *SHT3X) Reset(i2c *i2c.I2C) error { |
| 290 |
lg.Debug("Reset sensor...") |
| 291 |
cmd := CMD_RESET |
| 292 |
_, err := i2c.WriteBytes(cmd) |
| 293 |
if err != nil { |
| 294 |
return err |
| 295 |
} |
| 296 |
v.lastCmd = cmd |
| 297 |
// Power-up time from specification |
| 298 |
time.Sleep(time.Microsecond * 1500) |
| 299 |
return nil |
| 300 |
} |
| 301 |
|
| 302 |
// SetHeaterStatus enable or disable heater. |
| 303 |
func (v *SHT3X) SetHeaterStatus(i2c *i2c.I2C, enableHeater bool) error { |
| 304 |
lg.Debug("Setting heater on/off...") |
| 305 |
var cmd []byte |
| 306 |
if enableHeater { |
| 307 |
cmd = CMD_ENABLE_HEATER |
| 308 |
} else { |
| 309 |
cmd = CMD_DISABLE_HEATER |
| 310 |
} |
| 311 |
_, err := i2c.WriteBytes(cmd) |
| 312 |
if err != nil { |
| 313 |
return err |
| 314 |
} |
| 315 |
v.lastCmd = cmd |
| 316 |
// No conversion time defined in docs for this command, |
| 317 |
// but error thrown out, if no any pause provided. |
| 318 |
time.Sleep(time.Millisecond * 1) |
| 319 |
return nil |
| 320 |
} |
| 321 |
|
| 322 |
// GetHeaterStatus return heater status: enabled (true) or disabled (false). |
| 323 |
func (v *SHT3X) GetHeaterStatus(i2c *i2c.I2C) (bool, error) { |
| 324 |
lg.Debug("Getting heater status...") |
| 325 |
v.lastStatusReg = nil |
| 326 |
ur, err := v.ReadStatusReg(i2c) |
| 327 |
if err != nil { |
| 328 |
return false, err |
| 329 |
} |
| 330 |
return (StatusRegFlag)(ur)&HEATER_ENABLED != 0, nil |
| 331 |
} |
| 332 |
|
| 333 |
// GetAlertPendingStatus return alert pending status: found (true) or not (false). |
| 334 |
func (v *SHT3X) GetAlertPendingStatus(i2c *i2c.I2C) (bool, error) { |
| 335 |
lg.Debug("Getting alert pending status...") |
| 336 |
v.lastStatusReg = nil |
| 337 |
ur, err := v.ReadStatusReg(i2c) |
| 338 |
if err != nil { |
| 339 |
return false, err |
| 340 |
} |
| 341 |
return (StatusRegFlag)(ur)&ALERT_PENDING != 0, nil |
| 342 |
} |
| 343 |
|
| 344 |
// GetHumidityAlertStatus return humidity alert pending status: found (true) or not (false). |
| 345 |
func (v *SHT3X) GetHumidityAlertStatus(i2c *i2c.I2C) (bool, error) { |
| 346 |
lg.Debug("Getting humidity alert status...") |
| 347 |
v.lastStatusReg = nil |
| 348 |
ur, err := v.ReadStatusReg(i2c) |
| 349 |
if err != nil { |
| 350 |
return false, err |
| 351 |
} |
| 352 |
return (StatusRegFlag)(ur)&HUMIDITY_ALERT != 0, nil |
| 353 |
} |
| 354 |
|
| 355 |
// GetTemperatureAlertStatus return humidity alert pending status: found (true) or not (false). |
| 356 |
func (v *SHT3X) GetTemperatureAlertStatus(i2c *i2c.I2C) (bool, error) { |
| 357 |
lg.Debug("Getting temperature alert status...") |
| 358 |
v.lastStatusReg = nil |
| 359 |
ur, err := v.ReadStatusReg(i2c) |
| 360 |
if err != nil { |
| 361 |
return false, err |
| 362 |
} |
| 363 |
return (StatusRegFlag)(ur)&TEMPERATURE_ALERT != 0, nil |
| 364 |
} |
| 365 |
|
| 366 |
// CheckResetDetected return system reset detected : found (true) or not (false). |
| 367 |
func (v *SHT3X) CheckResetDetected(i2c *i2c.I2C) (bool, error) { |
| 368 |
lg.Debug("Checking system reset status...") |
| 369 |
v.lastStatusReg = nil |
| 370 |
ur, err := v.ReadStatusReg(i2c) |
| 371 |
if err != nil { |
| 372 |
return false, err |
| 373 |
} |
| 374 |
return (StatusRegFlag)(ur)&RESET_DETECTED != 0, nil |
| 375 |
} |
| 376 |
|
| 377 |
// CheckCommandFailed return last command status: failed (true) or not (false). |
| 378 |
func (v *SHT3X) CheckCommandFailed(i2c *i2c.I2C) (bool, error) { |
| 379 |
lg.Debug("Checking last command status...") |
| 380 |
v.lastStatusReg = nil |
| 381 |
ur, err := v.ReadStatusReg(i2c) |
| 382 |
if err != nil { |
| 383 |
return false, err |
| 384 |
} |
| 385 |
return (StatusRegFlag)(ur)&COMMAND_FAILED != 0, nil |
| 386 |
} |
| 387 |
|
| 388 |
// CheckWrittedChecksumIsIncorrect return last command status: not correct (true) correct (false). |
| 389 |
func (v *SHT3X) CheckWrittenChecksumIsIncorrect(i2c *i2c.I2C) (bool, error) { |
| 390 |
lg.Debug("Checking last written data checksum status...") |
| 391 |
v.lastStatusReg = nil |
| 392 |
ur, err := v.ReadStatusReg(i2c) |
| 393 |
if err != nil { |
| 394 |
return false, err |
| 395 |
} |
| 396 |
return (StatusRegFlag)(ur)&WRITE_DATA_CRC_FAILED != 0, nil |
| 397 |
} |
| 398 |
|
| 399 |
// initiateMeasure used to initiate temperature and humidity measurement process. |
| 400 |
func (v *SHT3X) initiateMeasure(i2c *i2c.I2C, cmd []byte, |
| 401 |
precision MeasureRepeatability) error { |
| 402 |
|
| 403 |
_, err := i2c.WriteBytes(cmd) |
| 404 |
if err != nil { |
| 405 |
return err |
| 406 |
} |
| 407 |
v.lastCmd = cmd |
| 408 |
|
| 409 |
// Wait according to conversion time specification |
| 410 |
pause := precision.GetMeasureTime() |
| 411 |
time.Sleep(pause) |
| 412 |
return nil |
| 413 |
} |
| 414 |
|
| 415 |
// ReadUncompTemperatureAndHumidity returns uncompensated humidity and |
| 416 |
// temperature obtained from sensor in "single shot mode". |
| 417 |
func (v *SHT3X) ReadUncompTemperatureAndHumidity(i2c *i2c.I2C, |
| 418 |
precision MeasureRepeatability) (uint16, uint16, error) { |
| 419 |
|
| 420 |
lg.Debug("Measuring temperature and humidity...") |
| 421 |
var cmd []byte |
| 422 |
switch precision { |
| 423 |
case RepeatabilityLow: |
| 424 |
cmd = CMD_SINGLE_MEASURE_LOW |
| 425 |
case RepeatabilityMedium: |
| 426 |
cmd = CMD_SINGLE_MEASURE_MEDIUM |
| 427 |
case RepeatabilityHigh: |
| 428 |
cmd = CMD_SINGLE_MEASURE_HIGH |
| 429 |
} |
| 430 |
err := v.initiateMeasure(i2c, cmd, precision) |
| 431 |
if err != nil { |
| 432 |
return 0, 0, err |
| 433 |
} |
| 434 |
|
| 435 |
data, err := v.readDataWithCRCCheck(i2c, 2) |
| 436 |
if err != nil { |
| 437 |
return 0, 0, err |
| 438 |
} |
| 439 |
return data[0], data[1], nil |
| 440 |
} |
| 441 |
|
| 442 |
// ReadTemperatureAndRelativeHumidity returns humidity and |
| 443 |
// temperature obtained from sensor in "single shot mode". |
| 444 |
func (v *SHT3X) ReadTemperatureAndRelativeHumidity(i2c *i2c.I2C, |
| 445 |
precision MeasureRepeatability) (float32, float32, error) { |
| 446 |
|
| 447 |
ut, urh, err := v.ReadUncompTemperatureAndHumidity(i2c, precision) |
| 448 |
if err != nil { |
| 449 |
return 0, 0, err |
| 450 |
} |
| 451 |
lg.Debugf("Temperature and humidity uncompensated = %v, %v", ut, urh) |
| 452 |
temp := v.uncompTemperatureToCelsius(ut) |
| 453 |
rh := v.uncompHumidityToRelativeHumidity(urh) |
| 454 |
return temp, rh, nil |
| 455 |
} |
| 456 |
|
| 457 |
// Convert uncompensated humidity to relative humidity. |
| 458 |
func (v *SHT3X) uncompHumidityToRelativeHumidity(uh uint16) float32 { |
| 459 |
rh := float32(uh) * 100 / (0x10000 - 1) |
| 460 |
rh2 := round32(rh, 2) |
| 461 |
return rh2 |
| 462 |
} |
| 463 |
|
| 464 |
// Convert uncompensated temperature to Celsius value. |
| 465 |
func (v *SHT3X) uncompTemperatureToCelsius(ut uint16) float32 { |
| 466 |
temp := float32(ut)*175/(0x10000-1) - 45 |
| 467 |
temp2 := round32(temp, 2) |
| 468 |
return temp2 |
| 469 |
} |
| 470 |
|
| 471 |
// Reverse conversion of relative humidity to uncompensated one. |
| 472 |
func (v *SHT3X) relativeHumidityToUncompHimidity(rh float32) uint16 { |
| 473 |
uh := uint16(rh * (0x10000 - 1) / 100) |
| 474 |
return uh |
| 475 |
} |
| 476 |
|
| 477 |
// Reverse conversion of Celsius to uncompensated temperature. |
| 478 |
func (v *SHT3X) celsiusToUncompTemperature(celsius float32) uint16 { |
| 479 |
ut := uint16((celsius + 45) * (0x10000 - 1) / 175) |
| 480 |
return ut |
| 481 |
} |
| 482 |
|
| 483 |
// Select proper periodic measurement command depending on |
| 484 |
// PeriodicMeasure and MeasureRepeatability parameters. |
| 485 |
func (v *SHT3X) getPeriodicMeasurementCommand(period PeriodicMeasure, |
| 486 |
precision MeasureRepeatability) []byte { |
| 487 |
|
| 488 |
var cmd []byte |
| 489 |
|
| 490 |
switch period { |
| 491 |
case PeriodicHalfMPS: |
| 492 |
switch precision { |
| 493 |
case RepeatabilityLow: |
| 494 |
cmd = CMD_PERIOD_MEASURE_05MPS_LOW |
| 495 |
case RepeatabilityMedium: |
| 496 |
cmd = CMD_PERIOD_MEASURE_05MPS_MEDIUM |
| 497 |
case RepeatabilityHigh: |
| 498 |
cmd = CMD_PERIOD_MEASURE_05MPS_HIGH |
| 499 |
} |
| 500 |
case Periodic1MPS: |
| 501 |
switch precision { |
| 502 |
case RepeatabilityLow: |
| 503 |
cmd = CMD_PERIOD_MEASURE_1MPS_LOW |
| 504 |
case RepeatabilityMedium: |
| 505 |
cmd = CMD_PERIOD_MEASURE_1MPS_MEDIUM |
| 506 |
case RepeatabilityHigh: |
| 507 |
cmd = CMD_PERIOD_MEASURE_1MPS_HIGH |
| 508 |
} |
| 509 |
case Periodic2MPS: |
| 510 |
switch precision { |
| 511 |
case RepeatabilityLow: |
| 512 |
cmd = CMD_PERIOD_MEASURE_2MPS_LOW |
| 513 |
case RepeatabilityMedium: |
| 514 |
cmd = CMD_PERIOD_MEASURE_2MPS_MEDIUM |
| 515 |
case RepeatabilityHigh: |
| 516 |
cmd = CMD_PERIOD_MEASURE_2MPS_HIGH |
| 517 |
} |
| 518 |
case Periodic4MPS: |
| 519 |
switch precision { |
| 520 |
case RepeatabilityLow: |
| 521 |
cmd = CMD_PERIOD_MEASURE_4MPS_LOW |
| 522 |
case RepeatabilityMedium: |
| 523 |
cmd = CMD_PERIOD_MEASURE_4MPS_MEDIUM |
| 524 |
case RepeatabilityHigh: |
| 525 |
cmd = CMD_PERIOD_MEASURE_4MPS_HIGH |
| 526 |
} |
| 527 |
case Periodic10MPS: |
| 528 |
switch precision { |
| 529 |
case RepeatabilityLow: |
| 530 |
cmd = CMD_PERIOD_MEASURE_10MPS_LOW |
| 531 |
case RepeatabilityMedium: |
| 532 |
cmd = CMD_PERIOD_MEASURE_10MPS_MEDIUM |
| 533 |
case RepeatabilityHigh: |
| 534 |
cmd = CMD_PERIOD_MEASURE_10MPS_HIGH |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
return cmd |
| 539 |
} |
| 540 |
|
| 541 |
// StartPeriodicTemperatureAndHumidityMeasure send command to the sensor |
| 542 |
// to start continuous measurement process of temperature and humidity |
| 543 |
// with the pace defined by period parameter. Measurement process should be |
| 544 |
// interrupted by Break command. Use Fetch... methods to read results. |
| 545 |
func (v *SHT3X) StartPeriodicTemperatureAndHumidityMeasure(i2c *i2c.I2C, |
| 546 |
period PeriodicMeasure, precision MeasureRepeatability) error { |
| 547 |
|
| 548 |
cmd := v.getPeriodicMeasurementCommand(period, precision) |
| 549 |
err := v.initiateMeasure(i2c, cmd, precision) |
| 550 |
if err != nil { |
| 551 |
return err |
| 552 |
} |
| 553 |
v.lastPeriodic = period |
| 554 |
v.lastPrecision = precision |
| 555 |
|
| 556 |
return nil |
| 557 |
} |
| 558 |
|
| 559 |
// Break interrupt "periodic data acquisition mode" and |
| 560 |
// return sensor to "single shot mode". |
| 561 |
func (v *SHT3X) Break(i2c *i2c.I2C) error { |
| 562 |
lg.Debug("Interrupt periodic data acquisition mode...") |
| 563 |
cmd := CMD_BREAK |
| 564 |
_, err := i2c.WriteBytes(cmd) |
| 565 |
if err != nil { |
| 566 |
return err |
| 567 |
} |
| 568 |
v.lastCmd = cmd |
| 569 |
return nil |
| 570 |
} |
| 571 |
|
| 572 |
// FetchUncompTemperatureAndHumidity return |
| 573 |
// uncompensated temperature and humidity obtained from sensor. |
| 574 |
func (v *SHT3X) FetchUncompTemperatureAndHumidity(i2c *i2c.I2C) (ut uint16, uh uint16, err error) { |
| 575 |
// Create default context |
| 576 |
ctx := context.Background() |
| 577 |
// Reroute call |
| 578 |
return v.FetchUncompTemperatureAndHumidityWithContext(ctx, |
| 579 |
i2c) |
| 580 |
} |
| 581 |
|
| 582 |
// FetchUncompTemperatureAndHumidityWithContext return |
| 583 |
// uncompensated temperature and humidity obtained from sensor. |
| 584 |
// Use context parameter, since operation is time consuming |
| 585 |
// (can take up to 2 seconds, waiting for results). |
| 586 |
func (v *SHT3X) FetchUncompTemperatureAndHumidityWithContext(parent context.Context, |
| 587 |
i2c *i2c.I2C) (ut uint16, uh uint16, err error) { |
| 588 |
|
| 589 |
cmd := v.getPeriodicMeasurementCommand(v.lastPeriodic, v.lastPrecision) |
| 590 |
if cmd == nil || !reflect.DeepEqual(cmd, v.lastCmd) { |
| 591 |
return 0, 0, errors.New("Can't fetch measurement results, since no measurement initiated") |
| 592 |
} |
| 593 |
_, err = i2c.WriteBytes(CMD_PERIOD_FETCH) |
| 594 |
if err != nil { |
| 595 |
return 0, 0, err |
| 596 |
} |
| 597 |
|
| 598 |
// Create context with cancellation possibility. |
| 599 |
ctx, cancel := context.WithCancel(parent) |
| 600 |
// use done channel as a trigger to exit from signal waiting goroutine |
| 601 |
done := make(chan struct{}) |
| 602 |
defer close(done) |
| 603 |
// build actual signal list to control |
| 604 |
signals := []os.Signal{os.Kill, os.Interrupt} |
| 605 |
if shell.IsLinuxMacOSFreeBSD() { |
| 606 |
signals = append(signals, syscall.SIGTERM) |
| 607 |
} |
| 608 |
// run goroutine waiting for OS termination events, including keyboard Ctrl+C. |
| 609 |
shell.CloseContextOnSignals(cancel, done, signals...) |
| 610 |
|
| 611 |
retryCount := 5 |
| 612 |
var data []uint16 |
| 613 |
timeDur := v.lastPeriodic.GetWaitDuration() |
| 614 |
first := true |
| 615 |
for retryCount >= 0 { |
| 616 |
data, err = v.readDataWithCRCCheck(i2c, 2) |
| 617 |
// Once sensor doesn't ready provide data, sensor is replying with i2c NACK |
| 618 |
// and it throw error "read /dev/i2c-x: no such device or address". |
| 619 |
// So, we are retrying after pause specific to period parameter |
| 620 |
// which define "measures per second" value. |
| 621 |
if err != nil { |
| 622 |
if retryCount == 0 { |
| 623 |
return 0, 0, err |
| 624 |
} |
| 625 |
// sleep timeDur time |
| 626 |
select { |
| 627 |
// check for termination request |
| 628 |
case <-ctx.Done(): |
| 629 |
// interrupt loop, if pending termination |
| 630 |
return 0, 0, ctx.Err() |
| 631 |
// sleep before new attempt. |
| 632 |
case <-time.After(timeDur): |
| 633 |
} |
| 634 |
if first { |
| 635 |
timeDur = timeDur / 10 |
| 636 |
} |
| 637 |
retryCount-- |
| 638 |
} else { |
| 639 |
break |
| 640 |
} |
| 641 |
first = false |
| 642 |
} |
| 643 |
return data[0], data[1], nil |
| 644 |
} |
| 645 |
|
| 646 |
// FetchTemperatureAndRelativeHumidity wait for uncompensated temperature |
| 647 |
// and humidity values and convert them to float values (Celsius and related humidity). |
| 648 |
func (v *SHT3X) FetchTemperatureAndRelativeHumidity(i2c *i2c.I2C) (temp float32, hum float32, err error) { |
| 649 |
// Create default context |
| 650 |
ctx := context.Background() |
| 651 |
// Reroute call |
| 652 |
return v.FetchTemperatureAndRelativeHumidityWithContext(ctx, i2c) |
| 653 |
} |
| 654 |
|
| 655 |
// FetchTemperatureAndRelativeHumidityWithContext wait for uncompensated temperature |
| 656 |
// and humidity values and convert them to float values (Celsius and related humidity). |
| 657 |
// Use context parameter, since operation is time consuming |
| 658 |
// (can take up to 2 seconds, waiting for results). |
| 659 |
func (v *SHT3X) FetchTemperatureAndRelativeHumidityWithContext(parent context.Context, |
| 660 |
i2c *i2c.I2C) (temp float32, hum float32, err error) { |
| 661 |
|
| 662 |
ut, urh, err := v.FetchUncompTemperatureAndHumidityWithContext(parent, i2c) |
| 663 |
if err != nil { |
| 664 |
return 0, 0, err |
| 665 |
} |
| 666 |
lg.Debugf("Temperature and RH uncompensated = %v, %v", ut, urh) |
| 667 |
temp = v.uncompTemperatureToCelsius(ut) |
| 668 |
hum = v.uncompHumidityToRelativeHumidity(urh) |
| 669 |
return temp, hum, nil |
| 670 |
} |
| 671 |
|
| 672 |
// Read alert temperature and humidity limits from sensor. |
| 673 |
func (v *SHT3X) readAlertData(i2c *i2c.I2C, cmd []byte) (float32, float32, error) { |
| 674 |
_, err := i2c.WriteBytes(cmd) |
| 675 |
if err != nil { |
| 676 |
return 0, 0, err |
| 677 |
} |
| 678 |
v.lastCmd = cmd |
| 679 |
data, err := v.readDataWithCRCCheck(i2c, 1) |
| 680 |
if err != nil { |
| 681 |
return 0, 0, err |
| 682 |
} |
| 683 |
|
| 684 |
uh := data[0] & 0xFE00 |
| 685 |
ut := data[0] & 0x01FF << 7 |
| 686 |
|
| 687 |
temp := v.uncompTemperatureToCelsius(ut) |
| 688 |
rh := v.uncompHumidityToRelativeHumidity(uh) |
| 689 |
return temp, rh, nil |
| 690 |
} |
| 691 |
|
| 692 |
// Write alert temperature and humidity limits to the sensor. |
| 693 |
func (v *SHT3X) writeAlertData(i2c *i2c.I2C, cmd []byte, temp, hum float32) error { |
| 694 |
ut := v.celsiusToUncompTemperature(temp) |
| 695 |
uh := v.relativeHumidityToUncompHimidity(hum) |
| 696 |
|
| 697 |
u := uh&0xFE00 | (ut & 0xFF80 >> 7) |
| 698 |
data := []byte{byte(u & 0xFF00 >> 8), byte(u & 0x00FF)} |
| 699 |
crc := calcCRC_SHT3X(0xFF, data) |
| 700 |
b := append(cmd, data...) |
| 701 |
b = append(b, crc) |
| 702 |
|
| 703 |
_, err := i2c.WriteBytes(b) |
| 704 |
if err != nil { |
| 705 |
return err |
| 706 |
} |
| 707 |
v.lastCmd = cmd |
| 708 |
// No conversion time defined in docs for this command, |
| 709 |
// but error thrown out, if no any pause provided. |
| 710 |
time.Sleep(time.Millisecond * 1) |
| 711 |
|
| 712 |
return nil |
| 713 |
} |
| 714 |
|
| 715 |
// ReadAlertHighSet read sensor alert HIGH SET limits |
| 716 |
// for temperature and humidity. |
| 717 |
func (v *SHT3X) ReadAlertHighSet(i2c *i2c.I2C) (float32, float32, error) { |
| 718 |
lg.Debug("Getting alert HIGH SET limit...") |
| 719 |
temp, rh, err := v.readAlertData(i2c, CMD_ALERT_READ_HIGH_SET) |
| 720 |
if err != nil { |
| 721 |
return 0, 0, err |
| 722 |
} |
| 723 |
return temp, rh, nil |
| 724 |
|
| 725 |
} |
| 726 |
|
| 727 |
// ReadAlertHighClear read sensor alert HIGH CLEAR limits |
| 728 |
// for temperature and humidity. |
| 729 |
func (v *SHT3X) ReadAlertHighClear(i2c *i2c.I2C) (float32, float32, error) { |
| 730 |
lg.Debug("Getting alert HIGH CLEAR limit...") |
| 731 |
temp, rh, err := v.readAlertData(i2c, CMD_ALERT_READ_HIGH_CLEAR) |
| 732 |
if err != nil { |
| 733 |
return 0, 0, err |
| 734 |
} |
| 735 |
return temp, rh, nil |
| 736 |
|
| 737 |
} |
| 738 |
|
| 739 |
// ReadAlertLowClear read sensor alert LOW CLEAR limits |
| 740 |
// for temperature and humidity. |
| 741 |
func (v *SHT3X) ReadAlertLowClear(i2c *i2c.I2C) (float32, float32, error) { |
| 742 |
lg.Debug("Getting alert LOW CLEAR limit...") |
| 743 |
temp, rh, err := v.readAlertData(i2c, CMD_ALERT_READ_LOW_CLEAR) |
| 744 |
if err != nil { |
| 745 |
return 0, 0, err |
| 746 |
} |
| 747 |
return temp, rh, nil |
| 748 |
|
| 749 |
} |
| 750 |
|
| 751 |
// ReadAlertLowSet read sensor alert LOW SET limits |
| 752 |
// for temperature and humidity. |
| 753 |
func (v *SHT3X) ReadAlertLowSet(i2c *i2c.I2C) (float32, float32, error) { |
| 754 |
lg.Debug("Getting alert LOW SET limit...") |
| 755 |
temp, rh, err := v.readAlertData(i2c, CMD_ALERT_READ_LOW_SET) |
| 756 |
if err != nil { |
| 757 |
return 0, 0, err |
| 758 |
} |
| 759 |
return temp, rh, nil |
| 760 |
|
| 761 |
} |
| 762 |
|
| 763 |
// WriteAlertHighSet write alert HIGH SET limits |
| 764 |
// for temperature and humidity to the sensor. |
| 765 |
func (v *SHT3X) WriteAlertHighSet(i2c *i2c.I2C, temp, hum float32) error { |
| 766 |
lg.Debug("Setting alert HIGH SET limit...") |
| 767 |
err := v.writeAlertData(i2c, CMD_ALERT_WRITE_HIGH_SET, temp, hum) |
| 768 |
if err != nil { |
| 769 |
return err |
| 770 |
} |
| 771 |
return nil |
| 772 |
|
| 773 |
} |
| 774 |
|
| 775 |
// WriteAlertHighClear write alert HIGH CLEAR limits |
| 776 |
// for temperature and humidity to the sensor. |
| 777 |
func (v *SHT3X) WriteAlertHighClear(i2c *i2c.I2C, temp, hum float32) error { |
| 778 |
lg.Debug("Setting alert HIGH CLEAR limit...") |
| 779 |
err := v.writeAlertData(i2c, CMD_ALERT_WRITE_HIGH_CLEAR, temp, hum) |
| 780 |
if err != nil { |
| 781 |
return err |
| 782 |
} |
| 783 |
return nil |
| 784 |
|
| 785 |
} |
| 786 |
|
| 787 |
// WriteAlertLowClear write alert LOW CLEAR limits |
| 788 |
// for temperature and humidity to the sensor. |
| 789 |
func (v *SHT3X) WriteAlertLowClear(i2c *i2c.I2C, temp, hum float32) error { |
| 790 |
lg.Debug("Setting alert LOW CLEAR limit...") |
| 791 |
err := v.writeAlertData(i2c, CMD_ALERT_WRITE_LOW_CLEAR, temp, hum) |
| 792 |
if err != nil { |
| 793 |
return err |
| 794 |
} |
| 795 |
return nil |
| 796 |
|
| 797 |
} |
| 798 |
|
| 799 |
// WriteAlertLowSet write alert LOW SET limits |
| 800 |
// for temperature and humidity to the sensor. |
| 801 |
func (v *SHT3X) WriteAlertLowSet(i2c *i2c.I2C, temp, hum float32) error { |
| 802 |
lg.Debug("Setting alert LOW SET limit...") |
| 803 |
err := v.writeAlertData(i2c, CMD_ALERT_WRITE_LOW_SET, temp, hum) |
| 804 |
if err != nil { |
| 805 |
return err |
| 806 |
} |
| 807 |
return nil |
| 808 |
|
| 809 |
} |