co2mon 0.2.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: 225e5dfeefcbbee476233c59435787a6e9583d27
4
+ data.tar.gz: 83d21e7b76b33449175c52bc4832f1340732fb31
5
+ SHA512:
6
+ metadata.gz: 1d0c191cff8e53fcac1c39461deec0cb9392dbbe5b57febe176674c7ca55f8101460f1ce750220c71be1a5ce4b754306d3697c542315fd57badf01581f144c7d
7
+ data.tar.gz: d5856cf0b733827f74dd5ec2736cc158af1a5ce9a42d3a6e9348de1c7b553f54e66f2942d8675401d583621dc9ff805103182abdbc7a1bddf74017c44e1e47de
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at arkadiy.butermanov@flatstack.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in co2mon.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Arkadiy Butermanov
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,7 @@
1
+ # Co2mon
2
+
3
+ Based on https://github.com/dmage/co2mon
4
+
5
+ ## Requirements
6
+
7
+ $ brew install hidapi
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "co2mon"
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
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/co2mon.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'co2mon/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "co2mon"
8
+ spec.version = Co2mon::VERSION
9
+ spec.authors = ["Arkadiy Butermanov"]
10
+ spec.email = ["arkadiy.butermanov@flatstack.com"]
11
+
12
+ spec.summary = %q{FFI bindings for MasterKit CO2 Monitor}
13
+ spec.homepage = "https://github.com/arkadiybutermanov/co2mon"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "bin"
18
+ spec.extensions << './ext/co2mon/extconf.rb'
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "ffi"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
@@ -0,0 +1,222 @@
1
+ /*
2
+ * co2mon - programming interface to CO2 sensor.
3
+ * Copyright (C) 2015 Oleg Bulatov <oleg@bulatov.me>
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ #include <fcntl.h>
20
+ #include <stdint.h>
21
+ #include <stdio.h>
22
+ #include <stdlib.h>
23
+ #include <string.h>
24
+ #include <time.h>
25
+ #include <unistd.h>
26
+ #include <hidapi/hidapi.h>
27
+ #include <ruby.h>
28
+
29
+ #define CODE_TAMB 0x42 /* Ambient Temperature */
30
+ #define CODE_CNTR 0x50 /* Relative Concentration of CO2 */
31
+
32
+ uint16_t co2mon_data[256];
33
+
34
+ typedef hid_device *co2mon_device;
35
+
36
+ typedef unsigned char co2mon_data_t[8];
37
+
38
+ hid_device *
39
+ co2mon_open_device()
40
+ {
41
+ hid_device *dev = hid_open(0x04d9, 0xa052, NULL);
42
+ return dev;
43
+ }
44
+
45
+ int
46
+ co2mon_device_path(hid_device *dev, char *str, size_t maxlen)
47
+ {
48
+ str[0] = '\0';
49
+ return 1;
50
+ }
51
+
52
+ int
53
+ co2mon_send_magic_table(hid_device *dev, co2mon_data_t magic_table)
54
+ {
55
+ int r = hid_send_feature_report(dev, magic_table, sizeof(co2mon_data_t));
56
+ if (r < 0 || r != sizeof(co2mon_data_t))
57
+ {
58
+ fprintf(stderr, "hid_send_feature_report: error\n");
59
+ return 0;
60
+ }
61
+ return 1;
62
+ }
63
+
64
+ static void
65
+ swap_char(unsigned char *a, unsigned char *b)
66
+ {
67
+ unsigned char tmp = *a;
68
+ *a = *b;
69
+ *b = tmp;
70
+ }
71
+
72
+ static void
73
+ decode_buf(co2mon_data_t result, co2mon_data_t buf, co2mon_data_t magic_table)
74
+ {
75
+ swap_char(&buf[0], &buf[2]);
76
+ swap_char(&buf[1], &buf[4]);
77
+ swap_char(&buf[3], &buf[7]);
78
+ swap_char(&buf[5], &buf[6]);
79
+
80
+ for (int i = 0; i < 8; ++i)
81
+ {
82
+ buf[i] ^= magic_table[i];
83
+ }
84
+
85
+ unsigned char tmp = (buf[7] << 5);
86
+ result[7] = (buf[6] << 5) | (buf[7] >> 3);
87
+ result[6] = (buf[5] << 5) | (buf[6] >> 3);
88
+ result[5] = (buf[4] << 5) | (buf[5] >> 3);
89
+ result[4] = (buf[3] << 5) | (buf[4] >> 3);
90
+ result[3] = (buf[2] << 5) | (buf[3] >> 3);
91
+ result[2] = (buf[1] << 5) | (buf[2] >> 3);
92
+ result[1] = (buf[0] << 5) | (buf[1] >> 3);
93
+ result[0] = tmp | (buf[0] >> 3);
94
+
95
+ const unsigned char magic_word[8] = "Htemp99e";
96
+ for (int i = 0; i < 8; ++i)
97
+ {
98
+ result[i] -= (magic_word[i] << 4) | (magic_word[i] >> 4);
99
+ }
100
+ }
101
+
102
+ int
103
+ co2mon_read_data(hid_device *dev, co2mon_data_t magic_table, co2mon_data_t result)
104
+ {
105
+ co2mon_data_t data = { 0 };
106
+ int actual_length = hid_read_timeout(dev, data, sizeof(co2mon_data_t), 5000 /* milliseconds */);
107
+ if (actual_length < 0)
108
+ {
109
+ fprintf(stderr, "hid_read_timeout: error\n");
110
+ return actual_length;
111
+ }
112
+ if (actual_length != sizeof(co2mon_data_t))
113
+ {
114
+ fprintf(stderr, "hid_read_timeout: transferred %d bytes, expected %lu bytes\n", actual_length, (unsigned long)sizeof(co2mon_data_t));
115
+ return 0;
116
+ }
117
+
118
+ decode_buf(result, data, magic_table);
119
+ return actual_length;
120
+ }
121
+
122
+ static double
123
+ decode_temperature(uint16_t w)
124
+ {
125
+ return (double)w * 0.0625 - 273.15;
126
+ }
127
+
128
+ static int *device_loop(co2mon_device dev)
129
+ {
130
+ co2mon_data_t magic_table = { 0 };
131
+ co2mon_data_t result;
132
+ int temp = 0;
133
+ int co = 0;
134
+ static int array [2];
135
+
136
+ if (!co2mon_send_magic_table(dev, magic_table))
137
+ {
138
+ fprintf(stderr, "Unable to send magic table to CO2 device\n");
139
+ return array;
140
+ }
141
+
142
+ while (co == 0 || temp == 0)
143
+ {
144
+ int r = co2mon_read_data(dev, magic_table, result);
145
+ if (r <= 0)
146
+ {
147
+ fprintf(stderr, "Error while reading data from device\n");
148
+ break;
149
+ }
150
+
151
+ if (result[4] != 0x0d)
152
+ {
153
+ fprintf(stderr, "Unexpected data from device (data[4] = %02hhx, want 0x0d)\n", result[4]);
154
+ continue;
155
+ }
156
+
157
+ unsigned char r0, r1, r2, r3, checksum;
158
+ r0 = result[0];
159
+ r1 = result[1];
160
+ r2 = result[2];
161
+ r3 = result[3];
162
+ checksum = r0 + r1 + r2;
163
+ if (checksum != r3)
164
+ {
165
+ fprintf(stderr, "checksum error (%02hhx, await %02hhx)\n", checksum, r3);
166
+ continue;
167
+ }
168
+
169
+ uint16_t w = (result[1] << 8) + result[2];
170
+
171
+ switch (r0)
172
+ {
173
+ case CODE_TAMB:
174
+ temp = (int)decode_temperature(w);
175
+
176
+ break;
177
+ case CODE_CNTR:
178
+ if ((unsigned)w > 3000) {
179
+ // Avoid reading spurious (uninitialized?) data
180
+ break;
181
+ }
182
+
183
+ co = (int)w;
184
+
185
+ break;
186
+ }
187
+ }
188
+
189
+ array[0] = co;
190
+ array[1] = temp;
191
+
192
+ return array;
193
+ }
194
+
195
+ static int *main_loop()
196
+ {
197
+ static int empty_result[2] = {0};
198
+
199
+ co2mon_device dev = co2mon_open_device();
200
+
201
+ if (dev == NULL)
202
+ {
203
+ return empty_result;
204
+ }
205
+
206
+ int *result = device_loop(dev);
207
+
208
+ hid_close(dev);
209
+
210
+ return result;
211
+ }
212
+
213
+ int *get()
214
+ {
215
+ hid_init();
216
+
217
+ int *result = main_loop();
218
+
219
+ hid_exit();
220
+
221
+ return result;
222
+ }
@@ -0,0 +1,21 @@
1
+ require 'mkmf'
2
+
3
+ LIBDIR = RbConfig::CONFIG['libdir']
4
+ INCLUDEDIR = RbConfig::CONFIG['includedir']
5
+ HEADER_DIRS = [
6
+ '/opt/local/include',
7
+ '/usr/local/include',
8
+ INCLUDEDIR,
9
+ '/usr/include'
10
+ ]
11
+ LIB_DIRS = [
12
+ '/opt/local/lib',
13
+ '/usr/local/lib',
14
+ LIBDIR,
15
+ '/usr/lib'
16
+ ]
17
+
18
+ dir_config('co2mon', HEADER_DIRS, LIB_DIRS)
19
+ find_header('hidapi/hidapi.h')
20
+ find_library('hidapi', "hid_init", "hid_exit")
21
+ create_makefile('co2mon')
data/lib/co2mon.rb ADDED
@@ -0,0 +1,20 @@
1
+ require "co2mon/version"
2
+ require "ffi"
3
+
4
+ module Co2mon
5
+ extend FFI::Library
6
+
7
+ ffi_lib File.expand_path("co2mon.bundle", __dir__)
8
+
9
+ attach_function :get, [], :pointer
10
+
11
+ def self.get_data
12
+ data = get.get_array_of_int32(0, 2)
13
+
14
+ {
15
+ :co2 => data[0],
16
+ :temp => data[1]
17
+ }
18
+ rescue => e
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Co2mon
2
+ VERSION = "0.2.0"
3
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: co2mon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Arkadiy Butermanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description:
56
+ email:
57
+ - arkadiy.butermanov@flatstack.com
58
+ executables: []
59
+ extensions:
60
+ - "./ext/co2mon/extconf.rb"
61
+ extra_rdoc_files: []
62
+ files:
63
+ - "./ext/co2mon/extconf.rb"
64
+ - ".gitignore"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - co2mon.gemspec
73
+ - ext/co2mon/co2mon.c
74
+ - ext/co2mon/extconf.rb
75
+ - lib/co2mon.rb
76
+ - lib/co2mon/version.rb
77
+ homepage: https://github.com/arkadiybutermanov/co2mon
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.5.1
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: FFI bindings for MasterKit CO2 Monitor
101
+ test_files: []