Class: Denko::Sensor::SHT3X

Inherits:
Object
  • Object
show all
Includes:
Behaviors::Poller, I2C::Peripheral
Defined in:
lib/denko/sensor/sht3x.rb

Constant Summary collapse

RESET =
0x30A2
RESET_TIME =
0.002
HEATER_OFF =
0x3066
HEATER_ON =
0x306D
FETCH_DATA =
0xE000
REPEATABILITY =
{
  high:   { lsb: 0x00, measurement_time: 0.016 },
  medium: { lsb: 0x0B, measurement_time: 0.007 },
  low:    { lsb: 0x16, measurement_time: 0.005 },
}
READ_STATUS_REGISTER =

Unused

0xF32D
CLEAR_STATUS_REGISTER =
0x3041
BREAK =
0x3093
ART =
0x2B32
CRC_INITIAL_VALUE =

CRC is same as AHT20 sensor. Copied from that file.

0xFF
CRC_POLYNOMIAL =
0x31
MSBIT_MASK =
0x80

Instance Attribute Summary

Attributes included from Behaviors::Threaded

#interrupts_enabled, #thread

Attributes included from Behaviors::Callbacks

#callback_mutex

Attributes included from I2C::Peripheral

#i2c_frequency, #i2c_repeated_start

Attributes included from Behaviors::BusPeripheral

#address

Attributes included from Behaviors::Component

#board

Instance Method Summary collapse

Methods included from Behaviors::Poller

#poll, #poll_using, #stop

Methods included from Behaviors::Threaded

#enable_interrupts, included, #stop, #stop_thread, #threaded, #threaded_loop

Methods included from Behaviors::Reader

#read, #read_using, #wait_for_read

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #initialize, #remove_callback, #update

Methods included from Behaviors::State

#initialize, #state

Methods included from I2C::Peripheral

#i2c_read, #i2c_write

Methods included from Behaviors::BusPeripheral

#atomically

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Method Details

#_readObject



46
47
48
49
50
# File 'lib/denko/sensor/sht3x.rb', line 46

def _read
  i2c_write [0x24, @measurement_lsb]
  sleep(@measurement_time)
  i2c_read(FETCH_DATA, 6)
end

#after_initialize(options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/denko/sensor/sht3x.rb', line 29

def after_initialize(options={})
  super(options)

  # Avoid repeated memory allocation for callback data and state.
  @reading     = { temperature: nil, humidity: nil }
  self.state   = { temperature: nil, humidity: nil }

  reset
  self.repeatability = :high
end

#before_initialize(options = {}) ⇒ Object



24
25
26
27
# File 'lib/denko/sensor/sht3x.rb', line 24

def before_initialize(options={})
  @i2c_address = 0x44
  super(options)
end

#calculate_crc(bytes) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/denko/sensor/sht3x.rb', line 108

def calculate_crc(bytes)
  crc = CRC_INITIAL_VALUE

  # Ignore last byte. That's the CRC value to compare with.
  bytes.take(bytes.length - 1).each do |byte|
    crc = crc ^ byte
    8.times do
      if (crc & MSBIT_MASK) > 0
        crc = (crc << 1) ^ CRC_POLYNOMIAL
      else
        crc = crc << 1
      end
    end
  end
  
  # Limit CRC size to 8 bits.
  crc = crc & 0xFF
end

#heater_offObject



98
99
100
101
# File 'lib/denko/sensor/sht3x.rb', line 98

def heater_off
  i2c_write [HEATER_OFF]
  @heater_on = false
end

#heater_off?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/denko/sensor/sht3x.rb', line 89

def heater_off?
  !@heater_on
end

#heater_onObject



93
94
95
96
# File 'lib/denko/sensor/sht3x.rb', line 93

def heater_on
  i2c_write [HEATER_ON]
  @heater_on = true
end

#heater_on?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/denko/sensor/sht3x.rb', line 85

def heater_on?
  @heater_on
end

#pre_callback_filter(bytes) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/denko/sensor/sht3x.rb', line 52

def pre_callback_filter(bytes)
  # Temperature is bytes 0 to 2: MSB, LSB, CRC
  if calculate_crc(bytes[0..2]) == bytes[2]
    t_raw = (bytes[0] << 8) | bytes[1]
    @reading[:temperature] = (175 * t_raw / 65535.0) - 45
  else
    @reading[:temperature] = nil
  end

  # Humidity is bytes 3 to 5: MSB, LSB, CRC
  if calculate_crc(bytes[3..5]) == bytes[5]
    h_raw = (bytes[3] << 8) | bytes[4]
    @reading[:humidity] = 100 * h_raw / 65535.0
  else
    @reading[:humidity] = nil
  end

  @reading
end

#repeatability=(key) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
# File 'lib/denko/sensor/sht3x.rb', line 40

def repeatability=(key)
  raise ArgumentError, "invalid repeatability setting: #{key}" unless REPEATABILITY.keys.include? key
  @measurement_lsb = REPEATABILITY[key][:lsb]
  @measurement_time = REPEATABILITY[key][:measurement_time]
end

#resetObject



79
80
81
82
83
# File 'lib/denko/sensor/sht3x.rb', line 79

def reset
  i2c_write [RESET]
  sleep RESET_TIME
  @heater_on = false
end

#update_state(reading) ⇒ Object



72
73
74
75
76
77
# File 'lib/denko/sensor/sht3x.rb', line 72

def update_state(reading)
  @state_mutex.synchronize do
    @state[:temperature] = reading[:temperature]
    @state[:humidity]    = reading[:humidity]
  end
end