kinetosis 0.0.2-universal-darwin-10

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Peter Hellberg
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ = Kinetosis
2
+
3
+ Read XYZ orientation data from the Sudden Motion Sensor
4
+ present in most recent Apple laptops.
5
+
6
+ Kinetosis is a small Ruby extension written in C.
7
+
8
+ It is largely based on the old {sudden_motion_sensor}[https://github.com/xunker/sudden_motion_sensor]
9
+ gem but I’ve removed the dependency on RubyInline and added a few specs.
10
+
11
+ I’ve tested Kinetosis on 1.9.2, 1.8.7 and REE.
12
+
13
+ == Prerequisites
14
+
15
+ * Apple laptop with the Sudden Motion Sensor
16
+ * Snow Leopard (Probably)
17
+
18
+ == Installation
19
+
20
+ gem install kinetosis
21
+
22
+ == Usage example
23
+
24
+ require 'kinetosis'
25
+
26
+ include Kinetosis
27
+
28
+ # Trap ^C and exit
29
+ trap("SIGINT") { exit! }
30
+
31
+ # Call and print the result from the xyz method
32
+ loop do
33
+ c = xyz.map { |v| v.to_s.ljust(4) }
34
+ puts "x: #{c[0]} y: #{c[1]} z: #{c[2]}"
35
+ end
36
+
37
+ === You would probably do something like this in the real world™:
38
+
39
+ require 'kinetosis'
40
+
41
+ class MacBookPro
42
+ include Kinetosis
43
+ end
44
+
45
+ mbp = MacBookPro.new
46
+
47
+ puts mbp.x
48
+
49
+ == LICENSE
50
+
51
+ Copyright (c) 2011 Peter Hellberg
52
+
53
+ Permission is hereby granted, free of charge, to any person obtaining a copy
54
+ of this software and associated documentation files (the "Software"), to deal
55
+ in the Software without restriction, including without limitation the rights
56
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
57
+ copies of the Software, and to permit persons to whom the Software is
58
+ furnished to do so, subject to the following conditions:
59
+
60
+ The above copyright notice and this permission notice shall be included in
61
+ all copies or substantial portions of the Software.
62
+
63
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
64
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
65
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
66
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
67
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
68
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
69
+ THE SOFTWARE.
@@ -0,0 +1,9 @@
1
+ require 'rake/testtask'
2
+
3
+ task :default => :test
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.test_files = FileList['spec/*_spec.rb']
7
+ t.ruby_opts = ['-rubygems'] if defined? Gem
8
+ t.ruby_opts << '-I.'
9
+ end
@@ -0,0 +1,13 @@
1
+ # Loads mkmf which is used to make makefiles for Ruby extensions
2
+ require 'mkmf'
3
+
4
+ $LDFLAGS << ' -x objective-c -framework IOKit'
5
+
6
+ # Give it a name
7
+ extension_name = 'kinetosis'
8
+
9
+ # The destination
10
+ dir_config(extension_name)
11
+
12
+ # Do the work
13
+ create_makefile(extension_name)
@@ -0,0 +1,87 @@
1
+ // Include the Ruby headers and goodies
2
+ #include "ruby.h"
3
+ #include <IOKit/IOKitLib.h>
4
+
5
+ // Defining a space for information and references about the module to be stored internally
6
+ VALUE Kinetosis = Qnil;
7
+
8
+ // Prototype for the initialization method - Ruby calls this, not you
9
+ void Init_kinetosis();
10
+
11
+ // Prototype for our method 'xyz' - methods are prefixed by 'method_' here
12
+ static VALUE method_xyz();
13
+
14
+ // The initialization method for this module
15
+ void Init_kinetosis() {
16
+ Kinetosis = rb_define_module("Kinetosis");
17
+ rb_define_method(Kinetosis, "xyz", method_xyz, 0);
18
+ }
19
+
20
+ static VALUE method_xyz(){
21
+ struct data {
22
+ signed short x;
23
+ signed short y;
24
+ signed short z;
25
+ char pad[34];
26
+ };
27
+
28
+ kern_return_t result;
29
+
30
+ mach_port_t masterPort;
31
+ IOMasterPort(MACH_PORT_NULL, &masterPort);
32
+ CFMutableDictionaryRef matchingDictionary = IOServiceMatching("SMCMotionSensor");
33
+
34
+ io_iterator_t iterator;
35
+ result = IOServiceGetMatchingServices(masterPort, matchingDictionary, &iterator);
36
+
37
+ if(result != KERN_SUCCESS) {
38
+ return rb_str_new2("Error");
39
+ }
40
+
41
+ io_object_t device = IOIteratorNext(iterator);
42
+ IOObjectRelease(iterator);
43
+ if(device == 0){
44
+ return rb_str_new2("Error");
45
+ }
46
+
47
+ io_connect_t dataPort;
48
+ result = IOServiceOpen(device, mach_task_self(), 0, &dataPort);
49
+ IOObjectRelease(device);
50
+
51
+ if(result != KERN_SUCCESS) {
52
+ return rb_str_new2("Error");
53
+ }
54
+
55
+ IOItemCount structureInputSize;
56
+ size_t structureOutputSize;
57
+
58
+ struct data inputStructure;
59
+ struct data outputStructure;
60
+ structureInputSize = sizeof(struct data);
61
+ structureOutputSize = sizeof(struct data);
62
+
63
+ memset(&inputStructure, 1, sizeof(inputStructure));
64
+ memset(&outputStructure, 0, sizeof(outputStructure));
65
+
66
+ result = IOConnectCallStructMethod(
67
+ (mach_port_t)dataPort,
68
+ (uint32_t)5,
69
+ (const void*)&inputStructure,
70
+ structureInputSize,
71
+ (void*)&outputStructure,
72
+ &structureOutputSize
73
+ );
74
+
75
+ if(result != KERN_SUCCESS) {
76
+ return rb_str_new2("Error");
77
+ }
78
+
79
+ IOServiceClose(dataPort);
80
+
81
+ VALUE coords = rb_ary_new2(3);
82
+ rb_ary_store(coords, 0, INT2FIX(outputStructure.x));
83
+ rb_ary_store(coords, 1, INT2FIX(outputStructure.y));
84
+ rb_ary_store(coords, 2, INT2FIX(outputStructure.z));
85
+
86
+ return coords;
87
+ }
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/../ext/kinetosis'
2
+
3
+ module Kinetosis
4
+ VERSION = '0.0.2'
5
+
6
+ def x
7
+ xyz[0]
8
+ end
9
+
10
+ def y
11
+ xyz[1]
12
+ end
13
+
14
+ def z
15
+ xyz[2]
16
+ end
17
+ end
@@ -0,0 +1,32 @@
1
+ require 'minitest/pride'
2
+ require 'minitest/autorun'
3
+ require 'minitest/spec'
4
+
5
+ require File.dirname(__FILE__) + '/../lib/kinetosis'
6
+
7
+ describe Kinetosis do
8
+ before do
9
+ @mbp = Object.new.extend(Kinetosis)
10
+ end
11
+
12
+ describe "when asked for xyz" do
13
+ it "should respond with an array consisting of three fixnums" do
14
+ @mbp.xyz.size.must_equal 3
15
+ @mbp.xyz.map { |v| v.class.must_equal Fixnum }
16
+ end
17
+ end
18
+
19
+ describe "when asked about x and y individually" do
20
+ it "should respond with integers" do
21
+ (-255..255).must_include @mbp.x
22
+ (-255..255).must_include @mbp.y
23
+ end
24
+ end
25
+
26
+ describe "when the laptop is standing on a table" do
27
+ it "should respond with an x = <5 and y = <0" do
28
+ (0..5).must_include @mbp.x
29
+ (-5..0).must_include @mbp.y
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kinetosis
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: universal-darwin-10
12
+ authors:
13
+ - Peter Hellberg
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-15 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: minitest
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Read the XYZ orientation from the Sudden Motion Sensor in most Macbooks.
36
+ email: peter@c7.se
37
+ executables: []
38
+
39
+ extensions:
40
+ - ext/extconf.rb
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ - MIT-LICENSE
44
+ files:
45
+ - lib/kinetosis.rb
46
+ - MIT-LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - .gemtest
50
+ - ext/extconf.rb
51
+ - ext/kinetosis.c
52
+ - spec/kinetosis_spec.rb
53
+ has_rdoc: true
54
+ homepage: https://github.com/peterhellberg/kinetosis
55
+ licenses:
56
+ - MIT-LICENSE
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --main
60
+ - README.rdoc
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ - ext
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 57
71
+ segments:
72
+ - 1
73
+ - 8
74
+ - 7
75
+ version: 1.8.7
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements:
86
+ - Apple laptop with the Sudden Motion Sensor
87
+ - Snow Leopard (probably)
88
+ rubyforge_project:
89
+ rubygems_version: 1.5.0
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: XYZ orientation from the Apple Sudden Motion Sensor
93
+ test_files:
94
+ - spec/kinetosis_spec.rb