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_ledchange ⇒ Object
Constructor Details
#initialize ⇒ SimpleWiimote
Returns a new instance of SimpleWiimote.
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 |
# File 'lib/simple_wiimote.rb', line 69 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'] @led = 4.times.map { Led.new self} end |
Instance Attribute Details
#led ⇒ Object
Returns the value of attribute led.
67 68 69 |
# File 'lib/simple_wiimote.rb', line 67 def led @led end |
#rumble ⇒ Object
Returns the value of attribute rumble.
67 68 69 |
# File 'lib/simple_wiimote.rb', line 67 def rumble @rumble end |
#terminator ⇒ Object
Returns the value of attribute terminator.
67 68 69 |
# File 'lib/simple_wiimote.rb', line 67 def terminator @terminator end |
Instance Method Details
#activate ⇒ Object
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/simple_wiimote.rb', line 102 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 @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
176 |
# File 'lib/simple_wiimote.rb', line 176 def close() @wiimote.close end |
#on_ledchange ⇒ Object
178 179 180 |
# File 'lib/simple_wiimote.rb', line 178 def on_ledchange() @wiimote.led = @led.map(&:state).reverse.join.to_i(2) end |