Class: Denko::Sensor::DS18B20
Constant Summary
collapse
- FAMILY_CODE =
0x28
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
#address
#interrupts_enabled, #thread
#callback_mutex
#address
#board
Instance Method Summary
collapse
#address_bytes, #copy_scratch, #extract_serial, #match, #read_scratch, #serial_number, #write_scratch
#poll, #poll_using, #stop
#enable_interrupts, included, #stop, #stop_thread, #threaded, #threaded_loop
#read, #read_using, #wait_for_read
#add_callback, #callbacks, #initialize, #remove_callback, #update
#initialize, #state
#before_initialize
#atomically, #before_initialize
#initialize, #micro_delay
Instance Method Details
#_read ⇒ Object
44
45
46
47
|
# File 'lib/denko/sensor/ds18b20.rb', line 44
def _read
convert
read_scratch(9) { |data| self.update(data) }
end
|
#convert ⇒ Object
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
|
#resolution ⇒ Object
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
|
#scratch ⇒ Object
6
7
8
|
# File 'lib/denko/sensor/ds18b20.rb', line 6
def scratch
@state ? @state[:raw] : read_scratch(9)
end
|
#set_convert_time ⇒ Object
28
29
30
|
# File 'lib/denko/sensor/ds18b20.rb', line 28
def set_convert_time
@convert_time = 0.75 / (2 ** (12 - @resolution))
end
|