Class: Denko::Sensor::DHT

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

Constant Summary

Constants included from Behaviors::Lifecycle

Behaviors::Lifecycle::CALLBACK_METHODS

Constants included from Behaviors::Reader

Behaviors::Reader::READ_WAIT_TIME

Constants included from Behaviors::InputPin

Behaviors::InputPin::INPUT_MODES

Instance Attribute Summary

Attributes included from Behaviors::Threaded

#interrupts_enabled, #thread

Attributes included from Behaviors::SinglePin

#mode, #pin

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 Behaviors::InputPin

#_stop_listener, #debounce_time=

Methods included from Behaviors::SinglePin

#convert_pins, #initialize_pins

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Method Details

#_readObject



23
24
25
# File 'lib/denko/sensor/dht.rb', line 23

def _read
  board.pulse_read(pin, reset: board.low, reset_time: 10_000, pulse_limit: 84, timeout: 100)
end

#crc(bytes) ⇒ Object



65
66
67
# File 'lib/denko/sensor/dht.rb', line 65

def crc(bytes)
  bytes[0..3].reduce(0, :+) & 0xFF == bytes[4]
end

#decode(data) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/denko/sensor/dht.rb', line 35

def decode(data)
  data = data.last(81)
  return { error: 'missing data' } unless data.length == 81
  data = data[0..79]

  bytes = []
  data.each_slice(16) do |b|
    byte = 0b00000000
    b.each_slice(2) do |x,y|
      bit = (y<x) ? 0 : 1
      byte = (byte << 1) | bit
    end
    bytes << byte
  end
  return { error: 'CRC failure' } unless crc(bytes)

  reading[:temperature] = ((bytes[2] << 8) | bytes[3]).to_f / 10
  reading[:humidity]    = ((bytes[0] << 8) | bytes[1]).to_f / 10

  reading
end

#pre_callback_filter(data) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/denko/sensor/dht.rb', line 27

def pre_callback_filter(data)
  if data.class == String
    decode(data.split(",").map(&:to_i))
  else
    decode(data)
  end
end

#readingObject



19
20
21
# File 'lib/denko/sensor/dht.rb', line 19

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

#stateObject



15
16
17
# File 'lib/denko/sensor/dht.rb', line 15

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

#update_state(reading) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/denko/sensor/dht.rb', line 57

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