dht_sensor 0.0.1

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: 4195274066348fe61c4ff857135100fc15439aee
4
+ data.tar.gz: 0d3acc0a87ac1c235dd8f845e3091a410619869b
5
+ SHA512:
6
+ metadata.gz: 484f98da5da92cb28d3aa43ea5c0d887a8d7df7f9f29fb3bbe4a9dbed006f16338c47e10f2a7e941b0a13edc55e281a105b63b7b42b42fd77011fac07ab3d63b
7
+ data.tar.gz: df1185ab78faa9deea047ea1356aea2408f2e776adbc632b8fd86eb1cbb84fb5ae45596133a77be74e997b6a27c1fcf2fc61e32dc497d548944633def04b5bfe
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dht_sensor.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Benjamin Guest
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # DhtSensor
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'dht_sensor'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install dht_sensor
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/dht_sensor/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 2. Write tests!
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/extensiontask"
3
+ require 'rspec/core/rake_task'
4
+
5
+ Rake::ExtensionTask.new 'dht_sensor' do |ext|
6
+ ext.lib_dir = "lib/dht_sensor"
7
+ end
8
+
9
+ RSpec::Core::RakeTask.new(:spec)
10
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dht_sensor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dht_sensor"
8
+ spec.version = DhtSensor::VERSION
9
+ spec.authors = ["Benjamin Guest"]
10
+ spec.email = ["benguest@gmail.com"]
11
+ spec.summary = %q{Ruby C extension to use the DHT-XX type humidity/temperature sensors}
12
+ spec.description = %q{Ruby C extension to access temperature and humidity reading taken by the DHT-XX type sensors, see (http://www.adafruit.com/products/385)}
13
+ spec.homepage = "http://github.com/bguest/dht_sensor"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+ spec.extensions = %w[ext/dht_sensor/extconf.rb]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake-compiler", "~> 0.9.2"
24
+ spec.add_development_dependency "rspec", "~> 2.14"
25
+ spec.add_development_dependency 'mocha', "~> 1.0"
26
+ spec.add_development_dependency "simplecov", "~> 0.8.2"
27
+ spec.add_development_dependency "rake", "~> 10.2"
28
+ end
data/example.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'dht_sensor'
2
+
3
+ sensor = DhtSensor.new(22,4)
4
+ temp, humidity = sensor.to_a
5
+ if temp.between?(-0.0001,0.0001) && humidity.between?(-0.0001,0.0001)
6
+ puts "Error: DHT22 is stupid"
7
+ puts "Temp: #{temp}, Humidity:#{humidity}"
8
+ else
9
+ puts "Temperature = #{temp*9/5+32} °F"
10
+ puts "Humidity = #{humidity}%"
11
+ end
@@ -0,0 +1,53 @@
1
+ // Include the Ruby headers and goodies
2
+ #include "ruby.h"
3
+ #include "dhtreader.h"
4
+
5
+ // Defining a space for information and references about the class to be stored internally
6
+ VALUE DhtSensor = Qnil;
7
+
8
+ // Prototype for the initialization method - Ruby calls this, not you
9
+ void Init_dht_sensor();
10
+
11
+ // Prototype for our methods - methods are prefixed by 'method_' here
12
+ VALUE method_initialize(VALUE self, VALUE type, VALUE pin);
13
+ VALUE method_to_a(VALUE self);
14
+
15
+ // The initialization method for this module
16
+ void Init_dht_sensor() {
17
+ DhtSensor = rb_define_class("DhtSensor", rb_cObject);
18
+ rb_define_method(DhtSensor, "initialize", method_initialize, 2);
19
+ rb_define_method(DhtSensor, "to_a", method_to_a, 0);
20
+ }
21
+
22
+ VALUE method_initialize(VALUE self, VALUE type, VALUE pin){
23
+ ID id_type = rb_intern("type");
24
+ ID id_pin = rb_intern("pin");
25
+
26
+ rb_ivar_set(self, id_type, type);
27
+ rb_ivar_set(self, id_pin, pin);
28
+
29
+ bcm2835_init();
30
+
31
+ return self;
32
+ }
33
+
34
+ VALUE method_to_a(VALUE self){
35
+ float temp, hum;
36
+ int type_i, pin_i;
37
+ VALUE return_array, temperature, humidity;
38
+
39
+ ID id_type = rb_intern("type");
40
+ ID id_pin = rb_intern("pin");
41
+
42
+ type_i = NUM2INT(rb_ivar_get(self, id_type));
43
+ pin_i = NUM2INT(rb_ivar_get(self, id_pin));
44
+
45
+ readDHT(type_i, pin_i, &temp, &hum);
46
+
47
+ return_array = rb_ary_new2(2);
48
+ temperature = DBL2NUM(temp);
49
+ humidity = DBL2NUM(hum);
50
+ rb_ary_push(return_array, temperature);
51
+ rb_ary_push(return_array, humidity);
52
+ return return_array;
53
+ }
@@ -0,0 +1,108 @@
1
+ /* Python module for DHT temperature/humidity sensors
2
+ *
3
+ * Modified by Qingping Hou from DHT reader example, original header:
4
+ *
5
+ * How to access GPIO registers from C-code on the Raspberry-Pi
6
+ * Example program
7
+ * 15-January-2012
8
+ * Dom and Gert
9
+ */
10
+ #define _BSD_SOURCE // for usleep
11
+
12
+ #define MAXTIMINGS 100
13
+
14
+ //#define DEBUG
15
+
16
+ #define DHT11 11
17
+ #define DHT22 22
18
+ #define AM2302 22
19
+
20
+ #include "dhtreader.h"
21
+
22
+ int readDHT(int type, int pin, float *temp_p, float *hum_p)
23
+ {
24
+ int counter = 0;
25
+ int laststate = HIGH;
26
+ int i = 0;
27
+ int j = 0;
28
+ int checksum = 0;
29
+ #ifdef DEBUG
30
+ int bitidx = 0;
31
+ int bits[250];
32
+ #endif
33
+ int data[100];
34
+
35
+ // Set GPIO pin to output
36
+ bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);
37
+
38
+ bcm2835_gpio_write(pin, HIGH);
39
+ usleep(500000); // 500 ms
40
+ bcm2835_gpio_write(pin, LOW);
41
+ usleep(20000);
42
+
43
+ bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);
44
+
45
+ data[0] = data[1] = data[2] = data[3] = data[4] = 0;
46
+
47
+ // wait for pin to drop?
48
+ while (bcm2835_gpio_lev(pin) == 1) {
49
+ usleep(1);
50
+ }
51
+
52
+ // read data!
53
+ for (i = 0; i < MAXTIMINGS; i++) {
54
+ counter = 0;
55
+ while ( bcm2835_gpio_lev(pin) == laststate) {
56
+ counter++;
57
+ //nanosleep(1); // overclocking might change this?
58
+ if (counter == 1000)
59
+ break;
60
+ }
61
+ laststate = bcm2835_gpio_lev(pin);
62
+ if (counter == 1000) break;
63
+ #ifdef DEBUG
64
+ bits[bitidx++] = counter;
65
+ #endif
66
+
67
+ if ((i>3) && (i%2 == 0)) {
68
+ // shove each bit into the storage bytes
69
+ data[j/8] <<= 1;
70
+ if (counter > 200)
71
+ data[j/8] |= 1;
72
+ j++;
73
+ }
74
+ }
75
+
76
+ #ifdef DEBUG
77
+ for (int ii=3; ii<bitidx; ii+=2) {
78
+ printf("bit %d: %d\n", ii-3, bits[ii]);
79
+ printf("bit %d: %d (%d)\n", ii-2, bits[ii+1], bits[ii+1] > 200);
80
+ }
81
+ printf("Data (%d): 0x%x 0x%x 0x%x 0x%x 0x%x\n", j, data[0], data[1], data[2], data[3], data[4]);
82
+ #endif
83
+
84
+ if (j >= 39) {
85
+ checksum = (data[0] + data[1] + data[2] + data[3]) & 0xFF;
86
+ if (data[4] == checksum) {
87
+ /* yay! checksum is valid */
88
+ if (type == DHT11) {
89
+ /*printf("Temp = %d *C, Hum = %d \%\n", data[2], data[0]);*/
90
+ *temp_p = (float)data[2];
91
+ *hum_p = (float)data[0];
92
+ } else if (type == DHT22) {
93
+ *hum_p = data[0] * 256 + data[1];
94
+ *hum_p /= 10;
95
+
96
+ *temp_p = (data[2] & 0x7F)* 256 + data[3];
97
+ *temp_p /= 10.0;
98
+ if (data[2] & 0x80)
99
+ *temp_p *= -1;
100
+ /*printf("Temp = %.1f *C, Hum = %.1f \%\n", f, h);*/
101
+ }
102
+ return 0;
103
+ }
104
+ return -2;
105
+ }
106
+
107
+ return -1;
108
+ }
@@ -0,0 +1,21 @@
1
+
2
+ /* for usleep */
3
+ //#define _BSD_SOURCE
4
+
5
+ #include <stdio.h>
6
+ #include <string.h>
7
+ #include <stdlib.h>
8
+ #include <dirent.h>
9
+ #include <fcntl.h>
10
+ #include <assert.h>
11
+ #include <unistd.h>
12
+ #include <sys/mman.h>
13
+ #include <sys/types.h>
14
+ #include <sys/stat.h>
15
+ #include <sys/time.h>
16
+ #include <bcm2835.h>
17
+ #include <unistd.h>
18
+
19
+ int readDHT(int type, int pin, float *temp_p, float *hum_p);
20
+ int readDHT2(int type, int pin, float *temp_p, float *hum_p);
21
+
@@ -0,0 +1,55 @@
1
+ # Loads mkmf which is used to make makefiles for Ruby extensions
2
+ require 'mkmf'
3
+
4
+ # Load Libraries
5
+
6
+ LIBDIR = RbConfig::CONFIG['libdir']
7
+ INCLUDEDIR = RbConfig::CONFIG['includedir']
8
+
9
+ HEADER_DIRS = [
10
+ # First search /opt/local for macports
11
+ '/opt/local/include',
12
+
13
+ # Then search /usr/local for people that installed from source
14
+ '/usr/local/include',
15
+
16
+ # Check the ruby install locations
17
+ INCLUDEDIR,
18
+
19
+ # Finally fall back to /usr
20
+ '/usr/include',
21
+ ]
22
+
23
+ LIB_DIRS = [
24
+ # First search /opt/local for macports
25
+ '/opt/local/lib',
26
+
27
+ # Then search /usr/local for people that installed from source
28
+ '/usr/local/lib',
29
+
30
+ # Check the ruby install locations
31
+ LIBDIR,
32
+
33
+ # Finally fall back to /usr
34
+ '/usr/lib',
35
+ ]
36
+ unless find_header('bcm2835.h')
37
+ abort 'bcm2835 is missing'
38
+ end
39
+
40
+ unless have_library('bcm2835') && append_library($libs, 'bcm2835')
41
+ abort "Can't Appended Library bcm2835!"
42
+ end
43
+
44
+ dir_config('bcm2835', HEADER_DIRS, LIB_DIRS)
45
+
46
+ $CFLAGS << ' -std=c99'
47
+
48
+ # Give it a name
49
+ #extension_name = 'dht_sensor'
50
+
51
+ # The destination
52
+ #dir_config(extension_name)
53
+
54
+ # Do the work
55
+ create_makefile("dht_sensor/dht_sensor")
@@ -0,0 +1,29 @@
1
+ class DhtStub
2
+ class NullLogger
3
+ def debug(*) end
4
+ end
5
+
6
+ # Note: #type and #pin are not yet available on the dth_sensor class
7
+ attr_reader :type, :pin
8
+
9
+ def initialize(type, pin, logger: NullLogger.new)
10
+ @type = type
11
+ logger.debug "DHT Type --> #{type}"
12
+
13
+ @pin = pin
14
+ logger.debug "DHT Pin --> #{pin}"
15
+
16
+ @temperature = 42.0
17
+ @humidity = 69.0
18
+
19
+ self
20
+ end
21
+
22
+ def to_a
23
+ [@temperature, @humidity]
24
+ end
25
+
26
+ private
27
+
28
+ attr_writer :humidity, :temperature
29
+ end
@@ -0,0 +1,3 @@
1
+ class DhtSensor
2
+ VERSION = "0.0.1"
3
+ end
data/lib/dht_sensor.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "dht_sensor/version"
2
+ require "dht_sensor/dht_sensor"
3
+
4
+ #module DhtSensor
5
+ ## Your code goes here...
6
+ #end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe DhtStub do
4
+
5
+ let(:dht_stub){DhtStub.new(22,4)}
6
+
7
+ before do
8
+ @logger = double()
9
+ end
10
+
11
+ describe '#new' do
12
+
13
+ it 'should set type and pin' do
14
+ dht_stub.type.should == 22
15
+ dht_stub.pin.should == 4
16
+ end
17
+
18
+ it 'should log that pin and type are set' do
19
+ @logger.expects(:debug).with('DHT Type --> 42')
20
+ @logger.expects(:debug).with('DHT Pin --> 3')
21
+ DhtStub.new(42,3, logger: @logger)
22
+ end
23
+ end
24
+
25
+ describe '#to_a' do
26
+ it 'should respond with a temperature humidity array' do
27
+ dht_stub.send(:humidity=,34)
28
+ dht_stub.send(:temperature=,40)
29
+
30
+ arry = dht_stub.to_a
31
+ arry[0].should be_within(0.001).of(40)
32
+ arry[1].should be_within(0.001).of(34)
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,5 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+ require 'dht_sensor/dht_stub'
4
+ require 'rspec'
5
+ require 'mocha/api'
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dht_sensor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin Guest
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-30 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake-compiler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.8.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.8.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.2'
97
+ description: Ruby C extension to access temperature and humidity reading taken by
98
+ the DHT-XX type sensors, see (http://www.adafruit.com/products/385)
99
+ email:
100
+ - benguest@gmail.com
101
+ executables: []
102
+ extensions:
103
+ - ext/dht_sensor/extconf.rb
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - dht_sensor.gemspec
112
+ - example.rb
113
+ - ext/dht_sensor/dht_sensor.c
114
+ - ext/dht_sensor/dhtreader.c
115
+ - ext/dht_sensor/dhtreader.h
116
+ - ext/dht_sensor/extconf.rb
117
+ - lib/dht_sensor.rb
118
+ - lib/dht_sensor/dht_stub.rb
119
+ - lib/dht_sensor/version.rb
120
+ - spec/dht_stub_spec.rb
121
+ - spec/spec_helper.rb
122
+ homepage: http://github.com/bguest/dht_sensor
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.2.2
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Ruby C extension to use the DHT-XX type humidity/temperature sensors
146
+ test_files:
147
+ - spec/dht_stub_spec.rb
148
+ - spec/spec_helper.rb