simple_wiimote 0.2.1 → 0.3.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.
Files changed (2) hide show
  1. data/lib/simple_wiimote.rb +50 -37
  2. metadata +1 -1
@@ -10,7 +10,7 @@ end
10
10
 
11
11
  class SimpleWiimote
12
12
 
13
- attr_accessor :terminator, :events, :led, :rumble
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
- @events = {
24
- '2' => lambda {|wm| puts 'you pressed 2'},
25
- '1' => lambda {|wm| puts 'you pressed 1'},
26
- 'b' => lambda {|wm| puts 'you pressed b'},
27
- 'a' => lambda {|wm| puts 'you pressed a'},
28
- 'minus' => lambda {|wm| puts 'you pressed minus'},
29
- 'void1' => nil,
30
- 'void2' => nil,
31
- 'home' => lambda {|wm| puts 'you pressed home'},
32
- 'left' => lambda {|wm| puts 'you pressed left'},
33
- 'right' => lambda {|wm| puts 'you pressed right'},
34
- 'down' => lambda {|wm| puts 'you pressed down'},
35
- 'up' => lambda {|wm| puts 'you pressed up'},
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
- procs_press = {
51
- Hash: proc {|x, wiimote| x[:on_buttonpress].call(wiimote)},
52
- Proc: proc {|x, wiimote| x.call(wiimote)}
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
- procs_press[@events[x].class.to_s.to_sym].call(@events[x], @wiimote)
85
+ button[:press].call(@events[x], @wiimote)
94
86
  end
95
87
 
96
88
  pressed.each do |x|
97
- procs_down[@events[x].class.to_s.to_sym].call(@events[x], @wiimote)
89
+ button[:down].call(@events[x], @wiimote)
98
90
  end
99
91
 
100
92
  expired_keypresses.each do |x|
101
- procs_up[@events[x].class.to_s.to_sym].call(@events[x], @wiimote)
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
- procs_up[@events[x].class.to_s.to_sym].call(@events[x], @wiimote)
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
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: simple_wiimote
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.1
5
+ version: 0.3.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Robertson