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.
Constructor Details
#initialize ⇒ SimpleWiimote
Returns a new instance of 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 113 114 115 |
# 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 ) on_deactivated() end |
#close ⇒ Object
119 |
# File 'lib/simple_wiimote.rb', line 119 def close() @wiimote.close end |