Class: Brewby::Inputs::DS18B20
- Inherits:
-
Object
- Object
- Brewby::Inputs::DS18B20
- Defined in:
- lib/brewby/inputs/ds18b20.rb
Instance Attribute Summary collapse
-
#device_path ⇒ Object
Returns the value of attribute device_path.
-
#hardware_id ⇒ Object
Returns the value of attribute hardware_id.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #device_file ⇒ Object
- #find_hardware_id ⇒ Object
-
#initialize(options = {}) ⇒ DS18B20
constructor
A new instance of DS18B20.
- #parse(raw_data) ⇒ Object
- #read ⇒ Object
- #read_raw ⇒ Object
- #to_fahrenheit(temp) ⇒ Object
- #w1_devices ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ DS18B20
Returns a new instance of DS18B20.
5 6 7 8 9 |
# File 'lib/brewby/inputs/ds18b20.rb', line 5 def initialize = {} @name = [:name] @device_path = [:device_path] || "/sys/bus/w1/devices" @hardware_id = [:hardware_id] || find_hardware_id end |
Instance Attribute Details
#device_path ⇒ Object
Returns the value of attribute device_path.
4 5 6 |
# File 'lib/brewby/inputs/ds18b20.rb', line 4 def device_path @device_path end |
#hardware_id ⇒ Object
Returns the value of attribute hardware_id.
4 5 6 |
# File 'lib/brewby/inputs/ds18b20.rb', line 4 def hardware_id @hardware_id end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/brewby/inputs/ds18b20.rb', line 4 def name @name end |
Instance Method Details
#device_file ⇒ Object
45 46 47 |
# File 'lib/brewby/inputs/ds18b20.rb', line 45 def device_file "/sys/bus/w1/devices/#{@hardware_id}/w1_slave" end |
#find_hardware_id ⇒ Object
11 12 13 |
# File 'lib/brewby/inputs/ds18b20.rb', line 11 def find_hardware_id w1_devices[0].gsub("#{device_path}/",'') end |
#parse(raw_data) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/brewby/inputs/ds18b20.rb', line 29 def parse raw_data if temp_data = raw_data.match(/t=([0-9]+)/) temp_data[1].to_f / 1000 else nil end end |
#read ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/brewby/inputs/ds18b20.rb', line 19 def read raw = read_raw if tempC = parse(raw) tempF = to_fahrenheit tempC else tempC end end |
#read_raw ⇒ Object
41 42 43 |
# File 'lib/brewby/inputs/ds18b20.rb', line 41 def read_raw File.read device_file end |
#to_fahrenheit(temp) ⇒ Object
37 38 39 |
# File 'lib/brewby/inputs/ds18b20.rb', line 37 def to_fahrenheit temp ((temp * 1.8) + 32).round(3) end |
#w1_devices ⇒ Object
15 16 17 |
# File 'lib/brewby/inputs/ds18b20.rb', line 15 def w1_devices Dir["#{device_path}/28*"] end |