ruby_buzz 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 13ed3645ae22adbdf04423671b8caf8103114680
4
+ data.tar.gz: 96e818999e882cd82d77bd8a120a782444cc3c95
5
+ SHA512:
6
+ metadata.gz: 4310f00d7c22788182ff3e39efc430315e7d3c81242e36c54cbc1d5d99eb1246e63968c724a625e41cd95e94e7269a49e79f159275cfd498f67046c98a64b775
7
+ data.tar.gz: 6732307f59e5db47647edf35c0b950c9dde5c509b6755f142bf528b68085d874ccb4d9b953acc1abd7962c526481c0c64b1f613a9e7928f6fb0e1cce0a24f828
data/LICENCE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Andrew Faraday
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,189 @@
1
+ ruby-buzz
2
+ ==========
3
+
4
+ A Ruby library for controlling the LEDs on buzz controllers in Linux.
5
+
6
+ * Linux only
7
+ * Wired buzz controllers only
8
+
9
+ *Warning:* ruby_buzz has to change some rights down in the /sys and /dev
10
+ folders of Linux in order to access the kernel. You will be asked for
11
+ your password in order to use ruby_buzz.
12
+
13
+ Example Scripts
14
+ ===============
15
+
16
+ The scripts folder contains examples of the input and output
17
+ functionality for the buzz controllers.
18
+
19
+ Turn on buzzer 1
20
+
21
+ `ruby scripts/light.rb 1 on`
22
+
23
+ -------------
24
+
25
+ Turn off buzzer 1
26
+
27
+ `ruby scripts/light.rb 1 off`
28
+
29
+ -------------
30
+
31
+ Watch the inputs
32
+
33
+ `ruby scripts/read.rb`
34
+
35
+ (press ctrl+c to stop)
36
+
37
+ -------------
38
+
39
+ Pushing the buzzer lights that buzzer
40
+
41
+ `ruby scripts/events.rb`
42
+
43
+ (press ctrl+c to stop)
44
+
45
+
46
+ Using it in your code
47
+ =====================
48
+
49
+ Lights
50
+ ------
51
+
52
+ Include the ruby_buzz library
53
+
54
+ `require 'ruby_buzz'`
55
+
56
+ The Buzz controller has 4 pads, each of which has four lights.
57
+ They can be accessed like so:
58
+
59
+ ```ruby
60
+ # Via the pads
61
+ RubyBuzz::Pad[0]
62
+ RubyBuzz::Pad[0].light
63
+ RubyBuzz::Pad.all
64
+ # A control for light 1, divorced from the pad.
65
+ RubyBuzz::Light.new(0)
66
+ ```
67
+
68
+ Control one light:
69
+
70
+ ```ruby
71
+ pad = RubyBuzz::Pad[0]
72
+ pad.light.on
73
+ sleep 1
74
+ pad.light.off
75
+ ```
76
+
77
+ A quick light show
78
+
79
+ ```ruby
80
+ RubyBuzz::Pad.all.each do |pad|
81
+ pad.light.on
82
+ sleep 0.1
83
+ end
84
+ RubyBuzz::Pad.all.each do |pad|
85
+ pad.light.off
86
+ sleep 0.1
87
+ end
88
+ ```
89
+
90
+ If you abandon your script before all the lights are turned off they
91
+ will say on indefinitely. You may want to rescue SystemExit and Interrupt
92
+ errors with `RubyBuzz::Light.all_off` E.g.
93
+
94
+ ```ruby
95
+ begin
96
+ loop do
97
+ RubyBuzz::Pad[rand(4)].light.on
98
+ RubyBuzz::Pad[rand(4)].light.off
99
+ sleep 0.1
100
+ end
101
+ rescue SystemExit, Interrupt
102
+ RubyBuzz::Light.all_off
103
+ end
104
+ ```
105
+
106
+ Button events
107
+ -------------
108
+
109
+ Include the ruby_buzz library
110
+
111
+ `require 'ruby_buzz'`
112
+
113
+ The Buzz controller has 4 pads, each of which has four buttons named:
114
+
115
+ * buzz
116
+ * blue
117
+ * orange
118
+ * green
119
+ * yellow
120
+
121
+ They can be found like this:
122
+
123
+ ```ruby
124
+ RubyBuzz::Pad[0]
125
+ RubyBuzz::Pad[0].buttons
126
+ RubyBuzz::Pad[0].buttons[:buzz]
127
+ RubyBuzz::Pad.all
128
+ ```
129
+
130
+ You can define a block of code to be run by a given button like this:
131
+
132
+ ```ruby
133
+ RubyBuzz::Pad[0].add_event(
134
+ :buzz,
135
+ lambda {
136
+ puts "Player 1 buzzed!"
137
+ }
138
+ )
139
+ ```
140
+
141
+ You can debug the actions you've added to a button with trigger_events
142
+
143
+ ```ruby
144
+ RubyBuzz::Pad[0].trigger_events
145
+ ```
146
+
147
+ How about getting it to run when you press the button?
148
+
149
+ The RubyBuzz::Device class is responsible for reading the raw input
150
+ from the Buzz controllers via the linux terminal. Because it's reading
151
+ a data stream, it needs to start a background process to allow it to
152
+ work while other ruby code is operating.
153
+
154
+ You can start background process, which executes the events you added
155
+ to the buttons, with start_watching.
156
+
157
+ ```ruby
158
+ device = RubyBuzz::Device.new
159
+ RubyBuzz::Pad.all.each do |pad|
160
+ pad.add_event(:buzz, lambda { puts "Buzz!" }
161
+ )
162
+ device.start_watching
163
+ ```
164
+
165
+ Note: This process will end when your ruby process ends, but if you
166
+ want to stop it before that stage, you can call `device.stop_watching`
167
+
168
+ If you want to do nothing other than watch the buttons, you may want
169
+ to follow start_watching with an empty loop.
170
+
171
+ ```ruby
172
+ device = RubyBuzz::Device.new
173
+ RubyBuzz::Pad.all.each do |pad|
174
+ pad.add_event(:buzz, lambda { puts "Buzz!" }
175
+ )
176
+ device.start_watching
177
+
178
+ loop do
179
+
180
+ end
181
+ ```
182
+
183
+ Acknowledgements
184
+ ===============
185
+
186
+ This repo includes a modified version of the devinput class from
187
+ https://github.com/kamaradclimber/libdevinput
188
+ Which was forked from
189
+ https://github.com/prullmann/libdevinput
@@ -0,0 +1,37 @@
1
+ module RubyBuzz
2
+ class Button
3
+
4
+ @@buttons = []
5
+
6
+ # events will be an array of lambdas
7
+ attr_accessor :name, :code, :events
8
+
9
+ def initialize(code, name, pad)
10
+ @code = code
11
+ @name = name
12
+ @pad = pad
13
+ @events = []
14
+ @@buttons << self
15
+ end
16
+
17
+ def self.find(code)
18
+ @@buttons.detect { |b| b.code == code }
19
+ end
20
+
21
+ def add_event(proc)
22
+ @events << proc
23
+ end
24
+
25
+ def trigger_events
26
+ @events.each do |event|
27
+ event.call
28
+ end
29
+ end
30
+
31
+ def self.trigger_key(code)
32
+ btn = self.find(code)
33
+ btn.trigger_events
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ module RubyBuzz
4
+ class Device
5
+
6
+ require 'time'
7
+
8
+ # The worker is a thread which is watching the device
9
+ attr_accessor :worker
10
+
11
+ DEFAULT_FILE_NAME = "/dev/input/by-id/usb-Logitech_Logitech_Buzz_tm__Controller_V1-event-if00"
12
+
13
+ Event = Struct.new(:tv_sec, :tv_usec, :type, :code, :value)
14
+ # open Event class and add some convenience methods
15
+ class Event
16
+ def time;
17
+ Time.at(tv_sec)
18
+ end
19
+
20
+ def to_s
21
+ type_s = type.to_s
22
+ code_s = code.to_s
23
+ value_s = value.to_s
24
+ "#{time.iso8601} type: #{type_s} code: #{code_s} value: #{value_s}"
25
+ end
26
+ end
27
+
28
+ def initialize(filename=nil)
29
+ @dev = File.open(filename || DEFAULT_FILE_NAME)
30
+ @block_size = 24
31
+ rescue Errno::ENOENT => er
32
+ puts "Could not find device: are your controllers plugged in?"
33
+ raise er
34
+ end
35
+
36
+ def format
37
+ 'qqSSl'
38
+ end
39
+
40
+
41
+ def read
42
+ bin = @dev.read @block_size
43
+ Event.new *bin.unpack(format)
44
+ end
45
+
46
+ def each
47
+ begin
48
+ loop do
49
+ event = read
50
+ next unless event.type == 1
51
+ next unless event.value == 1
52
+ yield event
53
+ end
54
+ rescue Errno::ENODEV
55
+ end
56
+ end
57
+
58
+
59
+ def start_watching
60
+ return if @worker
61
+ @worker = Thread.new do
62
+ loop do
63
+ event = read
64
+ next unless event.type == 1
65
+ next unless event.value == 1
66
+ RubyBuzz::Button.trigger_key(event.code)
67
+ end
68
+ end
69
+ end
70
+
71
+ def stop_watching
72
+ @worker.kill
73
+ @worker = nil
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,33 @@
1
+ module RubyBuzz
2
+ class Light
3
+
4
+ @@lights = []
5
+ attr_accessor :file
6
+
7
+ def initialize(index)
8
+ @file = `ls /sys/class/leds/*buzz#{index + 1}/brightness`
9
+ @@lights << self
10
+ end
11
+
12
+ def on
13
+ `echo 1 > #{file}`
14
+ return nil
15
+ end
16
+
17
+ def off
18
+ `echo 0 > #{file}`
19
+ return nil
20
+ end
21
+
22
+ def self.all_off
23
+ @@lights.each{|l|l.off}
24
+ return nil
25
+ end
26
+
27
+ def self.all_on
28
+ @@lights.each{|l|l.on }
29
+ return nil
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,72 @@
1
+ # Each of these is an individual players controller
2
+ module RubyBuzz
3
+ class Pad
4
+
5
+ # {event_code => button_name}
6
+ MAPPINGS = [
7
+ {
8
+ 704 => :buzz,
9
+ 705 => :yellow,
10
+ 706 => :green,
11
+ 707 => :orange,
12
+ 708 => :blue
13
+ },
14
+ {
15
+ 709 => :buzz,
16
+ 710 => :yellow,
17
+ 711 => :green,
18
+ 712 => :orange,
19
+ 713 => :blue
20
+ },
21
+ {
22
+ 714 => :buzz,
23
+ 715 => :yellow,
24
+ 716 => :green,
25
+ 717 => :orange,
26
+ 718 => :blue
27
+ },
28
+ {
29
+ 719 => :buzz,
30
+ 720 => :yellow,
31
+ 721 => :green,
32
+ 722 => :orange,
33
+ 723 => :blue
34
+ }
35
+ ]
36
+
37
+ attr_accessor :buttons, :light
38
+
39
+ def self.init_mappings
40
+ @@pads = []
41
+ MAPPINGS.each_with_index do |mapping, index|
42
+ @@pads << new(mapping, index)
43
+ end
44
+ end
45
+
46
+ def self.all
47
+ @@pads
48
+ end
49
+
50
+ def self.[](index)
51
+ @@pads[index]
52
+ end
53
+
54
+ def initialize(mapping, index)
55
+ @index = index
56
+ @buttons = {}
57
+ @light = RubyBuzz::Light.new(index)
58
+ mapping.each do |code, name|
59
+ @buttons[name] = RubyBuzz::Button.new(code, name, self)
60
+ end
61
+ end
62
+
63
+ def add_event(button_name, proc)
64
+ @buttons[button_name].add_event proc
65
+ end
66
+
67
+ def trigger_event(button_name)
68
+ @buttons[button_name].trigger_events
69
+ end
70
+
71
+ end
72
+ end
data/lib/ruby_buzz.rb ADDED
@@ -0,0 +1,9 @@
1
+ `sudo chmod 777 /sys/class/leds/*/brightness`
2
+ `sudo chmod 777 /dev/input/event*`
3
+
4
+ require_relative './ruby_buzz/device.rb'
5
+ require_relative './ruby_buzz/button.rb'
6
+ require_relative './ruby_buzz/light.rb'
7
+ require_relative './ruby_buzz/pad.rb'
8
+
9
+ RubyBuzz::Pad.init_mappings
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_buzz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Faraday
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Light control and button event observers for wired Buzz™ controllers
14
+ in Ruby on Linux
15
+ email: andrewfaraday@hotmail.co.uk
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENCE
21
+ - README.md
22
+ - lib/ruby_buzz.rb
23
+ - lib/ruby_buzz/button.rb
24
+ - lib/ruby_buzz/device.rb
25
+ - lib/ruby_buzz/light.rb
26
+ - lib/ruby_buzz/pad.rb
27
+ homepage: https://github.com/AJFaraday/ruby-buzz
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 2.0.0
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.4.6
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Buzz™ controller support for Ruby in Linux
51
+ test_files: []