| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package sht3x |
| 23 |
|
| 24 |
import ( |
| 25 |
"bytes" |
| 26 |
"context" |
| 27 |
"encoding/binary" |
| 28 |
"errors" |
| 29 |
"os" |
| •30 |
"os/signal" |
| 31 |
"reflect" |
| 32 |
"syscall" |
| 33 |
"time" |
| 34 |
|
| 35 |
i2c "github.com/d2r2/go-i2c" |
| 36 |
"github.com/davecgh/go-spew/spew" |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
var ( |
| 41 |
|
| 42 |
CMD_SINGLE_MEASURE_HIGH_CSE = []byte{0x2C, 0x06} |
| 43 |
CMD_SINGLE_MEASURE_MEDIUM_CSE = []byte{0x2C, 0x0D} |
| 44 |
CMD_SINGLE_MEASURE_LOW_CSE = []byte{0x2C, 0x10} |
| 45 |
CMD_SINGLE_MEASURE_HIGH = []byte{0x24, 0x00} |
| 46 |
CMD_SINGLE_MEASURE_MEDIUM = []byte{0x24, 0x0B} |
| 47 |
CMD_SINGLE_MEASURE_LOW = []byte{0x24, 0x16} |
| 48 |
|
| 49 |
|
| 50 |
CMD_PERIOD_MEASURE_05MPS_HIGH = []byte{0x20, 0x32} |
| 51 |
CMD_PERIOD_MEASURE_05MPS_MEDIUM = []byte{0x20, 0x24} |
| 52 |
CMD_PERIOD_MEASURE_05MPS_LOW = []byte{0x20, 0x2F} |
| 53 |
CMD_PERIOD_MEASURE_1MPS_HIGH = []byte{0x21, 0x30} |
| 54 |
CMD_PERIOD_MEASURE_1MPS_MEDIUM = []byte{0x21, 0x26} |
| 55 |
CMD_PERIOD_MEASURE_1MPS_LOW = []byte{0x21, 0x2D} |
| 56 |
CMD_PERIOD_MEASURE_2MPS_HIGH = []byte{0x22, 0x36} |
| 57 |
CMD_PERIOD_MEASURE_2MPS_MEDIUM = []byte{0x22, 0x20} |
| 58 |
CMD_PERIOD_MEASURE_2MPS_LOW = []byte{0x22, 0x2B} |
| 59 |
CMD_PERIOD_MEASURE_4MPS_HIGH = []byte{0x23, 0x34} |
| 60 |
CMD_PERIOD_MEASURE_4MPS_MEDIUM = []byte{0x23, 0x22} |
| 61 |
CMD_PERIOD_MEASURE_4MPS_LOW = []byte{0x23, 0x29} |
| 62 |
CMD_PERIOD_MEASURE_10MPS_HIGH = []byte{0x27, 0x37} |
| 63 |
CMD_PERIOD_MEASURE_10MPS_MEDIUM = []byte{0x27, 0x21} |
| 64 |
CMD_PERIOD_MEASURE_10MPS_LOW = []byte{0x27, 0x2A} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
CMD_ALERT_READ_HIGH_SET = []byte{0xE1, 0x1F} |
| 70 |
CMD_ALERT_READ_HIGH_CLEAR = []byte{0xE1, 0x14} |
| 71 |
CMD_ALERT_READ_LOW_CLEAR = []byte{0xE1, 0x09} |
| 72 |
CMD_ALERT_READ_LOW_SET = []byte{0xE1, 0x02} |
| 73 |
CMD_ALERT_WRITE_HIGH_SET = []byte{0x61, 0x1D} |
| 74 |
CMD_ALERT_WRITE_HIGH_CLEAR = []byte{0x61, 0x16} |
| 75 |
CMD_ALERT_WRITE_LOW_CLEAR = []byte{0x61, 0x0B} |
| 76 |
CMD_ALERT_WRITE_LOW_SET = []byte{0x61, 0x00} |
| 77 |
|
| 78 |
|
| 79 |
CMD_ENABLE_HEATER = []byte{0x30, 0x6D} |
| 80 |
CMD_DISABLE_HEATER = []byte{0x30, 0x66} |
| 81 |
|
| 82 |
|
| 83 |
CMD_READ_STATUS_REG = []byte{0xF3, 0x2D} |
| 84 |
CMD_CLEAR_STATUS_REG = []byte{0x30, 0x41} |
| 85 |
|
| 86 |
|
| 87 |
CMD_PERIOD_FETCH = []byte{0xE0, 0x00} |
| 88 |
CMD_ART = []byte{0x2B, 0x32} |
| 89 |
CMD_BREAK = []byte{0x30, 0x93} |
| 90 |
CMD_RESET = []byte{0x30, 0xA2} |
| 91 |
) |
| 92 |
|
| 93 |
|
| 94 |
type MeasureRepeatability int |
| 95 |
|
| 96 |
const ( |
| 97 |
RepeatabilityLow MeasureRepeatability = iota + 1 |
| 98 |
RepeatabilityMedium |
| 99 |
RepeatabilityHigh |
| 100 |
) |
| 101 |
|
| 102 |
|
| 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 |
|
| 117 |
|
| 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 |
|
| 132 |
|
| 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 |
|
| 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 |
|
| 177 |
|
| 178 |
type PeriodicMeasure int |
| 179 |
|
| 180 |
const ( |
| 181 |
PeriodicHalfMPS PeriodicMeasure = iota + 1 |
| 182 |
Periodic1MPS |
| 183 |
Periodic2MPS |
| 184 |
Periodic4MPS |
| 185 |
Periodic10MPS |
| 186 |
) |
| 187 |
|
| 188 |
|
| 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 |
|
| 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 |
|
| 225 |
type SHT3X struct { |
| 226 |
lastStatusReg *uint16 |
| 227 |
lastCmd []byte |
| 228 |
lastPeriodic PeriodicMeasure |
| 229 |
lastPrecision MeasureRepeatability |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
func NewSHT3X() *SHT3X { |
| 234 |
v := &SHT3X{} |
| 235 |
return v |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 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 |
|
| 257 |
|
| 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 |
|
| 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 |
|
| 298 |
time.Sleep(time.Microsecond * 1500) |
| 299 |
return nil |
| 300 |
} |
| 301 |
|
| 302 |
|
| 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 |
|
| 317 |
|
| 318 |
time.Sleep(time.Millisecond * 1) |
| 319 |
return nil |
| 320 |
} |
| 321 |
|
| 322 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 410 |
pause := precision.GetMeasureTime() |
| 411 |
time.Sleep(pause) |
| 412 |
return nil |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 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 |
|
| 443 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 472 |
func (v *SHT3X) relativeHumidityToUncompHimidity(rh float32) uint16 { |
| 473 |
uh := uint16(rh * (0x10000 - 1) / 100) |
| 474 |
return uh |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
func (v *SHT3X) celsiusToUncompTemperature(celsius float32) uint16 { |
| 479 |
ut := uint16((celsius + 45) * (0x10000 - 1) / 175) |
| 480 |
return ut |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 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 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 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 |
|
| 560 |
|
| 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 |
|
| 573 |
|
| 574 |
func (v *SHT3X) FetchUncompTemperatureAndHumidity(i2c *i2c.I2C) (ut uint16, uh uint16, err error) { |
| 575 |
|
| 576 |
ctx := context.Background() |
| 577 |
|
| 578 |
return v.FetchUncompTemperatureAndHumidityWithContext(ctx, |
| 579 |
i2c) |
| 580 |
} |
| 581 |
|
| •582 |
func CloseContextOnSignals(cancel context.CancelFunc, quit chan struct{}) { |
| 583 |
channel := make(chan os.Signal, 1) |
| 584 |
signal.Notify(channel, os.Kill, os.Interrupt, syscall.SIGTERM) |
| 585 |
go func() { |
| 586 |
select { |
| 587 |
case <-channel: |
| 588 |
if cancel != nil { |
| 589 |
cancel() |
| 590 |
} |
| 591 |
case <-quit: |
| 592 |
|
| 593 |
} |
| 594 |
}() |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
func (v *SHT3X) FetchUncompTemperatureAndHumidityWithContext(parent context.Context, |
| 602 |
i2c *i2c.I2C) (ut uint16, uh uint16, err error) { |
| 603 |
|
| 604 |
cmd := v.getPeriodicMeasurementCommand(v.lastPeriodic, v.lastPrecision) |
| 605 |
if cmd == nil || !reflect.DeepEqual(cmd, v.lastCmd) { |
| 606 |
return 0, 0, errors.New("Can't fetch measurement results, since no measurement initiated") |
| 607 |
} |
| 608 |
_, err = i2c.WriteBytes(CMD_PERIOD_FETCH) |
| 609 |
if err != nil { |
| 610 |
return 0, 0, err |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
ctx, cancel := context.WithCancel(parent) |
| 615 |
|
| 616 |
done := make(chan struct{}) |
| 617 |
defer close(done) |
| 618 |
|
| •619 |
CloseContextOnSignals(cancel, done) |
| 620 |
|
| 621 |
retryCount := 5 |
| 622 |
var data []uint16 |
| 623 |
timeDur := v.lastPeriodic.GetWaitDuration() |
| 624 |
first := true |
| 625 |
for retryCount >= 0 { |
| 626 |
data, err = v.readDataWithCRCCheck(i2c, 2) |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
if err != nil { |
| 632 |
if retryCount == 0 { |
| 633 |
return 0, 0, err |
| 634 |
} |
| 635 |
|
| 636 |
select { |
| 637 |
|
| 638 |
case <-ctx.Done(): |
| 639 |
|
| 640 |
return 0, 0, ctx.Err() |
| 641 |
|
| 642 |
case <-time.After(timeDur): |
| 643 |
} |
| 644 |
if first { |
| 645 |
timeDur = timeDur / 10 |
| 646 |
} |
| 647 |
retryCount-- |
| 648 |
} else { |
| 649 |
break |
| 650 |
} |
| 651 |
first = false |
| 652 |
} |
| 653 |
return data[0], data[1], nil |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
func (v *SHT3X) FetchTemperatureAndRelativeHumidity(i2c *i2c.I2C) (temp float32, hum float32, err error) { |
| 659 |
|
| 660 |
ctx := context.Background() |
| 661 |
|
| 662 |
return v.FetchTemperatureAndRelativeHumidityWithContext(ctx, i2c) |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
func (v *SHT3X) FetchTemperatureAndRelativeHumidityWithContext(parent context.Context, |
| 670 |
i2c *i2c.I2C) (temp float32, hum float32, err error) { |
| 671 |
|
| 672 |
ut, urh, err := v.FetchUncompTemperatureAndHumidityWithContext(parent, i2c) |
| 673 |
if err != nil { |
| 674 |
return 0, 0, err |
| 675 |
} |
| 676 |
lg.Debugf("Temperature and RH uncompensated = %v, %v", ut, urh) |
| 677 |
temp = v.uncompTemperatureToCelsius(ut) |
| 678 |
hum = v.uncompHumidityToRelativeHumidity(urh) |
| 679 |
return temp, hum, nil |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
func (v *SHT3X) readAlertData(i2c *i2c.I2C, cmd []byte) (float32, float32, error) { |
| 684 |
_, err := i2c.WriteBytes(cmd) |
| 685 |
if err != nil { |
| 686 |
return 0, 0, err |
| 687 |
} |
| 688 |
v.lastCmd = cmd |
| 689 |
data, err := v.readDataWithCRCCheck(i2c, 1) |
| 690 |
if err != nil { |
| 691 |
return 0, 0, err |
| 692 |
} |
| 693 |
|
| 694 |
uh := data[0] & 0xFE00 |
| 695 |
ut := data[0] & 0x01FF << 7 |
| 696 |
|
| 697 |
temp := v.uncompTemperatureToCelsius(ut) |
| 698 |
rh := v.uncompHumidityToRelativeHumidity(uh) |
| 699 |
return temp, rh, nil |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
func (v *SHT3X) writeAlertData(i2c *i2c.I2C, cmd []byte, temp, hum float32) error { |
| 704 |
ut := v.celsiusToUncompTemperature(temp) |
| 705 |
uh := v.relativeHumidityToUncompHimidity(hum) |
| 706 |
|
| 707 |
u := uh&0xFE00 | (ut & 0xFF80 >> 7) |
| 708 |
data := []byte{byte(u & 0xFF00 >> 8), byte(u & 0x00FF)} |
| 709 |
crc := calcCRC_SHT3X(0xFF, data) |
| 710 |
b := append(cmd, data...) |
| 711 |
b = append(b, crc) |
| 712 |
|
| 713 |
_, err := i2c.WriteBytes(b) |
| 714 |
if err != nil { |
| 715 |
return err |
| 716 |
} |
| 717 |
v.lastCmd = cmd |
| 718 |
|
| 719 |
|
| 720 |
time.Sleep(time.Millisecond * 1) |
| 721 |
|
| 722 |
return nil |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
func (v *SHT3X) ReadAlertHighSet(i2c *i2c.I2C) (float32, float32, error) { |
| 728 |
lg.Debug("Getting alert HIGH SET limit...") |
| 729 |
temp, rh, err := v.readAlertData(i2c, CMD_ALERT_READ_HIGH_SET) |
| 730 |
if err != nil { |
| 731 |
return 0, 0, err |
| 732 |
} |
| 733 |
return temp, rh, nil |
| 734 |
|
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
func (v *SHT3X) ReadAlertHighClear(i2c *i2c.I2C) (float32, float32, error) { |
| 740 |
lg.Debug("Getting alert HIGH CLEAR limit...") |
| 741 |
temp, rh, err := v.readAlertData(i2c, CMD_ALERT_READ_HIGH_CLEAR) |
| 742 |
if err != nil { |
| 743 |
return 0, 0, err |
| 744 |
} |
| 745 |
return temp, rh, nil |
| 746 |
|
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
func (v *SHT3X) ReadAlertLowClear(i2c *i2c.I2C) (float32, float32, error) { |
| 752 |
lg.Debug("Getting alert LOW CLEAR limit...") |
| 753 |
temp, rh, err := v.readAlertData(i2c, CMD_ALERT_READ_LOW_CLEAR) |
| 754 |
if err != nil { |
| 755 |
return 0, 0, err |
| 756 |
} |
| 757 |
return temp, rh, nil |
| 758 |
|
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
func (v *SHT3X) ReadAlertLowSet(i2c *i2c.I2C) (float32, float32, error) { |
| 764 |
lg.Debug("Getting alert LOW SET limit...") |
| 765 |
temp, rh, err := v.readAlertData(i2c, CMD_ALERT_READ_LOW_SET) |
| 766 |
if err != nil { |
| 767 |
return 0, 0, err |
| 768 |
} |
| 769 |
return temp, rh, nil |
| 770 |
|
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
func (v *SHT3X) WriteAlertHighSet(i2c *i2c.I2C, temp, hum float32) error { |
| 776 |
lg.Debug("Setting alert HIGH SET limit...") |
| 777 |
err := v.writeAlertData(i2c, CMD_ALERT_WRITE_HIGH_SET, temp, hum) |
| 778 |
if err != nil { |
| 779 |
return err |
| 780 |
} |
| 781 |
return nil |
| 782 |
|
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
func (v *SHT3X) WriteAlertHighClear(i2c *i2c.I2C, temp, hum float32) error { |
| 788 |
lg.Debug("Setting alert HIGH CLEAR limit...") |
| 789 |
err := v.writeAlertData(i2c, CMD_ALERT_WRITE_HIGH_CLEAR, temp, hum) |
| 790 |
if err != nil { |
| 791 |
return err |
| 792 |
} |
| 793 |
return nil |
| 794 |
|
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
func (v *SHT3X) WriteAlertLowClear(i2c *i2c.I2C, temp, hum float32) error { |
| 800 |
lg.Debug("Setting alert LOW CLEAR limit...") |
| 801 |
err := v.writeAlertData(i2c, CMD_ALERT_WRITE_LOW_CLEAR, temp, hum) |
| 802 |
if err != nil { |
| 803 |
return err |
| 804 |
} |
| 805 |
return nil |
| 806 |
|
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
func (v *SHT3X) WriteAlertLowSet(i2c *i2c.I2C, temp, hum float32) error { |
| 812 |
lg.Debug("Setting alert LOW SET limit...") |
| 813 |
err := v.writeAlertData(i2c, CMD_ALERT_WRITE_LOW_SET, temp, hum) |
| 814 |
if err != nil { |
| 815 |
return err |
| 816 |
} |
| 817 |
return nil |
| 818 |
|
| 819 |
} |