Class: Denko::Sensor::DS18B20

Inherits:
OneWire::Peripheral show all
Includes:
Behaviors::Lifecycle, TemperatureHelper
Defined in:
lib/denko/sensor/ds18b20.rb

Constant Summary collapse

FAMILY_CODE =
0x28

Constants included from Behaviors::Lifecycle

Behaviors::Lifecycle::CALLBACK_METHODS

Constants included from OneWire::Constants

OneWire::Constants::ALARM_SEARCH, OneWire::Constants::CONVERT_T, OneWire::Constants::COPY_SCRATCH, OneWire::Constants::MATCH_ROM, OneWire::Constants::READ_POWER_SUPPLY, OneWire::Constants::READ_ROM, OneWire::Constants::READ_SCRATCH, OneWire::Constants::RECALL_EEPROM, OneWire::Constants::SEARCH_ROM, OneWire::Constants::SKIP_ROM, OneWire::Constants::WRITE_SCRATCH

Constants included from Behaviors::Reader

Behaviors::Reader::READ_WAIT_TIME

Instance Attribute Summary collapse

Attributes included from Behaviors::Threaded

#interrupts_enabled, #thread

Attributes included from Behaviors::Component

#board, #params

Instance Method Summary collapse

Methods included from TemperatureHelper

#temperature, #temperature_f, #temperature_k

Methods included from Behaviors::Lifecycle

included

Methods inherited from OneWire::Peripheral

#address_bytes, #copy_scratch, #extract_serial, #match, #read_scratch, #serial_number, #write_scratch

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::BusPeripheralAddressed

#address

Methods included from Behaviors::BusPeripheral

#atomically

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Attribute Details

#resolutionObject

Returns the value of attribute resolution.



66
67
68
# File 'lib/denko/sensor/ds18b20.rb', line 66

def resolution
  @resolution
end

Instance Method Details

#_readObject



16
17
18
19
# File 'lib/denko/sensor/ds18b20.rb', line 16

def _read
  convert
  read_scratch(9) { |data| self.update(data) }
end

#convertObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/denko/sensor/ds18b20.rb', line 21

def convert
  set_convert_time

  atomically do
    match
    bus.write(CONVERT_T)
    sleep @convert_time if bus.parasite_power
  end
  sleep @convert_time unless bus.parasite_power
end

#decode_resolution(bytes) ⇒ Object



52
53
54
55
56
# File 'lib/denko/sensor/ds18b20.rb', line 52

def decode_resolution(bytes)
  config_byte = bytes[4]
  offset = config_byte >> 5
  offset + 9
end

#decode_temperature(bytes) ⇒ Object

Temperature is the first 16 bits (2 bytes of 9 read). It’s a signed, 2’s complement, little-endian decimal. LSB = 2 ^ -4.



62
63
64
# File 'lib/denko/sensor/ds18b20.rb', line 62

def decode_temperature(bytes)
  bytes[0..1].pack('C*').unpack('s<')[0] * (2.0 ** -4)
end

#pre_callback_filter(bytes) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/denko/sensor/ds18b20.rb', line 36

def pre_callback_filter(bytes)
  return { crc_error: true } unless OneWire::Helper.crc(bytes)

  @resolution ||= decode_resolution(bytes)
  reading[:temperature] = decode_temperature(bytes)

  reading
end

#readingObject



12
13
14
# File 'lib/denko/sensor/ds18b20.rb', line 12

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

#set_convert_timeObject



32
33
34
# File 'lib/denko/sensor/ds18b20.rb', line 32

def set_convert_time
  @convert_time = 0.75 / (2 ** (12 - (@resolution || 12)))
end

#stateObject



8
9
10
# File 'lib/denko/sensor/ds18b20.rb', line 8

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

#update_state(reading) ⇒ Object



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

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