ds18b20 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cd777d91d73f1ead320d2aa633a934b517ad424f
4
+ data.tar.gz: 236c03f35d0c660dcb51fa17e05f3290a2e6a51d
5
+ SHA512:
6
+ metadata.gz: b83e3c0000f296d1379dc089c5396edc03576bb71c40337e3d8c723ec064365977c9e32482654f616d1cf58d70273b51fb454b4d3c69495c1423a7d23f6665c5
7
+ data.tar.gz: 72096fa272b2d6094ca35600ac9adadee63f7b86235ba5b9238e1b14e70f5747b230d42d2d560cc7e4474af8c56cda129d4aefc6ea0e4861b4cd06d0dd2dc987
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ds18b20.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ds18b20 (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.0)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ ds18b20!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 1.16.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Owais
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ NOTE: In Development
2
+
3
+ [![Build Status](https://travis-ci.org/owaiswiz/ds18b20.svg?branch=master)](https://travis-ci.org/owaiswiz/ds18b20)
4
+
5
+ # DS18B20
6
+
7
+ This gem makes interfacing with the DS18B20 temperature easier over 1-Wire.
8
+
9
+
10
+ After setting it up correctly make sure you have the correct file path where it's output is being recorded.
11
+ The file name on raspberry pi should be `w1_slave`. If you don't know where the file is located - please read : [DS18B20 - Temperature Sensing](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing?view=all).
12
+
13
+
14
+ After making sure that you have the file path and that output looks similar to one in the tutorial above, you can easily use this gem following the instructions below (not yet released - soon).
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'ds18b20'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install ds18b20
31
+
32
+ ## Usage
33
+
34
+ In-development
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/owaiswiz/ds18b20.
43
+
44
+ ## License
45
+
46
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ds18b20"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/ds18b20.gemspec ADDED
@@ -0,0 +1,26 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ds18b20/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ds18b20"
8
+ spec.version = Ds18b20::VERSION
9
+ spec.authors = ["Owais"]
10
+ spec.email = ["owaiswiz@gmail.com"]
11
+
12
+ spec.summary = %q{A Gem to easily interface with the DS18B20 Temperature Sensor Over 1-Wire}
13
+ spec.homepage = "https://github.com/owaiswiz/ds18b20.git"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
data/lib/ds18b20.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "ds18b20/version"
2
+ require "ds18b20/parser"
3
+ module Ds18b20
4
+ end
@@ -0,0 +1,13 @@
1
+ module Ds18b20
2
+ class InvalidReadingError < StandardError
3
+ def initialize
4
+ super("An error occurred while reading the temperature.\nPlease make sure that the DS18B20 sensor is properly installed and you've specified the correct file.")
5
+ end
6
+ end
7
+
8
+ class InvalidFileSpecifiedError < StandardError
9
+ def initialize
10
+ super("An error occured while trying to read the file. Either the file you specified is not correct or the data in the file is not the one that is being expected by this program.\nPlease make sure that the file you specified has the data in a similar manner as this file: https://github.com/owaiswiz/ds18b20/blob/master/spec/support/valid_sample_positive_24_187C")
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,73 @@
1
+ require 'ds18b20/exceptions'
2
+ module Ds18b20
3
+ class Parser
4
+
5
+ ERROR_READINGS = [85000,127687]
6
+ MAXIMUM_RETRIES = 5
7
+
8
+ def initialize(file_location)
9
+ @file_location = file_location
10
+ @retries = 0
11
+ end
12
+
13
+ def get_temperature_celsius
14
+ get_temperature_from_file
15
+ end
16
+
17
+ def get_temperature_fahrenheit
18
+ (get_temperature_from_file * 9/5.0) + 32
19
+ end
20
+
21
+ def get_temperature_kelvin
22
+ get_temperature_from_file + 273.15
23
+ end
24
+
25
+ alias_method :celsius, :get_temperature_celsius
26
+ alias_method :fahrenheit, :get_temperature_fahrenheit
27
+ alias_method :kelvin, :get_temperature_kelvin
28
+
29
+ private
30
+ def get_temperature_from_file
31
+ content = read_file
32
+ data = content.split("\n")[1].split
33
+ raw_hex = (data[1][1] + data[0]).hex
34
+ reading_int = data.last.split("=").last.to_i
35
+
36
+ if ERROR_READINGS.include?(reading_int) || content.include?("crc=00 NO")
37
+ raise Ds18b20::InvalidReadingError
38
+ end
39
+
40
+ if raw_hex < 2048
41
+ # The result is positive and can be returned
42
+ # after dividing by 16
43
+ return raw_hex/16.0
44
+ else
45
+ # The result is negative and can be returned
46
+ # after taking 2's complement and dividing by 16
47
+ return -(twos_complement(raw_hex)/16.0)
48
+ end
49
+
50
+ rescue Ds18b20::InvalidReadingError
51
+ if @retries < MAXIMUM_RETRIES
52
+ @retries += 1
53
+ sleep 0.1
54
+ retry
55
+ else
56
+ raise Ds18b20::InvalidReadingError
57
+ end
58
+ end
59
+
60
+ def read_file
61
+ file = File.read(@file_location)
62
+ if file && file.include?("t=")
63
+ return file
64
+ end
65
+ raise Ds18b20::InvalidFileSpecifiedError
66
+ end
67
+
68
+ def twos_complement(number)
69
+ (number ^ ((1<<12) - 1)) + 1
70
+ end
71
+ end
72
+ end
73
+
@@ -0,0 +1,3 @@
1
+ module Ds18b20
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ds18b20
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Owais
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - owaiswiz@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - ds18b20.gemspec
73
+ - lib/ds18b20.rb
74
+ - lib/ds18b20/exceptions.rb
75
+ - lib/ds18b20/parser.rb
76
+ - lib/ds18b20/version.rb
77
+ homepage: https://github.com/owaiswiz/ds18b20.git
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.10
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A Gem to easily interface with the DS18B20 Temperature Sensor Over 1-Wire
101
+ test_files: []