Class: PiDriver::Utils::Interrupt

Inherits:
Object
  • Object
show all
Defined in:
lib/pi_driver/utils/interrupt.rb

Instance Method Summary collapse

Constructor Details

#initialize(edge, &block) ⇒ Interrupt

Returns a new instance of Interrupt.



4
5
6
7
8
9
10
# File 'lib/pi_driver/utils/interrupt.rb', line 4

def initialize(edge, &block)
  @argument_helper = Utils::ArgumentHelper.new prefix: 'Utils::Interrupt'
  @edge = edge

  @argument_helper.check(:block, block_given?, [true])
  @check = block
end

Instance Method Details

#clearObject



27
28
29
# File 'lib/pi_driver/utils/interrupt.rb', line 27

def clear
  @thread.kill
end

#startObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pi_driver/utils/interrupt.rb', line 12

def start
  @argument_helper.check(:block, block_given?, [true])

  @thread = Thread.new do
    last_state = check
    loop do
      new_state = check
      edge = get_current_edge(new_state, last_state)
      yield edge if valid_edge? edge
      last_state = new_state
    end
  end
  @thread.tap { |me| me.abort_on_exception = true }
end