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(debug: false) ⇒ SimpleWiimote
constructor
A new instance of SimpleWiimote.
- #on_ledchange ⇒ Object
Constructor Details
#initialize(debug: false) ⇒ SimpleWiimote
Returns a new instance of SimpleWiimote.
40 41 42 43 44 45 46 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 |
# File 'lib/simple_wiimote.rb', line 40 def initialize(debug: false) @debug = debug 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'] @led = 4.times.map { Led.new self} end |
Instance Attribute Details
#led ⇒ Object
Returns the value of attribute led.
38 39 40 |
# File 'lib/simple_wiimote.rb', line 38 def led @led end |
#rumble ⇒ Object
Returns the value of attribute rumble.
38 39 40 |
# File 'lib/simple_wiimote.rb', line 38 def rumble @rumble end |
#terminator ⇒ Object
Returns the value of attribute terminator.
38 39 40 |
# File 'lib/simple_wiimote.rb', line 38 def terminator @terminator end |
Instance Method Details
#activate ⇒ Object
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/simple_wiimote.rb', line 75 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 sleep 0.01 yield(@wiimote) if block_given? sleep 0.01 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 puts ('@terminator: ' + @terminator.inspect).debug if @debug puts ('pressed: ' + pressed.inspect).debug if @debug @wiimote.active = (not ([@terminator].flatten & 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
152 |
# File 'lib/simple_wiimote.rb', line 152 def close() @wiimote.close end |
#on_ledchange ⇒ Object
154 155 156 |
# File 'lib/simple_wiimote.rb', line 154 def on_ledchange() @wiimote.led = @led.map(&:state).reverse.join.to_i(2) end |