Neurogami-wiiremotejruby 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ Thu Jun 18 22:13:04 MST 2009
2
+ Moved code, but not git history, over from gitorious
3
+
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ Neurogami::SwingSet
2
+ ===================
3
+
4
+ by James Britt / Neurogami
5
+
6
+ http://www.neurogami.com
7
+
8
+
9
+ DESCRIPTION:
10
+ -------------------
11
+
12
+ WRITE ME!
13
+
14
+ FEATURES/PROBLEMS:
15
+ -------------------
16
+
17
+ WRITE ME!
18
+
19
+ SYNOPSIS
20
+ ------------
21
+
22
+ Install the gem.
23
+
24
+ In the root of your project, run
25
+
26
+ wiiremotejruby
27
+
28
+
29
+ This will copy over the lib files under lib/ruby/wiitemotejruby
30
+
31
+
32
+ WRITE ME!
33
+
34
+ REQUIREMENTS
35
+ -------------------
36
+ WRITE ME!
37
+
38
+ INSTALL
39
+ -------------------
40
+
41
+ sudo gem install Neurogami-WiiRemoteJRuby
42
+
43
+
44
+ LICENSE
45
+ --------------
46
+
47
+ (The MIT License)
48
+
49
+ Copyright (c) 2009 James Britt
50
+
51
+ Permission is hereby granted, free of charge, to any person obtaining
52
+ a copy of this software and associated documentation files (the
53
+ 'Software'), to deal in the Software without restriction, including
54
+ without limitation the rights to use, copy, modify, merge, publish,
55
+ distribute, sublicense, and/or sell copies of the Software, and to
56
+ permit persons to whom the Software is furnished to do so, subject to
57
+ the following conditions:
58
+
59
+ The above copyright notice and this permission notice shall be
60
+ included in all copies or substantial portions of the Software.
61
+
62
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
63
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
64
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
65
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
66
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
67
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
68
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ # Look in the tasks/setup.rb file for the various options that can be
2
+ # configured in this Rakefile. The .rake files in the tasks directory
3
+ # are where the options are used.
4
+
5
+ begin
6
+ require 'bones'
7
+ Bones.setup
8
+ rescue LoadError
9
+ begin
10
+ load 'tasks/setup.rb'
11
+ rescue LoadError
12
+ raise RuntimeError, '### please install the "bones" gem ###'
13
+ end
14
+ end
15
+
16
+ ensure_in_path 'lib'
17
+ require 'wiiremotejruby_utils'
18
+
19
+ task :default do
20
+ puts "There is no default task."
21
+ end
22
+
23
+ PROJ.name = 'wiiremotejruby'
24
+ PROJ.authors = 'James Britt / Neurogami'
25
+ PROJ.email = 'james@neurogami.com'
26
+ PROJ.url = 'http://github.com/Neurogami/wiiremotejruby/tree/master'
27
+ PROJ.version = Neurogami::WiiRemoteJRuby::VERSION
28
+ #PROJ.rubyforge.name = 'wiiremotejruby'
29
+ PROJ.readme_file = 'README.md'
30
+ PROJ.summary = "Nicer Ruby wrapper using the WiiRemoteJ library."
31
+ PROJ.spec.opts << '--color'
32
+
33
+ # EOF
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path( File.join(File.dirname(__FILE__), %w[.. lib wiiremotejruby_utils]))
4
+
5
+ # Put your code here
6
+
7
+ Neurogami::WiiRemoteJRuby.copy_over
8
+ # EOF
@@ -0,0 +1,6 @@
1
+
2
+ require 'wiiremotejruby_utils'
3
+
4
+ Neurogami::WiiRemoteJRuby.require_all_libs_relative_to(__FILE__)
5
+
6
+
@@ -0,0 +1,221 @@
1
+ class Java::WiiremotejEvent::WRButtonEvent
2
+
3
+ def details= h
4
+ @details = h
5
+ end
6
+
7
+ def details
8
+ @details || {}
9
+ end
10
+ end
11
+
12
+ module Neurogami
13
+
14
+ class Wiimote
15
+
16
+ def self.attach owner, map = {}
17
+ @owner = owner
18
+
19
+ warn "Finding remote .."
20
+
21
+ remote = nil
22
+
23
+ begin
24
+ remote = WiiRemoteJ::WiiRemoteJ.findRemote
25
+ warn "remote: #{remote.pretty_inspect}"
26
+ rescue Exception
27
+ warn "Error calling findRemote: #{$!}"
28
+ raise
29
+ end
30
+
31
+ unless remote
32
+ sleep 2
33
+ raise "Wiimote class could not attache to Wiimote!"
34
+ end
35
+
36
+
37
+ listener = WiiRemoteListener.new(remote)
38
+ listener.event_map(owner, map )
39
+ remote.addWiiRemoteListener(listener)
40
+ remote.accelerometerEnabled = true
41
+
42
+ # Need this if you want to track IR events
43
+ #remote.setIRSensorEnabled(true, WiiRemoteJEvent::WRIREvent.FULL, WiiRemoteJ::IRSensitivitySettings::WII_LEVEL_4)
44
+ # remote.enableContinuous
45
+
46
+ # This works, giving both IR and pitch, roll info
47
+ remote.setIRSensorEnabled(true, WiiRemoteJEvent::WRIREvent.BASIC, WiiRemoteJ::IRSensitivitySettings::WII_LEVEL_3)
48
+
49
+ # But this only gives IR data; pitch stuff ends up NaN or zero
50
+ #remote.setIRSensorEnabled(true, WiiRemoteJEvent::WRIREvent.FULL, WiiRemoteJ::IRSensitivitySettings::WII_LEVEL_3)
51
+
52
+ # remote.setIRSensorEnabled(true, WiiRemoteJEvent::WRIREvent.EXTENDED, WiiRemoteJ::IRSensitivitySettings::WII_LEVEL_3)
53
+ # remote.enableContinuous
54
+ remote.setLEDIlluminated(0, true);
55
+
56
+ remote
57
+ end
58
+ end
59
+
60
+
61
+ def add_to_mappings map
62
+ STDERR.puts( ":DEBUG #{__FILE__}:#{__LINE__}" ) if ENV['JAMES_SCA_JDEV_MACHINE'] # JGBDEBUG
63
+ listener = WiiRemoteListener.new(self)
64
+ listener.event_map(nil, map )
65
+ self.addWiiRemoteListener(listener)
66
+ # remote.accelerometerEnabled = true
67
+ self
68
+ end
69
+
70
+ class WiiRemoteListener
71
+ @@button_masks = {}
72
+
73
+ def initialize remote
74
+ @remote = remote
75
+ @accelerometer_source = true #//true = wii remote, false = nunchuk
76
+ warn " Created WiiRemoteListener"
77
+ STDERR.puts( ":DEBUG #{__FILE__}:#{__LINE__}" ) if ENV['JAMES_SCA_JDEV_MACHINE'] # JGBDEBUG
78
+ end
79
+
80
+ def event_map owner, map={}
81
+ @owner = owner
82
+ @maps ||= []
83
+ @maps << map
84
+ end
85
+
86
+ def accelerometer_source
87
+ @accelerometer_source
88
+ end
89
+
90
+ def combined_input_received evt
91
+ end
92
+
93
+ # So: There are several different buttons tha can be pressed
94
+ # The wiimote jar thing does not offer a way to simply *ask* for
95
+ # the button value. Instead, you have to play 20 questions.
96
+ #
97
+ #
98
+ def button_input_received event
99
+ dispatch_on_button event
100
+ end
101
+
102
+ def maps_contain key
103
+ @maps.each do |map|
104
+ return true if map[key]
105
+ end
106
+ false
107
+ end
108
+
109
+ def maps_handle_event event_key, event
110
+ @maps.each do |map|
111
+ map[event_key].call event
112
+ end
113
+ end
114
+
115
+ def acceleration_input_received event
116
+ return unless maps_contain :motion_sensing_event
117
+ maps_handle_event :motion_sensing_event, event
118
+ end
119
+
120
+ #
121
+ def IRInputReceived event
122
+ return unless maps_contain :ir_sensing_event
123
+ maps_handle_event :ir_sensing_event, event
124
+
125
+ end
126
+
127
+
128
+ def method_missing m, *args
129
+ m = " WiiRemoteListener mm: #{m}, #{args.inspect}"
130
+ STDERR.puts( ":DEBUG #{__FILE__}:#{__LINE__}\n#{}" ) if ENV['JAMES_SCA_JDEV_MACHINE'] # JGBDEBUG
131
+ end
132
+
133
+
134
+ # Here's the thing:
135
+ # A user can be doing any number of things with buttons, including
136
+ # pressing multiple buttons, holding buttons down, releasing buttons.
137
+ #
138
+ # We might want to loop over the mapping and only see if we have
139
+ # a matching event.
140
+ # For example,
141
+ # :wiimote_button_b maps to the B button pressed.
142
+ # :wiimote_button_b_wiimote_button_a would map to both A + B pressed.
143
+ # We still get lots of combos:
144
+ # :wiimote_button_b_only
145
+ #
146
+ # There's also 'is' and 'was' ... ?
147
+ #
148
+ # and 'released'
149
+ #
150
+ # :wiimote_button_b_was_released
151
+ #
152
+ # We need some formal syntax so we can create code:
153
+ #
154
+ # :wiimote_button_<x>_<action>
155
+ #
156
+ # isAnyPressed(int buttonMask)
157
+ # Returns true if any of the given buttons are pressed; otherwise false.
158
+ # boolean isOnlyPressed(int buttonMask)
159
+ # Returns true if all of the given buttons are pressed and no others are; otherwise false.
160
+ # boolean isPressed(int buttonMask)
161
+ # Returns true if all of the given buttons are pressed; otherwise false.
162
+ # boolean wasOnlyPressed(int buttonMask)
163
+ # Returns true if all of the given buttons were pressed and no others are; otherwise false.
164
+ # boolean wasPressed(int buttonMask)
165
+ # Returns true if all of the given buttons were pressed; otherwise false.
166
+ # boolean wasReleased(int buttonMask)
167
+ # Returns true if one of the given buttons wa
168
+ #
169
+ # We need to figure out, for each mapping, the button mask, and then the action to
170
+ # look for.
171
+ # Given -
172
+ # { :buttons => :two, :action => :only_pressed } => lambda {|e| exit_button_action_performed e },
173
+ # { :buttons => [:two, :b], :action => :released } => lambda {|e| release_example e },
174
+ #
175
+ ##
176
+ def dispatch_on_button event
177
+
178
+ @maps.each do |map|
179
+
180
+ map[:buttons].each do |m|
181
+ mask = buttons_to_mask(m[:buttons])
182
+ action = m[:action]
183
+ # Can we do some dynaimic invokation here?
184
+ event.details = {:buttons => m[:buttons] , :action => action };
185
+ case action
186
+ when :any_pressed
187
+ m[:handler].call(event) if event.isAnyPressed(mask)
188
+ when :is_only_pressed
189
+ m[:handler].call(event) if event.isOnlyPressed(mask)
190
+ when :pressed
191
+ m[:handler].call(event) if event.isPressed(mask)
192
+ when :was_only_pressed
193
+ m[:handler].call(event) if event.wasOnlyPressed(mask)
194
+ when :was_pressed
195
+ m[:handler].call(event) if event.wasPressed(mask)
196
+ when :was_released
197
+ m[:handler].call(event) if event.wasReleased(mask)
198
+ end
199
+
200
+ end
201
+ end
202
+
203
+ end
204
+
205
+
206
+ def buttons_to_mask buttons
207
+ if buttons.is_a?(Array)
208
+ mask = 0
209
+ buttons.each do |button|
210
+ @@button_masks[button] ||= eval "WiiRemoteJEvent::WRButtonEvent::#{button.to_s.upcase}"
211
+ mask =+ @@button_masks[button]
212
+ end
213
+ mask
214
+ else
215
+ @@button_masks[buttons] ||= eval "WiiRemoteJEvent::WRButtonEvent::#{buttons.to_s.upcase}"
216
+ @@button_masks[buttons]
217
+ end
218
+ end
219
+ end
220
+ end
221
+
@@ -0,0 +1,82 @@
1
+ require 'spinner_dialog'
2
+
3
+ class GlobalWiimote
4
+
5
+ @@wiimote = nil
6
+
7
+ def self.wiimote
8
+ @@wiimote
9
+ end
10
+
11
+ def self.wiimote= wiimote
12
+ @@wiimote = wiimote
13
+ end
14
+
15
+ def self.add_to_mappings wiimote, map
16
+ listener = Neurogami::WiiRemoteListener.new(wiimote)
17
+ listener.event_map(nil, map )
18
+ wiimote.addWiiRemoteListener(listener)
19
+ wiimote
20
+ end
21
+
22
+ end
23
+
24
+ module Neurogami
25
+
26
+ module Wiimotable
27
+ def wiimote_me event_map, max_attempts = nil
28
+ unless GlobalWiimote.wiimote
29
+ attempts_so_far = 0
30
+ begin
31
+ attempts_so_far += 1
32
+ transfer[:attempt_number] = attempts_so_far
33
+ signal :set_discovery_mode
34
+ GlobalWiimote.wiimote = Wiimote.attach self, event_map
35
+ GlobalWiimote.wiimote.setLEDIlluminated(0, true);
36
+ rescue Exception => e
37
+ signal :end_discovery_mode
38
+ if max_attempts.nil? || attempts_so_far < max_attempts
39
+ retry
40
+ else
41
+ raise e
42
+ end
43
+ end
44
+ else
45
+ # GlobalWiimote.wiimote = Wiimote.attach self, event_map
46
+ GlobalWiimote.add_to_mappings GlobalWiimote.wiimote, event_map
47
+ end
48
+ ensure
49
+ signal :end_discovery_mode
50
+ GlobalWiimote.wiimote
51
+ end
52
+
53
+ def clean_up_and_exit
54
+ GlobalWiimote.wiimote.setLEDIlluminated(0, false);
55
+ GlobalWiimote.wiimote.disconnect
56
+ java.lang.System.exit(0)
57
+ end
58
+ end
59
+
60
+
61
+
62
+ module WiimotableView
63
+
64
+ def set_discovery_mode model, transfer
65
+ begin
66
+ @spinner = SpinnerDialog.new(["Attempt #{transfer[:attempt_number] }", "Press buttons 1+2 together ..."])
67
+ @spinner.visible = true
68
+ rescue Exception
69
+ warn "Error creating SpinnerDialog: #{$!}"
70
+ raise
71
+ end
72
+ end
73
+
74
+ def end_discovery_mode model, transfer
75
+ return unless @spinner
76
+ @spinner.visible = false
77
+ @spinner.hide
78
+ @spinner = nil
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,67 @@
1
+ require 'fileutils'
2
+
3
+ module Neurogami
4
+ module WiiRemoteJRuby
5
+
6
+ # :stopdoc:
7
+ VERSION = '0.1.0'
8
+ LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
9
+ PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
10
+ # :startdoc:
11
+
12
+ # Returns the version string for the library.
13
+ #
14
+ def self.version
15
+ VERSION
16
+ end
17
+
18
+ # Returns the library path for the module. If any arguments are given,
19
+ # they will be joined to the end of the libray path using
20
+ # <tt>File.join</tt>.
21
+ #
22
+ def self.libpath( *args )
23
+ args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
24
+ end
25
+
26
+ # Returns the lpath for the module. If any arguments are given,
27
+ # they will be joined to the end of the path using
28
+ # <tt>File.join</tt>.
29
+ #
30
+ def self.path( *args )
31
+ args.empty? ? PATH : ::File.join(PATH, args.flatten)
32
+ end
33
+
34
+ # Utility method used to rquire all files ending in .rb that lie in the
35
+ # directory below this file that has the same name as the filename passed
36
+ # in. Optionally, a specific _directory_ name can be passed in such that
37
+ # the _filename_ does not have to be equivalent to the directory.
38
+ #
39
+ def self.require_all_libs_relative_to( fname, dir = nil )
40
+ dir ||= ::File.basename(fname, '.*')
41
+ search_me = ::File.expand_path(
42
+ ::File.join(::File.dirname(fname), dir, '**', '*.rb'))
43
+ Dir.glob(search_me).sort.each {|rb| require rb}
44
+ end
45
+
46
+
47
+ def self.copy_over base_path = 'lib/ruby/', end_dir = 'wiiremotejruby'
48
+ full_path = base_path + end_dir
49
+ require 'fileutils'
50
+ files = %w{wiimotable.rb wii_remote.rb }
51
+ here = File.dirname(File.expand_path(__FILE__))
52
+
53
+ FileUtils.mkdir_p(base_path) unless File.exist?(base_path)
54
+ FileUtils.mkdir_p(full_path ) unless File.exist?(full_path )
55
+
56
+ files.each do |file|
57
+ if File.exist? "#{full_path }/#{file}"
58
+ warn "It seems that the wiiremotejruby files already exist. Remove or rename them, and try again."
59
+ exit
60
+ end
61
+ FileUtils.cp_r "#{here}/wiiremotejruby/#{file}", full_path , :verbose => true
62
+ end
63
+ end
64
+
65
+ end
66
+ end
67
+
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Neurogami-wiiremotejruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Britt / Neurogami
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-18 00:00:00 -07:00
13
+ default_executable: wiiremotejruby
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bones
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.5.0
24
+ version:
25
+ description: ""
26
+ email: james@neurogami.com
27
+ executables:
28
+ - wiiremotejruby
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - History.txt
33
+ - bin/wiiremotejruby
34
+ files:
35
+ - History.txt
36
+ - README.md
37
+ - Rakefile
38
+ - bin/wiiremotejruby
39
+ - lib/wiiremotejruby.rb
40
+ - lib/wiiremotejruby/wii_remote.rb
41
+ - lib/wiiremotejruby/wiimotable.rb
42
+ - lib/wiiremotejruby_utils.rb
43
+ has_rdoc: false
44
+ homepage: http://github.com/Neurogami/wiiremotejruby/tree/master
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --main
48
+ - README.md
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project: !binary |
66
+ AA==
67
+
68
+ rubygems_version: 1.2.0
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Nicer Ruby wrapper using the WiiRemoteJ library.
72
+ test_files: []
73
+