simple_wiimote 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/simple_wiimote.rb +75 -18
  2. metadata +2 -2
@@ -19,37 +19,94 @@ class SimpleWiimote
19
19
  @wiimote = WiiMote.new
20
20
  @wiimote.rpt_mode = WiiMote::RPT_BTN | WiiMote::RPT_ACC
21
21
  @wiimote.active = false
22
-
22
+
23
23
  @events = {
24
- WiiMote::BTN_PLUS => -> {puts 'you pressed plus'},
25
- WiiMote::BTN_UP => -> {puts 'you pressed up'},
26
- WiiMote::BTN_DOWN => -> {puts 'you pressed down'},
27
- WiiMote::BTN_RIGHT => -> {puts 'you pressed right'},
28
- WiiMote::BTN_LEFT => -> {puts 'you pressed left'},
29
- WiiMote::BTN_HOME => -> {puts 'you pressed home'},
30
- WiiMote::BTN_MINUS => -> {puts 'you pressed minus'},
31
- WiiMote::BTN_A => -> {puts 'you pressed a'},
32
- WiiMote::BTN_B => -> {puts 'you pressed b'},
33
- WiiMote::BTN_1 => -> {puts 'you pressed 1'},
34
- WiiMote::BTN_2 => -> {puts 'you pressed 2'}
24
+ '2' => -> {puts 'you pressed 2'},
25
+ '1' => -> {puts 'you pressed 1'},
26
+ 'b' => -> {puts 'you pressed b'},
27
+ 'a' => -> {puts 'you pressed a'},
28
+ 'minus' => -> {puts 'you pressed minus'},
29
+ 'void1' => nil,
30
+ 'void2' => nil,
31
+ 'home' => -> {puts 'you pressed home'},
32
+ 'left' => -> {puts 'you pressed left'},
33
+ 'right' => -> {puts 'you pressed right'},
34
+ 'down' => -> {puts 'you pressed down'},
35
+ 'up' => -> {puts 'you pressed up'},
36
+ 'plus' => {
37
+ on_buttondown: -> {puts 'you pressed plus'},
38
+ on_buttonup: -> {puts 'plus button raised'}
39
+ }
35
40
  }
36
41
 
37
- @terminator = WiiMote::BTN_A
42
+ @terminator = ['1','2']
38
43
  end
39
44
 
40
45
  def activate()
41
46
 
47
+ previously_pressed, pressed = [], []
48
+
42
49
  begin
50
+
43
51
  @wiimote.get_state
44
52
  @wiimote.active = true
45
53
  yield(@wiimote) if block_given?
46
54
 
47
- if @wiimote.buttons > 0 then
48
- r = @events[@wiimote.buttons]
49
- sleep 0.2
50
- r.call if r
55
+ if @wiimote.buttons > 0 then
56
+
57
+ val = @wiimote.buttons
58
+
59
+ @events.keys.reverse.each_with_index do |x,i|
60
+
61
+ n = @events.length - 1 - i
62
+ next if x == 32 or x == 64
63
+
64
+ xval = 2**n
65
+ (val -= xval; pressed << x) if xval <= val
66
+
67
+ end
68
+
69
+ @wiimote.active = (not (@terminator & pressed) == @terminator)
70
+
71
+ new_keypresses = pressed - previously_pressed
72
+ expired_keypresses = previously_pressed - pressed
73
+
74
+ previously_pressed = pressed
75
+ pressed = []
76
+
77
+ procs1 = {
78
+ Hash: lambda {|x| x[:on_buttondown].call},
79
+ Proc: lambda {|x| x.call}
80
+ }
81
+
82
+ procs2 = {
83
+ Hash: lambda {|x| x[:on_buttonup].call},
84
+ Proc: lambda {|x| }
85
+ }
86
+
87
+ new_keypresses.each do |x|
88
+ procs1[@events[x].class.to_s.to_sym].call(@events[x])
89
+ end
90
+
91
+ expired_keypresses.each do |x|
92
+ procs2[@events[x].class.to_s.to_sym].call(@events[x])
93
+ end
94
+
95
+ else
96
+
97
+ if previously_pressed.length > 0 then
98
+
99
+ expired_keypresses = previously_pressed
100
+
101
+ expired_keypresses.each do |x|
102
+ procs2[@events[x].class.to_s.to_sym].call(@events[x])
103
+ end
104
+
105
+ previously_pressed, expired_keypressed, pressed = [], [], []
106
+ end
51
107
  end
52
- end until (@wiimote.buttons & @terminator ) != 0 || !@wiimote.active
108
+
109
+ end until ( not @wiimote.active )
53
110
  end
54
111
 
55
112
  def led=(val) @led = @wiimote.led = val 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.1.4
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Robertson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-12-01 00:00:00 Z
13
+ date: 2012-12-19 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: cwiid