simple_wiimote 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/simple_wiimote.rb +50 -37
- metadata +1 -1
data/lib/simple_wiimote.rb
CHANGED
@@ -10,7 +10,7 @@ end
|
|
10
10
|
|
11
11
|
class SimpleWiimote
|
12
12
|
|
13
|
-
attr_accessor :terminator, :
|
13
|
+
attr_accessor :terminator, :led, :rumble
|
14
14
|
|
15
15
|
def initialize()
|
16
16
|
|
@@ -19,26 +19,27 @@ class SimpleWiimote
|
|
19
19
|
@wiimote = WiiMote.new
|
20
20
|
@wiimote.rpt_mode = WiiMote::RPT_BTN | WiiMote::RPT_ACC
|
21
21
|
@wiimote.active = false
|
22
|
+
|
23
|
+
btn_states = %w(press down up)
|
24
|
+
buttons = %w(2 1 b a minus void1 void2 home left right down up plus)
|
25
|
+
|
26
|
+
@events = buttons.inject({}) do |r,x|
|
27
|
+
|
28
|
+
h = btn_states.inject({}) do |r,state|
|
22
29
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
'plus' => {
|
37
|
-
on_buttonpress: lambda {|wm| puts 'you pressed plus'},
|
38
|
-
on_buttondown: lambda {|wm| puts wm.acc.inspect},
|
39
|
-
on_buttonup: lambda {|wm| puts 'plus button raised'}
|
40
|
-
}
|
41
|
-
}
|
30
|
+
label = ("on_button" + state).to_sym
|
31
|
+
|
32
|
+
event = lambda do |wm|
|
33
|
+
method_name = ("on_btn_%s_%s" % [x, state]).to_sym
|
34
|
+
method(method_name).call(wm) if self.respond_to? method_name
|
35
|
+
end
|
36
|
+
|
37
|
+
r.merge label => event
|
38
|
+
end
|
39
|
+
r.merge x => h
|
40
|
+
end
|
41
|
+
|
42
|
+
@events['void1'] = @events['void2'] = nil
|
42
43
|
|
43
44
|
@terminator = ['1','2']
|
44
45
|
end
|
@@ -47,21 +48,12 @@ class SimpleWiimote
|
|
47
48
|
|
48
49
|
previously_pressed, pressed = [], []
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
button = {
|
52
|
+
press: proc {|x, wiimote| x[:on_buttonpress].call wiimote},
|
53
|
+
down: lambda {|x, wiimote| x[:on_buttondown].call wiimote},
|
54
|
+
up: proc {|x, wiimote| x[:on_buttonup].call wiimote}
|
53
55
|
}
|
54
56
|
|
55
|
-
procs_down = {
|
56
|
-
Hash: lambda {|x, wiimote| x[:on_buttondown].call(wiimote)},
|
57
|
-
Proc: proc { }
|
58
|
-
}
|
59
|
-
|
60
|
-
procs_up = {
|
61
|
-
Hash: proc {|x, wiimote| x[:on_buttonup].call(wiimote)},
|
62
|
-
Proc: proc { }
|
63
|
-
}
|
64
|
-
|
65
57
|
begin
|
66
58
|
|
67
59
|
@wiimote.get_state
|
@@ -90,15 +82,15 @@ class SimpleWiimote
|
|
90
82
|
previously_pressed = pressed
|
91
83
|
|
92
84
|
new_keypresses.each do |x|
|
93
|
-
|
85
|
+
button[:press].call(@events[x], @wiimote)
|
94
86
|
end
|
95
87
|
|
96
88
|
pressed.each do |x|
|
97
|
-
|
89
|
+
button[:down].call(@events[x], @wiimote)
|
98
90
|
end
|
99
91
|
|
100
92
|
expired_keypresses.each do |x|
|
101
|
-
|
93
|
+
button[:up].call(@events[x], @wiimote)
|
102
94
|
end
|
103
95
|
|
104
96
|
pressed = []
|
@@ -109,7 +101,7 @@ class SimpleWiimote
|
|
109
101
|
expired_keypresses = previously_pressed
|
110
102
|
|
111
103
|
expired_keypresses.each do |x|
|
112
|
-
|
104
|
+
button[:up].call(@events[x], @wiimote)
|
113
105
|
end
|
114
106
|
|
115
107
|
previously_pressed, expired_keypressed, pressed = [], [], []
|
@@ -122,4 +114,25 @@ class SimpleWiimote
|
|
122
114
|
def led=(val) @led = @wiimote.led = val end
|
123
115
|
def rumble=(bool) @rumble = @wiimote.rumble = bool end
|
124
116
|
def close() @wiimote.close end
|
117
|
+
|
118
|
+
def on_btn_2_press(wm) puts 'button 2 pressed' end
|
119
|
+
def on_btn_1_press(wm) puts 'button 1 pressed' end
|
120
|
+
def on_btn_b_press(wm) puts 'button b pressed' end
|
121
|
+
def on_btn_a_press(wm) puts 'button a pressed' end
|
122
|
+
def on_btn_minus_press(wm) puts 'button minus pressed' end
|
123
|
+
def on_btn_void1_press(wm) puts 'button void1 pressed' end
|
124
|
+
def on_btn_void2_press(wm) puts 'button void2 pressed' end
|
125
|
+
def on_btn_home_press(wm) puts 'button home pressed' end
|
126
|
+
def on_btn_left_press(wm) puts 'button left pressed' end
|
127
|
+
def on_btn_right_press(wm) puts 'button right pressed' end
|
128
|
+
def on_btn_down_press(wm) puts 'button down pressed' end
|
129
|
+
def on_btn_up_press(wm) puts 'button up pressed' end
|
130
|
+
def on_btn_plus_press(wm) puts 'button plus pressed' end
|
131
|
+
|
132
|
+
def on_btn_b_up(wm) puts 'button b up' end
|
133
|
+
|
134
|
+
def on_btn_b_down(wm)
|
135
|
+
puts 'button b down: ' + wm.acc.inspect
|
136
|
+
end
|
137
|
+
|
125
138
|
end
|