Class: Denko::Sensor::SHT4X

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

Constant Summary collapse

I2C_ADDRESS =
0x44
RESET =
0x94
RESET_TIME =
0.001
READ_SERIAL_NUMBER =
0x89
REPEATABILITIES =
{
  high:   {command: 0xFD, time: 0.010},
  medium: {command: 0xF6, time: 0.005},
  low:    {command: 0xE0, time: 0.003},
}
CRC_INITIAL_VALUE =

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

0xFF
CRC_POLYNOMIAL =
0x31
MSBIT_MASK =
0x80

Constants included from Behaviors::Lifecycle

Behaviors::Lifecycle::CALLBACK_METHODS

Constants included from Behaviors::Reader

Behaviors::Reader::READ_WAIT_TIME

Constants included from I2C::Peripheral

I2C::Peripheral::I2C_FREQUENCY, I2C::Peripheral::I2C_REPEATED_START

Instance Attribute Summary

Attributes included from Behaviors::Threaded

#interrupts_enabled, #thread

Attributes included from I2C::Peripheral

#i2c_frequency, #i2c_repeated_start

Attributes included from Behaviors::Component

#board, #params

Instance Method Summary collapse

Methods included from HumidityHelper

#humidity

Methods included from TemperatureHelper

#temperature, #temperature_f, #temperature_k

Methods included from Behaviors::Lifecycle

included

Methods included from Behaviors::Poller

#poll, #poll_using, #stop

Methods included from Behaviors::Threaded

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

Methods included from Behaviors::Reader

#read, #read_busy?, #read_nb, #read_raw, #read_using, #update

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #remove_callback, #update

Methods included from I2C::Peripheral

#address, #i2c_default, #i2c_read, #i2c_read_raw, #i2c_write

Methods included from Behaviors::BusPeripheralAddressed

#address

Methods included from Behaviors::BusPeripheral

#atomically

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Method Details

#_readObject



41
42
43
44
45
# File 'lib/denko/sensor/sht4x.rb', line 41

def _read
  i2c_write [@measurement_command]
  sleep(@measurement_time)
  i2c_read(6)
end

#calculate_crc(bytes) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/denko/sensor/sht4x.rb', line 105

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

#parse_serial(bytes) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/denko/sensor/sht4x.rb', line 91

def parse_serial(bytes)
  # Serial bytes are laid out as [b0, b1, crc0+1, b2, b3, crc2+3]
  if calculate_crc(bytes[0..2]) == bytes[2] && calculate_crc(bytes[3..5]) == bytes[5]
    @serial = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[3] << 8) | bytes[4]
  else
    @serial = nil
  end
end

#pre_callback_filter(bytes) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/denko/sensor/sht4x.rb', line 47

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

#read_serialObject



84
85
86
87
88
89
# File 'lib/denko/sensor/sht4x.rb', line 84

def read_serial
  i2c_write [READ_SERIAL_NUMBER]
  sleep RESET_TIME
  bytes = i2c_read_raw(6)
  parse_serial(bytes)
end

#readingObject



31
32
33
# File 'lib/denko/sensor/sht4x.rb', line 31

def reading
  @reading ||= { temperature: nil, humidity: nil }
end

#repeatability=(key) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
# File 'lib/denko/sensor/sht4x.rb', line 35

def repeatability=(key)
  raise ArgumentError, "invalid repeatability setting: #{key}" unless REPEATABILITIES.keys.include? key
  @measurement_command = REPEATABILITIES[key][:command]
  @measurement_time = REPEATABILITIES[key][:time]
end

#resetObject



75
76
77
78
# File 'lib/denko/sensor/sht4x.rb', line 75

def reset
  i2c_write [RESET]
  sleep RESET_TIME
end

#serialObject



80
81
82
# File 'lib/denko/sensor/sht4x.rb', line 80

def serial
  @serial ||= read_serial
end

#stateObject



27
28
29
# File 'lib/denko/sensor/sht4x.rb', line 27

def state
  @state ||= { temperature: nil, humidity: nil }
end

#update_state(reading) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/denko/sensor/sht4x.rb', line 67

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