cwiid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ cwiid
2
+ =====
3
+
4
+ **Author**: Jan Wedekind
5
+ **Copyright**: 2010
6
+ **License**: GPL
7
+
8
+ Synopsis
9
+ --------
10
+
11
+ This Ruby extension provides access to a Wii Remote via L. Donnie Smith's libcwiid.
12
+
13
+ Installation
14
+ ------------
15
+
16
+ To install this Ruby extension, use the following command:
17
+
18
+ $ sudo gem install cwiid
19
+
20
+ Alternatively you can build and install the Ruby extension from source as follows:
21
+
22
+ $ rake
23
+ $ sudo rake install
24
+
25
+ Usage
26
+ -----
27
+
28
+ Simply run Interactive Ruby:
29
+
30
+ $ irb
31
+
32
+ Here's a small example displaying the state of the accelerometer:
33
+
34
+ #!/usr/bin/env ruby
35
+ require 'rubygems'
36
+ require 'cwiid'
37
+ require 'hornetseye_rmagick'
38
+ require 'hornetseye_xorg'
39
+ include Hornetseye
40
+ include Magick
41
+ wiimote = WiiMote.new
42
+ X11Display.show do |display|
43
+ display.status = wiimote.buttons != WiiMote::BTN_1
44
+ wiimote.get_state
45
+ acc = wiimote.acc
46
+ image = Image.new 240, 240, HatchFill.new( 'white', 'lightcyan2' )
47
+ gc = Draw.new
48
+ gc.fill_opacity 0
49
+ gc.stroke_width 2
50
+ gc.stroke 'red'
51
+ gc.line 120, 120, acc[0], acc[2]
52
+ gc.stroke 'green'
53
+ gc.line 120, 120, acc[1], acc[2]
54
+ gc.draw image
55
+ image.to_multiarray
56
+ end
57
+
@@ -0,0 +1,177 @@
1
+ #!/usr/bin/env ruby
2
+ require 'date'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/packagetask'
6
+ require 'rake/loaders/makefile'
7
+ require 'rbconfig'
8
+
9
+ PKG_NAME = 'cwiid'
10
+ PKG_VERSION = '0.1.0'
11
+ CFG = RbConfig::CONFIG
12
+ CXX = ENV[ 'CXX' ] || 'g++'
13
+ RB_FILES = FileList[ 'lib/**/*.rb' ]
14
+ CC_FILES = FileList[ 'ext/*.cc' ]
15
+ HH_FILES = FileList[ 'ext/*.hh' ] + FileList[ 'ext/*.tcc' ]
16
+ TC_FILES = FileList[ 'test/tc_*.rb' ]
17
+ TS_FILES = FileList[ 'test/ts_*.rb' ]
18
+ SO_FILE = "ext/cwiid.#{CFG[ 'DLEXT' ]}"
19
+ PKG_FILES = [ 'Rakefile', 'README.md', 'COPYING', '.document' ] +
20
+ RB_FILES + CC_FILES + HH_FILES + TS_FILES + TC_FILES
21
+ BIN_FILES = [ 'README.md', 'COPYING', '.document', SO_FILE ] +
22
+ RB_FILES + TS_FILES + TC_FILES
23
+ SUMMARY = %q{Using the Wii Remote with Ruby}
24
+ DESCRIPTION = %q{This Ruby extension provides an inerface to access a Wii Remote using the libcwiid library.}
25
+ AUTHOR = %q{Jan Wedekind}
26
+ EMAIL = %q{jan@wedesoft.de}
27
+ HOMEPAGE = %q{http://wedesoft.github.com/cwiid/}
28
+
29
+ OBJ = CC_FILES.ext 'o'
30
+ $CXXFLAGS = "-DNDEBUG #{CFG[ 'CPPFLAGS' ]} #{CFG[ 'CFLAGS' ]}"
31
+ if CFG[ 'rubyhdrdir' ]
32
+ $CXXFLAGS = "#{$CXXFLAGS} -I#{CFG[ 'rubyhdrdir' ]} " +
33
+ "-I#{CFG[ 'rubyhdrdir' ]}/#{CFG[ 'arch' ]}"
34
+ else
35
+ $CXXFLAGS = "#{$CXXFLAGS} -I#{CFG[ 'archdir' ]}"
36
+ end
37
+ $LIBRUBYARG = "-L#{CFG[ 'libdir' ]} #{CFG[ 'LIBRUBYARG' ]} #{CFG[ 'LDFLAGS' ]} " +
38
+ "#{CFG[ 'SOLIBS' ]} #{CFG[ 'DLDLIBS' ]}"
39
+ $SITELIBDIR = CFG[ 'sitelibdir' ]
40
+ $SITEARCHDIR = CFG[ 'sitearchdir' ]
41
+ $LDSHARED = CFG[ 'LDSHARED' ][ CFG[ 'LDSHARED' ].index( ' ' ) .. -1 ]
42
+
43
+ task :default => :all
44
+
45
+ desc 'Compile Ruby extension (default)'
46
+ task :all => [ SO_FILE ]
47
+
48
+ file SO_FILE => OBJ do |t|
49
+ sh "#{CXX} -shared -o #{t.name} #{OBJ} -lcwiid #{$LIBRUBYARG}"
50
+ end
51
+
52
+ task :test => [ SO_FILE ]
53
+
54
+ desc 'Install Ruby extension'
55
+ task :install => :all do
56
+ verbose true do
57
+ for f in RB_FILES do
58
+ FileUtils.mkdir_p "#{$SITELIBDIR}/#{File.dirname f.gsub( /^lib\//, '' )}"
59
+ FileUtils.cp_r f, "#{$SITELIBDIR}/#{f.gsub( /^lib\//, '' )}"
60
+ end
61
+ FileUtils.mkdir_p $SITEARCHDIR
62
+ FileUtils.cp SO_FILE, "#{$SITEARCHDIR}/#{File.basename SO_FILE}"
63
+ end
64
+ end
65
+
66
+ desc 'Uninstall Ruby extension'
67
+ task :uninstall do
68
+ verbose true do
69
+ for f in RB_FILES do
70
+ FileUtils.rm_f "#{$SITELIBDIR}/#{f.gsub /^lib\//, ''}"
71
+ end
72
+ FileUtils.rm_f "#{$SITEARCHDIR}/#{File.basename SO_FILE}"
73
+ end
74
+ end
75
+
76
+ Rake::TestTask.new do |t|
77
+ t.libs << 'ext'
78
+ t.test_files = TC_FILES
79
+ end
80
+
81
+ begin
82
+ require 'yard'
83
+ YARD::Rake::YardocTask.new :yard do |y|
84
+ y.options << '--no-private'
85
+ y.files << FileList[ 'lib/**/*.rb' ]
86
+ end
87
+ rescue LoadError
88
+ STDERR.puts 'Please install \'yard\' if you want to generate documentation'
89
+ end
90
+
91
+ Rake::PackageTask.new PKG_NAME, PKG_VERSION do |p|
92
+ p.need_tar = true
93
+ p.package_files = PKG_FILES
94
+ end
95
+
96
+ begin
97
+ require 'rubygems'
98
+ require 'rubygems/builder'
99
+ $SPEC = Gem::Specification.new do |s|
100
+ s.name = PKG_NAME
101
+ s.version = PKG_VERSION
102
+ s.platform = Gem::Platform::RUBY
103
+ s.date = Date.today.to_s
104
+ s.summary = SUMMARY
105
+ s.description = DESCRIPTION
106
+ s.author = AUTHOR
107
+ s.email = EMAIL
108
+ s.homepage = HOMEPAGE
109
+ s.files = PKG_FILES
110
+ s.test_files = TC_FILES
111
+ s.require_paths = [ 'lib', 'ext' ]
112
+ s.extensions = %w{Rakefile}
113
+ s.has_rdoc = 'yard'
114
+ s.extra_rdoc_files = []
115
+ s.rdoc_options = %w{--no-private}
116
+ s.add_development_dependency %q{rake}
117
+ end
118
+ GEM_SOURCE = "#{PKG_NAME}-#{PKG_VERSION}.gem"
119
+ $BINSPEC = Gem::Specification.new do |s|
120
+ s.name = PKG_NAME
121
+ s.version = PKG_VERSION
122
+ s.platform = Gem::Platform::CURRENT
123
+ s.date = Date.today.to_s
124
+ s.summary = SUMMARY
125
+ s.description = DESCRIPTION
126
+ s.author = AUTHOR
127
+ s.email = EMAIL
128
+ s.homepage = HOMEPAGE
129
+ s.files = BIN_FILES
130
+ s.test_files = TC_FILES
131
+ s.require_paths = [ 'lib', 'ext' ]
132
+ s.has_rdoc = 'yard'
133
+ s.extra_rdoc_files = []
134
+ s.rdoc_options = %w{--no-private}
135
+ end
136
+ GEM_BINARY = "#{PKG_NAME}-#{PKG_VERSION}-#{$BINSPEC.platform}.gem"
137
+ desc "Build the gem file #{GEM_SOURCE}"
138
+ task :gem => [ "pkg/#{GEM_SOURCE}" ]
139
+ file "pkg/#{GEM_SOURCE}" => [ 'pkg' ] + $SPEC.files do
140
+ when_writing 'Creating GEM' do
141
+ Gem::Builder.new( $SPEC ).build
142
+ verbose true do
143
+ FileUtils.mv GEM_SOURCE, "pkg/#{GEM_SOURCE}"
144
+ end
145
+ end
146
+ end
147
+ desc "Build the gem file #{GEM_BINARY}"
148
+ task :gem_binary => [ "pkg/#{GEM_BINARY}" ]
149
+ file "pkg/#{GEM_BINARY}" => [ 'pkg' ] + $BINSPEC.files do
150
+ when_writing 'Creating binary GEM' do
151
+ Gem::Builder.new( $BINSPEC ).build
152
+ verbose true do
153
+ FileUtils.mv GEM_BINARY, "pkg/#{GEM_BINARY}"
154
+ end
155
+ end
156
+ end
157
+ rescue LoadError
158
+ STDERR.puts 'Please install \'rubygems\' if you want to create Gem packages'
159
+ end
160
+
161
+ rule '.o' => '.cc' do |t|
162
+ sh "#{CXX} #{$CXXFLAGS} -c -o #{t.name} #{t.source}"
163
+ end
164
+
165
+ file ".depends.mf" do |t|
166
+ sh "g++ -MM #{CC_FILES.join ' '} | " +
167
+ "sed -e :a -e N -e 's/\\n/\\$/g' -e ta | " +
168
+ "sed -e 's/ *\\\\\\$ */ /g' -e 's/\\$/\\n/g' | sed -e 's/^/ext\\//' > #{t.name}"
169
+ end
170
+ CC_FILES.each do |t|
171
+ file t.ext(".o") => t
172
+ end
173
+ import ".depends.mf"
174
+
175
+ CLEAN.include 'ext/*.o'
176
+ CLOBBER.include SO_FILE, 'doc', '.yardoc', '.depends.mf'
177
+
@@ -0,0 +1,50 @@
1
+ /* HornetsEye - Computer Vision with Ruby
2
+ Copyright (C) 2006, 2007, 2008, 2009, 2010 Jan Wedekind
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ #ifndef ERROR_HH
17
+ #define ERROR_HH
18
+
19
+ #include <exception>
20
+ #include <sstream>
21
+
22
+ class Error: public std::exception
23
+ {
24
+ public:
25
+ Error(void) {}
26
+ Error( Error &e ): std::exception( e )
27
+ { m_message << e.m_message.str(); }
28
+ virtual ~Error(void) throw() {}
29
+ template< typename T >
30
+ std::ostream &operator<<( const T &t )
31
+ { m_message << t; return m_message; }
32
+ std::ostream &operator<<( std::ostream& (*__pf)(std::ostream&) )
33
+ { (*__pf)( m_message ); return m_message; }
34
+ virtual const char* what(void) const throw() {
35
+ temp = m_message.str();
36
+ return temp.c_str();
37
+ }
38
+ protected:
39
+ std::ostringstream m_message;
40
+ mutable std::string temp;
41
+ };
42
+
43
+ #define ERRORMACRO( condition, class, params, message ) \
44
+ if ( !( condition ) ) { \
45
+ class _e params; \
46
+ _e << message; \
47
+ throw _e; \
48
+ };
49
+
50
+ #endif
@@ -0,0 +1,35 @@
1
+ /* ruby-cwiid - Wii Remote interface.
2
+ Copyright (C) 2010 Jan Wedekind
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ #include "wiimote.hh"
17
+ #ifdef WIN32
18
+ #define DLLEXPORT __declspec(dllexport)
19
+ #define DLLLOCAL
20
+ #else
21
+ #define DLLEXPORT __attribute__ ((visibility("default")))
22
+ #define DLLLOCAL __attribute__ ((visibility("hidden")))
23
+ #endif
24
+
25
+ extern "C" DLLEXPORT void Init_cwiid(void);
26
+
27
+ extern "C" {
28
+
29
+ void Init_cwiid(void)
30
+ {
31
+ WiiMote::registerRubyClass();
32
+ rb_require( "cwiid_ext.rb" );
33
+ }
34
+
35
+ }
@@ -0,0 +1,54 @@
1
+ /* HornetsEye - Computer Vision with Ruby
2
+ Copyright (C) 2006, 2007 Jan Wedekind
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ #ifndef HORNETSEYE_RUBYINC_HH
17
+ #define HORNETSEYE_RUBYINC_HH
18
+
19
+ #ifdef RSHIFT
20
+ #undef RSHIFT
21
+ #endif
22
+
23
+ #define gettimeofday rubygettimeofday
24
+ #define timezone rubygettimezone
25
+ #include <ruby.h>
26
+ // #include <version.h>
27
+ #undef timezone
28
+ #undef gettimeofday
29
+ #ifdef read
30
+ #undef read
31
+ #endif
32
+ #ifdef write
33
+ #undef write
34
+ #endif
35
+ #ifdef RGB
36
+ #undef RGB
37
+ #endif
38
+
39
+ #ifndef RUBY_VERSION_NUMBER
40
+ #define RUBY_VERSION_NUMBER ( RUBY_VERSION_MAJOR * 10000 + \
41
+ RUBY_VERSION_MINOR * 100 + \
42
+ RUBY_VERSION_TEENY )
43
+ #endif
44
+
45
+ #ifndef RUBY_METHOD_FUNC
46
+ #define RUBY_METHOD_FUNC(func) ((VALUE (*)(ANYARGS))func)
47
+ #endif
48
+
49
+ #ifndef xfree
50
+ #define xfree free
51
+ #endif
52
+
53
+ #endif
54
+
@@ -0,0 +1,333 @@
1
+ /* ruby-cwiid - Wii Remote interface.
2
+ Copyright (C) 2010 Jan Wedekind
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ #include "wiimote.hh"
17
+
18
+ using namespace std;
19
+
20
+ VALUE WiiMote::cRubyClass = Qnil;
21
+
22
+ WiiMote *WiiMote::current = NULL;
23
+
24
+ static void staticErr( cwiid_wiimote_t *wiimote, const char *s, va_list ap )
25
+ {
26
+ WiiMote::current->err( s, ap );
27
+ }
28
+
29
+ WiiMote::WiiMote(void) throw (Error):
30
+ m_wiimote(NULL), m_error(false)
31
+ {
32
+ bdaddr_t bdaddrAny = { { 0, 0, 0, 0, 0, 0 } };
33
+ current = this;
34
+ cwiid_set_err( staticErr );
35
+ memset( &m_state, 0, sizeof(m_state) );
36
+ m_wiimote = cwiid_open( &bdaddrAny, 0 );
37
+ ERRORMACRO( !m_error, Error, , "Failed to connect to Wii Remote: "
38
+ << m_errorMsg );
39
+ }
40
+
41
+ WiiMote::~WiiMote(void)
42
+ {
43
+ close();
44
+ }
45
+
46
+ void WiiMote::close(void)
47
+ {
48
+ if ( m_wiimote != NULL ) {
49
+ cwiid_close( m_wiimote );
50
+ m_wiimote = NULL;
51
+ current = NULL;
52
+ cwiid_set_err( cwiid_err_default );
53
+ };
54
+ }
55
+
56
+ void WiiMote::requestStatus(void) throw (Error)
57
+ {
58
+ m_error = false;
59
+ cwiid_request_status( m_wiimote );
60
+ ERRORMACRO( !m_error, Error, , "Status request error: " << m_errorMsg );
61
+ }
62
+
63
+ void WiiMote::getState(void) throw (Error)
64
+ {
65
+ m_error = false;
66
+ cwiid_get_state( m_wiimote, &m_state );
67
+ ERRORMACRO( !m_error, Error, , "Error getting state: " << m_errorMsg );
68
+ }
69
+
70
+ unsigned char WiiMote::getRptMode(void)
71
+ {
72
+ return m_state.rpt_mode;
73
+ }
74
+
75
+ void WiiMote::setRptMode( unsigned char mode ) throw (Error)
76
+ {
77
+ m_error = false;
78
+ cwiid_set_rpt_mode( m_wiimote, mode );
79
+ ERRORMACRO( !m_error, Error, , "Error setting RPT mode: " << m_errorMsg );
80
+ m_state.rpt_mode = mode;
81
+ }
82
+
83
+ unsigned char WiiMote::getBattery(void)
84
+ {
85
+ return m_state.battery;
86
+ }
87
+
88
+ unsigned char WiiMote::getLED(void)
89
+ {
90
+ return m_state.led;
91
+ }
92
+
93
+ void WiiMote::setLED( unsigned char state ) throw (Error)
94
+ {
95
+ m_error = false;
96
+ cwiid_set_led( m_wiimote, state );
97
+ ERRORMACRO( !m_error, Error, , "Error setting LED state: " << m_errorMsg );
98
+ m_state.led = state;
99
+ }
100
+
101
+ bool WiiMote::getRumble(void)
102
+ {
103
+ return m_state.rumble != 0;
104
+ }
105
+
106
+ void WiiMote::setRumble( bool state ) throw( Error )
107
+ {
108
+ m_error = false;
109
+ cwiid_set_rumble( m_wiimote, state ? 1 : 0 );
110
+ ERRORMACRO( !m_error, Error, , "Error setting rumble state: " << m_errorMsg );
111
+ m_state.rumble = state ? 1 : 0;
112
+ }
113
+
114
+ unsigned short int WiiMote::getButtons(void)
115
+ {
116
+ return m_state.buttons;
117
+ }
118
+
119
+ unsigned char WiiMote::getAcc( int id )
120
+ {
121
+ return m_state.acc[ id ];
122
+ }
123
+
124
+ bool WiiMote::irValid( int i )
125
+ {
126
+ return m_state.ir_src[ i ].valid != 0;
127
+ }
128
+
129
+ unsigned short int WiiMote::getIRX( int i )
130
+ {
131
+ return m_state.ir_src[ i ].pos[ CWIID_X ];
132
+ }
133
+
134
+ unsigned short int WiiMote::getIRY( int i )
135
+ {
136
+ return m_state.ir_src[ i ].pos[ CWIID_Y ];
137
+ }
138
+
139
+ char WiiMote::getIRSize( int i )
140
+ {
141
+ return m_state.ir_src[ i ].size;
142
+ }
143
+
144
+ void WiiMote::err( const char *s, va_list ap )
145
+ {
146
+ char buffer[4096];
147
+ vsnprintf( &buffer[0], sizeof(buffer), s, ap );
148
+ m_errorMsg = buffer;
149
+ m_error = true;
150
+ }
151
+
152
+ VALUE WiiMote::registerRubyClass(void)
153
+ {
154
+ cRubyClass = rb_define_class( "WiiMote", rb_cObject );
155
+ rb_define_const( cRubyClass, "RPT_STATUS", INT2NUM( CWIID_RPT_STATUS ) );
156
+ rb_define_const( cRubyClass, "RPT_BTN", INT2NUM( CWIID_RPT_BTN ) );
157
+ rb_define_const( cRubyClass, "RPT_ACC", INT2NUM( CWIID_RPT_ACC ) );
158
+ rb_define_const( cRubyClass, "RPT_IR", INT2NUM( CWIID_RPT_IR ) );
159
+ rb_define_const( cRubyClass, "RPT_NUNCHUK", INT2NUM( CWIID_RPT_NUNCHUK ) );
160
+ rb_define_const( cRubyClass, "RPT_CLASSIC", INT2NUM( CWIID_RPT_CLASSIC ) );
161
+ rb_define_const( cRubyClass, "RPT_BALANCE", INT2NUM( CWIID_RPT_BALANCE ) );
162
+ rb_define_const( cRubyClass, "RPT_MOTIONPLUS", INT2NUM( CWIID_RPT_MOTIONPLUS ) );
163
+ rb_define_const( cRubyClass, "BTN_2", INT2NUM( CWIID_BTN_2 ) );
164
+ rb_define_const( cRubyClass, "BTN_1", INT2NUM( CWIID_BTN_1 ) );
165
+ rb_define_const( cRubyClass, "BTN_B", INT2NUM( CWIID_BTN_B ) );
166
+ rb_define_const( cRubyClass, "BTN_A", INT2NUM( CWIID_BTN_A ) );
167
+ rb_define_const( cRubyClass, "BTN_MINUS", INT2NUM( CWIID_BTN_MINUS ) );
168
+ rb_define_const( cRubyClass, "BTN_HOME", INT2NUM( CWIID_BTN_HOME ) );
169
+ rb_define_const( cRubyClass, "BTN_LEFT", INT2NUM( CWIID_BTN_LEFT ) );
170
+ rb_define_const( cRubyClass, "BTN_RIGHT", INT2NUM( CWIID_BTN_RIGHT ) );
171
+ rb_define_const( cRubyClass, "BTN_DOWN", INT2NUM( CWIID_BTN_DOWN ) );
172
+ rb_define_const( cRubyClass, "BTN_UP", INT2NUM( CWIID_BTN_UP ) );
173
+ rb_define_const( cRubyClass, "BTN_PLUS", INT2NUM( CWIID_BTN_PLUS ) );
174
+ rb_define_const( cRubyClass, "LED1_ON", INT2NUM( CWIID_LED1_ON ) );
175
+ rb_define_const( cRubyClass, "LED2_ON", INT2NUM( CWIID_LED2_ON ) );
176
+ rb_define_const( cRubyClass, "LED3_ON", INT2NUM( CWIID_LED3_ON ) );
177
+ rb_define_const( cRubyClass, "LED4_ON", INT2NUM( CWIID_LED4_ON ) );
178
+ rb_define_const( cRubyClass, "BATTERY_MAX", INT2NUM( CWIID_BATTERY_MAX ) );
179
+ rb_define_singleton_method( cRubyClass, "new",
180
+ RUBY_METHOD_FUNC( wrapNew ), 0 );
181
+ rb_define_method( cRubyClass, "close",
182
+ RUBY_METHOD_FUNC( wrapClose ), 0 );
183
+ rb_define_method( cRubyClass, "request_status",
184
+ RUBY_METHOD_FUNC( wrapRequestStatus ), 0 );
185
+ rb_define_method( cRubyClass, "get_state",
186
+ RUBY_METHOD_FUNC( wrapGetState ), 0 );
187
+ rb_define_method( cRubyClass, "rpt_mode",
188
+ RUBY_METHOD_FUNC( wrapGetRptMode ), 0 );
189
+ rb_define_method( cRubyClass, "rpt_mode=",
190
+ RUBY_METHOD_FUNC( wrapSetRptMode ), 1 );
191
+ rb_define_method( cRubyClass, "battery",
192
+ RUBY_METHOD_FUNC( wrapGetBattery ), 0 );
193
+ rb_define_method( cRubyClass, "led",
194
+ RUBY_METHOD_FUNC( wrapGetLED ), 0 );
195
+ rb_define_method( cRubyClass, "led=",
196
+ RUBY_METHOD_FUNC( wrapSetLED ), 1 );
197
+ rb_define_method( cRubyClass, "rumble",
198
+ RUBY_METHOD_FUNC( wrapGetRumble ), 0 );
199
+ rb_define_method( cRubyClass, "rumble=",
200
+ RUBY_METHOD_FUNC( wrapSetRumble ), 1 );
201
+ rb_define_method( cRubyClass, "buttons",
202
+ RUBY_METHOD_FUNC( wrapGetButtons ), 0 );
203
+ rb_define_method( cRubyClass, "acc",
204
+ RUBY_METHOD_FUNC( wrapGetAcc ), 0 );
205
+ rb_define_method( cRubyClass, "ir",
206
+ RUBY_METHOD_FUNC( wrapGetIR ), 0 );
207
+ return cRubyClass;
208
+ }
209
+
210
+ void WiiMote::deleteRubyObject( void *ptr )
211
+ {
212
+ delete (WiiMotePtr *)ptr;
213
+ }
214
+
215
+ VALUE WiiMote::wrapNew( VALUE rbClass )
216
+ {
217
+ VALUE retVal = Qnil;
218
+ try {
219
+ WiiMotePtr ptr( new WiiMote );
220
+ retVal = Data_Wrap_Struct( rbClass, 0, deleteRubyObject,
221
+ new WiiMotePtr( ptr ) );
222
+ } catch ( exception &e ) {
223
+ rb_raise( rb_eRuntimeError, "%s", e.what() );
224
+ };
225
+ return retVal;
226
+ }
227
+
228
+ VALUE WiiMote::wrapClose( VALUE rbSelf )
229
+ {
230
+ WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
231
+ (*self)->close();
232
+ return rbSelf;
233
+ }
234
+
235
+ VALUE WiiMote::wrapRequestStatus( VALUE rbSelf )
236
+ {
237
+ WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
238
+ (*self)->requestStatus();
239
+ return rbSelf;
240
+ }
241
+
242
+ VALUE WiiMote::wrapGetState( VALUE rbSelf )
243
+ {
244
+ WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
245
+ (*self)->getState();
246
+ return rbSelf;
247
+ }
248
+
249
+ VALUE WiiMote::wrapGetRptMode( VALUE rbSelf )
250
+ {
251
+ WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
252
+ (*self)->getRptMode();
253
+ return rbSelf;
254
+ }
255
+
256
+ VALUE WiiMote::wrapSetRptMode( VALUE rbSelf, VALUE rbMode )
257
+ {
258
+ try {
259
+ WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
260
+ (*self)->setRptMode( NUM2INT( rbMode ) );
261
+ } catch ( exception &e ) {
262
+ rb_raise( rb_eRuntimeError, "%s", e.what() );
263
+ };
264
+ return rbMode;
265
+ }
266
+
267
+ VALUE WiiMote::wrapGetBattery( VALUE rbSelf )
268
+ {
269
+ WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
270
+ return INT2NUM( (*self)->getBattery() );
271
+ }
272
+
273
+ VALUE WiiMote::wrapGetLED( VALUE rbSelf )
274
+ {
275
+ WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
276
+ return INT2NUM( (*self)->getLED() );
277
+ }
278
+
279
+ VALUE WiiMote::wrapSetLED( VALUE rbSelf, VALUE rbState )
280
+ {
281
+ try {
282
+ WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
283
+ (*self)->setLED( NUM2INT( rbState ) );
284
+ } catch ( exception &e ) {
285
+ rb_raise( rb_eRuntimeError, "%s", e.what() );
286
+ };
287
+ return rbState;
288
+ }
289
+
290
+ VALUE WiiMote::wrapGetRumble( VALUE rbSelf )
291
+ {
292
+ WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
293
+ return (*self)->getRumble() ? Qtrue : Qfalse;
294
+ }
295
+
296
+ VALUE WiiMote::wrapSetRumble( VALUE rbSelf, VALUE rbState )
297
+ {
298
+ try {
299
+ WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
300
+ (*self)->setRumble( rbState == Qtrue );
301
+ } catch ( exception &e ) {
302
+ rb_raise( rb_eRuntimeError, "%s", e.what() );
303
+ };
304
+ return rbState;
305
+ }
306
+
307
+ VALUE WiiMote::wrapGetButtons( VALUE rbSelf )
308
+ {
309
+ WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
310
+ return INT2NUM( (*self)->getButtons() );
311
+ }
312
+
313
+ VALUE WiiMote::wrapGetAcc( VALUE rbSelf )
314
+ {
315
+ WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
316
+ return rb_ary_new3( 3, INT2NUM( (*self)->getAcc( 0 ) ),
317
+ INT2NUM( (*self)->getAcc( 1 ) ),
318
+ INT2NUM( (*self)->getAcc( 2 ) ) );
319
+ }
320
+
321
+ VALUE WiiMote::wrapGetIR( VALUE rbSelf )
322
+ {
323
+ VALUE rbRetVal = rb_ary_new();
324
+ WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
325
+ for ( int i=0; i<CWIID_IR_SRC_COUNT; i++ )
326
+ if ( (*self)->irValid( i ) )
327
+ rb_ary_push( rbRetVal, rb_ary_new3( 3,
328
+ INT2NUM( (*self)->getIRX( i ) ),
329
+ INT2NUM( (*self)->getIRY( i ) ),
330
+ INT2NUM( (*self)->getIRSize( i ) ) ) );
331
+ return rbRetVal;
332
+ }
333
+