Class: SimpleWiimote
- Inherits:
-
Object
- Object
- SimpleWiimote
- Defined in:
- lib/simple_wiimote.rb
Instance Attribute Summary collapse
-
#led ⇒ Object
Returns the value of attribute led.
-
#rumble ⇒ Object
Returns the value of attribute rumble.
-
#terminator ⇒ Object
Returns the value of attribute terminator.
Instance Method Summary collapse
- #activate ⇒ Object
- #close ⇒ Object
-
#initialize ⇒ SimpleWiimote
constructor
A new instance of SimpleWiimote.
- #on_btn_1_press(wm) ⇒ Object
- #on_btn_2_press(wm) ⇒ Object
- #on_btn_a_press(wm) ⇒ Object
- #on_btn_b_down(wm) ⇒ Object
- #on_btn_b_press(wm) ⇒ Object
- #on_btn_b_up(wm) ⇒ Object
- #on_btn_down_press(wm) ⇒ Object
- #on_btn_home_press(wm) ⇒ Object
- #on_btn_left_press(wm) ⇒ Object
- #on_btn_minus_press(wm) ⇒ Object
- #on_btn_plus_press(wm) ⇒ Object
- #on_btn_right_press(wm) ⇒ Object
- #on_btn_up_press(wm) ⇒ Object
- #on_btn_void1_press(wm) ⇒ Object
- #on_btn_void2_press(wm) ⇒ Object
Constructor Details
#initialize ⇒ SimpleWiimote
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/simple_wiimote.rb', line 15 def initialize() puts 'Put Wiimote in discoverable mode now (press 1+2)...' @wiimote = WiiMote.new @wiimote.rpt_mode = WiiMote::RPT_BTN | WiiMote::RPT_ACC @wiimote.active = false btn_states = %w(press down up) = %w(2 1 b a minus void1 void2 home left right down up plus) @events = .inject({}) do |r,x| h = btn_states.inject({}) do |r,state| label = ("on_button" + state).to_sym event = lambda do |wm| method_name = ("on_btn_%s_%s" % [x, state]).to_sym method(method_name).call(wm) if self.respond_to? method_name end r.merge label => event end r.merge x => h end @events['void1'] = @events['void2'] = nil @terminator = ['1','2'] end |
Instance Attribute Details
#led ⇒ Object
Returns the value of attribute led.
13 14 15 |
# File 'lib/simple_wiimote.rb', line 13 def led @led end |
#rumble ⇒ Object
Returns the value of attribute rumble.
13 14 15 |
# File 'lib/simple_wiimote.rb', line 13 def rumble @rumble end |
#terminator ⇒ Object
Returns the value of attribute terminator.
13 14 15 |
# File 'lib/simple_wiimote.rb', line 13 def terminator @terminator end |
Instance Method Details
#activate ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/simple_wiimote.rb', line 47 def activate() previously_pressed, pressed = [], [] = { press: proc {|x, wiimote| x[:on_buttonpress].call wiimote}, down: lambda {|x, wiimote| x[:on_buttondown].call wiimote}, up: proc {|x, wiimote| x[:on_buttonup].call wiimote} } begin @wiimote.get_state @wiimote.active = true yield(@wiimote) if block_given? if @wiimote. > 0 then val = @wiimote. @events.keys.reverse.each_with_index do |x,i| n = @events.length - 1 - i next if x == 32 or x == 64 xval = 2**n (val -= xval; pressed << x) if xval <= val end @wiimote.active = (not (@terminator & pressed) == @terminator) new_keypresses = pressed - previously_pressed expired_keypresses = previously_pressed - pressed previously_pressed = pressed new_keypresses.each do |x| [:press].call(@events[x], @wiimote) end pressed.each do |x| [:down].call(@events[x], @wiimote) end expired_keypresses.each do |x| [:up].call(@events[x], @wiimote) end pressed = [] else if previously_pressed.length > 0 then expired_keypresses = previously_pressed expired_keypresses.each do |x| [:up].call(@events[x], @wiimote) end previously_pressed, expired_keypressed, pressed = [], [], [] end end end until ( not @wiimote.active ) end |
#close ⇒ Object
116 |
# File 'lib/simple_wiimote.rb', line 116 def close() @wiimote.close end |
#on_btn_1_press(wm) ⇒ Object
119 |
# File 'lib/simple_wiimote.rb', line 119 def on_btn_1_press(wm) puts 'button 1 pressed' end |
#on_btn_2_press(wm) ⇒ Object
118 |
# File 'lib/simple_wiimote.rb', line 118 def on_btn_2_press(wm) puts 'button 2 pressed' end |
#on_btn_a_press(wm) ⇒ Object
121 |
# File 'lib/simple_wiimote.rb', line 121 def on_btn_a_press(wm) puts 'button a pressed' end |
#on_btn_b_down(wm) ⇒ Object
134 135 136 |
# File 'lib/simple_wiimote.rb', line 134 def on_btn_b_down(wm) puts 'button b down: ' + wm.acc.inspect end |
#on_btn_b_press(wm) ⇒ Object
120 |
# File 'lib/simple_wiimote.rb', line 120 def on_btn_b_press(wm) puts 'button b pressed' end |
#on_btn_b_up(wm) ⇒ Object
132 |
# File 'lib/simple_wiimote.rb', line 132 def on_btn_b_up(wm) puts 'button b up' end |
#on_btn_down_press(wm) ⇒ Object
128 |
# File 'lib/simple_wiimote.rb', line 128 def on_btn_down_press(wm) puts 'button down pressed' end |
#on_btn_home_press(wm) ⇒ Object
125 |
# File 'lib/simple_wiimote.rb', line 125 def on_btn_home_press(wm) puts 'button home pressed' end |
#on_btn_left_press(wm) ⇒ Object
126 |
# File 'lib/simple_wiimote.rb', line 126 def on_btn_left_press(wm) puts 'button left pressed' end |
#on_btn_minus_press(wm) ⇒ Object
122 |
# File 'lib/simple_wiimote.rb', line 122 def on_btn_minus_press(wm) puts 'button minus pressed' end |
#on_btn_plus_press(wm) ⇒ Object
130 |
# File 'lib/simple_wiimote.rb', line 130 def on_btn_plus_press(wm) puts 'button plus pressed' end |
#on_btn_right_press(wm) ⇒ Object
127 |
# File 'lib/simple_wiimote.rb', line 127 def on_btn_right_press(wm) puts 'button right pressed' end |
#on_btn_up_press(wm) ⇒ Object
129 |
# File 'lib/simple_wiimote.rb', line 129 def on_btn_up_press(wm) puts 'button up pressed' end |
#on_btn_void1_press(wm) ⇒ Object
123 |
# File 'lib/simple_wiimote.rb', line 123 def on_btn_void1_press(wm) puts 'button void1 pressed' end |
#on_btn_void2_press(wm) ⇒ Object
124 |
# File 'lib/simple_wiimote.rb', line 124 def on_btn_void2_press(wm) puts 'button void2 pressed' end |