Class: Denko::Sensor::DS18B20

Inherits:
OneWire::Peripheral show all
Defined in:
lib/denko/sensor/ds18b20.rb

Constant Summary collapse

FAMILY_CODE =
0x28

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

Instance Attribute Summary

Attributes inherited from OneWire::Peripheral

#address

Attributes included from Behaviors::Threaded

#interrupts_enabled, #thread

Attributes included from Behaviors::Callbacks

#callback_mutex

Attributes included from Behaviors::BusPeripheral

#address

Attributes included from Behaviors::Component

#board

Instance Method Summary collapse

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, #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 Behaviors::BusPeripheralAddressed

#before_initialize

Methods included from Behaviors::BusPeripheral

#atomically, #before_initialize

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Method Details

#_readObject



44
45
46
47
# File 'lib/denko/sensor/ds18b20.rb', line 44

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

#convertObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/denko/sensor/ds18b20.rb', line 32

def convert
  @resolution ||= 12
  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



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

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.



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

def decode_temperature(bytes)
  celsius = bytes[0..1].pack('C*').unpack('s<')[0] * (2.0 ** -4)
  fahrenheit = (celsius * 1.8 + 32).round(4)

  {celsius: celsius, fahrenheit: fahrenheit}
end

#pre_callback_filter(bytes) ⇒ Object



49
50
51
52
53
54
# File 'lib/denko/sensor/ds18b20.rb', line 49

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

  decode_temperature(bytes).merge(raw: bytes)
end

#resolutionObject



10
11
12
# File 'lib/denko/sensor/ds18b20.rb', line 10

def resolution
  @resolution ||= decode_resolution(scratch)
end

#resolution=(bits) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/denko/sensor/ds18b20.rb', line 14

def resolution=(bits)
  unless (9..12).include?(bits)
    raise ArgumentError, 'Invalid DS18B20 resolution, expected 9 to 12'
  end

  return bits if decode_resolution(scratch) == bits

  eeprom = scratch[2..4]
  eeprom[2] = 0b00011111 | ((bits - 9) << 5)
  write_scratch(eeprom)
  copy_scratch
  @resolution = bits
end

#scratchObject



6
7
8
# File 'lib/denko/sensor/ds18b20.rb', line 6

def scratch
  @state ? @state[:raw] : read_scratch(9)
end

#set_convert_timeObject



28
29
30
# File 'lib/denko/sensor/ds18b20.rb', line 28

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