Sha256: 8b7ad000a081853931cbb90c379a63ddf6b83aa1e7556f26254dadee641a834a

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

module Brewby
  module Inputs
    class DS18B20
      attr_accessor :name, :hardware_id, :device_path
      def initialize options = {}
        @name = options[:name]
        @device_path = options[:device_path] || "/sys/bus/w1/devices"
        @hardware_id = options[:hardware_id] || find_hardware_id
      end

      def find_hardware_id
        w1_devices[0].gsub("#{device_path}/",'')
      end

      def w1_devices
        Dir["#{device_path}/28*"]
      end

      def read
        raw = read_raw

        if tempC = parse(raw)
          tempF = to_fahrenheit tempC
        else
          tempC
        end
      end

      def parse raw_data
        if temp_data = raw_data.match(/t=([0-9]+)/)
          temp_data[1].to_f / 1000
        else
          nil
        end
      end

      def to_fahrenheit temp
        ((temp * 1.8) + 32).round(3)
      end

      def read_raw
        File.read device_file
      end

      def device_file
        "/sys/bus/w1/devices/#{@hardware_id}/w1_slave"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
brewby-0.1.1 lib/brewby/inputs/ds18b20.rb
brewby-0.1.0 lib/brewby/inputs/ds18b20.rb