Module: Denko::Behaviors::Threaded

Included in:
AnalogIO::Output, Poller, DigitalIO::Output, Motor::Servo, SPI::OutputRegister
Defined in:
lib/denko/behaviors/threaded.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#interrupts_enabledObject (readonly)

Returns the value of attribute interrupts_enabled.



4
5
6
# File 'lib/denko/behaviors/threaded.rb', line 4

def interrupts_enabled
  @interrupts_enabled
end

#threadObject (readonly)

Returns the value of attribute thread.



4
5
6
# File 'lib/denko/behaviors/threaded.rb', line 4

def thread
  @thread
end

Class Method Details

.included(base) ⇒ Object



14
15
16
# File 'lib/denko/behaviors/threaded.rb', line 14

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#enable_interruptsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/denko/behaviors/threaded.rb', line 39

def enable_interrupts
  interrupts = self.class.class_eval('@@interrupts') rescue []
  interrupts.each do |method_name|
    standard_method = self.method(method_name)

    singleton_class.send(:define_method, method_name) do |*args|
      stop_thread unless (Thread.current == @thread)
      standard_method.call(*args)
    end
  end

  @interrupts_enabled = true
end

#stopObject



34
35
36
37
# File 'lib/denko/behaviors/threaded.rb', line 34

def stop
  stop_thread
  super if defined?(super)
end

#stop_threadObject



30
31
32
# File 'lib/denko/behaviors/threaded.rb', line 30

def stop_thread
  @thread.kill if @thread
end

#threaded(&block) ⇒ Object



18
19
20
21
22
# File 'lib/denko/behaviors/threaded.rb', line 18

def threaded(&block)
  stop_thread
  enable_interrupts unless interrupts_enabled
  @thread = Thread.new(&block)
end

#threaded_loop(&block) ⇒ Object



24
25
26
27
28
# File 'lib/denko/behaviors/threaded.rb', line 24

def threaded_loop(&block)
  threaded do
    loop(&block)
  end
end