Class: Powermate::Device
- Inherits:
-
Object
- Object
- Powermate::Device
- Defined in:
- lib/powermate/device.rb
Constant Summary collapse
- SIZE =
attr_reader :file SIZE = 24
48
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(path) ⇒ Device
constructor
A new instance of Device.
- #on_change(&block) ⇒ Object
- #on_clockwise(&block) ⇒ Object
- #on_counter_clockwise(&block) ⇒ Object
- #on_rotate(&block) ⇒ Object
- #open ⇒ Object
Constructor Details
#initialize(path) ⇒ Device
Returns a new instance of Device.
9 10 11 12 13 14 15 16 17 |
# File 'lib/powermate/device.rb', line 9 def initialize path @file = File.open path, File::Constants::RDONLY @reader = nil @on_change_blocks =[] @on_rotate_blocks=[] @on_clockwise_blocks=[] @on_counter_clockwise_blocks=[] @pressed = false end |
Class Method Details
Instance Method Details
#close ⇒ Object
72 73 74 75 |
# File 'lib/powermate/device.rb', line 72 def close @reader = nil !@reader end |
#on_change(&block) ⇒ Object
25 26 27 28 |
# File 'lib/powermate/device.rb', line 25 def on_change &block @on_change_blocks.push block self end |
#on_clockwise(&block) ⇒ Object
35 36 37 38 |
# File 'lib/powermate/device.rb', line 35 def on_clockwise &block @on_clockwise_blocks.push block self end |
#on_counter_clockwise(&block) ⇒ Object
40 41 42 43 |
# File 'lib/powermate/device.rb', line 40 def on_counter_clockwise &block @on_counter_clockwise_blocks.push block self end |
#on_rotate(&block) ⇒ Object
30 31 32 33 |
# File 'lib/powermate/device.rb', line 30 def on_rotate &block @on_rotate_blocks.push block self end |
#open ⇒ Object
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 |
# File 'lib/powermate/device.rb', line 46 def open return if @reader @reader = Thread.new{ #trash any old events #@file.readbyte() #throw out the first byte, its junk bytes =[] while @reader do bytes << @file.readbyte() if bytes.size == SIZE event = Powermate::Event.new(bytes, @pressed) bytes = [] @pressed = event.pressed @on_change_blocks.each{|w| w.call(event) } @on_rotate_blocks.each{|w| w.call(event) } if event.clockwise || event.counter_clockwise @on_clockwise_blocks.each{|w| w.call(event) } if event.clockwise @on_counter_clockwise_blocks.each{|w| w.call(event) } if event.counter_clockwise end end } !!@reader end |